#!/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 + '\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 = '\n\n' last_level -= 1 html_toc += '' return html_toc ################################################################################ #check if there is input file if len(sys.argv) < 2: print("ERROR: Exit . Need more arguments") sys.exit(0) md_fn = sys.argv[1] #template loader loader = FileSystemLoader( "/home/fam/downloads/source/repos/md-site-py3/src/templ" ) #loader = FileSystemLoader( "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() ## 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 #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(),escape=False,plugins=['strikethrough', 'footnotes', 'table', 'speedup']) markdown = mistune.create_markdown( escape=False, #plugins=['strikethrough', 'footnotes', 'table', 'speedup'], plugins=['url','table'], renderer=TocAnchorRenderer() ) md_rend = html_toc + markdown(data) #print t.render( article=article, block = md_rend ) print(t.render( article=article, block = md_rend )) #print(raw_tags) f.close() # generate style # all styles http://help.farbox.com/pygments.html # pygmentize -f html -S colorful > syntax.css