summaryrefslogtreecommitdiffstats
path: root/sphinx/highlighting.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/highlighting.py')
-rw-r--r--sphinx/highlighting.py59
1 files changed, 39 insertions, 20 deletions
diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py
index 7e0d94a..65a6dc3 100644
--- a/sphinx/highlighting.py
+++ b/sphinx/highlighting.py
@@ -42,9 +42,7 @@ lexer_classes: dict[str, type[Lexer] | partial[Lexer]] = {
}
-escape_hl_chars = {ord('\\'): '\\PYGZbs{}',
- ord('{'): '\\PYGZob{}',
- ord('}'): '\\PYGZcb{}'}
+escape_hl_chars = {ord('\\'): '\\PYGZbs{}', ord('{'): '\\PYGZob{}', ord('}'): '\\PYGZcb{}'}
# used if Pygments is available
# MEMO: no use of \protected here to avoid having to do hyperref extras,
@@ -57,7 +55,7 @@ escape_hl_chars = {ord('\\'): '\\PYGZbs{}',
# MEMO: the Pygments escapes with \char`\<char> syntax, if the document
# uses old OT1 font encoding, work correctly only in monospace font.
# MEMO: the Pygmentize output mark-up is always with a {} after.
-_LATEX_ADD_STYLES = r'''
+_LATEX_ADD_STYLES = r"""
% Sphinx redefinitions
% Originally to obtain a straight single quote via package textcomp, then
% to fix problems for the 5.0.0 inline code highlighting (captions!).
@@ -82,7 +80,7 @@ _LATEX_ADD_STYLES = r'''
% use \protected to allow syntax highlighting in captions
\protected\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+{\PYG@do{#2}}}
\makeatother
-'''
+"""
class PygmentsBridge:
@@ -91,8 +89,9 @@ class PygmentsBridge:
html_formatter = HtmlFormatter
latex_formatter = LatexFormatter
- def __init__(self, dest: str = 'html', stylename: str = 'sphinx',
- latex_engine: str | None = None) -> None:
+ def __init__(
+ self, dest: str = 'html', stylename: str = 'sphinx', latex_engine: str | None = None
+ ) -> None:
self.dest = dest
self.latex_engine = latex_engine
@@ -104,8 +103,8 @@ class PygmentsBridge:
self.formatter = self.latex_formatter
self.formatter_args['commandprefix'] = 'PYG'
- def get_style(self, stylename: str) -> Style:
- if stylename is None or stylename == 'sphinx':
+ def get_style(self, stylename: str) -> type[Style]:
+ if not stylename or stylename == 'sphinx':
return SphinxStyle
elif stylename == 'none':
return NoneStyle
@@ -119,8 +118,14 @@ class PygmentsBridge:
kwargs.update(self.formatter_args)
return self.formatter(**kwargs)
- def get_lexer(self, source: str, lang: str, opts: dict | None = None,
- force: bool = False, location: Any = None) -> Lexer:
+ def get_lexer(
+ self,
+ source: str,
+ lang: str,
+ opts: dict | None = None,
+ force: bool = False,
+ location: Any = None,
+ ) -> Lexer:
if not opts:
opts = {}
@@ -146,8 +151,9 @@ class PygmentsBridge:
else:
lexer = get_lexer_by_name(lang, **opts)
except ClassNotFound:
- logger.warning(__('Pygments lexer name %r is not known'), lang,
- location=location)
+ logger.warning(
+ __('Pygments lexer name %r is not known'), lang, location=location
+ )
lexer = lexer_classes['none'](**opts)
if not force:
@@ -155,8 +161,15 @@ class PygmentsBridge:
return lexer
- def highlight_block(self, source: str, lang: str, opts: dict | None = None,
- force: bool = False, location: Any = None, **kwargs: Any) -> str:
+ def highlight_block(
+ self,
+ source: str,
+ lang: str,
+ opts: dict | None = None,
+ force: bool = False,
+ location: Any = None,
+ **kwargs: Any,
+ ) -> str:
if not isinstance(source, str):
source = source.decode()
@@ -173,11 +186,17 @@ class PygmentsBridge:
lang = 'none' # automatic highlighting failed.
else:
logger.warning(
- __('Lexing literal_block %r as "%s" resulted in an error at token: %r. '
- 'Retrying in relaxed mode.'),
- source, lang, str(err),
- type='misc', subtype='highlighting_failure',
- location=location)
+ __(
+ 'Lexing literal_block %r as "%s" resulted in an error at token: %r. '
+ 'Retrying in relaxed mode.'
+ ),
+ source,
+ lang,
+ str(err),
+ type='misc',
+ subtype='highlighting_failure',
+ location=location,
+ )
if force:
lang = 'none'
else: