^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/usr/bin/env python3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # -*- coding: utf-8; mode: python -*-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) # pylint: disable=C0330, R0903, R0912
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) u"""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) flat-table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) ~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) Implementation of the ``flat-table`` reST-directive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) :copyright: Copyright (C) 2016 Markus Heiser
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) :license: GPL Version 2, June 1991 see linux/COPYING for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) The ``flat-table`` (:py:class:`FlatTable`) is a double-stage list similar to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) the ``list-table`` with some additional features:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * *column-span*: with the role ``cspan`` a cell can be extended through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) additional columns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * *row-span*: with the role ``rspan`` a cell can be extended through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) additional rows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * *auto span* rightmost cell of a table row over the missing cells on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) right side of that table-row. With Option ``:fill-cells:`` this behavior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) can changed from *auto span* to *auto fill*, which automaticly inserts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) (empty) cells instead of spanning the last cell.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) Options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * header-rows: [int] count of header rows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * stub-columns: [int] count of stub columns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * widths: [[int] [int] ... ] widths of columns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * fill-cells: instead of autospann missing cells, insert missing cells
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) roles:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * cspan: [int] additionale columns (*morecols*)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * rspan: [int] additionale rows (*morerows*)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) """
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) # imports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) import sys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) from docutils import nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) from docutils.parsers.rst import directives, roles
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) from docutils.parsers.rst.directives.tables import Table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) from docutils.utils import SystemMessagePropagation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) # common globals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) __version__ = '1.0'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) PY3 = sys.version_info[0] == 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) PY2 = sys.version_info[0] == 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if PY3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) # pylint: disable=C0103, W0622
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) unicode = str
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) basestring = str
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) def setup(app):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) app.add_directive("flat-table", FlatTable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) roles.register_local_role('cspan', c_span)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) roles.register_local_role('rspan', r_span)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return dict(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) version = __version__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) parallel_read_safe = True,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) parallel_write_safe = True
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) def c_span(name, rawtext, text, lineno, inliner, options=None, content=None):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) # pylint: disable=W0613
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) options = options if options is not None else {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) content = content if content is not None else []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) nodelist = [colSpan(span=int(text))]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) msglist = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return nodelist, msglist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) def r_span(name, rawtext, text, lineno, inliner, options=None, content=None):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) # pylint: disable=W0613
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) options = options if options is not None else {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) content = content if content is not None else []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) nodelist = [rowSpan(span=int(text))]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) msglist = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return nodelist, msglist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) class rowSpan(nodes.General, nodes.Element): pass # pylint: disable=C0103,C0321
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) class colSpan(nodes.General, nodes.Element): pass # pylint: disable=C0103,C0321
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) class FlatTable(Table):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) u"""FlatTable (``flat-table``) directive"""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) option_spec = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 'name': directives.unchanged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) , 'class': directives.class_option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) , 'header-rows': directives.nonnegative_int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) , 'stub-columns': directives.nonnegative_int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) , 'widths': directives.positive_int_list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) , 'fill-cells' : directives.flag }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) def run(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if not self.content:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) error = self.state_machine.reporter.error(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 'The "%s" directive is empty; content required.' % self.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) nodes.literal_block(self.block_text, self.block_text),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) line=self.lineno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return [error]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) title, messages = self.make_title()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) node = nodes.Element() # anonymous container for parsing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) self.state.nested_parse(self.content, self.content_offset, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) tableBuilder = ListTableBuilder(self)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) tableBuilder.parseFlatTableNode(node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) tableNode = tableBuilder.buildTableNode()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) # SDK.CONSOLE() # print --> tableNode.asdom().toprettyxml()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if title:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) tableNode.insert(0, title)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return [tableNode] + messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) class ListTableBuilder(object):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) # ==============================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) u"""Builds a table from a double-stage list"""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) def __init__(self, directive):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) self.directive = directive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) self.rows = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) self.max_cols = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) def buildTableNode(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) colwidths = self.directive.get_column_widths(self.max_cols)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if isinstance(colwidths, tuple):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) # Since docutils 0.13, get_column_widths returns a (widths,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) # colwidths) tuple, where widths is a string (i.e. 'auto').
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) # See https://sourceforge.net/p/docutils/patches/120/.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) colwidths = colwidths[1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) stub_columns = self.directive.options.get('stub-columns', 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) header_rows = self.directive.options.get('header-rows', 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) table = nodes.table()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) tgroup = nodes.tgroup(cols=len(colwidths))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) table += tgroup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) for colwidth in colwidths:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) colspec = nodes.colspec(colwidth=colwidth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) # FIXME: It seems, that the stub method only works well in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) # absence of rowspan (observed by the html buidler, the docutils-xml
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) # build seems OK). This is not extraordinary, because there exists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) # no table directive (except *this* flat-table) which allows to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) # define coexistent of rowspan and stubs (there was no use-case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) # before flat-table). This should be reviewed (later).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if stub_columns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) colspec.attributes['stub'] = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) stub_columns -= 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) tgroup += colspec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) stub_columns = self.directive.options.get('stub-columns', 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if header_rows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) thead = nodes.thead()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) tgroup += thead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) for row in self.rows[:header_rows]:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) thead += self.buildTableRowNode(row)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) tbody = nodes.tbody()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) tgroup += tbody
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) for row in self.rows[header_rows:]:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) tbody += self.buildTableRowNode(row)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) def buildTableRowNode(self, row_data, classes=None):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) classes = [] if classes is None else classes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) row = nodes.row()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) for cell in row_data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if cell is None:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) cspan, rspan, cellElements = cell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) attributes = {"classes" : classes}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if rspan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) attributes['morerows'] = rspan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if cspan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) attributes['morecols'] = cspan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) entry = nodes.entry(**attributes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) entry.extend(cellElements)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) row += entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return row
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) def raiseError(self, msg):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) error = self.directive.state_machine.reporter.error(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) msg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) , nodes.literal_block(self.directive.block_text
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) , self.directive.block_text)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) , line = self.directive.lineno )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) raise SystemMessagePropagation(error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) def parseFlatTableNode(self, node):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) u"""parses the node from a :py:class:`FlatTable` directive's body"""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if len(node) != 1 or not isinstance(node[0], nodes.bullet_list):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) self.raiseError(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 'Error parsing content block for the "%s" directive: '
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 'exactly one bullet list expected.' % self.directive.name )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) for rowNum, rowItem in enumerate(node[0]):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) row = self.parseRowItem(rowItem, rowNum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) self.rows.append(row)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) self.roundOffTableDefinition()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) def roundOffTableDefinition(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) u"""Round off the table definition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) This method rounds off the table definition in :py:member:`rows`.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * This method inserts the needed ``None`` values for the missing cells
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) arising from spanning cells over rows and/or columns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * recount the :py:member:`max_cols`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * Autospan or fill (option ``fill-cells``) missing cells on the right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) side of the table-row
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) """
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) y = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) while y < len(self.rows):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) x = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) while x < len(self.rows[y]):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) cell = self.rows[y][x]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if cell is None:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) x += 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) cspan, rspan = cell[:2]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) # handle colspan in current row
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) for c in range(cspan):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) try:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) self.rows[y].insert(x+c+1, None)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) except: # pylint: disable=W0702
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) # the user sets ambiguous rowspans
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) pass # SDK.CONSOLE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) # handle colspan in spanned rows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) for r in range(rspan):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) for c in range(cspan + 1):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) try:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) self.rows[y+r+1].insert(x+c, None)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) except: # pylint: disable=W0702
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) # the user sets ambiguous rowspans
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) pass # SDK.CONSOLE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) x += 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) y += 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) # Insert the missing cells on the right side. For this, first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) # re-calculate the max columns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) for row in self.rows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if self.max_cols < len(row):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) self.max_cols = len(row)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) # fill with empty cells or cellspan?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) fill_cells = False
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if 'fill-cells' in self.directive.options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) fill_cells = True
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) for row in self.rows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) x = self.max_cols - len(row)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if x and not fill_cells:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if row[-1] is None:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) row.append( ( x - 1, 0, []) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) cspan, rspan, content = row[-1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) row[-1] = (cspan + x, rspan, content)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) elif x and fill_cells:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) for i in range(x):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) row.append( (0, 0, nodes.comment()) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) def pprint(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) # for debugging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) retVal = "[ "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) for row in self.rows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) retVal += "[ "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) for col in row:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if col is None:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) retVal += ('%r' % col)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) retVal += "\n , "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) content = col[2][0].astext()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if len (content) > 30:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) content = content[:30] + "..."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) retVal += ('(cspan=%s, rspan=%s, %r)'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) % (col[0], col[1], content))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) retVal += "]\n , "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) retVal = retVal[:-2]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) retVal += "]\n , "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) retVal = retVal[:-2]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return retVal + "]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) def parseRowItem(self, rowItem, rowNum):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) row = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) childNo = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) error = False
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) cell = None
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) target = None
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) for child in rowItem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (isinstance(child , nodes.comment)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) or isinstance(child, nodes.system_message)):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) elif isinstance(child , nodes.target):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) target = child
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) elif isinstance(child, nodes.bullet_list):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) childNo += 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) cell = child
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) error = True
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if childNo != 1 or error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) self.raiseError(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 'Error parsing content block for the "%s" directive: '
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 'two-level bullet list expected, but row %s does not '
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 'contain a second-level bullet list.'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) % (self.directive.name, rowNum + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) for cellItem in cell:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) cspan, rspan, cellElements = self.parseCellItem(cellItem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if target is not None:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) cellElements.insert(0, target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) row.append( (cspan, rspan, cellElements) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return row
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) def parseCellItem(self, cellItem):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) # search and remove cspan, rspan colspec from the first element in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) # this listItem (field).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) cspan = rspan = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if not len(cellItem):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return cspan, rspan, []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) for elem in cellItem[0]:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if isinstance(elem, colSpan):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) cspan = elem.get("span")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) elem.parent.remove(elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if isinstance(elem, rowSpan):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) rspan = elem.get("span")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) elem.parent.remove(elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return cspan, rspan, cellItem[:]