#!/usr/bin/python3 import os import os.path import sys #from jinja2 import Environment, PackageLoader, Template, FileSystemLoader from jinja2 import Environment, PackageLoader, Template, FileSystemLoader import mistune from mistune.util import escape as escape_text, safe_entity ENABLE_CODE_HL=True #if ENABLE_CODE_HL: from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import html #global name for templates article = {}# 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' def block_html(self, html: str) -> str: #if self._escape: # return '
' + escape_text(html) + '
\n' #return html + '\n' return html def block_code(self, code: str, info=None) -> str: if ENABLE_CODE_HL: if info: lexer = get_lexer_by_name(info, stripall=True) #formatter = html.HtmlFormatter() formatter = html.HtmlFormatter(linenos=1) return highlight(code, lexer, formatter) return '' + mistune.escape(code) + '
'
else:
#default behaviout from the renderer
#html = '' + escape_text(code) + '
\n'
return ""
################################################################################
#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
################################################################################
#
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 = '