Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5460 Make JS refactoring compatible with old templates #5500

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions sphinx/builders/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,11 +1564,22 @@ def convert_html_js_files(app, config):
config.html_js_files = html_js_files # type: ignore


def add_documentation_options_js(app, config):
# type: (Sphinx, Config) -> None
"""Adds path of ``add_documentation_options.js_t`` to the ``html_js_files``
variable. This avoids issues with older templates that use a custom
layout file."""
config.html_js_files.append('documentation_options.js')


def setup_js_tag_helper(app, pagename, templatexname, context, doctree):
# type: (Sphinx, unicode, unicode, Dict, nodes.Node) -> None
"""Set up js_tag() template helper.
"""Set up js_tag() template helper and add special handling for
``add_documentation_options``.

.. note:: This set up function is added to keep compatibility with webhelper.
.. note:: This set up function is added to keep compatibility with
webhelper, as well as with older templates that use a custom
layout file.
"""
pathto = context.get('pathto')

Expand All @@ -1586,6 +1597,10 @@ def js_tag(js):
attrs.append('%s="%s"' % (key, htmlescape(value, True)))
if js.filename:
attrs.append('src="%s"' % pathto(js.filename, resource=True))
# special handling of 'documentation_options.js'
if 'documentation_options.js' in js.filename:
attrs.append('id="documentation_options"')
attrs.append('data-url_root="%s"' % pathto('', 1))
else:
# str value (old styled)
attrs.append('type="text/javascript"')
Expand Down Expand Up @@ -1663,6 +1678,7 @@ def setup(app):

# event handlers
app.connect('config-inited', convert_html_css_files)
app.connect('config-inited', add_documentation_options_js)
app.connect('config-inited', convert_html_js_files)
app.connect('builder-inited', validate_math_renderer)
app.connect('html-page-context', setup_js_tag_helper)
Expand Down
1 change: 0 additions & 1 deletion sphinx/themes/basic/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ <h3>{{ _('Navigation') }}</h3>
{%- endmacro %}

{%- macro script() %}
<script type="text/javascript" id="documentation_options" data-url_root="{{ pathto('', 1) }}" src="{{ pathto('_static/documentation_options.js', 1) }}"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As commented above, 3rd generation themes does not use js_tag() helper. So this would not be moved.

In addition, to support this tag by js_tag(), we have to give a special code for this like you modified. I feel it is too much.

{%- for js in script_files %}
{{ js_tag(js) }}
{%- endfor %}
Expand Down
4 changes: 3 additions & 1 deletion sphinx/themes/basic/static/documentation_options.js_t
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var DOCUMENTATION_OPTIONS = {
URL_ROOT: document.getElementById("documentation_options").getAttribute('data-url_root'),
URL_ROOT: document.getElementById("documentation_options") ?
document.getElementById("documentation_options").getAttribute('data-url_root') :
DOCUMENTATION_OPTIONS.URL_ROOT,
VERSION: '{{ release|e }}',
LANGUAGE: '{{ language }}',
COLLAPSE_INDEX: false,
Expand Down