summaryrefslogtreecommitdiff
path: root/sphinx/highlighting.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-02-21 20:39:50 +0000
committerGeorg Brandl <georg@python.org>2008-02-21 20:39:50 +0000
commit5740db88e8e8ce44fb9dffe37f3a542859d1d589 (patch)
treed372a291d8d3309f2ff983c0e8f8ad4b1ed0f59e /sphinx/highlighting.py
parent92271705fd5337d65b3803eade0100d8e311a96d (diff)
downloadsphinx-git-5740db88e8e8ce44fb9dffe37f3a542859d1d589.tar.gz
Patch #2154 from Amaury: allow '...' in Python snippets; it's often
used to indicate some left-out code.
Diffstat (limited to 'sphinx/highlighting.py')
-rw-r--r--sphinx/highlighting.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py
index 1734a669b..8729758ce 100644
--- a/sphinx/highlighting.py
+++ b/sphinx/highlighting.py
@@ -70,13 +70,23 @@ def highlight_block(source, lang, dest='html'):
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: