[Python-checkins] r60939 - doctools/trunk/sphinx/highlighting.py
georg.brandl
python-checkins at python.org
Thu Feb 21 21:39:51 CET 2008
Author: georg.brandl
Date: Thu Feb 21 21:39:50 2008
New Revision: 60939
Modified:
doctools/trunk/sphinx/highlighting.py
Log:
Patch #2154 from Amaury: allow '...' in Python snippets; it's often
used to indicate some left-out code.
Modified: doctools/trunk/sphinx/highlighting.py
==============================================================================
--- doctools/trunk/sphinx/highlighting.py (original)
+++ doctools/trunk/sphinx/highlighting.py Thu Feb 21 21:39:50 2008
@@ -70,13 +70,23 @@
lexer = lexers['pycon']
else:
# maybe Python -- try parsing it
+ src = source + '\n'
+
+ # Replace "..." by a special mark,
+ # which is also a valid python expression
+ mark = "__highlighting__ellipsis__"
+ src = src.replace("...", mark)
+
+ # lines beginning with "..." are probably placeholders for suite
+ import re
+ src = re.sub(r"(?m)^(\s*)" + mark + "(.)", r"\1"+ mark + r"# \2", src)
+
+ # if we're using 2.5, use the with statement
+ if sys.version_info >= (2, 5):
+ src = 'from __future__ import with_statement\n' + src
+
try:
- # if we're using 2.5, use the with statement
- if sys.version_info >= (2, 5):
- parser.suite('from __future__ import with_statement\n' +
- source + '\n')
- else:
- parser.suite(source + '\n')
+ parser.suite(src)
except (SyntaxError, UnicodeEncodeError):
return unhighlighted()
else:
More information about the Python-checkins
mailing list