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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#!/usr/bin/python2
import os
import os.path
import sys
from jinja2 import Environment, PackageLoader, Template, FileSystemLoader
import mistune
#global name for templates
article = {}
class TocMixin():
"""TOC mixin for Renderer, mix this with Renderer::
class TocRenderer(TocMixin, Renderer):
pass
toc = TocRenderer()
md = mistune.Markdown(renderer=toc)
# required in this order
toc.reset_toc() # initial the status
md.parse(text) # parse for headers
toc.render_toc(level=3) # render TOC HTML
"""
def reset_toc(self):
self.toc_tree = []
self.toc_count = 0
self.title = None
self.img_count = -1
def header(self, text, level, raw=None):
rv = ""
if level != 1:
rv = '<h%d id="toc-%d">%s</h%d>\n' % (
level, self.toc_count, text, level
)
else:
self.title = text
self.toc_tree.append((self.toc_count, text, level, raw))
self.toc_count += 1
return rv
#Lets do previev image
def image(self, src, title, alt_text):
prev_img = src.split(".")
prev_img = prev_img[0]+"_prev."+prev_img[1]
self.img_count += 1
return "<a href="+src+"><img src=\""+prev_img+"\" alt=\""+alt_text+"\"></a>"
def render_toc(self, level=3):
"""Render TOC to HTML.
:param level: render toc to the given level
"""
return ''.join(self._iter_toc(level))
def _iter_toc(self, level):
first_level = None
last_level = None
cnt = 0
if (self.title != None):
yield '<h1>%s</h1>\n' % (self.title)
yield '<ul id="table-of-content">\n'
for toc in self.toc_tree:
index, text, l, raw = toc
index = cnt
if l > level:
# ignore this level
continue
if first_level is None:
# based on first level
first_level = l
last_level = l
#yield '<li><a href="#toc-%d">%s</a>' % (cnt, text)
yield ''
elif last_level == l:
yield '</li>\n<li><a href="#toc-%d">%s</a>' % (cnt, text)
elif last_level == l - 1:
last_level = l
yield '<ul>\n<li><a href="#toc-%d">%s</a>' % (cnt, text)
elif last_level > l:
# close indention
yield '</li>'
while last_level > l:
yield '</ul>\n</li>\n'
last_level -= 1
yield '<li><a href="#toc-%d">%s</a>' % (cnt, text)
cnt = cnt + 1
# close tags
yield '</li>\n'
while last_level > first_level:
yield '</ul>\n<!--</li>-->\n'
last_level -= 1
yield '</ul>\n'
self.toc_count = 0
class TocRenderer(TocMixin, mistune.Renderer):
pass
################################################################################
#get first tags and use them to configure some bits
def get_tags(data):
text = ""
tag_section = True
for line in data.split("\n"):
if tag_section:
tag = line.split(":")
l = len(tag)
if l != 2:
tag_section = False
text += line+"\n"
else:
article[tag[0]] = tag[1]
else:
text += line+"\n"
return text
################################################################################
#check if there is input file
if len(sys.argv) < 2:
print "Need more arguments"
sys.exit(0)
md_fn = sys.argv[1]
#template loader
loader = FileSystemLoader( "/home/fam/downloads/source/repos/md-site/src/templ" )
templ_env = Environment( loader = loader )
t = templ_env.get_template("main.thtml")
#get md file
f = open( md_fn, "r" )
data = f.read()
data = get_tags(data)
toc = TocRenderer()
md = mistune.Markdown(rule=True,renderer=toc)
toc.reset_toc()
md.parse(data)
md_rend_toc = toc.render_toc(level=3)
md_rend = md_rend_toc + md.render( data )
print t.render( article=article, block = md_rend )
|