[python] fix another bug in pygments.highlight()

Summary: Turns out there was also a problem with how this function deals with final newlines...

Reviewed By: jeremydubreil

Differential Revision: D4191154

fbshipit-source-id: fc10517
master
Jules Villard 8 years ago committed by Facebook Github Bot
parent 0ed033a32e
commit 81856c0d15

@ -73,7 +73,18 @@ def syntax_highlighting(source_name, mode, s):
while (i < len(s) and s[i] == '\n'): while (i < len(s) and s[i] == '\n'):
initial_newlines += '\n' initial_newlines += '\n'
i += 1 i += 1
return initial_newlines + pygments.highlight(s, lexer, formatter) # pygments.highlight() also insists that all string end with exactly one
# newline character regardless of the input string!
final_newlines = ''
i = 1
while (i <= len(s) and s[-i] == '\n'):
final_newlines += '\n'
i += 1
colorized_string = pygments.highlight(s, lexer, formatter)
# strip the result from pygments.highlight() to get rid of the
# potentially spurious final newline, and also to continue to
# work in case the bugs in pygments.highlight() gets fixed.
return initial_newlines + colorized_string.strip('\n') + final_newlines
def color(s, color, mode): def color(s, color, mode):

@ -72,7 +72,7 @@ def build_source_context(source_name, mode, report_line):
# avoid going past the end of the file # avoid going past the end of the file
for line in source_file: for line in source_file:
last_line = line_number last_line = line_number
if start_line <= line_number <= end_line: if start_line <= line_number < end_line:
excerpt += line excerpt += line
elif line_number > end_line: elif line_number > end_line:
# OPTIM: no need to read past the last line of the excerpt # OPTIM: no need to read past the last line of the excerpt

Loading…
Cancel
Save