diff options
Diffstat (limited to 'sphinx/highlighting.py')
-rw-r--r-- | sphinx/highlighting.py | 40 |
1 files changed, 12 insertions, 28 deletions
diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index df422321..c30c9ef3 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -63,12 +63,6 @@ _LATEX_STYLES = r''' \newcommand\PYGZcb{\char`\}} ''' -parsing_exceptions = (SyntaxError, UnicodeEncodeError) -if sys.version_info < (2, 5): - # Python <= 2.4 raises MemoryError when parsing an - # invalid encoding cookie - parsing_exceptions += MemoryError, - class PygmentsBridge(object): # Set these attributes if you want to have different Pygments formatters @@ -131,10 +125,6 @@ class PygmentsBridge(object): # lines beginning with "..." are probably placeholders for suite 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 - if sys.version_info < (3, 0) and isinstance(src, unicode): # Non-ASCII chars will only occur in string literals # and comments. If we wanted to give them to the parser @@ -143,18 +133,12 @@ class PygmentsBridge(object): # just replace all non-ASCII characters. src = src.encode('ascii', 'replace') - if (3, 0) <= sys.version_info < (3, 2): - # Python 3.1 can't process '\r' as linesep. - # `parser.suite("print('hello')\r\n")` cause error. - if '\r\n' in src: - src = src.replace('\r\n', '\n') - if parser is None: return True try: parser.suite(src) - except parsing_exceptions: + except (SyntaxError, UnicodeEncodeError): return False else: return True @@ -175,7 +159,7 @@ class PygmentsBridge(object): if self.try_parse(source): lexer = lexers['python'] else: - return self.unhighlighted(source) + lexer = lexers['none'] else: lexer = lexers['python'] elif lang in ('python3', 'py3') and source.startswith('>>>'): @@ -185,7 +169,7 @@ class PygmentsBridge(object): try: lexer = guess_lexer(source) except Exception: - return self.unhighlighted(source) + lexer = lexers['none'] else: if lang in lexers: lexer = lexers[lang] @@ -195,7 +179,7 @@ class PygmentsBridge(object): except ClassNotFound: if warn: warn('Pygments lexer name %r is not known' % lang) - return self.unhighlighted(source) + lexer = lexers['none'] else: raise else: @@ -207,19 +191,19 @@ class PygmentsBridge(object): source = doctest.doctestopt_re.sub('', source) # highlight via Pygments + formatter = self.get_formatter(**kwargs) try: - formatter = self.get_formatter(**kwargs) hlsource = highlight(source, lexer, formatter) - if self.dest == 'html': - return hlsource - else: - if not isinstance(hlsource, unicode): # Py2 / Pygments < 1.6 - hlsource = hlsource.decode() - return hlsource.translate(tex_hl_escape_map_new) except ErrorToken: # this is most probably not the selected language, # so let it pass unhighlighted - return self.unhighlighted(source) + hlsource = highlight(source, lexers['none'], formatter) + if self.dest == 'html': + return hlsource + else: + if not isinstance(hlsource, unicode): # Py2 / Pygments < 1.6 + hlsource = hlsource.decode() + return hlsource.translate(tex_hl_escape_map_new) def get_stylesheet(self): if not pygments: |