Georg Brandl avatar Georg Brandl committed 204abaf

Closes #695: When the highlight language "python" is specified explicitly, do not try to parse the code to recognize non-Python snippets.

Thanks to Jonas Haag for the patch.

Comments (0)

Files changed (5)

 * PR#36: Make the "bibliography to TOC" fix in LaTeX output specific to
   the document class.
 
+* #695: When the highlight language "python" is specified explicitly,
+  do not try to parse the code to recognize non-Python snippets.
+
 
 Release 1.1.2 (Nov 1, 2011) -- 1.1.1 is a silly version number anyway!
 ======================================================================

sphinx/highlighting.py

         else:
             return True
 
-    def highlight_block(self, source, lang, warn=None, **kwargs):
+    def highlight_block(self, source, lang, warn=None, force=False, **kwargs):
         if not isinstance(source, unicode):
             source = source.decode()
         if not pygments:
             if source.startswith('>>>'):
                 # interactive session
                 lexer = lexers['pycon']
-            else:
+            elif not force:
                 # maybe Python -- try parsing it
                 if self.try_parse(source):
                     lexer = lexers['python']
                 else:
                     return self.unhighlighted(source)
+            else:
+                lexer = lexers['python']
         elif lang in ('python3', 'py3') and source.startswith('>>>'):
             # for py3, recognize interactive sessions, but do not try parsing...
             lexer = lexers['pycon3']

sphinx/writers/html.py

         lang = self.highlightlang
         linenos = node.rawsource.count('\n') >= \
                   self.highlightlinenothreshold - 1
+        highlight_args = node.get('highlight_args', {})
         if node.has_key('language'):
             # code-block directives
             lang = node['language']
+            highlight_args['force'] = True
         if node.has_key('linenos'):
             linenos = node['linenos']
-        highlight_args = node.get('highlight_args', {})
         def warner(msg):
             self.builder.warn(msg, (self.builder.current_docname, node.line))
         highlighted = self.highlighter.highlight_block(

sphinx/writers/latex.py

         code = self.verbatim.rstrip('\n')
         lang = self.hlsettingstack[-1][0]
         linenos = code.count('\n') >= self.hlsettingstack[-1][1] - 1
+        highlight_args = node.get('highlight_args', {})
         if 'language' in node:
             # code-block directives
             lang = node['language']
+            highlight_args['force'] = True
         if 'linenos' in node:
             linenos = node['linenos']
-        highlight_args = node.get('highlight_args', {})
         def warner(msg):
             self.builder.warn(msg, (self.curfilestack[-1], node.line))
         hlcode = self.highlighter.highlight_block(code, lang, warn=warner,

tests/test_build_html.py

             r'def'),
         (".//div[@class='inc-tab3 highlight-text']//pre",
             r'-| |-'),
-        (".//div[@class='inc-tab8 highlight-python']//pre",
+        (".//div[@class='inc-tab8 highlight-python']//pre/span",
             r'-|      |-'),
     ])
     HTML_XPATH['subdir/includes.html'].extend([
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.