Merge pull request #6326 from tk0miya/refactor_highlighting

Refactor sphinx.highlighting
This commit is contained in:
Takeshi KOMIYA
2019-05-12 17:03:01 +09:00
committed by GitHub

View File

@@ -32,6 +32,7 @@ if False:
# For type annotation # For type annotation
from typing import Any, Dict # NOQA from typing import Any, Dict # NOQA
from pygments.formatter import Formatter # NOQA from pygments.formatter import Formatter # NOQA
from pygments.style import Style # NOQA
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -69,16 +70,8 @@ class PygmentsBridge:
def __init__(self, dest='html', stylename='sphinx', trim_doctest_flags=None): def __init__(self, dest='html', stylename='sphinx', trim_doctest_flags=None):
# type: (str, str, bool) -> None # type: (str, str, bool) -> None
self.dest = dest self.dest = dest
if stylename is None or stylename == 'sphinx':
style = SphinxStyle style = self.get_style(stylename)
elif stylename == 'none':
style = NoneStyle
elif '.' in stylename:
module, stylename = stylename.rsplit('.', 1)
style = getattr(__import__(module, None, None, ['__name__']),
stylename)
else:
style = get_style_by_name(stylename)
self.formatter_args = {'style': style} # type: Dict[str, Any] self.formatter_args = {'style': style} # type: Dict[str, Any]
if dest == 'html': if dest == 'html':
self.formatter = self.html_formatter self.formatter = self.html_formatter
@@ -91,6 +84,18 @@ class PygmentsBridge:
warnings.warn('trim_doctest_flags option for PygmentsBridge is now deprecated.', warnings.warn('trim_doctest_flags option for PygmentsBridge is now deprecated.',
RemovedInSphinx30Warning, stacklevel=2) RemovedInSphinx30Warning, stacklevel=2)
def get_style(self, stylename):
# type: (str) -> Style
if stylename is None or stylename == 'sphinx':
return SphinxStyle
elif stylename == 'none':
return NoneStyle
elif '.' in stylename:
module, stylename = stylename.rsplit('.', 1)
return getattr(__import__(module, None, None, ['__name__']), stylename)
else:
return get_style_by_name(stylename)
def get_formatter(self, **kwargs): def get_formatter(self, **kwargs):
# type: (Any) -> Formatter # type: (Any) -> Formatter
kwargs.update(self.formatter_args) kwargs.update(self.formatter_args)
@@ -110,11 +115,8 @@ class PygmentsBridge:
return '\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n' + \ return '\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n' + \
source + '\\end{Verbatim}\n' source + '\\end{Verbatim}\n'
def highlight_block(self, source, lang, opts=None, location=None, force=False, **kwargs): def get_lexer(self, source, lang, opts=None, location=None):
# type: (str, str, Any, Any, bool, Any) -> str # type: (str, str, Any, Any) -> Lexer
if not isinstance(source, str):
source = source.decode()
# find out which lexer to use # find out which lexer to use
if lang in ('py', 'python'): if lang in ('py', 'python'):
if source.startswith('>>>'): if source.startswith('>>>'):
@@ -145,6 +147,15 @@ class PygmentsBridge:
else: else:
lexer.add_filter('raiseonerror') 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
if not isinstance(source, str):
source = source.decode()
lexer = self.get_lexer(source, lang, opts, location)
# trim doctest options if wanted # trim doctest options if wanted
if isinstance(lexer, PythonConsoleLexer) and self.trim_doctest_flags: if isinstance(lexer, PythonConsoleLexer) and self.trim_doctest_flags:
source = doctest.blankline_re.sub('', source) source = doctest.blankline_re.sub('', source)
@@ -165,6 +176,7 @@ class PygmentsBridge:
type='misc', subtype='highlighting_failure', type='misc', subtype='highlighting_failure',
location=location) location=location)
hlsource = highlight(source, lexers['none'], formatter) hlsource = highlight(source, lexers['none'], formatter)
if self.dest == 'html': if self.dest == 'html':
return hlsource return hlsource
else: else: