Apply "unindent" to highlighted code blocks too.

This commit is contained in:
Sergio Martínez Portela 2022-12-20 00:56:53 +01:00
parent 2d0f71bca1
commit fcb44ca1a6

View File

@ -462,6 +462,12 @@ def render_block(content, acc, _class, is_code):
acc.append('<code>') acc.append('<code>')
# Remove indentation common to all lines # Remove indentation common to all lines
acc.append(unindent(content))
if is_code:
acc.append('</code>')
acc.append('</pre>')
def unindent(content):
base_indentation = min([ base_indentation = min([
len(l) - len(l.lstrip(' ')) len(l) - len(l.lstrip(' '))
for l in content.split('\n') for l in content.split('\n')
@ -471,18 +477,15 @@ def render_block(content, acc, _class, is_code):
l[base_indentation:] l[base_indentation:]
for l in content.split('\n') for l in content.split('\n')
] ]
return '\n'.join(content_lines)
acc.append('\n'.join(content_lines))
if is_code:
acc.append('</code>')
acc.append('</pre>')
def render_code_block(element, acc, headline, graph): def render_code_block(element, acc, headline, graph):
code = element.lines code = element.lines
if element.arguments is not None and len(element.arguments) > 0 : if element.arguments is not None and len(element.arguments) > 0 :
try: try:
lexer = pygments.lexers.get_lexer_by_name(element.arguments.split()[0], stripall=True) lexer = pygments.lexers.get_lexer_by_name(element.arguments.split()[0], stripall=True)
content = pygments.highlight(code, content = pygments.highlight(unindent(code),
lexer, lexer,
pygments.formatters.HtmlFormatter() pygments.formatters.HtmlFormatter()
) )