diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-06-01 19:02:54 +0300 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-06-01 19:02:54 +0300 |
commit | 4c19ab705873035abf1c7bed2cc4f3b7f63eeeb6 (patch) | |
tree | 068380c3bf7a13d4add9c0dd75b205013306d732 /sphinx/highlighting.py | |
parent | ce3c5735cd45308e887f66ac231284acd59e0b5c (diff) | |
parent | 54d5fcfaebc3364044761d30c0fed6bd4d3052c3 (diff) |
Merge commit '54d5fcfaebc3364044761d30c0fed6bd4d3052c3'
Diffstat (limited to 'sphinx/highlighting.py')
-rw-r--r-- | sphinx/highlighting.py | 71 |
1 files changed, 41 insertions, 30 deletions
diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index 995ca65d3..45a1298b9 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -8,6 +8,8 @@ :license: BSD, see LICENSE for details. """ +from functools import partial + from pygments import highlight from pygments.filters import ErrorToken from pygments.formatters import HtmlFormatter, LatexFormatter @@ -32,17 +34,16 @@ if False: logger = logging.getLogger(__name__) -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), +lexers = {} # type: Dict[str, Lexer] +lexer_classes = { + 'none': partial(TextLexer, stripnl=False), + 'python': partial(PythonLexer, stripnl=False), + 'python3': partial(Python3Lexer, stripnl=False), + 'pycon': partial(PythonConsoleLexer, stripnl=False), + 'pycon3': partial(PythonConsoleLexer, python3=True, stripnl=False), + 'rest': partial(RstLexer, stripnl=False), + 'c': partial(CLexer, stripnl=False), } # type: Dict[str, Lexer] -for _lexer in lexers.values(): - _lexer.add_filter('raiseonerror') escape_hl_chars = {ord('\\'): '\\PYGZbs{}', @@ -91,46 +92,55 @@ class PygmentsBridge: kwargs.update(self.formatter_args) return self.formatter(**kwargs) - def get_lexer(self, source, lang, opts=None, location=None): - # type: (str, str, Any, Any) -> Lexer + def get_lexer(self, source, lang, opts=None, force=False, location=None): + # type: (str, str, Dict, bool, Any) -> Lexer + if not opts: + opts = {} + # find out which lexer to use if lang in ('py', 'python'): if source.startswith('>>>'): # interactive session - lexer = lexers['pycon'] + lang = 'pycon' else: - lexer = lexers['python'] + lang = 'python' elif lang in ('py3', 'python3', 'default'): if source.startswith('>>>'): - lexer = lexers['pycon3'] + lang = 'pycon3' else: - lexer = lexers['python3'] + lang = 'python3' elif lang == 'guess': try: lexer = guess_lexer(source) except Exception: lexer = lexers['none'] + + if lang in lexers: + lexer = lexers[lang] + elif lang in lexer_classes: + lexer = lexer_classes[lang](**opts) else: - if lang in lexers: - lexer = lexers[lang] - else: - try: - lexer = lexers[lang] = get_lexer_by_name(lang, **(opts or {})) - except ClassNotFound: - logger.warning(__('Pygments lexer name %r is not known'), lang, - location=location) - lexer = lexers['none'] + try: + if lang == 'guess': + lexer = guess_lexer(lang, **opts) else: - lexer.add_filter('raiseonerror') + lexer = get_lexer_by_name(lang, **opts) + except ClassNotFound: + logger.warning(__('Pygments lexer name %r is not known'), lang, + location=location) + lexer = lexer_classes['none'](**opts) + + if not force: + lexer.add_filter('raiseonerror') return lexer - def highlight_block(self, source, lang, opts=None, location=None, force=False, **kwargs): - # type: (str, str, Any, Any, bool, Any) -> str + def highlight_block(self, source, lang, opts=None, force=False, location=None, **kwargs): + # type: (str, str, Dict, bool, Any, Any) -> str if not isinstance(source, str): source = source.decode() - lexer = self.get_lexer(source, lang, opts, location) + lexer = self.get_lexer(source, lang, opts, force, location) # highlight via Pygments formatter = self.get_formatter(**kwargs) @@ -146,7 +156,8 @@ class PygmentsBridge: 'Highlighting skipped.'), lang, type='misc', subtype='highlighting_failure', location=location) - hlsource = highlight(source, lexers['none'], formatter) + lexer = self.get_lexer(source, 'none', opts, force, location) + hlsource = highlight(source, lexer, formatter) if self.dest == 'html': return hlsource |