mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Make pygments unconditional, it is required by setup.py anyway.
This commit is contained in:
@@ -24,46 +24,32 @@ from sphinx.util.pycompat import htmlescape
|
|||||||
from sphinx.util.texescape import tex_hl_escape_map_new
|
from sphinx.util.texescape import tex_hl_escape_map_new
|
||||||
from sphinx.ext import doctest
|
from sphinx.ext import doctest
|
||||||
|
|
||||||
try:
|
from pygments import highlight
|
||||||
import pygments
|
from pygments.lexers import PythonLexer, PythonConsoleLexer, CLexer, \
|
||||||
from pygments import highlight
|
TextLexer, RstLexer
|
||||||
from pygments.lexers import PythonLexer, PythonConsoleLexer, CLexer, \
|
from pygments.lexers import get_lexer_by_name, guess_lexer
|
||||||
TextLexer, RstLexer
|
from pygments.formatters import HtmlFormatter, LatexFormatter
|
||||||
from pygments.lexers import get_lexer_by_name, guess_lexer
|
from pygments.filters import ErrorToken
|
||||||
from pygments.formatters import HtmlFormatter, LatexFormatter
|
from pygments.styles import get_style_by_name
|
||||||
from pygments.filters import ErrorToken
|
from pygments.util import ClassNotFound
|
||||||
from pygments.styles import get_style_by_name
|
from sphinx.pygments_styles import SphinxStyle, NoneStyle
|
||||||
from pygments.util import ClassNotFound
|
|
||||||
from sphinx.pygments_styles import SphinxStyle, NoneStyle
|
|
||||||
except ImportError:
|
|
||||||
pygments = None
|
|
||||||
lexers = None
|
|
||||||
HtmlFormatter = LatexFormatter = None
|
|
||||||
else:
|
|
||||||
|
|
||||||
lexers = dict(
|
lexers = dict(
|
||||||
none = TextLexer(),
|
none = TextLexer(),
|
||||||
python = PythonLexer(),
|
python = PythonLexer(),
|
||||||
pycon = PythonConsoleLexer(),
|
pycon = PythonConsoleLexer(),
|
||||||
pycon3 = PythonConsoleLexer(python3=True),
|
pycon3 = PythonConsoleLexer(python3=True),
|
||||||
rest = RstLexer(),
|
rest = RstLexer(),
|
||||||
c = CLexer(),
|
c = CLexer(),
|
||||||
)
|
)
|
||||||
for _lexer in lexers.values():
|
for _lexer in lexers.values():
|
||||||
_lexer.add_filter('raiseonerror')
|
_lexer.add_filter('raiseonerror')
|
||||||
|
|
||||||
|
|
||||||
escape_hl_chars = {ord(u'\\'): u'\\PYGZbs{}',
|
escape_hl_chars = {ord(u'\\'): u'\\PYGZbs{}',
|
||||||
ord(u'{'): u'\\PYGZob{}',
|
ord(u'{'): u'\\PYGZob{}',
|
||||||
ord(u'}'): u'\\PYGZcb{}'}
|
ord(u'}'): u'\\PYGZcb{}'}
|
||||||
|
|
||||||
# used if Pygments is not available
|
|
||||||
_LATEX_STYLES = r'''
|
|
||||||
\newcommand\PYGZbs{\char`\\}
|
|
||||||
\newcommand\PYGZob{\char`\{}
|
|
||||||
\newcommand\PYGZcb{\char`\}}
|
|
||||||
'''
|
|
||||||
|
|
||||||
# used if Pygments is available
|
# used if Pygments is available
|
||||||
# use textcomp quote to get a true single quote
|
# use textcomp quote to get a true single quote
|
||||||
_LATEX_ADD_STYLES = r'''
|
_LATEX_ADD_STYLES = r'''
|
||||||
@@ -80,8 +66,6 @@ class PygmentsBridge(object):
|
|||||||
def __init__(self, dest='html', stylename='sphinx',
|
def __init__(self, dest='html', stylename='sphinx',
|
||||||
trim_doctest_flags=False):
|
trim_doctest_flags=False):
|
||||||
self.dest = dest
|
self.dest = dest
|
||||||
if not pygments:
|
|
||||||
return
|
|
||||||
if stylename is None or stylename == 'sphinx':
|
if stylename is None or stylename == 'sphinx':
|
||||||
style = SphinxStyle
|
style = SphinxStyle
|
||||||
elif stylename == 'none':
|
elif stylename == 'none':
|
||||||
@@ -153,8 +137,6 @@ class PygmentsBridge(object):
|
|||||||
def highlight_block(self, source, lang, warn=None, force=False, **kwargs):
|
def highlight_block(self, source, lang, warn=None, force=False, **kwargs):
|
||||||
if not isinstance(source, text_type):
|
if not isinstance(source, text_type):
|
||||||
source = source.decode()
|
source = source.decode()
|
||||||
if not pygments:
|
|
||||||
return self.unhighlighted(source)
|
|
||||||
|
|
||||||
# find out which lexer to use
|
# find out which lexer to use
|
||||||
if lang in ('py', 'python'):
|
if lang in ('py', 'python'):
|
||||||
@@ -213,11 +195,6 @@ class PygmentsBridge(object):
|
|||||||
return hlsource.translate(tex_hl_escape_map_new)
|
return hlsource.translate(tex_hl_escape_map_new)
|
||||||
|
|
||||||
def get_stylesheet(self):
|
def get_stylesheet(self):
|
||||||
if not pygments:
|
|
||||||
if self.dest == 'latex':
|
|
||||||
return _LATEX_STYLES
|
|
||||||
# no HTML styles needed
|
|
||||||
return ''
|
|
||||||
formatter = self.get_formatter()
|
formatter = self.get_formatter()
|
||||||
if self.dest == 'html':
|
if self.dest == 'html':
|
||||||
return formatter.get_style_defs('.highlight')
|
return formatter.get_style_defs('.highlight')
|
||||||
|
Reference in New Issue
Block a user