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