import sphinx
from docutils.parsers.rst import directives
from docutils.parsers.rst.directives import tables
from packaging import version
class ListTableWithSummary(tables.ListTable):
option_spec = {'summary': directives.unchanged}
option_spec.update(tables.ListTable.option_spec)
def run(self):
result = super().run()
summary = self.options.get('summary')
if summary:
table_node = result[0]
table_node["summary"] = summary
return result
class HTMLTranslator(sphinx.writers.html5.HTML5Translator):
def visit_paragraph(self, node):
children = node.children
if len(children) == 1 and children[0].astext() == "No relevant notes":
atts = {"style": "clip: rect(1px, 1px, 1px, 1px);"
"clip-path: inset(50%);"
"height: 1px;"
"overflow: hidden;"
"position: absolute;"
"white-space: nowrap;"
"width: 1px;"
}
self.body.append(self.starttag(node, 'p', '', **atts))
else:
super().visit_paragraph(node)
def visit_table(self, node):
if version.parse(sphinx.__version__) > version.parse('4.2.0'):
self._table_row_indices = [0]
else:
self._table_row_index = 0
atts = {}
classes = [cls.strip(' \t\n') \
for cls in self.settings.table_style.split(',')]
classes.insert(0, "docutils")
classes.append('align-%s' % node.get('align', 'default'))
if 'width' in node:
atts['style'] = 'width: %s' % node['width']
if 'summary' in node:
atts['summary'] = node['summary']
tag = self.starttag(node, 'table', CLASS=' '.join(classes), **atts)
self.body.append(tag)
def setup(app):
app.add_directive('list-table-with-summary', ListTableWithSummary)
app.set_translator("html", HTMLTranslator, override=True)