Merge pull request #3405 from tk0miya/3377_support_table_align

#3377: support :align: option for tables
This commit is contained in:
Takeshi KOMIYA
2017-02-11 11:14:32 +09:00
committed by GitHub
8 changed files with 115 additions and 16 deletions

View File

@@ -48,6 +48,7 @@ Features added
(refs: #3379, #3381)
* #3402: Allow to suppress "download file not readable" warnings using
:confval:`suppress_warnings`.
* #3377: latex: Add support for Docutils 0.13 ``:align:`` option for tables
Bugs fixed
----------

View File

@@ -1,4 +1,12 @@
\begin{longtable}<%= table.get_colspec() %>
\begin{longtable}
<%- if table.align == 'center' -%>
[c]
<%- elif table.align == 'left' -%>
[l]
<%- elif table.align == 'right' -%>
[r]
<%- endif -%>
<%= table.get_colspec() %>
<%- if table.caption -%>
\caption{<%= ''.join(table.caption) %>}<%= labels %>\\
<% endif -%>

View File

@@ -1,3 +1,10 @@
<%- if table.align %>
<%- if table.align == 'center' -%>
\begin{center}
<%- elif table.align in ('left', 'right') -%>
\begin{flush<%= table.align%>}
<%- endif -%>
<% endif -%>
<%- if table.caption -%>
\begin{threeparttable}
\capstart\caption{<%= ''.join(table.caption) %>}<%= labels %>
@@ -10,3 +17,10 @@
<%- if table.caption %>
\end{threeparttable}
<%- endif %>
<%- if table.align %>
<%- if table.align == 'center' -%>
\end{center}
<%- elif table.align in ('left', 'right') -%>
\end{flush<%= table.align%>}
<% endif -%>
<% endif -%>

View File

@@ -1,3 +1,10 @@
<%- if table.align %>
<%- if table.align == 'center' -%>
\begin{center}
<%- elif table.align in ('left', 'right') -%>
\begin{flush<%= table.align%>}
<%- endif -%>
<% endif -%>
<%- if table.caption -%>
\begin{threeparttable}
\capstart\caption{<%= ''.join(table.caption) %>}<%= labels %>
@@ -7,6 +14,13 @@
<%= ''.join(table.header) %>
<%=- ''.join(table.body) %>
\end{tabulary}
<%- if table.align %>
<%- if table.align == 'center' -%>
\end{center}
<%- elif table.align in ('left', 'right') -%>
\end{flush<%= table.align%>}
<% endif -%>
<% endif -%>
<%- if table.caption %>
\end{threeparttable}
<%- endif %>

View File

@@ -320,6 +320,7 @@ class Table(object):
# type: (nodes.table) -> None
self.header = [] # type: List[unicode]
self.body = [] # type: List[unicode]
self.align = node.get('align')
self.colcount = 0
self.colspec = None # type: unicode
self.colwidths = [] # type: List[int]

View File

@@ -30,6 +30,21 @@ longtable having :widths: option
cell3-1 cell3-2
======= =======
longtable having :align: option
-------------------------------
.. table::
:align: right
:class: longtable
======= =======
header1 header2
======= =======
cell1-1 cell1-2
cell2-1 cell2-2
cell3-1 cell3-2
======= =======
longtable with tabularcolumn
----------------------------

View File

@@ -43,6 +43,35 @@ table having :widths: option
cell3-1 cell3-2
======= =======
table having :align: option (tabulary)
--------------------------------------
.. table::
:align: center
======= =======
header1 header2
======= =======
cell1-1 cell1-2
cell2-1 cell2-2
cell3-1 cell3-2
======= =======
table having :align: option (tabular)
-------------------------------------
.. table::
:align: left
:widths: 30,70
======= =======
header1 header2
======= =======
cell1-1 cell1-2
cell2-1 cell2-2
cell3-1 cell3-2
======= =======
table with tabularcolumn
------------------------

View File

@@ -820,7 +820,8 @@ def test_maxlistdepth_at_ten(app, status, warning):
@pytest.mark.skipif(docutils.__version_info__ < (0, 13),
reason='docutils-0.13 or above is required')
@pytest.mark.sphinx('latex', testroot='latex-table')
def test_latex_table(app, status, warning):
@pytest.mark.test_params(shared_result='test_latex_table')
def test_latex_table_tabulars(app, status, warning):
app.builder.build_all()
result = (app.outdir / 'test.tex').text(encoding='utf8')
tables = {}
@@ -859,14 +860,19 @@ def test_latex_table(app, status, warning):
# table having :widths: option
table = tables['table having :widths: option']
assert ('\\noindent\\begin{tabular}{|\\X{30}{100}|\\X{70}{100}|}' in table)
assert ('\\hline\n'
'\\sphinxstylethead{\\relax \nheader1\n\\unskip}\\relax &'
'\\sphinxstylethead{\\relax \nheader2\n\\unskip}\\relax' in table)
assert ('\\hline\ncell1-1\n&\ncell1-2\n\\\\' in table)
assert ('\\hline\ncell2-1\n&\ncell2-2\n\\\\' in table)
assert ('\\hline\ncell3-1\n&\ncell3-2\n\\\\' in table)
assert ('\\hline\n\\end{tabular}' in table)
# table having :align: option (tabulary)
table = tables['table having :align: option (tabulary)']
assert ('\\begin{center}\\noindent\\begin{tabulary}{\\linewidth}{|L|L|}\n' in table)
assert ('\\hline\n\\end{tabulary}\\end{center}' in table)
# table having :align: option (tabular)
table = tables['table having :align: option (tabular)']
assert ('\\begin{flushleft}'
'\\noindent\\begin{tabular}{|\X{30}{100}|\X{70}{100}|}\n' in table)
assert ('\\hline\n\\end{tabular}\\end{flushleft}' in table)
# table with tabularcolumn
table = tables['table with tabularcolumn']
assert ('\\noindent\\begin{tabulary}{\\linewidth}{|c|c|}' in table)
@@ -876,14 +882,7 @@ def test_latex_table(app, status, warning):
assert ('\\begin{threeparttable}\n\\capstart\\caption{caption for table}'
'\\label{\\detokenize{tabular:id1}}' in table)
assert ('\\noindent\\begin{tabulary}{\\linewidth}{|L|L|}' in table)
assert ('\\hline\n'
'\\sphinxstylethead{\\relax \nheader1\n\\unskip}\\relax &'
'\\sphinxstylethead{\\relax \nheader2\n\\unskip}\\relax' in table)
assert ('\\hline\ncell1-1\n&\ncell1-2\n\\\\' in table)
assert ('\\hline\ncell2-1\n&\ncell2-2\n\\\\' in table)
assert ('\\hline\ncell3-1\n&\ncell3-2\n\\\\' in table)
assert ('\\hline\n\\end{tabulary}' in table)
assert ('\\end{threeparttable}' in table)
assert ('\\hline\n\\end{tabulary}\n\\end{threeparttable}' in table)
# table having verbatim
table = tables['table having verbatim']
@@ -897,6 +896,19 @@ def test_latex_table(app, status, warning):
table = tables['table having both :widths: and problematic cell']
assert ('\\noindent\\begin{tabular}{|\\X{30}{100}|\\X{70}{100}|}' in table)
@pytest.mark.skipif(docutils.__version_info__ < (0, 13),
reason='docutils-0.13 or above is required')
@pytest.mark.sphinx('latex', testroot='latex-table')
@pytest.mark.test_params(shared_result='test_latex_table')
def test_latex_table_longtable(app, status, warning):
app.builder.build_all()
result = (app.outdir / 'test.tex').text(encoding='utf8')
tables = {}
for chap in re.split(r'\\section{', result)[1:]:
sectname, content = chap.split('}', 1)
tables[sectname] = content.strip()
# longtable
table = tables['longtable']
assert ('\\begin{longtable}{|l|l|}\n\\hline' in table)
@@ -922,6 +934,11 @@ def test_latex_table(app, status, warning):
table = tables['longtable having :widths: option']
assert ('\\begin{longtable}{|\\X{30}{100}|\\X{70}{100}|}' in table)
# longtable having :align: option
table = tables['longtable having :align: option']
assert ('\\begin{longtable}[r]{|l|l|}\n' in table)
assert ('\\hline\n\\end{longtable}' in table)
# longtable with tabularcolumn
table = tables['longtable with tabularcolumn']
assert ('\\begin{longtable}{|c|c|}' in table)