|
3 | 3 | # University of Illinois/NCSA Open Source License. Both these licenses can be
|
4 | 4 | # found in the LICENSE file.
|
5 | 5 |
|
6 |
| -"""Sphinx ReadTheDocs theme. |
| 6 | +""" |
| 7 | +Sphinx Read the Docs theme. |
7 | 8 |
|
8 | 9 | From https://github.com/ryan-roemer/sphinx-bootstrap-theme.
|
9 |
| -
|
10 | 10 | """
|
| 11 | + |
11 | 12 | import os
|
| 13 | +from os import path |
| 14 | +from sys import version_info as python_version |
12 | 15 |
|
13 |
| -VERSION = (0, 1, 6) |
| 16 | +from sphinx import version_info as sphinx_version |
| 17 | +from sphinx.locale import _ |
| 18 | +from sphinx.util.logging import getLogger |
14 | 19 |
|
15 |
| -__version__ = ".".join(str(v) for v in VERSION) |
| 20 | + |
| 21 | +__version__ = '3.0.1' |
16 | 22 | __version_full__ = __version__
|
17 | 23 |
|
| 24 | +logger = getLogger(__name__) |
| 25 | + |
18 | 26 |
|
19 | 27 | def get_html_theme_path():
|
20 | 28 | """Return list of HTML theme paths."""
|
21 |
| - cur_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) |
| 29 | + logger.warning( |
| 30 | + _('Calling get_html_theme_path is deprecated. If you are calling it to define html_theme_path, you are safe to remove that code.') |
| 31 | + ) |
| 32 | + |
| 33 | + cur_dir = path.abspath(path.dirname(path.dirname(__file__))) |
22 | 34 | return cur_dir
|
| 35 | + |
| 36 | + |
| 37 | +def config_initiated(app, config): |
| 38 | + theme_options = config.html_theme_options or {} |
| 39 | + if theme_options.get('canonical_url'): |
| 40 | + logger.warning( |
| 41 | + _('The canonical_url option is deprecated, use the html_baseurl option from Sphinx instead.') |
| 42 | + ) |
| 43 | + |
| 44 | + if theme_options.get("analytics_id"): |
| 45 | + logger.warning( |
| 46 | + _('The analytics_id option is deprecated, use the sphinxcontrib-googleanalytics extension instead.') |
| 47 | + ) |
| 48 | + |
| 49 | + if theme_options.get("analytics_anonymize_ip"): |
| 50 | + logger.warning( |
| 51 | + _('The analytics_anonymize_ip option is deprecated, use the sphinxcontrib-googleanalytics extension instead.') |
| 52 | + ) |
| 53 | + |
| 54 | + if "extra_css_files" in config.html_context: |
| 55 | + logger.warning( |
| 56 | + _('The extra_css_file option is deprecated, use the html_css_files option from Sphinx instead.') |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def extend_html_context(app, pagename, templatename, context, doctree): |
| 61 | + # Add ``sphinx_version_info`` tuple for use in Jinja templates |
| 62 | + context['sphinx_version_info'] = sphinx_version |
| 63 | + |
| 64 | + # Inject all the Read the Docs environment variables in the context: |
| 65 | + # https://docs.readthedocs.io/en/stable/reference/environment-variables.html |
| 66 | + context['READTHEDOCS'] = os.environ.get("READTHEDOCS", False) == "True" |
| 67 | + if context['READTHEDOCS']: |
| 68 | + for key, value in os.environ.items(): |
| 69 | + if key.startswith("READTHEDOCS_"): |
| 70 | + context[key] = value |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +# See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package |
| 75 | +def setup(app): |
| 76 | + if python_version[0] < 3: |
| 77 | + logger.error("Python 2 is not supported with sphinx_rtd_theme, update to Python 3.") |
| 78 | + |
| 79 | + app.require_sphinx('6.0') |
| 80 | + if app.config.html4_writer: |
| 81 | + logger.error("'html4_writer' is not supported with sphinx_rtd_theme.") |
| 82 | + |
| 83 | + # Since Sphinx 6, jquery isn't bundled anymore and we need to ensure that |
| 84 | + # the sphinxcontrib-jquery extension is enabled. |
| 85 | + # See: https://dev.readthedocs.io/en/latest/design/sphinx-jquery.html |
| 86 | + if sphinx_version >= (6, 0, 0): |
| 87 | + # Documentation of Sphinx guarantees that an extension is added and |
| 88 | + # enabled at most once. |
| 89 | + # See: https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx.application.Sphinx.setup_extension |
| 90 | + app.setup_extension("sphinxcontrib.jquery") |
| 91 | + # However, we need to call the extension's callback since setup_extension doesn't do it |
| 92 | + # See: https://github.com/sphinx-contrib/jquery/issues/23 |
| 93 | + from sphinxcontrib.jquery import add_js_files as jquery_add_js_files |
| 94 | + jquery_add_js_files(app, app.config) |
| 95 | + |
| 96 | + # Register the theme that can be referenced without adding a theme path |
| 97 | + app.add_html_theme('sphinx_rtd_theme', path.abspath(path.dirname(__file__))) |
| 98 | + |
| 99 | + # Add Sphinx message catalog for newer versions of Sphinx |
| 100 | + # See http://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx.application.Sphinx.add_message_catalog |
| 101 | + rtd_locale_path = path.join(path.abspath(path.dirname(__file__)), 'locale') |
| 102 | + app.add_message_catalog('sphinx', rtd_locale_path) |
| 103 | + app.connect('config-inited', config_initiated) |
| 104 | + |
| 105 | + # sphinx emits the permalink icon for headers, so choose one more in keeping with our theme |
| 106 | + app.config.html_permalinks_icon = "\uf0c1" |
| 107 | + |
| 108 | + # Extend the default context when rendering the templates. |
| 109 | + app.connect("html-page-context", extend_html_context) |
| 110 | + |
| 111 | + return {'parallel_read_safe': True, 'parallel_write_safe': True} |
0 commit comments