aboutsummaryrefslogtreecommitdiff
path: root/converter.py
blob: 9c8ee77470a31acdb342f34e6e442b6cadc007c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3

from optparse import OptionParser
from os.path import getctime
from time import ctime
from datetime import datetime
from re import search, sub

"""Command line arguments"""
usage = "usage: %prog [options] file..."
parser = OptionParser(usage=usage)
parser.add_option('-i', '--initial-html', dest='header',
                  help='File for initial template of compiled HTML',
                  default='initial.html')
parser.add_option('-o', '--output-type', dest='output_type',
                  help='Type of output (filename | date)',
                  default='filename')
(options, args) = parser.parse_args()


"""Valid argument testing
TODO: Add support for directories
"""
markdown_files = []
for file in args:
    if search(".md", file) is not None:
        # Is a markdown file
        markdown_files.append(file)
    else:
        print(file, "is not a markdown file, skipping")


"""Read header"""
header = []
with open(options.header, 'r') as file:
    header = file.readlines()

"""Read and compile markdown to HTML"""
markdown_compiled = []
for filename in markdown_files:
    file_lines = []
    with open(filename, 'r') as file:
        file_lines = file.readlines()
    content_parsed = []
    started_paragraphing = False

    for line in file_lines:
        # NOTE: When parsing regexes, use \g<number> for the object capture
        # Rules
        if search(r"^#(.*)", line):
            line = sub(r"^#(.*)", r"<h1>\g<1></h1>", line)
        elif line.strip() == '' and started_paragraphing:
            line = "</p>\n"
            started_paragraphing = False
        else:
            # simple text
            line = sub(r"\*\*(.*)\*\*", r"<strong>\g<1></strong>", line)
            line = sub(r"\*(.*)\*", r"<i>\g<1></i>", line)
            if not started_paragraphing:
                started_paragraphing = True
                line = "<p>\n" + line
        content_parsed.append(line)
    markdown_compiled.append({'name': filename, 'content': content_parsed})

"""Template time"""
for md in markdown_compiled:
    header_copy = ''.join(header).replace("%title%", md['name'])\
                                 .replace("%body%", ''.join(md['content']))
    file_name = ""
    if options.output_type.startswith("f"):
        file_name = md['name'].replace('.md', '.html')
    else:
        time = datetime.strptime(
            ctime(getctime(md['name'])), "%a %b %d %H:%M:%S %Y")
        file_name = str(time).replace(" ", "@") + ".html"
    with open(file_name, 'w') as file:
        file.write(header_copy)

print("Done")