summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2016-05-22 16:25:49 +0100
committerFreeArtMan <dos21h@gmail.com>2016-05-22 16:25:49 +0100
commit5d1395063460945524c2e6ac6194e61ef40c220a (patch)
tree926433e831a71c118d36ab22ab5a6bc57a5bba2a /src
parentc8270aa6fcb9aaac7b9d35c672f59995687aefd9 (diff)
downloadmd-site-5d1395063460945524c2e6ac6194e61ef40c220a.tar.gz
md-site-5d1395063460945524c2e6ac6194e61ef40c220a.zip
Added TOC for mistune
Diffstat (limited to 'src')
-rwxr-xr-xsrc/md.py102
-rw-r--r--src/templ/base.thtml2
2 files changed, 100 insertions, 4 deletions
diff --git a/src/md.py b/src/md.py
index d07df0f..a37d5ce 100755
--- a/src/md.py
+++ b/src/md.py
@@ -7,6 +7,97 @@ from jinja2 import Environment, PackageLoader, Template, FileSystemLoader
import mistune
+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
+
+ 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
+
+ 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
+
+################################################################################
#check if there is input file
if len(sys.argv) < 2:
print "Need more arguments"
@@ -15,7 +106,7 @@ if len(sys.argv) < 2:
md_fn = sys.argv[1]
#template loader
-loader = FileSystemLoader( "/home/fam/prog/python/mdsite/templ/" )
+loader = FileSystemLoader( "/home/fam/downloads/source/repos/md-site/src/templ" )
templ_env = Environment( loader = loader )
t = templ_env.get_template("main.thtml")
@@ -23,7 +114,12 @@ t = templ_env.get_template("main.thtml")
#get md file
f = open( md_fn, "r" )
data = f.read()
-md = mistune.Markdown(rule=True)
-md_rend = md.render( 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( block = md_rend ) \ No newline at end of file
diff --git a/src/templ/base.thtml b/src/templ/base.thtml
index 99ec99a..6b67f02 100644
--- a/src/templ/base.thtml
+++ b/src/templ/base.thtml
@@ -19,7 +19,7 @@
<a href="/writeup.md" class="headerlink">WriteUp</a>|
<a href="http://git.main.lv" class="headerlink">Projects</a>|
<a href="telnet://main.lv">BBS</a>|
- <a href="http://archive.main.lv">Archive</a>
+ <a href="http://archive.main.lv">Archive</a>|
<a href="/links.md" class="headerlink">Links</a>|
<a href="/about.md" class="headerlink">About</a>
</nav>