-
Notifications
You must be signed in to change notification settings - Fork 0
/
md2html.py
executable file
·129 lines (108 loc) · 3.33 KB
/
md2html.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
import argparse
try:
import htmlmin
minification_available = True
except ImportError:
minification_available = False
def find_anchor_tags(line):
recursion = False
index = line.find('[')
if index != -1 and line.find(']') > -1:
if line[index-1] != '!':
#this is not an image tag
if len(line.split('[')) > 2:
recursion = True
#remove one link at a time
(before, line) = line.split('[',1)
(alt, line) = line.split('](',1)
(link,after) = line.split(')',1)
line = before + '<a href="' + link +'">' + alt + '</a>' + after
if recursion:
line = find_anchor_tags(line)
return line
def convert(infile, outfile, minify):
if outfile is None:
outfile = infile.split('.md')[0] + '.html'
try:
f = open(infile)
w = open(outfile, 'w')
except IOError as e:
print 'Unable to proceed *** '+ str(e)
return
pre = 0
blockquote = 0
ul = 0
stringBuffer = '''
<html><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><head><title>Testing Code</title><style>
body {color: black; font-family: Arial, sans-serif, serif}
h1,h2,h3 {color: rebeccapurple;}
</style>
</head><body>
'''
for line in f:
# first handle for <pre></pre> tags
if(line.startswith(' ') ):
if pre == 0:
stringBuffer += '<pre>'
pre = 1
stringBuffer += line
elif line.startswith('>') :
if blockquote == 0:
stringBuffer += '<blockquote>'
blockquote = 1
stringBuffer += line.strip('>')
elif line.startswith('-'):
if ul == 0:
stringBuffer += '<ul>'
ul = 1
stringBuffer += '<li>' + line.strip().strip('-') + '</li>'
else:
if pre == 1:
stringBuffer += '</pre>'
pre = 0
if blockquote == 1:
stringBuffer += '</blockquote>'
blockquote = 0
if ul == 1:
stringBuffer += '</ul>'
ul = 0
line = line.strip()
# now run the link check
line = find_anchor_tags(line)
if(line.startswith("#")):
if(line.startswith("###")):
stringBuffer += "<h3>" + line.split('###')[1] + "</h3>"
elif(line.startswith("##")):
stringBuffer += "<h2>" + line.split('##')[1] + "</h2>"
elif(line.startswith("#")):
stringBuffer += "<h1>" + line.split('#')[1] + "</h1>"
else:
#check if it is an image
if( line.find("<img ") > -1):
stringBuffer += line
elif( line.startswith('![') ):
#image tag
(alt, link) = line.split('![')[1].split(']')
link = link.strip('(').strip(')')
stringBuffer += "<img style=\"float: right;\" src=\""+ link + "\" alt=\"" + alt + "\" />"
else:
stringBuffer += "<p>" + line + "</p>"
stringBuffer += "</body></html>"
#check if minification is necessary
if minify:
if minification_available:
stringBuffer = htmlmin.minify(stringBuffer)
else:
print 'Library htmlmin is not available. Proceeding with no minification.'
w.write(stringBuffer)
f.close()
w.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Converts a markdown file to HTML")
parser.add_argument("infile", help="Markdown file to convert")
parser.add_argument("-outfile", help="HTML file name for converted file, default is <infile>.html")
parser.add_argument("-minify", help="Minfiy the HTML output.",type=bool)
args = parser.parse_args()
print 'Converting ' + args.infile + 'to HTML..'
convert(args.infile, args.outfile, args.minify)
print 'Done.'