diff options
author | Mark Lodato <lodatom@gmail.com> | 2011-07-06 21:52:10 -0400 |
---|---|---|
committer | Mark Lodato <lodatom@gmail.com> | 2011-07-06 21:52:10 -0400 |
commit | cab5a5edc165a524aacac1f858376e8f5446d25a (patch) | |
tree | a6f8f5a9351ea94e10ee25690f26227ec05f8d4f /sphinx/highlighting.py | |
parent | b5f53e1972393161fdd00c240bc47b9953134627 (diff) | |
download | sphinx-git-cab5a5edc165a524aacac1f858376e8f5446d25a.tar.gz |
PygmentsBridge: construct new formatter each time
Previously, we pre-constructed two different formatters, one with line
numbers and one without. This made the code rather confusing and also
was not scalable for options that take arguments, such as 'hl_lines'.
Now, we construct a new HTMLFormatter or LatexFormatter on each call to
highlight_block().
Diffstat (limited to 'sphinx/highlighting.py')
-rw-r--r-- | sphinx/highlighting.py | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index 76bcb122d..75c8be13a 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -94,14 +94,12 @@ class PygmentsBridge(object): else: style = get_style_by_name(stylename) self.trim_doctest_flags = trim_doctest_flags + self.formatter_args = {'style' : style} if dest == 'html': - self.fmter = {False: self.html_formatter(style=style), - True: self.html_formatter(style=style, linenos=True)} + self.formatter = self.html_formatter else: - self.fmter = {False: self.latex_formatter(style=style, - commandprefix='PYG'), - True: self.latex_formatter(style=style, linenos=True, - commandprefix='PYG')} + self.formatter = self.latex_formatter + self.formatter_args['commandprefix'] = 'PYG' def unhighlighted(self, source): if self.dest == 'html': @@ -200,29 +198,36 @@ class PygmentsBridge(object): # highlight via Pygments try: + formatter = self.get_formatter(linenos=bool(linenos)) + hlsource = highlight(source, lexer, formatter) if self.dest == 'html': - return highlight(source, lexer, self.fmter[bool(linenos)]) + return hlsource + elif hlsource.startswith(r'\begin{Verbatim}[commandchars=\\\{\}'): + # Pygments >= 1.2 + return hlsource.translate(tex_hl_escape_map_new) else: - hlsource = highlight(source, lexer, self.fmter[bool(linenos)]) - if hlsource.startswith(r'\begin{Verbatim}[commandchars=\\\{\}'): - # Pygments >= 1.2 - return hlsource.translate(tex_hl_escape_map_new) return hlsource.translate(tex_hl_escape_map_old) except ErrorToken: # this is most probably not the selected language, # so let it pass unhighlighted return self.unhighlighted(source) + def get_formatter(self, **kwargs_orig): + kwargs = self.formatter_args.copy() + kwargs.update(kwargs_orig) + return self.formatter(**kwargs) + def get_stylesheet(self): if not pygments: if self.dest == 'latex': return _LATEX_STYLES # no HTML styles needed return '' + formatter = self.get_formatter() if self.dest == 'html': - return self.fmter[0].get_style_defs('.highlight') + return formatter.get_style_defs('.highlight') else: - styledefs = self.fmter[0].get_style_defs() + styledefs = formatter.get_style_defs() # workaround for Pygments < 0.12 if styledefs.startswith('\\newcommand\\at{@}'): styledefs += _LATEX_STYLES |