Apply Syntax Highlight on code blocks.

This commit is contained in:
Sergio Martínez Portela 2022-11-15 21:11:36 +01:00
parent e7dc8ad1e7
commit 9a883d90dd

View File

@ -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('</pre>')
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>{title} @ {SITE_NAME}</title>
<meta http-equiv="refresh" content="0;./{topLevelHeadline.id}.node.html#{headline.id}" />
<link href="../static/style.css" rel="stylesheet"/>
<link href="../static/light-syntax.css" rel="stylesheet"/>
</head>
<body>
<nav>
@ -677,6 +696,7 @@ def as_document(html, title):
<meta charset="utf-8">
<title>{title} @ {SITE_NAME}</title>
<link href="../static/style.css" rel="stylesheet"/>
<link href="../static/light-syntax.css" rel="stylesheet"/>
<script type="text/javascript">
function toggle_expand(header_id) {{
var e = document.getElementById(header_id);