diff options
author | Arturs Artamonovs <dos21h@gmail.com> | 2023-02-04 14:12:42 +0000 |
---|---|---|
committer | Arturs Artamonovs <dos21h@gmail.com> | 2023-02-04 14:12:42 +0000 |
commit | 80ac6d89d8c9fc29d7afcdfcf0ebd7fa7b19a074 (patch) | |
tree | 372b1fc1cb5a01b4bfc581c31014673b67ac028d | |
parent | 11eb2fc7f8e80bbaca30dfadf5c3552e8953a74b (diff) | |
download | md-site-80ac6d89d8c9fc29d7afcdfcf0ebd7fa7b19a074.tar.gz md-site-80ac6d89d8c9fc29d7afcdfcf0ebd7fa7b19a074.zip |
Add some failover rendering 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 |