From 80ac6d89d8c9fc29d7afcdfcf0ebd7fa7b19a074 Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Sat, 4 Feb 2023 14:12:42 +0000 Subject: Add some failover rendering options --- src/md.py | 55 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/src/md.py b/src/md.py index 14d7930..365dbd0 100755 --- a/src/md.py +++ b/src/md.py @@ -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 = "
No render!
" 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: + 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 = '
' + escape_text(code) + '
\n' + render_failed = False + if render_failed: #default behaviout from the renderer - #html = '
' + escape_text(code) + '
\n' - - return "" + render_html = '
' + escape_text(code) + '
\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 -- cgit v1.2.3