Anonymous avatar Anonymous committed 6975feb

Only highlight snippets as Python if they can be parsed as Python.

Comments (0)

Files changed (4)

sphinx/addnodes.py

 # sets the highlighting language for literal blocks
 class highlightlang(nodes.Element): pass
 
-# doesn't apply further text processors, e.g. smartypants
+# like emphasis, but doesn't apply further text processors, e.g. smartypants
 class literal_emphasis(nodes.emphasis): pass
 
 # make them known to docutils. this is needed, because the HTMl writer

sphinx/highlighting.py

 """
 
 import cgi
+import parser
 from collections import defaultdict
 
 try:
         return '<pre>' + cgi.escape(source) + '</pre>\n'
     if lang == 'python':
         if source.startswith('>>>'):
+            # interactive session
             lexer = lexers['pycon']
         else:
-            lexer = lexers['python']
+            # maybe Python -- try parsing it
+            try:
+                parser.suite(source + '\n')
+            except SyntaxError:
+                return '<pre>' + cgi.escape(source) + '</pre>\n'
+            else:
+                lexer = lexers['python']
     else:
         lexer = lexers[lang]
     try:
         return highlight(source, lexer, fmter)
     except ErrorToken:
-        # this is most probably not Python, so let it pass textonly
+        # this is most probably not Python, so let it pass unhighlighted
         return '<pre>' + cgi.escape(source) + '</pre>\n'
 
 def get_stylesheet():

sphinx/style/default.css

 }
 
 table.docutils td, table.docutils th {
-    padding: 0 8px 2px 0;
+    padding: 1px 8px 1px 0;
     border-top: 0;
     border-left: 0;
     border-right: 0;
 tt {
     font-family: 'Bitstream Vera Sans Mono', monospace;
     background-color: #ecf0f3;
-    padding: 1px;
+    padding: 0 1px 0 1px;
 }
 
 tt.descname {

sphinx/style/traditional.css

 tt {
     font-family: 'Bitstream Vera Sans Mono', monospace;
     background-color: #ecf0f3;
-    padding: 1px;
+    padding: 0 1px 0 1px;
 }
 
 tt.descname {
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.