diff --git a/CHANGES b/CHANGES index 2cf1b72cb..c6004c3f0 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,8 @@ Release 0.6.4 (in development) ============================== +* Restore compatibility with Pygments >= 1.2. + * #295: Fix escaping of hyperref targets in LaTeX output. * #302: Fix links generated by the ``:doc:`` role for LaTeX output. diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index 3f029dc7a..3de968806 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -20,7 +20,7 @@ except ImportError: # parser is not available on Jython parser = None -from sphinx.util.texescape import tex_hl_escape_map +from sphinx.util.texescape import tex_hl_escape_map_old, tex_hl_escape_map_new try: import pygments @@ -127,7 +127,7 @@ class PygmentsBridge(object): # first, escape highlighting characters like Pygments does source = source.translate(escape_hl_chars) # then, escape all characters nonrepresentable in LaTeX - source = source.translate(tex_hl_escape_map) + source = source.translate(tex_hl_escape_map_old) return '\\begin{Verbatim}[commandchars=@\\[\\]]\n' + \ source + '\\end{Verbatim}\n' @@ -204,7 +204,10 @@ class PygmentsBridge(object): return highlight(source, lexer, self.fmter[bool(linenos)]) else: hlsource = highlight(source, lexer, self.fmter[bool(linenos)]) - return hlsource.translate(tex_hl_escape_map) + if hlsource.startswith(r'\begin{Verbatim}[commandchars=\\\{\}'): + # Pygments >= 1.2 + return hlsource.translate(tex_hl_escape_map_new) + return hlsource.translate(tex_hl_escape_map_old) except ErrorToken: # this is most probably not the selected language, # so let it pass unhighlighted diff --git a/sphinx/util/texescape.py b/sphinx/util/texescape.py index 00d0325c4..737908295 100644 --- a/sphinx/util/texescape.py +++ b/sphinx/util/texescape.py @@ -99,8 +99,9 @@ tex_replacements = [ ] tex_escape_map = {} -tex_hl_escape_map = {} -_new_cmd_chars = {ord(u'\\'): u'@', ord(u'{'): u'[', ord(u'}'): u']'} +tex_hl_escape_map_old = {} # replacement map for Pygments <= 1.1 +tex_hl_escape_map_new = {} # replacement map for Pygments >= 1.2 +_old_cmd_chars = {ord(u'\\'): u'@', ord(u'{'): u'[', ord(u'}'): u']'} def init(): for a, b in tex_replacements: @@ -108,4 +109,5 @@ def init(): for a, b in tex_replacements: if a in u'[]{}\\': continue - tex_hl_escape_map[ord(a)] = b.translate(_new_cmd_chars) + tex_hl_escape_map_new[ord(a)] = b + tex_hl_escape_map_old[ord(a)] = b.translate(_old_cmd_chars)