Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit cad5845

Browse files
authored
Merge pull request #62 from readthedocs/get-domain-data
Increase the amount of data we're saving during build
2 parents 82e0858 + c13a5aa commit cad5845

File tree

4 files changed

+99
-8
lines changed

4 files changed

+99
-8
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ notifications:
1919
- readthedocs:y3hjODOi7EIz1JAbD1Zb41sz#random
2020
on_success: change
2121
on_failure: always
22+
branches:
23+
only:
24+
- master

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Releasing
2323

2424
#. Increment the version in ``setup.py``
2525
#. Tag the release in git: ``git tag $NEW_VERSION``.
26-
#. Push the tag to GitHub: ``git push --tags origin``
26+
#. Push the tag to GitHub: ``git push --tags origin master``
2727
#. Upload the package to PyPI:
2828

2929
.. code:: bash

readthedocs_ext/readthedocs.py

+79-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
StandaloneHTMLBuilder)
1616
from sphinx.util.console import bold
1717

18+
1819
from .embed import EmbedDirective
1920
from .mixins import BuilderMixin
2021

@@ -27,10 +28,20 @@
2728
log = getLogger(__name__)
2829

2930
DEFAULT_STATIC_URL = 'https://assets.readthedocs.org/static/'
31+
ONLINE_BUILDERS = [
32+
'readthedocs', 'readthedocsdirhtml', 'readthedocssinglehtml'
33+
]
34+
# Only run JSON output once during HTML build
35+
# This saves resources and keeps filepaths correct,
36+
# because singlehtml filepaths are different
37+
JSON_BUILDERS = [
38+
'html', 'dirhtml',
39+
'readthedocs', 'readthedocsdirhtml'
40+
]
3041

3142
# Whitelist keys that we want to output
3243
# to the json artifacts.
33-
KEYS = [
44+
JSON_KEYS = [
3445
'body',
3546
'title',
3647
'sourcename',
@@ -65,15 +76,12 @@ def update_body(app, pagename, templatename, context, doctree):
6576
"""
6677

6778
STATIC_URL = context.get('STATIC_URL', DEFAULT_STATIC_URL)
68-
online_builders = [
69-
'readthedocs', 'readthedocsdirhtml', 'readthedocssinglehtml'
70-
]
7179
if app.builder.name == 'readthedocssinglehtmllocalmedia':
7280
if 'html_theme' in context and context['html_theme'] == 'sphinx_rtd_theme':
7381
theme_css = '_static/css/theme.css'
7482
else:
7583
theme_css = '_static/css/badge_only.css'
76-
elif app.builder.name in online_builders:
84+
elif app.builder.name in ONLINE_BUILDERS:
7785
if 'html_theme' in context and context['html_theme'] == 'sphinx_rtd_theme':
7886
theme_css = '%scss/sphinx_rtd_theme.css' % STATIC_URL
7987
else:
@@ -148,6 +156,8 @@ def generate_json_artifacts(app, pagename, templatename, context, doctree):
148156
149157
This way we can skip generating this in other build step.
150158
"""
159+
if app.builder.name not in JSON_BUILDERS:
160+
return
151161
try:
152162
# We need to get the output directory where the docs are built
153163
# _build/json.
@@ -161,7 +171,7 @@ def generate_json_artifacts(app, pagename, templatename, context, doctree):
161171
with open(outjson, 'w+') as json_file:
162172
to_context = {
163173
key: context.get(key, '')
164-
for key in KEYS
174+
for key in JSON_KEYS
165175
}
166176
json.dump(to_context, json_file, indent=4)
167177
except TypeError:
@@ -172,12 +182,73 @@ def generate_json_artifacts(app, pagename, templatename, context, doctree):
172182
log.exception(
173183
'Fail to save JSON output for page {page}'.format(page=outjson)
174184
)
175-
except Exception as e:
185+
except Exception:
176186
log.exception(
177187
'Failure in JSON search dump for page {page}'.format(page=outjson)
178188
)
179189

180190

191+
def dump_sphinx_data(app, exception):
192+
"""
193+
Dump data that is only in memory during Sphinx build.
194+
This is mostly used for search indexing.
195+
196+
This includes:
197+
198+
* `paths`: A mapping of HTML Filename -> RST file
199+
* `pages`: A mapping of HTML Filename -> Sphinx Page name
200+
* `titles`: A mapping of HTML Filename -> Page Title
201+
* `types`: A mapping of Sphinx Domain type slugs -> human-readable name for that type
202+
203+
"""
204+
if app.builder.name not in JSON_BUILDERS or exception:
205+
return
206+
try:
207+
types = {}
208+
titles = {}
209+
paths = {}
210+
pages = {}
211+
212+
for domain_name, domain_obj in app.env.domains.items():
213+
for type_name, type_obj in domain_obj.object_types.items():
214+
key = "{}:{}".format(domain_name, type_name)
215+
types[key] = str(type_obj.lname)
216+
217+
for page, title in app.env.titles.items():
218+
page_uri = app.builder.get_target_uri(page)
219+
titles[page_uri] = title.astext()
220+
paths[page_uri] = app.env.doc2path(page, base=None)
221+
pages[page_uri] = page
222+
223+
to_dump = {
224+
'types': types,
225+
'titles': titles,
226+
'paths': paths,
227+
'pages': pages,
228+
}
229+
230+
# We need to get the output directory where the docs are built
231+
# _build/json.
232+
build_json = os.path.abspath(
233+
os.path.join(app.outdir, '..', 'json')
234+
)
235+
outjson = os.path.join(build_json, 'readthedocs-sphinx-domain-names.json')
236+
with open(outjson, 'w+') as json_file:
237+
json.dump(to_dump, json_file, indent=4)
238+
except TypeError:
239+
log.exception(
240+
'Fail to encode JSON for object names'
241+
)
242+
except IOError:
243+
log.exception(
244+
'Fail to save JSON for object names'
245+
)
246+
except Exception:
247+
log.exception(
248+
'Failure in JSON search dump for object names'
249+
)
250+
251+
181252
class HtmlBuilderMixin(BuilderMixin):
182253

183254
static_readthedocs_files = [
@@ -271,6 +342,7 @@ def setup(app):
271342
app.connect('builder-inited', finalize_media)
272343
app.connect('html-page-context', update_body)
273344
app.connect('html-page-context', generate_json_artifacts)
345+
app.connect('build-finished', dump_sphinx_data)
274346

275347
# Embed
276348
app.add_directive('readthedocs-embed', EmbedDirective)

tests/test_integration.py

+16
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ def test_generate_json_artifacts(self):
7272
],
7373
)
7474

75+
def test_generate_json_domain_artifacts(self):
76+
self._run_test(
77+
'pyexample-json',
78+
'_build/json/readthedocs-sphinx-domain-names.json',
79+
[
80+
# types
81+
'"js:class": "class"',
82+
# pages
83+
'"index.html": "index"',
84+
# paths
85+
'"index.html": "index.rst"',
86+
# titles
87+
'"index.html": "Welcome to pyexample',
88+
],
89+
)
90+
7591
def test_escape_js_vars(self):
7692
with build_output('pyexample', '_build/readthedocs/escape\' this js.html',
7793
builder='readthedocs') as data:

0 commit comments

Comments
 (0)