forked from pydata/pydata-sphinx-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
225 lines (182 loc) · 8.08 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
Bootstrap-based sphinx theme from the PyData community
"""
import os
from sphinx.errors import ExtensionError
from .bootstrap_html_translator import BootstrapHTML5Translator
import docutils
__version__ = "0.4.2dev0"
def add_toctree_functions(app, pagename, templatename, context, doctree):
"""Add functions so Jinja templates can add toctree objects.
This converts the docutils nodes into a nested dictionary that Jinja can
use in our templating.
"""
from sphinx.environment.adapters.toctree import TocTree
def get_nav_object(maxdepth=None, collapse=True, **kwargs):
"""Return a list of nav links that can be accessed from Jinja.
Parameters
----------
maxdepth: int
How many layers of TocTree will be returned
collapse: bool
Whether to only include sub-pages of the currently-active page,
instead of sub-pages of all top-level pages of the site.
kwargs: key/val pairs
Passed to the `TocTree.get_toctree_for` Sphinx method
"""
# The TocTree will contain the full site TocTree including sub-pages.
# "collapse=True" collapses sub-pages of non-active TOC pages.
# maxdepth controls how many TOC levels are returned
toctree = TocTree(app.env).get_toctree_for(
pagename, app.builder, collapse=collapse, maxdepth=maxdepth, **kwargs
)
# If no toctree is defined (AKA a single-page site), skip this
if toctree is None:
return []
# toctree has this structure
# <caption>
# <bullet_list>
# <list_item classes="toctree-l1">
# <list_item classes="toctree-l1">
# `list_item`s are the actual TOC links and are the only thing we want
toc_items = [
item
for child in toctree.children
for item in child
if isinstance(item, docutils.nodes.list_item)
]
# Now convert our docutils nodes into dicts that Jinja can use
nav = [docutils_node_to_jinja(child, only_pages=True) for child in toc_items]
return nav
def get_page_toc_object():
"""Return a list of within-page TOC links that can be accessed from Jinja."""
self_toc = TocTree(app.env).get_toc_for(pagename, app.builder)
try:
# If there's only one child, assume we have a single "title" as top header
# so start the TOC at the first item's children (AKA, level 2 headers)
if len(self_toc.children) == 1:
nav = docutils_node_to_jinja(self_toc.children[0]).get("children", [])
else:
nav = [docutils_node_to_jinja(item) for item in self_toc.children]
return nav
except Exception:
return {}
def navbar_align_class():
"""Return the class that aligns the navbar based on config."""
align = context.get("theme_navbar_align", "content")
align_options = {
"content": ("col-lg-9", "mr-auto"),
"left": ("", "mr-auto"),
"right": ("", "ml-auto"),
}
if align not in align_options:
raise ValueError(
(
"Theme optione navbar_align must be one of"
f"{align_options.keys()}, got: {align}"
)
)
return align_options[align]
context["get_nav_object"] = get_nav_object
context["get_page_toc_object"] = get_page_toc_object
context["navbar_align_class"] = navbar_align_class
def docutils_node_to_jinja(list_item, only_pages=False):
"""Convert a docutils node to a structure that can be read by Jinja.
Parameters
----------
list_item : docutils list_item node
A parent item, potentially with children, corresponding to the level
of a TocTree.
only_pages : bool
Only include items for full pages in the output dictionary. Exclude
anchor links (TOC items with a URL that starts with #)
Returns
-------
nav : dict
The TocTree, converted into a dictionary with key/values that work
within Jinja.
"""
if not list_item.children:
return None
# We assume this structure of a list item:
# <list_item>
# <compact_paragraph >
# <reference> <-- the thing we want
reference = list_item.children[0].children[0]
title = reference.astext()
url = reference.attributes["refuri"]
active = "current" in list_item.attributes["classes"]
# If we've got an anchor link, skip it if we wish
if only_pages and "#" in url:
return None
# Converting the docutils attributes into jinja-friendly objects
nav = {}
nav["title"] = title
nav["url"] = url
nav["active"] = active
# Recursively convert children as well
# If there are sub-pages for this list_item, there should be two children:
# a paragraph, and a bullet_list.
nav["children"] = []
if len(list_item.children) > 1:
# The `.children` of the bullet_list has the nodes of the sub-pages.
subpage_list = list_item.children[1].children
for sub_page in subpage_list:
child_nav = docutils_node_to_jinja(sub_page, only_pages=only_pages)
if child_nav is not None:
nav["children"].append(child_nav)
return nav
# -----------------------------------------------------------------------------
def setup_edit_url(app, pagename, templatename, context, doctree):
"""Add a function that jinja can access for returning the edit URL of a page."""
def get_edit_url():
"""Return a URL for an "edit this page" link."""
required_values = ["github_user", "github_repo", "github_version"]
for val in required_values:
if not context.get(val):
raise ExtensionError(
"Missing required value for `edit this page` button. "
"Add %s to your `html_context` configuration" % val
)
# Enable optional custom github url for self-hosted github instances
github_url = "https://github.com"
if context.get("github_url"):
github_url = context["github_url"]
github_user = context["github_user"]
github_repo = context["github_repo"]
github_version = context["github_version"]
file_name = f"{pagename}{context['page_source_suffix']}"
# Make sure that doc_path has a path separator only if it exists (to avoid //)
doc_path = context.get("doc_path", "")
if doc_path and not doc_path.endswith("/"):
doc_path = f"{doc_path}/"
# Build the URL for "edit this button"
url_edit = (
f"{github_url}/{github_user}/{github_repo}"
f"/edit/{github_version}/{doc_path}{file_name}"
)
return url_edit
context["get_edit_url"] = get_edit_url
# Ensure that the max TOC level is an integer
context["theme_show_toc_level"] = int(context.get("theme_show_toc_level", 1))
# -----------------------------------------------------------------------------
def get_html_theme_path():
"""Return list of HTML theme paths."""
theme_path = os.path.abspath(os.path.dirname(__file__))
return [theme_path]
def setup(app):
theme_path = get_html_theme_path()[0]
app.add_html_theme("pydata_sphinx_theme", theme_path)
app.set_translator("html", BootstrapHTML5Translator)
# Read the Docs uses ``readthedocs`` as the name of the build, and also
# uses a special "dirhtml" builder so we need to replace these both with
# our custom HTML builder
app.set_translator("readthedocs", BootstrapHTML5Translator, override=True)
app.set_translator("readthedocsdirhtml", BootstrapHTML5Translator, override=True)
app.connect("html-page-context", setup_edit_url)
app.connect("html-page-context", add_toctree_functions)
# Update templates for sidebar
pkgdir = os.path.abspath(os.path.dirname(__file__))
path_templates = os.path.join(pkgdir, "_templates")
app.config.templates_path.append(path_templates)
return {"parallel_read_safe": True, "parallel_write_safe": True}