Debian Codegraph @

docs/_exts/bootstrap.py

raw
1 # BSD 3-Clause License
2 #
3 # Copyright (c) 2018, pandas
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 met:
8 #
9 # * Redistributions of source code must retain the above copyright notice, this
10 # list of conditions and the following disclaimer.
11 #
12 # * Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # * Neither the name of the copyright holder nor the names of its
17 # contributors may be used to endorse or promote products derived from
18 # this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 # Based on https://github.com/pydata/pydata-sphinx-theme
32
33 from docutils import nodes
34
35 import sphinx
36 from sphinx.ext.autosummary import autosummary_table
37 from sphinx.locale import admonitionlabels
38
39 import types
40
41 class BootstrapHTML5TranslatorMixin:
42 def __init__(self, *args, **kwds):
43 super().__init__(*args, **kwds)
44 self.settings.table_style = "table"
45
46 def starttag(self, *args, **kwargs):
47 """ensure an aria-level is set for any heading role"""
48 if kwargs.get("ROLE") == "heading" and "ARIA-LEVEL" not in kwargs:
49 kwargs["ARIA-LEVEL"] = "2"
50 return super().starttag(*args, **kwargs)
51
52 def visit_admonition(self, node, name: str = '') -> None:
53 admonitionclasses = {
54 'attention': 'alert-primary',
55 'caution': 'alert-secondary',
56 'danger': 'alert-danger',
57 'error': 'alert-danger',
58 'hint': 'alert-secondary',
59 'important': 'alert-primary',
60 'note': 'alert-info',
61 'seealso': 'alert-info',
62 'tip': 'alert-info',
63 'warning': 'alert-warning',
64 }
65
66 self.body.append(self.starttag(
67 node, 'div', CLASS=('alert ' + admonitionclasses[name])))
68 if name:
69 self.body.append(
70 self.starttag(node, 'div', '', CLASS='h5'))
71 self.body.append(str(admonitionlabels[name]))
72 self.body.append('</div>')
73
74 def depart_admonition(self, node) -> None:
75 self.body.append('</div>\n')
76
77 def visit_table(self, node):
78 # init the attributes
79 atts = {}
80
81 self._table_row_indices.append(0)
82
83 # get the classes
84 classes = [cls.strip(" \t\n") for cls in self.settings.table_style.split(",")]
85
86 # we're looking at the 'real_table', which is wrapped by an autosummary
87 if isinstance(node.parent, autosummary_table):
88 classes += ["autosummary"]
89
90 # add the width if set in a style attribute
91 if "width" in node:
92 atts["style"] = f'width: {node["width"]}'
93
94 # add specific class if align is set
95 if "align" in node:
96 classes.append(f'table-{node["align"]}')
97
98 tag = self.starttag(node, "table", CLASS=" ".join(classes), **atts)
99 self.body.append(tag)
100
101 def setup_translators(app):
102 if app.builder.format != "html":
103 return
104
105 if not app.registry.translators.items():
106 translator = types.new_class(
107 "BootstrapHTML5Translator",
108 (
109 BootstrapHTML5TranslatorMixin,
110 app.builder.default_translator_class,
111 ),
112 {},
113 )
114 app.set_translator(app.builder.name, translator, override=True)
115 else:
116 for name, klass in app.registry.translators.items():
117 translator = types.new_class(
118 "BootstrapHTML5Translator",
119 (
120 BootstrapHTML5TranslatorMixin,
121 klass,
122 ),
123 {},
124 )
125 app.set_translator(name, translator, override=True)
126
127 def setup(app):
128 app.connect("builder-inited", setup_translators)