mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Only highlight snippets as Python if they can be parsed as Python.
This commit is contained in:
parent
3d44c1cb7f
commit
134590216d
@ -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
|
||||
|
@ -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():
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user