Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/sphinx-doc/sphinx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2019-05-28 19:26:18 +0300
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2019-06-01 06:48:15 +0300
commite9c5e3656fc74c66eb31f897c9f485a10912a2c9 (patch)
tree6280f2f897d1ab8363421497cae49213e6660703 /sphinx/highlighting.py
parent1769280348c57484dc7db7f83e5594a54522abc3 (diff)
highlight: Enable raiseonerror only if force=False
Diffstat (limited to 'sphinx/highlighting.py')
-rw-r--r--sphinx/highlighting.py67
1 files changed, 38 insertions, 29 deletions
diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py
index dd602708a..fba7226c2 100644
--- a/sphinx/highlighting.py
+++ b/sphinx/highlighting.py
@@ -10,6 +10,7 @@
import html
import warnings
+from functools import partial
from pygments import highlight
from pygments.filters import ErrorToken
@@ -37,17 +38,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': TextLexer,
+ 'python': PythonLexer,
+ 'python3': Python3Lexer,
+ 'pycon': PythonConsoleLexer,
+ 'pycon3': partial(PythonConsoleLexer, python3=True),
+ 'rest': RstLexer,
+ 'c': CLexer,
} # type: Dict[str, Lexer]
-for _lexer in lexers.values():
- _lexer.add_filter('raiseonerror')
escape_hl_chars = {ord('\\'): '\\PYGZbs{}',
@@ -115,46 +115,55 @@ class PygmentsBridge:
return '\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n' + \
source + '\\end{Verbatim}\n'
- 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
+ lexer_options = {'strip': True}
+ lexer_options.update(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](**lexer_options)
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, **lexer_options)
else:
- lexer.add_filter('raiseonerror')
+ lexer = get_lexer_by_name(lang, **lexer_options)
+ except ClassNotFound:
+ logger.warning(__('Pygments lexer name %r is not known'), lang,
+ location=location)
+ lexer = lexer_classes['none'](**lexer_options)
+
+ 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)
# trim doctest options if wanted
if isinstance(lexer, PythonConsoleLexer) and self.trim_doctest_flags: