|
From: <mi...@us...> - 2021-09-24 13:35:28
|
Revision: 8834
http://sourceforge.net/p/docutils/code/8834
Author: milde
Date: 2021-09-24 13:35:26 +0000 (Fri, 24 Sep 2021)
Log Message:
-----------
Unify behaviour of "widths" option.
Check that the lenght of an integer list equals the number of
table columns also for the "table" directive.
TODO: use the last value for additional columns
and ignore values for missing columns instead?
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docutils/parsers/rst/directives/tables.py
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2021-09-24 12:52:39 UTC (rev 8833)
+++ trunk/docutils/HISTORY.txt 2021-09-24 13:35:26 UTC (rev 8834)
@@ -42,6 +42,12 @@
- Add `class` option to `Raw` directive.
+* docutils/parsers/rst/directives/tables.py:
+
+ - Unify behaviour of "widths" option: check that the lenght of an
+ integer list equals the number of table columns also for the "table"
+ directive.
+
* docutils/tools/math/math2html.py,
docutils/tools/math/tex2unicode.py,
docutils/writers/html5/math.css
Modified: trunk/docutils/docutils/parsers/rst/directives/tables.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/tables.py 2021-09-24 12:52:39 UTC (rev 8833)
+++ trunk/docutils/docutils/parsers/rst/directives/tables.py 2021-09-24 13:35:26 UTC (rev 8834)
@@ -103,17 +103,18 @@
def widths(self):
return self.options.get('widths', '')
- def get_column_widths(self, max_cols):
+ def get_column_widths(self, n_cols):
if isinstance(self.widths, list):
- if len(self.widths) != max_cols:
+ if len(self.widths) != n_cols:
+ # TODO: use last value for missing columns?
error = self.state_machine.reporter.error(
'"%s" widths do not match the number of columns in table '
- '(%s).' % (self.name, max_cols), nodes.literal_block(
+ '(%s).' % (self.name, n_cols), nodes.literal_block(
self.block_text, self.block_text), line=self.lineno)
raise SystemMessagePropagation(error)
col_widths = self.widths
- elif max_cols:
- col_widths = [100 // max_cols] * max_cols
+ elif n_cols:
+ col_widths = [100 // n_cols] * n_cols
else:
error = self.state_machine.reporter.error(
'No table data detected in CSV file.', nodes.literal_block(
@@ -151,18 +152,19 @@
self.set_table_width(table_node)
if 'align' in self.options:
table_node['align'] = self.options.get('align')
- tgroup = table_node[0]
if isinstance(self.widths, list):
+ tgroup = table_node[0]
+ try:
+ col_widths = self.get_column_widths(tgroup["cols"])
+ except SystemMessagePropagation as detail:
+ return [detail.args[0]]
colspecs = [child for child in tgroup.children
if child.tagname == 'colspec']
- for colspec, col_width in zip(colspecs, self.widths):
+ for colspec, col_width in zip(colspecs, col_widths):
colspec['colwidth'] = col_width
- # @@@ the colwidths argument for <tgroup> is not part of the
- # XML Exchange Table spec (https://www.oasis-open.org/specs/tm9901.htm)
- # and hence violates the docutils.dtd.
if self.widths == 'auto':
table_node['classes'] += ['colwidths-auto']
- elif self.widths: # "grid" or list of integers
+ elif self.widths: # override "table-style" setting
table_node['classes'] += ['colwidths-given']
self.add_name(table_node)
if title:
@@ -483,7 +485,7 @@
table = nodes.table()
if self.widths == 'auto':
table['classes'] += ['colwidths-auto']
- elif self.widths: # "grid" or list of integers
+ elif self.widths: # override "table-style" setting
table['classes'] += ['colwidths-given']
tgroup = nodes.tgroup(cols=len(col_widths))
table += tgroup
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|