Commits

Mark Lodato committed e45b224

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().

  • Participants
  • Parent commits 2ce5bc6

Comments (0)

Files changed (1)

File sphinx/highlighting.py

         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':
 
         # 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