diff options
Diffstat (limited to 'sphinx/highlighting.py')
-rw-r--r-- | sphinx/highlighting.py | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index f52e99019..5fac63963 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ sphinx.highlighting ~~~~~~~~~~~~~~~~~~~ @@ -9,6 +8,7 @@ :license: BSD, see LICENSE for details. """ +import html import warnings from pygments import highlight @@ -20,14 +20,12 @@ from pygments.lexers import PythonLexer, Python3Lexer, PythonConsoleLexer, \ CLexer, TextLexer, RstLexer from pygments.styles import get_style_by_name from pygments.util import ClassNotFound -from six import text_type from sphinx.deprecation import RemovedInSphinx30Warning from sphinx.ext import doctest from sphinx.locale import __ from sphinx.pygments_styles import SphinxStyle, NoneStyle from sphinx.util import logging -from sphinx.util.pycompat import htmlescape from sphinx.util.texescape import tex_hl_escape_map_new if False: @@ -38,22 +36,22 @@ if False: logger = logging.getLogger(__name__) -lexers = dict( - none = TextLexer(stripnl=False), - python = PythonLexer(stripnl=False), - python3 = Python3Lexer(stripnl=False), - pycon = PythonConsoleLexer(stripnl=False), - pycon3 = PythonConsoleLexer(python3=True, stripnl=False), - rest = RstLexer(stripnl=False), - c = CLexer(stripnl=False), -) # type: Dict[unicode, Lexer] +lexers = { + 'none': TextLexer(stripnl=False), + 'python': PythonLexer(stripnl=False), + 'python3': Python3Lexer(stripnl=False), + 'pycon': PythonConsoleLexer(stripnl=False), + 'pycon3': PythonConsoleLexer(python3=True, stripnl=False), + 'rest': RstLexer(stripnl=False), + 'c': CLexer(stripnl=False), +} # type: Dict[str, Lexer] for _lexer in lexers.values(): _lexer.add_filter('raiseonerror') -escape_hl_chars = {ord(u'\\'): u'\\PYGZbs{}', - ord(u'{'): u'\\PYGZob{}', - ord(u'}'): u'\\PYGZcb{}'} +escape_hl_chars = {ord('\\'): '\\PYGZbs{}', + ord('{'): '\\PYGZob{}', + ord('}'): '\\PYGZcb{}'} # used if Pygments is available # use textcomp quote to get a true single quote @@ -62,14 +60,14 @@ _LATEX_ADD_STYLES = r''' ''' -class PygmentsBridge(object): +class PygmentsBridge: # Set these attributes if you want to have different Pygments formatters # than the default ones. html_formatter = HtmlFormatter latex_formatter = LatexFormatter def __init__(self, dest='html', stylename='sphinx', trim_doctest_flags=None): - # type: (unicode, unicode, bool) -> None + # type: (str, str, bool) -> None self.dest = dest if stylename is None or stylename == 'sphinx': style = SphinxStyle @@ -81,7 +79,7 @@ class PygmentsBridge(object): stylename) else: style = get_style_by_name(stylename) - self.formatter_args = {'style': style} # type: Dict[unicode, Any] + self.formatter_args = {'style': style} # type: Dict[str, Any] if dest == 'html': self.formatter = self.html_formatter else: @@ -95,15 +93,15 @@ class PygmentsBridge(object): def get_formatter(self, **kwargs): # type: (Any) -> Formatter - kwargs.update(self.formatter_args) # type: ignore + kwargs.update(self.formatter_args) return self.formatter(**kwargs) def unhighlighted(self, source): - # type: (unicode) -> unicode + # type: (str) -> str warnings.warn('PygmentsBridge.unhighlighted() is now deprecated.', RemovedInSphinx30Warning, stacklevel=2) if self.dest == 'html': - return '<pre>' + htmlescape(source) + '</pre>\n' + return '<pre>' + html.escape(source) + '</pre>\n' else: # first, escape highlighting characters like Pygments does source = source.translate(escape_hl_chars) @@ -113,8 +111,8 @@ class PygmentsBridge(object): source + '\\end{Verbatim}\n' def highlight_block(self, source, lang, opts=None, location=None, force=False, **kwargs): - # type: (unicode, unicode, Any, Any, bool, Any) -> unicode - if not isinstance(source, text_type): + # type: (str, str, Any, Any, bool, Any) -> str + if not isinstance(source, str): source = source.decode() # find out which lexer to use @@ -173,7 +171,7 @@ class PygmentsBridge(object): return hlsource.translate(tex_hl_escape_map_new) def get_stylesheet(self): - # type: () -> unicode + # type: () -> str formatter = self.get_formatter() if self.dest == 'html': return formatter.get_style_defs('.highlight') |