-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
132 lines (101 loc) · 3.02 KB
/
generate.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
#!/usr/bin/env python2.6
import re
import sys
import os
import markdown.markdown2 as markdown
from markdown.template import header, footer, secHeader, secFooter
def readfile(fname):
f = open(fname)
d = f.read()
f.close()
return d
def writefile(fname, contents):
f = open(fname, 'w')
f.write(contents)
f.close()
def getFilenameMatch(fname):
"""Finds the closest match to the filename. No html files returned."""
files = os.listdir('.')
if not files:
return False
blacklist = ['.html', '.py', '~'] # Cannot be in the filenames!
for f in files:
skip = False
for b in blacklist:
if b in f:
skip = True
break
if skip:
continue
if f.find(fname) == 0:
return f
return False
def markupFirst(x):
"""Markup to add before markdown parsed."""
# Global changes
x = re.sub(r'\t(.*)', r'\1', x) # Remove leading tab
x = re.sub(r'\* (.*) -- ', r'* **\1** - ', x) # auto-bold
x = re.sub(r'\* (.*): ', r'* *\1*: ', x) # auto-italics
x = re.sub(r' - ', r' – ', x) # endash
# Arrows
x = re.sub(r'-?->', r'→', x) # ->
x = re.sub(r'<--?', r'←', x) # <-
# Tables
x = re.sub(r'<t>', r'<table><tr><td>', x) # endash
x = re.sub(r'<col>', r'</td><td>', x) # endash
x = re.sub(r'<row>', r'</td></tr><tr><td>', x) # endash
x = re.sub(r'</t>', r'</td></tr></table>', x) # endash
# Global molecule/etc. notation
x = re.sub(r'Na+', r'Na<sup>+</sup>', x) # Na+
x = re.sub(r'K+', r'K<sup>+</sup>', x) # K+
x = re.sub(r'[Cc]a\+2', r'Ca<sup>+2</sup>', x) # Ca+2
x = re.sub(r'IPv3', r'IP<sub>3</sub>', x) # IP3
x = re.sub(r'alpha', r'α', x) # Alpha
x = re.sub(r'beta', r'β', x) # Beta
x = re.sub(r'gamma', r'γ', x) # Gamma
x = re.sub(r'a/b', r'α/β', x) # Alpha/Beta
# Directives
x = re.sub(r'center: (.*)$', r'<p class="cen">\1</p>', x) # Center line
x = re.sub(r'centerAll: (.*)', r'<p class="cen">\1</p>', x, re.DOTALL) # Center all
print x
return x
def markupAfter(x):
"""Markup to add after markdown parsed."""
# External links
#x = re.sub(r'<a href="(.*)">(.*)</a>',
# r'<a href="\1" rel="external">\2</a>', x)
#x = re.sub(r'<ul>',
# r'<ul class="incremental">', x) # show-first
# Remove paragraphs until I figure out markdown on paragraphs...
x = re.sub(r'<p>', r'', x)
x = re.sub(r'</p>', r'', x)
return x
def parseContents(data):
"""Run the content through the markdown parser and my own parsing"""
parsed = []
for x in data.split("\n\n"):
if not x.strip():
continue
# Comments
if x[0:4] == '<!--':
continue
if x[0:5] == '-----':
continue
x = markupFirst(x)
parsed.append(markdown.markdown(x))
output = header + "\n"
for x in parsed:
x = markupAfter(x)
output += x + "\n" #secHeader + x + secFooter + "\n"
output += footer
return output
def main():
if len(sys.argv) < 2:
sys.exit("Need argument")
infile = getFilenameMatch(sys.argv[1])
if not infile:
sys.exit("File doesn't exist.")
outfile = infile + ".html"
data = parseContents(readfile(infile))
writefile(outfile, data)
if __name__ == '__main__': main()