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

This commit is contained in:
Georg Brandl 2007-08-04 23:33:10 +00:00
parent 3d44c1cb7f
commit 134590216d
4 changed files with 14 additions and 6 deletions

View File

@ -50,7 +50,7 @@ class compact_paragraph(nodes.paragraph): pass
# 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

View File

@ -10,6 +10,7 @@
"""
import cgi
import parser
from collections import defaultdict
try:
@ -57,15 +58,22 @@ def highlight_block(source, lang):
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():

View File

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

View File

@ -606,7 +606,7 @@ pre {
tt {
font-family: 'Bitstream Vera Sans Mono', monospace;
background-color: #ecf0f3;
padding: 1px;
padding: 0 1px 0 1px;
}
tt.descname {