mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Add highlighting for LaTeX builder.
This commit is contained in:
@@ -673,7 +673,7 @@ class LaTeXBuilder(Builder):
|
||||
components=(docwriter,)).get_default_values()
|
||||
|
||||
# XXX get names of toplevels automatically?
|
||||
for docname in ["library"]:#, "distutils", "documenting", "extending",
|
||||
for docname in ["c-api"]:#, "distutils", "documenting", "extending",
|
||||
#"howto", "install", "library", "reference",
|
||||
#"tutorial", "using"]:
|
||||
# XXX whatsnew missing
|
||||
|
||||
@@ -18,7 +18,7 @@ try:
|
||||
from pygments import highlight
|
||||
from pygments.lexers import PythonLexer, PythonConsoleLexer, CLexer, \
|
||||
TextLexer, RstLexer
|
||||
from pygments.formatters import HtmlFormatter
|
||||
from pygments.formatters import HtmlFormatter, LatexFormatter
|
||||
from pygments.filters import ErrorToken
|
||||
from pygments.style import Style
|
||||
from pygments.styles.friendly import FriendlyStyle
|
||||
@@ -28,7 +28,8 @@ except ImportError:
|
||||
else:
|
||||
class PythonDocStyle(Style):
|
||||
"""
|
||||
Like friendly, but a bit darker to enhance contrast on the green background.
|
||||
Like friendly, but a bit darker to enhance contrast on
|
||||
the green background.
|
||||
"""
|
||||
|
||||
background_color = '#eeffcc'
|
||||
@@ -51,12 +52,18 @@ else:
|
||||
for _lexer in lexers.values():
|
||||
_lexer.add_filter('raiseonerror')
|
||||
|
||||
fmter = HtmlFormatter(style=PythonDocStyle)
|
||||
hfmter = HtmlFormatter(style=PythonDocStyle)
|
||||
lfmter = LatexFormatter(style=PythonDocStyle)
|
||||
|
||||
|
||||
def highlight_block(source, lang):
|
||||
def highlight_block(source, lang, dest='html'):
|
||||
def unhighlighted():
|
||||
if dest == 'html':
|
||||
return '<pre>' + cgi.escape(source) + '</pre>\n'
|
||||
else:
|
||||
return source
|
||||
if not pygments:
|
||||
return '<pre>' + cgi.escape(source) + '</pre>\n'
|
||||
return unhighlighted()
|
||||
if lang == 'python':
|
||||
if source.startswith('>>>'):
|
||||
# interactive session
|
||||
@@ -67,16 +74,16 @@ def highlight_block(source, lang):
|
||||
parser.suite('from __future__ import with_statement\n' +
|
||||
source + '\n')
|
||||
except (SyntaxError, UnicodeEncodeError):
|
||||
return '<pre>' + cgi.escape(source) + '</pre>\n'
|
||||
return unhighlighted()
|
||||
else:
|
||||
lexer = lexers['python']
|
||||
else:
|
||||
lexer = lexers[lang]
|
||||
try:
|
||||
return highlight(source, lexer, fmter)
|
||||
return highlight(source, lexer, dest == 'html' and hfmter or lfmter)
|
||||
except ErrorToken:
|
||||
# this is most probably not Python, so let it pass unhighlighted
|
||||
return '<pre>' + cgi.escape(source) + '</pre>\n'
|
||||
return unhighlighted()
|
||||
|
||||
def get_stylesheet():
|
||||
return fmter.get_style_defs()
|
||||
def get_stylesheet(dest='html'):
|
||||
return (dest == 'html' and hfmter or lfmter).get_style_defs()
|
||||
|
||||
@@ -19,6 +19,7 @@ import string
|
||||
from docutils import frontend, nodes, languages, writers, utils
|
||||
|
||||
from . import addnodes
|
||||
from . import highlighting
|
||||
|
||||
|
||||
HEADER = r'''%% Generated by Sphinx.
|
||||
@@ -103,7 +104,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
||||
self.highlightlang = 'python'
|
||||
self.written_ids = set()
|
||||
# flags
|
||||
self.verbatim = 0
|
||||
self.verbatim = None
|
||||
self.in_title = 0
|
||||
self.first_document = 1
|
||||
self.this_is_the_title = 1
|
||||
@@ -111,6 +112,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
||||
|
||||
def astext(self):
|
||||
return (HEADER % self.options) + \
|
||||
highlighting.get_stylesheet('latex') + '\n\n' + \
|
||||
u''.join(self.body) + \
|
||||
(FOOTER % self.options)
|
||||
|
||||
@@ -517,11 +519,14 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
||||
raise nodes.SkipNode
|
||||
|
||||
def visit_literal_block(self, node):
|
||||
self.body.append('\n\\begin{Verbatim}\n')
|
||||
self.verbatim = 1
|
||||
#self.body.append('\n\\begin{Verbatim}\n')
|
||||
self.verbatim = ''
|
||||
def depart_literal_block(self, node):
|
||||
self.body.append('\n\\end{Verbatim}\n')
|
||||
self.verbatim = 0
|
||||
#self.body.append('\n\\end{Verbatim}\n')
|
||||
self.body.append('\n' + highlighting.highlight_block(self.verbatim,
|
||||
self.highlightlang,
|
||||
'latex'))
|
||||
self.verbatim = None
|
||||
visit_doctest_block = visit_literal_block
|
||||
depart_doctest_block = depart_literal_block
|
||||
|
||||
@@ -585,8 +590,6 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
||||
]
|
||||
|
||||
def encode(self, text):
|
||||
if self.verbatim:
|
||||
return text
|
||||
for x, y in self.replacements:
|
||||
text = text.replace(x, y)
|
||||
if self.literal_whitespace:
|
||||
@@ -596,7 +599,10 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
||||
return text
|
||||
|
||||
def visit_Text(self, node):
|
||||
self.body.append(self.encode(node.astext()))
|
||||
if self.verbatim is not None:
|
||||
self.verbatim += node.astext()
|
||||
else:
|
||||
self.body.append(self.encode(node.astext()))
|
||||
def depart_Text(self, node):
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user