-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd_to_html.py
executable file
·140 lines (127 loc) · 4.46 KB
/
md_to_html.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import argparse
import re
from enum import Enum
class LineTypes(Enum):
EMPTY = 0
HEADER = 1
PLAINTEXT = 2
EMPTY_IN_DIV = 3
LIST_ELEM = 4
def all_dashes(s):
for c in s:
if(c != '-'): return False
return True
def bold(s):
out='<b>'
l = len(s.group(0))
out+=s.group(0)[2:(l-2)]
out+='</b>'
return out
def italic(s):
out='<i>'
l = len(s.group(0))
out+=s.group(0)[1:(l-1)]
out+='</i>'
return out
def link(s):
split = s.group(0).split('](')
out='<a href=\"'
out+=split[1].replace(')','').replace('uasatucla.org', 'uas.seas.ucla.edu')
out+='\">'
out+=split[0][1:]
out+='</a>'
return out
def image(s):
split = s.group(0).split('](')
out='<img src='
out+=split[1].replace(')','')
out+=' alt=\"'
out+=split[0][2:]
out+='\"/>'
return out
def main():
parser = argparse.ArgumentParser(description='Convert MD file to our HTML format')
parser.add_argument('infile', metavar='I', type=str, nargs=1, help='File to be parsed')
parser.add_argument('outfile', metavar='O', type=str, nargs=1, help='File to be output')
args=parser.parse_args()
infile=args.infile[0]
outfile=args.outfile[0]
print(f'Converting md file {infile} to html')
of = open(outfile, 'w')
template = open('md/input-template-start.html', 'r')
while (line := template.readline()):
of.write(line)
template.close()
of.write("\n")
inf = open(infile, 'r')
reading = False
found = False
while (line := inf.readline()):
clean = line.strip()
if not reading and all_dashes(clean):
if found:
reading=True
prev_line = LineTypes.EMPTY
continue
else:
found=True
if reading:
clean = re.sub(r'<!--(.)*-->', '', clean) # Remove any comments
h_check = clean.replace('#', '')
if len(h_check) > 0 and len(h_check) != len(clean) and h_check[0] == ' ': # header
if prev_line == LineTypes.PLAINTEXT or prev_line == LineTypes.EMPTY_IN_DIV:
of.write('</div>')
if prev_line == LineTypes.LIST_ELEM:
of.write('</ul>')
h_lev = len(clean)-len(h_check)
of.write(f'<h{h_lev}>')
of.write(h_check[1:])
of.write(f'</h{h_lev}>\n')
prev_line = LineTypes.HEADER
elif len(clean) == 0:
if prev_line == LineTypes.LIST_ELEM:
of.write('</ul>')
if prev_line == LineTypes.PLAINTEXT or prev_line == LineTypes.EMPTY_IN_DIV:
if len(clean) == 0 and prev_line == LineTypes.PLAINTEXT:
of.write('<br>')
prev_line = LineTypes.EMPTY_IN_DIV
else:
prev_line = LineTypes.EMPTY
elif clean[0:2] == '* ' or clean[0] == '-' or clean[0:2] == '* ':
if prev_line == LineTypes.PLAINTEXT or prev_line == LineTypes.EMPTY_IN_DIV:
of.write('</div>')
if prev_line != LineTypes.LIST_ELEM:
of.write('<ul>')
of.write('<li>')
clean = re.sub('\*\*.*?\*\*', bold, clean[2:])
clean = re.sub('\[.*?\]\(.*?\)', link, clean)
of.write(clean)
of.write('</li>')
prev_line = LineTypes.LIST_ELEM
else:
if clean == '<br>':
continue
if prev_line == LineTypes.LIST_ELEM:
of.write('</ul>')
if prev_line != LineTypes.PLAINTEXT and prev_line != LineTypes.EMPTY_IN_DIV:
of.write('<div class=\"autogen-md\">')
clean = re.sub('!\[.*?]\(.*?\)', image, clean)
clean = re.sub('\*\*.*?\*\*', bold, clean)
clean = re.sub('\*.*?\*', italic, clean)
clean = re.sub('\[.*?\]\(.*?\)', link, clean)
of.write(clean)
prev_line = LineTypes.PLAINTEXT
#print(clean)
#print(clean)
if prev_line == LineTypes.LIST_ELEM:
of.write('</ul>')
elif prev_line == LineTypes.PLAINTEXT:
of.write('</div>')
inf.close()
template = open('md/input-template-end.html', 'r')
while (line := template.readline()):
of.write(line)
template.close()
of.close()
if __name__=="__main__":
main()