Skip to content

Commit

Permalink
Prevent the diff viewer from styling .txt files.
Browse files Browse the repository at this point in the history
Pygments assumes that .txt files should be styled using ResourceLexer by
default, when guessing by filename. This is very often wrong, and
results in a bunch of error indicators in the style, for practically
every word in the file.

We now maintain a blacklist of filename extensions that won't be styled.
It so far just contains .txt files.

Testing Done:
Loaded a text file in the diff viewer before the fix, and saw all the
error indicators.

Loaded a text file after the fix and saw that it looked like a plain
text file.

Reviewed at https://reviews.reviewboard.org/r/7623/
  • Loading branch information
chipx86 committed Sep 14, 2015
1 parent 44cdfa5 commit f4a7da4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions reviewboard/diffviewer/chunk_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class DiffChunkGenerator(object):
STYLED_MAX_LINE_LEN = 1000
STYLED_MAX_LIMIT_BYTES = 200000 # 200KB

# A list of filename extensions that won't be styled.
STYLED_EXT_BLACKLIST = (
'.txt', # ResourceLexer is used as a default.
)

# Default tab size used in browsers.
TAB_SIZE = DiffOpcodeGenerator.TAB_SIZE

Expand Down Expand Up @@ -222,8 +227,11 @@ def _get_chunks_uncached(self):
try:
# TODO: Try to figure out the right lexer for these files
# once instead of twice.
markup_a = self._apply_pygments(old or '', source_file)
markup_b = self._apply_pygments(new or '', dest_file)
if not source_file.endswith(self.STYLED_EXT_BLACKLIST):
markup_a = self._apply_pygments(old or '', source_file)

if not dest_file.endswith(self.STYLED_EXT_BLACKLIST):
markup_b = self._apply_pygments(new or '', dest_file)
except:
pass

Expand Down

0 comments on commit f4a7da4

Please sign in to comment.