From 9a883d90dd1d8caa89dda26e6795828aa77f7b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 15 Nov 2022 21:11:36 +0100 Subject: [PATCH] Apply Syntax Highlight on code blocks. --- scripts/generate.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/scripts/generate.py b/scripts/generate.py index b2e0ec2..8bf0f3d 100644 --- a/scripts/generate.py +++ b/scripts/generate.py @@ -21,6 +21,10 @@ from org_rw import dump as dump_org from org_rw import load as load_org from org_rw import token_list_to_raw +import pygments +import pygments.lexers +import pygments.formatters + # Set custom states for state in ("NEXT", "MEETING", "Q", "PAUSED", "SOMETIME", "TRACK", "WAITING"): org_rw.DEFAULT_TODO_KEYWORDS.append(state) @@ -447,7 +451,21 @@ def render_block(content, acc, _class, is_code): acc.append('') def render_code_block(element, acc, headline, graph): - content = html.escape(element.lines) + code = element.lines + if element.arguments is not None and len(element.arguments) > 0 : + try: + lexer = pygments.lexers.get_lexer_by_name(element.arguments.split()[0], stripall=True) + content = pygments.highlight(code, + lexer, + pygments.formatters.HtmlFormatter() + ) + acc.append(content) + return + + except pygments.util.ClassNotFound: + pass + logging.error("Cannot find lexer for {}".format(element.subtype.lower())) + content = html.escape(code) render_block(content, acc, _class='code ' + element.subtype.lower(), is_code=True) @@ -588,6 +606,7 @@ def render_as_document(headline, doc, headlineLevel, graph, title): {title} @ {SITE_NAME} +