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 * #845: In code blocks, when the selected lexer fails, display line numbers
nevertheless if configured. nevertheless if configured.
* #929: Support parsed-literal blocks in LaTeX output correctly.
Documentation Documentation
------------- -------------

View File

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

View File

@ -30,6 +30,8 @@
\RequirePackage{wrapfig} \RequirePackage{wrapfig}
% Separate paragraphs by space by default. % Separate paragraphs by space by default.
\RequirePackage{parskip} \RequirePackage{parskip}
% For parsed-literal blocks.
\RequirePackage{alltt}
% Redefine these colors to your liking in the preamble. % Redefine these colors to your liking in the preamble.
\definecolor{TitleColor}{rgb}{0.126,0.263,0.361} \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_figure_ids = set()
self.next_table_ids = set() self.next_table_ids = set()
# flags # flags
self.verbatim = None
self.in_title = 0 self.in_title = 0
self.in_production_list = 0 self.in_production_list = 0
self.in_footnote = 0 self.in_footnote = 0
@ -1318,36 +1317,40 @@ class LaTeXTranslator(nodes.NodeVisitor):
raise UnsupportedError('%s:%s: literal blocks in footnotes are ' raise UnsupportedError('%s:%s: literal blocks in footnotes are '
'not supported by LaTeX' % 'not supported by LaTeX' %
(self.curfilestack[-1], node.line)) (self.curfilestack[-1], node.line))
self.verbatim = '' 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', {})
if 'language' in node:
# code-block directives
lang = node['language']
highlight_args['force'] = True
if 'linenos' in node:
linenos = node['linenos']
def warner(msg):
self.builder.warn(msg, (self.curfilestack[-1], node.line))
hlcode = self.highlighter.highlight_block(code, lang, warn=warner,
linenos=linenos, **highlight_args)
# workaround for Unicode issue
hlcode = hlcode.replace(u'', u'@texteuro[]')
# must use original Verbatim environment and "tabular" environment
if self.table:
hlcode = hlcode.replace('\\begin{Verbatim}',
'\\begin{OriginalVerbatim}')
self.table.has_problematic = True
self.table.has_verbatim = True
# get consistent trailer
hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim}
hlcode = hlcode.rstrip() + '\n'
self.body.append('\n' + hlcode + '\\end{%sVerbatim}\n' %
(self.table and 'Original' or ''))
raise nodes.SkipNode
def depart_literal_block(self, node): def depart_literal_block(self, node):
code = self.verbatim.rstrip('\n') self.body.append('\n\\end{alltt}\n')
lang = self.hlsettingstack[-1][0]
linenos = code.count('\n') >= self.hlsettingstack[-1][1] - 1
highlight_args = node.get('highlight_args', {})
if 'language' in node:
# code-block directives
lang = node['language']
highlight_args['force'] = True
if 'linenos' in node:
linenos = node['linenos']
def warner(msg):
self.builder.warn(msg, (self.curfilestack[-1], node.line))
hlcode = self.highlighter.highlight_block(code, lang, warn=warner,
linenos=linenos, **highlight_args)
# workaround for Unicode issue
hlcode = hlcode.replace(u'', u'@texteuro[]')
# must use original Verbatim environment and "tabular" environment
if self.table:
hlcode = hlcode.replace('\\begin{Verbatim}',
'\\begin{OriginalVerbatim}')
self.table.has_problematic = True
self.table.has_verbatim = True
# get consistent trailer
hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim}
hlcode = hlcode.rstrip() + '\n'
self.body.append('\n' + hlcode + '\\end{%sVerbatim}\n' %
(self.table and 'Original' or ''))
self.verbatim = None
visit_doctest_block = visit_literal_block visit_doctest_block = visit_literal_block
depart_doctest_block = depart_literal_block depart_doctest_block = depart_literal_block
@ -1510,13 +1513,10 @@ class LaTeXTranslator(nodes.NodeVisitor):
return self.encode(text).replace('\\textasciitilde{}', '~') return self.encode(text).replace('\\textasciitilde{}', '~')
def visit_Text(self, node): def visit_Text(self, node):
if self.verbatim is not None: text = self.encode(node.astext())
self.verbatim += node.astext() if not self.no_contractions:
else: text = educate_quotes_latex(text)
text = self.encode(node.astext()) self.body.append(text)
if not self.no_contractions:
text = educate_quotes_latex(text)
self.body.append(text)
def depart_Text(self, node): def depart_Text(self, node):
pass pass