summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArturs Artamonovs <dos21h@gmail.com>2023-01-29 12:26:48 +0000
committerArturs Artamonovs <dos21h@gmail.com>2023-01-29 12:26:48 +0000
commitcf3ea4c1cf323f71e8e680af074d95e91810ba9e (patch)
tree390b91eeb5a550f53fd9442783236ff4eaf264b1
parent66fa71a8f11b6ce5e8471b533f67cc3a1fdb85a8 (diff)
downloadmd-site-cf3ea4c1cf323f71e8e680af074d95e91810ba9e.tar.gz
md-site-cf3ea4c1cf323f71e8e680af074d95e91810ba9e.zip
Update toc generator id's
-rwxr-xr-xsrc/md.py178
1 files changed, 76 insertions, 102 deletions
diff --git a/src/md.py b/src/md.py
index 60fb574..bcca756 100755
--- a/src/md.py
+++ b/src/md.py
@@ -12,103 +12,17 @@ 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
+class TocAnchorRenderer(mistune.HTMLRenderer):
+ count = 1
+ def heading(self, text, level, **attrs):
+ tag = 'h' + str(level)
+ html = '<' + tag
+ _id = attrs.get('id')
+ #if _id:
+ # html += ' id="toc-' + _id + '"'
+ html += ' id="toc-' + str(self.count) + '"'
+ self.count += 1
+ return html + '>' + text + '</' + tag + '>\n'
################################################################################
#get first tags and use them to configure some bits
@@ -129,6 +43,53 @@ def get_tags(data):
return text
################################################################################
+#
+def generate_toc(tags):
+ cnt = 3
+ headers_list = []
+ for tag in tags:
+ if tag["type"] == "heading":
+ attrs = tag["attrs"]
+ #attrs["id"] = "toc-none"
+ headers_list.append({"header":tag["children"][0]["raw"], "level":attrs["level"]})
+ #print(headers_list)
+ ############################
+ #generate toc
+ html_toc = '<ul id="table-of-content">\n'
+ first_level = None
+ last_level = None
+ cnt = 1
+ for el in headers_list:
+ l = int(el["level"])
+
+ if first_level is None:
+ first_level = l
+ last_level = l
+ #same level
+ elif last_level == l:
+ html_toc += '</li>\n<li><a href="#toc-%d">%s</a>' % (cnt, el["header"] )
+ # if last level smaller then open one more
+ elif last_level == l - 1:
+ last_level = l
+ html_toc += '<ul>\n<li><a href="#toc-%d">%s</a>' % (cnt, el["header"])
+ elif last_level > l:
+ # close indention
+ html_toc += '</li>'
+ while last_level > l:
+ html_toc += '</ul>\n</li>\n'
+ last_level -= 1
+ html_toc += '<li><a href="#toc-%d">%s</a>' % (cnt, el["header"])
+ cnt += 1
+
+ html_toc += '</li>\n'
+ while last_level > first_level:
+ html_toc += '</ul>\n<!--</li>-->\n'
+ last_level -= 1
+
+ html_toc += '</ul>'
+ return html_toc
+
+################################################################################
#check if there is input file
if len(sys.argv) < 2:
print("ERROR: Exit . Need more arguments")
@@ -138,9 +99,9 @@ md_fn = sys.argv[1]
#template loader
loader = FileSystemLoader( "/home/fam/downloads/source/repos/md-site/src/templ" )
-templ_env = Environment( loader = loader )#
+templ_env = Environment( loader = loader )
-t = templ_env.get_template("main.thtml")#
+t = templ_env.get_template("main.thtml")
##get md file
f = open( md_fn, "r" )
@@ -148,13 +109,26 @@ data = f.read()
data = get_tags(data)
#toc = TocRenderer()
## Create AST rendered
+markdown_raw = mistune.create_markdown(renderer=None)
+raw_tags = markdown_raw(data)
+html_toc = generate_toc(raw_tags)
+###########################
## Create AST -> Markdown renderer
-
-## Mistune generate MTL
-md_rend = mistune.html(data)
+#markdown_ast = mistune.create_markdown(renderer='ast')
+#print(markdown_ast(raw_tags))
+
+custom_render = True
+if not custom_render:
+ ## Mistune generate MTL
+ md_rend = html_toc + mistune.html(data)
+else:
+ ## Mistune with custom renderer
+ markdown = mistune.create_markdown(renderer=TocAnchorRenderer())
+ md_rend = html_toc + markdown(data)
#print t.render( article=article, block = md_rend )
print(t.render( article=article, block = md_rend ))
+
f.close() \ No newline at end of file