Closes #929: Support parsed-literal blocks in LaTeX output correctly.

This commit is contained in:
Georg Brandl 2014-01-11 10:19:52 +01:00
parent b29226a060
commit 0103ff2a2b
4 changed files with 42 additions and 38 deletions

View File

@ -49,6 +49,8 @@ Bugs fixed
* #845: In code blocks, when the selected lexer fails, display line numbers
nevertheless if configured.
* #929: Support parsed-literal blocks in LaTeX output correctly.
Documentation
-------------

View File

@ -84,7 +84,7 @@ class MathDirective(Directive):
def latex_visit_math(self, node):
self.body.append('$' + node['latex'] + '$')
self.body.append('\\(' + node['latex'] + '\\)')
raise nodes.SkipNode
def latex_visit_displaymath(self, node):

View File

@ -30,6 +30,8 @@
\RequirePackage{wrapfig}
% Separate paragraphs by space by default.
\RequirePackage{parskip}
% For parsed-literal blocks.
\RequirePackage{alltt}
% Redefine these colors to your liking in the preamble.
\definecolor{TitleColor}{rgb}{0.126,0.263,0.361}

View File

@ -264,7 +264,6 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.next_figure_ids = set()
self.next_table_ids = set()
# flags
self.verbatim = None
self.in_title = 0
self.in_production_list = 0
self.in_footnote = 0
@ -1318,9 +1317,11 @@ class LaTeXTranslator(nodes.NodeVisitor):
raise UnsupportedError('%s:%s: literal blocks in footnotes are '
'not supported by LaTeX' %
(self.curfilestack[-1], node.line))
self.verbatim = ''
def depart_literal_block(self, node):
code = self.verbatim.rstrip('\n')
if node.rawsource != node.astext():
# most probably a parsed-literal block -- don't highlight
self.body.append('\\begin{alltt}\n')
else:
code = node.astext().rstrip('\n')
lang = self.hlsettingstack[-1][0]
linenos = code.count('\n') >= self.hlsettingstack[-1][1] - 1
highlight_args = node.get('highlight_args', {})
@ -1347,7 +1348,9 @@ class LaTeXTranslator(nodes.NodeVisitor):
hlcode = hlcode.rstrip() + '\n'
self.body.append('\n' + hlcode + '\\end{%sVerbatim}\n' %
(self.table and 'Original' or ''))
self.verbatim = None
raise nodes.SkipNode
def depart_literal_block(self, node):
self.body.append('\n\\end{alltt}\n')
visit_doctest_block = visit_literal_block
depart_doctest_block = depart_literal_block
@ -1510,9 +1513,6 @@ class LaTeXTranslator(nodes.NodeVisitor):
return self.encode(text).replace('\\textasciitilde{}', '~')
def visit_Text(self, node):
if self.verbatim is not None:
self.verbatim += node.astext()
else:
text = self.encode(node.astext())
if not self.no_contractions:
text = educate_quotes_latex(text)