Skip to content

Commit 563e84a

Browse files
Custom Bootstrap-specific HTML5Translator (pandas-dev#21)
1 parent b4c3d7d commit 563e84a

File tree

3 files changed

+70
-24
lines changed

3 files changed

+70
-24
lines changed

pandas-docs/source/_themes/bootstrap_docs_theme/static/sphinx-bootstrap.css_t

+1-24
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ a.headerlink:hover {
4646
color: white;
4747
}
4848

49-
div.admonition p.admonition-title + p, div.deprecated p {
49+
div.deprecated p {
5050
display: inline;
5151
}
5252

@@ -62,33 +62,10 @@ div.highlight{
6262
background-color: white;
6363
}
6464

65-
div.note {
66-
background-color: #eee;
67-
border: 1px solid #ccc;
68-
}
69-
70-
div.seealso {
71-
background-color: #ffc;
72-
border: 1px solid #ff6;
73-
}
74-
7565
div.topic {
7666
background-color: #eee;
7767
}
7868

79-
div.warning {
80-
background-color: #ffe4e4;
81-
border: 1px solid #f66;
82-
}
83-
84-
p.admonition-title {
85-
display: inline;
86-
}
87-
88-
p.admonition-title:after {
89-
content: ":";
90-
}
91-
9269
pre {
9370
padding: 10px;
9471
background-color: rgb(250,250,250);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""A custom Sphinx HTML Translator for Bootstrap layout
2+
"""
3+
import sys
4+
import re
5+
6+
from docutils import nodes
7+
8+
from sphinx.locale import admonitionlabels, _
9+
from sphinx.writers.html5 import HTML5Translator
10+
11+
12+
# Mapping of admonition classes to Bootstrap contextual classes
13+
alert_classes = {
14+
"attention": "primary",
15+
"caution": "warning",
16+
"danger": "danger",
17+
"error": "danger",
18+
"hint": "info",
19+
"important": "primary",
20+
"note": "info",
21+
"seealso": "info",
22+
"tip": "primary",
23+
"warning": "warning",
24+
"todo": "info",
25+
"example": "info",
26+
}
27+
28+
29+
class BootstrapHTML5Translator(HTML5Translator):
30+
"""Custom HTML Translator for a Bootstrap-ified Sphinx layout
31+
32+
This is a specialization of the HTML5 Translator of sphinx.
33+
Only a couple of functions have been overridden to produce valid HTML to be
34+
directly styled with Bootstrap.
35+
"""
36+
37+
def __init__(self, *args, **kwds):
38+
super().__init__(*args, **kwds)
39+
self.settings.table_style = "table"
40+
41+
def visit_admonition(self, node, name=""):
42+
# type: (nodes.Element, str) -> None
43+
# copy of sphinx source to add alert classes
44+
classes = ["alert"]
45+
if name:
46+
classes.append("alert-{0}".format(alert_classes[name]))
47+
self.body.append(self.starttag(node, "div", CLASS=" ".join(classes)))
48+
if name:
49+
node.insert(0, nodes.title(name, admonitionlabels[name]))
50+
51+
def visit_table(self, node):
52+
# type: (nodes.Element) -> None
53+
# copy of sphinx source to *not* add 'docutils' and 'align-default' classes
54+
# but add 'table' class
55+
self.generate_targets_for_table(node)
56+
57+
self._table_row_index = 0
58+
59+
classes = [cls.strip(" \t\n") for cls in self.settings.table_style.split(",")]
60+
# classes.insert(0, "docutils") # compat
61+
# if 'align' in node:
62+
# classes.append('align-%s' % node['align'])
63+
tag = self.starttag(node, "table", CLASS=" ".join(classes))
64+
self.body.append(tag)

pandas-docs/source/conf.py

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
logger = logging.getLogger(__name__)
2525

2626

27+
sys.path.append('.')
28+
from bootstrap_html_translator import BootstrapHTML5Translator
29+
30+
2731
# -----------------------------------------------------------------------------
2832
# IPython monkeypath - set all code blocks to verbatim to speed-up doc build
2933

@@ -985,3 +989,4 @@ def setup(app):
985989
app.add_autodocumenter(AccessorMethodDocumenter)
986990
app.add_autodocumenter(AccessorCallableDocumenter)
987991
app.add_directive('autosummary', PandasAutosummary)
992+
app.set_translator('html', BootstrapHTML5Translator)

0 commit comments

Comments
 (0)