Remove workaround for pygments empty newlines; there's a real solution

This commit is contained in:
Klaas van Schelven
2024-03-29 18:35:43 +01:00
parent 475977529a
commit aca96e7dc9

View File

@@ -19,20 +19,13 @@ def _split(joined, lengths):
return result
def noempty(lines):
# if the line is empty, replace it with a space; this is needed to avoid pygments dropping empty lines (I've
# obvserved this for leading empty lines, but the problem might be more general)
# https://github.com/pygments/pygments/issues/2673
return [" " if not line else line for line in lines]
@register.filter
def pygmentize(value):
# first, get the actual code from the frame
lengths = [len(value['pre_context']), 1, len(value['post_context'])]
code = "\n".join(noempty(value['pre_context']) + [value['context_line']] + noempty(value['post_context']))
pygments_result = highlight(code, PythonLexer(), HtmlFormatter(nowrap=True))
code = "\n".join(value['pre_context'] + [value['context_line']] + value['post_context'])
pygments_result = highlight(code, PythonLexer(stripnl=False), HtmlFormatter(nowrap=True))
lines = pygments_result.split('\n')[:-1] # remove the last empty line, which is a result of split()
assert len(lines) == sum(lengths), "%d != %d" % (len(lines), sum(lengths))