mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Fix handling of Verbatim within tables.
This commit is contained in:
parent
c00189b04a
commit
f404b36a0e
@ -78,3 +78,12 @@ exists:
|
|||||||
By default, Sphinx uses a table layout with ``L`` for every column.
|
By default, Sphinx uses a table layout with ``L`` for every column.
|
||||||
|
|
||||||
.. versionadded:: 0.2.1
|
.. versionadded:: 0.2.1
|
||||||
|
|
||||||
|
.. warning::
|
||||||
|
|
||||||
|
Tables that contain literal blocks cannot be set with ``tabulary``. They are
|
||||||
|
therefore set with the standard LaTeX ``tabular`` environment. Also, the
|
||||||
|
verbatim environment used for literal blocks only works in ``p{width}``
|
||||||
|
columns, which means that by default, Sphinx generates such column specs for
|
||||||
|
such tables. Use the :dir:`tabularcolumns` directive to get finer control
|
||||||
|
over such tables.
|
||||||
|
@ -76,7 +76,9 @@ class Table(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.col = 0
|
self.col = 0
|
||||||
self.colcount = 0
|
self.colcount = 0
|
||||||
|
self.colspec = None
|
||||||
self.had_head = False
|
self.had_head = False
|
||||||
|
self.has_verbatim = False
|
||||||
|
|
||||||
|
|
||||||
class Desc(object):
|
class Desc(object):
|
||||||
@ -385,10 +387,32 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
|||||||
if self.table:
|
if self.table:
|
||||||
raise NotImplementedError('Nested tables are not supported.')
|
raise NotImplementedError('Nested tables are not supported.')
|
||||||
self.table = Table()
|
self.table = Table()
|
||||||
self.body.append('\n\\begin{tabulary}{\\textwidth}')
|
self.tablebody = []
|
||||||
|
# Redirect body output until table is finished.
|
||||||
|
self._body = self.body
|
||||||
|
self.body = self.tablebody
|
||||||
def depart_table(self, node):
|
def depart_table(self, node):
|
||||||
self.body.append('\\end{tabulary}\n\n')
|
self.body = self._body
|
||||||
|
if self.table.has_verbatim:
|
||||||
|
self.body.append('\n\\begin{tabular}')
|
||||||
|
else:
|
||||||
|
self.body.append('\n\\begin{tabulary}{\\textwidth}')
|
||||||
|
if self.table.colspec:
|
||||||
|
self.body.append(self.table.colspec)
|
||||||
|
else:
|
||||||
|
if self.table.has_verbatim:
|
||||||
|
colwidth = 0.95 / self.table.colcount
|
||||||
|
colspec = ('p{%.3f\\textwidth}|' % colwidth) * self.table.colcount
|
||||||
|
self.body.append('{|' + colspec + '}\n')
|
||||||
|
else:
|
||||||
|
self.body.append('{|' + ('L|' * self.table.colcount) + '}\n')
|
||||||
|
self.body.extend(self.tablebody)
|
||||||
|
if self.table.has_verbatim:
|
||||||
|
self.body.append('\\end{tabular}\n\n')
|
||||||
|
else:
|
||||||
|
self.body.append('\\end{tabulary}\n\n')
|
||||||
self.table = None
|
self.table = None
|
||||||
|
self.tablebody = None
|
||||||
|
|
||||||
def visit_colspec(self, node):
|
def visit_colspec(self, node):
|
||||||
self.table.colcount += 1
|
self.table.colcount += 1
|
||||||
@ -402,9 +426,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
|||||||
|
|
||||||
def visit_thead(self, node):
|
def visit_thead(self, node):
|
||||||
if self.next_table_colspec:
|
if self.next_table_colspec:
|
||||||
self.body.append('{%s}\n' % self.next_table_colspec)
|
self.table.colspec = '{%s}\n' % self.next_table_colspec
|
||||||
else:
|
|
||||||
self.body.append('{|' + ('L|' * self.table.colcount) + '}\n')
|
|
||||||
self.next_table_colspec = None
|
self.next_table_colspec = None
|
||||||
self.body.append('\\hline\n')
|
self.body.append('\\hline\n')
|
||||||
self.table.had_head = True
|
self.table.had_head = True
|
||||||
@ -727,10 +749,16 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
|||||||
hlcode = self.highlighter.highlight_block(code, lang, linenos)
|
hlcode = self.highlighter.highlight_block(code, lang, linenos)
|
||||||
# workaround for Unicode issue
|
# workaround for Unicode issue
|
||||||
hlcode = hlcode.replace(u'€', u'@texteuro[]')
|
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_verbatim = True
|
||||||
# get consistent trailer
|
# get consistent trailer
|
||||||
hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim}
|
hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim}
|
||||||
hlcode = hlcode.rstrip() + '\n'
|
hlcode = hlcode.rstrip() + '\n'
|
||||||
self.body.append('\n' + hlcode + '\\end{Verbatim}\n')
|
self.body.append('\n' + hlcode + '\\end{%sVerbatim}\n' %
|
||||||
|
(self.table and 'Original' or ''))
|
||||||
self.verbatim = None
|
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
|
||||||
|
@ -154,9 +154,9 @@
|
|||||||
\newcommand{\py@modulebadkey}{{--just-some-junk--}}
|
\newcommand{\py@modulebadkey}{{--just-some-junk--}}
|
||||||
|
|
||||||
% Redefine the Verbatim environment to allow border and background colors.
|
% Redefine the Verbatim environment to allow border and background colors.
|
||||||
%
|
% The original environment is still used for verbatims within tables.
|
||||||
\let\py@OldVerbatim=\Verbatim
|
\let\OriginalVerbatim=\Verbatim
|
||||||
\let\py@OldEndVerbatim=\endVerbatim
|
\let\endOriginalVerbatim=\endVerbatim
|
||||||
|
|
||||||
% Play with vpsace to be able to keep the indentation.
|
% Play with vpsace to be able to keep the indentation.
|
||||||
\newlength\distancetoright
|
\newlength\distancetoright
|
||||||
@ -188,10 +188,10 @@
|
|||||||
}%
|
}%
|
||||||
\item\MakeFramed {\FrameRestore}%
|
\item\MakeFramed {\FrameRestore}%
|
||||||
\small%
|
\small%
|
||||||
\py@OldVerbatim[#1]%
|
\OriginalVerbatim[#1]%
|
||||||
}
|
}
|
||||||
\renewcommand{\endVerbatim}{%
|
\renewcommand{\endVerbatim}{%
|
||||||
\py@OldEndVerbatim%
|
\endOriginalVerbatim%
|
||||||
\endMakeFramed%
|
\endMakeFramed%
|
||||||
\endlist%
|
\endlist%
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user