|
| 1 | +# Based on https://github.com/sphinx-doc/sphinx/blob/3.x/sphinx/ext/autosummary/__init__.py |
| 2 | +# |
| 3 | +# Copyright (c) 2007-2021 by the Sphinx team. |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are |
| 8 | +# met: |
| 9 | +# |
| 10 | +# * Redistributions of source code must retain the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer. |
| 12 | +# |
| 13 | +# * Redistributions in binary form must reproduce the above copyright |
| 14 | +# notice, this list of conditions and the following disclaimer in the |
| 15 | +# documentation and/or other materials provided with the distribution. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 | +# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +# |
| 29 | + |
| 30 | +# stdlib |
| 31 | +from itertools import chain |
| 32 | +from typing import List, Tuple |
| 33 | + |
| 34 | +# 3rd party |
| 35 | +from docutils import nodes |
| 36 | +from docutils.statemachine import StringList |
| 37 | +from domdf_python_tools import stringlist |
| 38 | +from sphinx import addnodes |
| 39 | +from sphinx.application import Sphinx |
| 40 | +from sphinx.config import Config |
| 41 | +from sphinx.ext.autosummary import Autosummary, autosummary_table |
| 42 | +from sphinx.util import rst |
| 43 | +from sphinx.util.docutils import SphinxDirective, switch_source_input |
| 44 | +from sphinx_toolbox import latex |
| 45 | + |
| 46 | + |
| 47 | +class AutosummaryWidths(Autosummary): |
| 48 | + |
| 49 | + def get_table(self, items: List[Tuple[str, str, str, str]]) -> List[nodes.Node]: |
| 50 | + """ |
| 51 | + Generate a proper list of table nodes for autosummary:: directive. |
| 52 | +
|
| 53 | + :param items: A a list produced by :meth:`~.get_items`. |
| 54 | + """ |
| 55 | + table_spec = addnodes.tabular_col_spec() |
| 56 | + # table_spec['spec'] = r'\Xx{1}{3}\Xx{2}{3}' |
| 57 | + # table_spec['spec'] = r'\Xx{3}{8}\Xx{5}{8}' |
| 58 | + # table_spec['spec'] = r'\Xx{7}{16}\Xx{9}{16}' |
| 59 | + |
| 60 | + widths = chain.from_iterable(getattr(self.state.document, "autosummary_widths", ((1, 2), (1, 2)))) |
| 61 | + table_spec["spec"] = r'\Xx{{{}}}{{{}}}\Xx{{{}}}{{{}}}'.format(*widths) |
| 62 | + |
| 63 | + table = autosummary_table('') |
| 64 | + real_table = nodes.table('', classes=["longtable"]) |
| 65 | + table.append(real_table) |
| 66 | + group = nodes.tgroup('', cols=2) |
| 67 | + real_table.append(group) |
| 68 | + group.append(nodes.colspec('', colwidth=10)) |
| 69 | + group.append(nodes.colspec('', colwidth=90)) |
| 70 | + body = nodes.tbody('') |
| 71 | + group.append(body) |
| 72 | + |
| 73 | + def append_row(*column_texts: str) -> None: |
| 74 | + row = nodes.row('') |
| 75 | + source, line = self.state_machine.get_source_and_line() |
| 76 | + for text in column_texts: |
| 77 | + node = nodes.paragraph('') |
| 78 | + vl = StringList() |
| 79 | + vl.append(text, "%s:%d:<autosummary>" % (source, line)) |
| 80 | + with switch_source_input(self.state, vl): |
| 81 | + self.state.nested_parse(vl, 0, node) |
| 82 | + try: |
| 83 | + if isinstance(node[0], nodes.paragraph): |
| 84 | + node = node[0] |
| 85 | + except IndexError: |
| 86 | + pass |
| 87 | + row.append(nodes.entry('', node)) |
| 88 | + body.append(row) |
| 89 | + |
| 90 | + for name, sig, summary, real_name in items: |
| 91 | + qualifier = "obj" |
| 92 | + if "nosignatures" not in self.options: |
| 93 | + col1 = ":{}:`{} <{}>`\\ {}".format(qualifier, name, real_name, rst.escape(sig).replace('(', "(")) |
| 94 | + else: |
| 95 | + col1 = f":{qualifier}:`{name} <{real_name}>`" |
| 96 | + col2 = summary |
| 97 | + append_row(col1, col2) |
| 98 | + |
| 99 | + return [table_spec, table] |
| 100 | + |
| 101 | + |
| 102 | +class WidthsDirective(SphinxDirective): |
| 103 | + required_arguments = 2 |
| 104 | + |
| 105 | + def run(self): |
| 106 | + widths = [arg.split('/') for arg in self.arguments] |
| 107 | + self.state.document.autosummary_widths = widths |
| 108 | + return [] |
| 109 | + |
| 110 | + |
| 111 | +def configure(app: Sphinx, config: Config): |
| 112 | + """ |
| 113 | +
|
| 114 | + :param app: |
| 115 | + :param config: |
| 116 | + """ |
| 117 | + |
| 118 | + latex_elements = getattr(app.config, "latex_elements", {}) # type: ignore |
| 119 | + |
| 120 | + latex_preamble = stringlist.StringList(latex_elements.get("preamble", '')) |
| 121 | + latex_preamble.blankline() |
| 122 | + latex_preamble.append(r"\makeatletter") |
| 123 | + latex_preamble.append(r"\newcolumntype{\Xx}[2]{>{\raggedright\arraybackslash}p{\dimexpr") |
| 124 | + latex_preamble.append(r" (\linewidth-\arrayrulewidth)*#1/#2-\tw@\tabcolsep-\arrayrulewidth\relax}}") |
| 125 | + latex_preamble.append(r"\makeatother") |
| 126 | + latex_preamble.blankline() |
| 127 | + |
| 128 | + latex_elements["preamble"] = str(latex_preamble) |
| 129 | + app.config.latex_elements = latex_elements # type: ignore |
| 130 | + |
| 131 | + |
| 132 | +def setup(app: Sphinx): |
| 133 | + app.add_directive("autosummary", AutosummaryWidths, override=True) |
| 134 | + app.add_directive("autosummary-widths", WidthsDirective) |
| 135 | + app.connect("build-finished", latex.replace_unknown_unicode) |
| 136 | + app.connect("config-inited", configure) |
0 commit comments