From cf3ea4c1cf323f71e8e680af074d95e91810ba9e Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Sun, 29 Jan 2023 12:26:48 +0000 Subject: Update toc generator id's --- src/md.py | 178 +++++++++++++++++++++++++++----------------------------------- 1 file 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 = '%s\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 "\""+alt_text+"\""# - -# 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 '

%s

\n' % (self.title)# - -# yield '\n\n' -# last_level -= 1# - -# yield '\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 + '\n' ################################################################################ #get first tags and use them to configure some bits @@ -128,6 +42,53 @@ def get_tags(data): text += line+"\n" 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 = '\n\n' + last_level -= 1 + + html_toc += '' + return html_toc + ################################################################################ #check if there is input file if len(sys.argv) < 2: @@ -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 -- cgit v1.2.3