Commits

Mark Lodato committed 1507481

PygmentsBridge: pass keyword arguments to Pygments

In Pygments.highlight_block, pass all extra keyword arguments, including
linenos, directly to the Pygments formatter. This allows custom
directives to use other Pygments features.

  • Participants
  • Parent commits e45b224

Comments (0)

Files changed (4)

sphinx/ext/viewcode.py

         # construct a page name for the highlighted source
         pagename = '_modules/' + modname.replace('.', '/')
         # highlight the source using the builder's highlighter
-        highlighted = highlighter.highlight_block(code, 'python', False)
+        highlighted = highlighter.highlight_block(code, 'python', linenos=False)
         # split the code into lines
         lines = highlighted.splitlines()
         # split off wrap markup from the first line of the actual code

sphinx/highlighting.py

         else:
             return True
 
-    def highlight_block(self, source, lang, linenos=False, warn=None):
+    def highlight_block(self, source, lang, warn=None, **kwargs):
         if not isinstance(source, unicode):
             source = source.decode()
         if not pygments:
 
         # highlight via Pygments
         try:
-            formatter = self.get_formatter(linenos=bool(linenos))
+            formatter = self.get_formatter(**kwargs)
             hlsource = highlight(source, lexer, formatter)
             if self.dest == 'html':
                 return hlsource

sphinx/writers/html.py

         def warner(msg):
             self.builder.warn(msg, (self.builder.current_docname, node.line))
         highlighted = self.highlighter.highlight_block(
-            node.rawsource, lang, linenos, warn=warner)
+            node.rawsource, lang, warn=warner, linenos=linenos)
         starttag = self.starttag(node, 'div', suffix='',
                                  CLASS='highlight-%s' % lang)
         self.body.append(starttag + highlighted + '</div>\n')

sphinx/writers/latex.py

             linenos = node['linenos']
         def warner(msg):
             self.builder.warn(msg, (self.curfilestack[-1], node.line))
-        hlcode = self.highlighter.highlight_block(code, lang, linenos,
-                                                  warn=warner)
+        hlcode = self.highlighter.highlight_block(code, lang, warn=warner,
+                                                  linenos=linenos)
         # workaround for Unicode issue
         hlcode = hlcode.replace(u'€', u'@texteuro[]')
         # must use original Verbatim environment and "tabular" environment