diff options
-rwxr-xr-x | src/md.py | 55 |
1 files changed, 39 insertions, 16 deletions
@@ -2,6 +2,9 @@ import os import os.path import sys +import traceback +import logging + #from jinja2 import Environment, PackageLoader, Template, FileSystemLoader @@ -37,24 +40,39 @@ class TocAnchorRenderer(mistune.HTMLRenderer): return html def block_code(self, code: str, info=None) -> str: + render_failed = True + render_html = "<pre><code>No render!</code></pre>" 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 '<pre><code>' + mistune.escape(code) + '</code></pre>' - else: + try: + lexer = get_lexer_by_name(info, stripall=True) + #formatter = html.HtmlFormatter() + formatter = html.HtmlFormatter(linenos=1) + render_html = highlight(code, lexer, formatter) + render_failed = False + except Exception as e: + + #print(traceback.format_exc()) + pass + else: + render_html = '<pre><code' + if info is not None: + info = safe_entity(info.strip()) + if info: + lang = info.split(None, 1)[0] + render_html += ' class="language-' + lang + '"' + render_html = render_html + '>' + escape_text(code) + '</code></pre>\n' + render_failed = False + if render_failed: #default behaviout from the renderer - #html = '<pre><code' - #if info is not None: - # info = safe_entity(info.strip()) - #if info: - # lang = info.split(None, 1)[0] - # html += ' class="language-' + lang + '"' - #return html + '>' + escape_text(code) + '</code></pre>\n' - - return "" + render_html = '<pre><code' + if info is not None: + info = safe_entity(info.strip()) + if info: + lang = info.split(None, 1)[0] + render_html += ' class="language-' + lang + '"' + render_html = render_html + '>' + escape_text(code) + '</code></pre>\n' + return render_html ################################################################################ @@ -84,7 +102,12 @@ def generate_toc(tags): if tag["type"] == "heading": attrs = tag["attrs"] #attrs["id"] = "toc-none" - headers_list.append({"header":tag["children"][0]["raw"], "level":attrs["level"]}) + try: + headers_list.append({"header":tag["children"][0]["raw"], "level":attrs["level"]}) + except Exception as e: + print("Cant add tag:",tag) + #except: + # print("ERROR:",tag) #print(headers_list) ############################ #generate toc |