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} +