2 # -*- coding: utf-8; mode: python -*-
3 # pylint: disable=C0330, R0903, R0912
9 Implementation of the ``flat-table`` reST-directive.
11 :copyright: Copyright (C) 2016 Markus Heiser
12 :license: GPL Version 2, June 1991 see linux/COPYING for details.
14 The ``flat-table`` (:py:class:`FlatTable`) is a double-stage list similar to
15 the ``list-table`` with some additional features:
17 * *column-span*: with the role ``cspan`` a cell can be extended through
20 * *row-span*: with the role ``rspan`` a cell can be extended through
23 * *auto span* rightmost cell of a table row over the missing cells on the
24 right side of that table-row. With Option ``:fill-cells:`` this behavior
25 can changed from *auto span* to *auto fill*, which automaticly inserts
26 (empty) cells instead of spanning the last cell.
30 * header-rows: [int] count of header rows
31 * stub-columns: [int] count of stub columns
32 * widths: [[int] [int] ... ] widths of columns
33 * fill-cells: instead of autospann missing cells, insert missing cells
37 * cspan: [int] additionale columns (*morecols*)
38 * rspan: [int] additionale rows (*morerows*)
41 # ==============================================================================
43 # ==============================================================================
47 from docutils import nodes
48 from docutils.parsers.rst import directives, roles
49 from docutils.parsers.rst.directives.tables import Table
50 from docutils.utils import SystemMessagePropagation
52 # ==============================================================================
54 # ==============================================================================
58 PY3 = sys.version_info[0] == 3
59 PY2 = sys.version_info[0] == 2
62 # pylint: disable=C0103, W0622
66 # ==============================================================================
68 # ==============================================================================
70 app.add_directive("flat-table", FlatTable)
71 roles.register_local_role('cspan', c_span)
72 roles.register_local_role('rspan', r_span)
75 version = __version__,
76 parallel_read_safe = True,
77 parallel_write_safe = True
80 # ==============================================================================
81 def c_span(name, rawtext, text, lineno, inliner, options=None, content=None):
82 # ==============================================================================
83 # pylint: disable=W0613
85 options = options if options is not None else {}
86 content = content if content is not None else []
87 nodelist = [colSpan(span=int(text))]
89 return nodelist, msglist
91 # ==============================================================================
92 def r_span(name, rawtext, text, lineno, inliner, options=None, content=None):
93 # ==============================================================================
94 # pylint: disable=W0613
96 options = options if options is not None else {}
97 content = content if content is not None else []
98 nodelist = [rowSpan(span=int(text))]
100 return nodelist, msglist
103 # ==============================================================================
104 class rowSpan(nodes.General, nodes.Element): pass # pylint: disable=C0103,C0321
105 class colSpan(nodes.General, nodes.Element): pass # pylint: disable=C0103,C0321
106 # ==============================================================================
108 # ==============================================================================
109 class FlatTable(Table):
110 # ==============================================================================
112 u"""FlatTable (``flat-table``) directive"""
115 'name': directives.unchanged
116 , 'class': directives.class_option
117 , 'header-rows': directives.nonnegative_int
118 , 'stub-columns': directives.nonnegative_int
119 , 'widths': directives.positive_int_list
120 , 'fill-cells' : directives.flag }
125 error = self.state_machine.reporter.error(
126 'The "%s" directive is empty; content required.' % self.name,
127 nodes.literal_block(self.block_text, self.block_text),
131 title, messages = self.make_title()
132 node = nodes.Element() # anonymous container for parsing
133 self.state.nested_parse(self.content, self.content_offset, node)
135 tableBuilder = ListTableBuilder(self)
136 tableBuilder.parseFlatTableNode(node)
137 tableNode = tableBuilder.buildTableNode()
138 # SDK.CONSOLE() # print --> tableNode.asdom().toprettyxml()
140 tableNode.insert(0, title)
141 return [tableNode] + messages
144 # ==============================================================================
145 class ListTableBuilder(object):
146 # ==============================================================================
148 u"""Builds a table from a double-stage list"""
150 def __init__(self, directive):
151 self.directive = directive
155 def buildTableNode(self):
157 colwidths = self.directive.get_column_widths(self.max_cols)
158 if isinstance(colwidths, tuple):
159 # Since docutils 0.13, get_column_widths returns a (widths,
160 # colwidths) tuple, where widths is a string (i.e. 'auto').
161 # See https://sourceforge.net/p/docutils/patches/120/.
162 colwidths = colwidths[1]
163 stub_columns = self.directive.options.get('stub-columns', 0)
164 header_rows = self.directive.options.get('header-rows', 0)
166 table = nodes.table()
167 tgroup = nodes.tgroup(cols=len(colwidths))
171 for colwidth in colwidths:
172 colspec = nodes.colspec(colwidth=colwidth)
173 # FIXME: It seems, that the stub method only works well in the
174 # absence of rowspan (observed by the html buidler, the docutils-xml
175 # build seems OK). This is not extraordinary, because there exists
176 # no table directive (except *this* flat-table) which allows to
177 # define coexistent of rowspan and stubs (there was no use-case
178 # before flat-table). This should be reviewed (later).
180 colspec.attributes['stub'] = 1
183 stub_columns = self.directive.options.get('stub-columns', 0)
186 thead = nodes.thead()
188 for row in self.rows[:header_rows]:
189 thead += self.buildTableRowNode(row)
191 tbody = nodes.tbody()
194 for row in self.rows[header_rows:]:
195 tbody += self.buildTableRowNode(row)
198 def buildTableRowNode(self, row_data, classes=None):
199 classes = [] if classes is None else classes
201 for cell in row_data:
204 cspan, rspan, cellElements = cell
206 attributes = {"classes" : classes}
208 attributes['morerows'] = rspan
210 attributes['morecols'] = cspan
211 entry = nodes.entry(**attributes)
212 entry.extend(cellElements)
216 def raiseError(self, msg):
217 error = self.directive.state_machine.reporter.error(
219 , nodes.literal_block(self.directive.block_text
220 , self.directive.block_text)
221 , line = self.directive.lineno )
222 raise SystemMessagePropagation(error)
224 def parseFlatTableNode(self, node):
225 u"""parses the node from a :py:class:`FlatTable` directive's body"""
227 if len(node) != 1 or not isinstance(node[0], nodes.bullet_list):
229 'Error parsing content block for the "%s" directive: '
230 'exactly one bullet list expected.' % self.directive.name )
232 for rowNum, rowItem in enumerate(node[0]):
233 row = self.parseRowItem(rowItem, rowNum)
234 self.rows.append(row)
235 self.roundOffTableDefinition()
237 def roundOffTableDefinition(self):
238 u"""Round off the table definition.
240 This method rounds off the table definition in :py:member:`rows`.
242 * This method inserts the needed ``None`` values for the missing cells
243 arising from spanning cells over rows and/or columns.
245 * recount the :py:member:`max_cols`
247 * Autospan or fill (option ``fill-cells``) missing cells on the right
248 side of the table-row
252 while y < len(self.rows):
255 while x < len(self.rows[y]):
256 cell = self.rows[y][x]
260 cspan, rspan = cell[:2]
261 # handle colspan in current row
262 for c in range(cspan):
264 self.rows[y].insert(x+c+1, None)
265 except: # pylint: disable=W0702
266 # the user sets ambiguous rowspans
268 # handle colspan in spanned rows
269 for r in range(rspan):
270 for c in range(cspan + 1):
272 self.rows[y+r+1].insert(x+c, None)
273 except: # pylint: disable=W0702
274 # the user sets ambiguous rowspans
279 # Insert the missing cells on the right side. For this, first
280 # re-calculate the max columns.
282 for row in self.rows:
283 if self.max_cols < len(row):
284 self.max_cols = len(row)
286 # fill with empty cells or cellspan?
289 if 'fill-cells' in self.directive.options:
292 for row in self.rows:
293 x = self.max_cols - len(row)
294 if x and not fill_cells:
296 row.append( ( x - 1, 0, []) )
298 cspan, rspan, content = row[-1]
299 row[-1] = (cspan + x, rspan, content)
300 elif x and fill_cells:
302 row.append( (0, 0, nodes.comment()) )
307 for row in self.rows:
311 retVal += ('%r' % col)
314 content = col[2][0].astext()
315 if len (content) > 30:
316 content = content[:30] + "..."
317 retVal += ('(cspan=%s, rspan=%s, %r)'
318 % (col[0], col[1], content))
325 def parseRowItem(self, rowItem, rowNum):
332 for child in rowItem:
333 if (isinstance(child , nodes.comment)
334 or isinstance(child, nodes.system_message)):
336 elif isinstance(child , nodes.target):
338 elif isinstance(child, nodes.bullet_list):
345 if childNo != 1 or error:
347 'Error parsing content block for the "%s" directive: '
348 'two-level bullet list expected, but row %s does not '
349 'contain a second-level bullet list.'
350 % (self.directive.name, rowNum + 1))
352 for cellItem in cell:
353 cspan, rspan, cellElements = self.parseCellItem(cellItem)
354 if target is not None:
355 cellElements.insert(0, target)
356 row.append( (cspan, rspan, cellElements) )
359 def parseCellItem(self, cellItem):
360 # search and remove cspan, rspan colspec from the first element in
361 # this listItem (field).
363 if not len(cellItem):
364 return cspan, rspan, []
365 for elem in cellItem[0]:
366 if isinstance(elem, colSpan):
367 cspan = elem.get("span")
368 elem.parent.remove(elem)
370 if isinstance(elem, rowSpan):
371 rspan = elem.get("span")
372 elem.parent.remove(elem)
374 return cspan, rspan, cellItem[:]