|
| 1 | +import os |
| 2 | + |
| 3 | +from sphinx import version_info |
| 4 | +from sphinx.util import logging |
| 5 | + |
| 6 | +import requests |
| 7 | + |
| 8 | +from .utils import get_github_username_repo, get_bitbucket_username_repo, get_gitlab_username_repo |
| 9 | + |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +# https://www.sphinx-doc.org/en/stable/extdev/appapi.html#event-html-page-context |
| 15 | +def manipulate_config(app, config): |
| 16 | + logger.info( |
| 17 | + 'Running "manipulate_config" from Read the Docs "sphinx_build_compatibility" extension. ' |
| 18 | + 'Consider removing it from your requirements and migrating your documentation accordingly. ' |
| 19 | + 'This extension is useful only as a transition but it will not be maintained.' |
| 20 | + ) |
| 21 | + |
| 22 | + # Add Read the Docs' static path. |
| 23 | + # Add to the end because it overwrites previous files. |
| 24 | + if not hasattr(config, "html_static_path"): |
| 25 | + config.html_static_path = [] |
| 26 | + if os.path.exists('_static'): |
| 27 | + config.html_static_path.append('_static') |
| 28 | + |
| 29 | + # Define this variable in case it's not defined by the user. |
| 30 | + # It defaults to `alabaster` which is the default from Sphinx. |
| 31 | + # https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_theme |
| 32 | + if not hasattr(config, "html_theme"): |
| 33 | + config.html_theme = 'alabaster' |
| 34 | + |
| 35 | + # Example: ``/docs/`` |
| 36 | + conf_py_path = "/" |
| 37 | + conf_py_path += os.path.relpath( |
| 38 | + str(app.srcdir), |
| 39 | + os.environ.get("READTHEDOCS_REPOSITORY_PATH"), |
| 40 | + ).strip("/") |
| 41 | + conf_py_path += "/" |
| 42 | + |
| 43 | + git_clone_url = os.environ.get("READTHEDOCS_GIT_CLONE_URL") |
| 44 | + github_user, github_repo = get_github_username_repo(git_clone_url) |
| 45 | + bitbucket_user, bitbucket_repo = get_bitbucket_username_repo(git_clone_url) |
| 46 | + gitlab_user, gitlab_repo = get_gitlab_username_repo(git_clone_url) |
| 47 | + |
| 48 | + project_slug = os.environ.get("READTHEDOCS_PROJECT") |
| 49 | + version_slug = os.environ.get("READTHEDOCS_VERSION") |
| 50 | + production_domain = os.environ.get("READTHEDOCS_PRODUCTION_DOMAIN", "readthedocs.org") |
| 51 | + |
| 52 | + scheme = "https" |
| 53 | + if production_domain.startswith("devthedocs"): |
| 54 | + scheme = "http" |
| 55 | + |
| 56 | + # We are using APIv2 to pull active versions, downloads and subprojects |
| 57 | + # because APIv3 requires a token. |
| 58 | + try: |
| 59 | + response_project = requests.get( |
| 60 | + f"{scheme}://{production_domain}/api/v3/projects/{project_slug}/", |
| 61 | + timeout=2, |
| 62 | + ).json() |
| 63 | + language = response_project["language"]["code"] |
| 64 | + except Exception: |
| 65 | + logger.warning( |
| 66 | + "An error ocurred when hitting API to fetch project language. Defaulting to 'en'.", |
| 67 | + exc_info=True, |
| 68 | + ) |
| 69 | + language = "en" |
| 70 | + |
| 71 | + try: |
| 72 | + response_versions = requests.get( |
| 73 | + f"{scheme}://{production_domain}/api/v3/projects/{project_slug}/versions/?active=true", |
| 74 | + timeout=2, |
| 75 | + ).json() |
| 76 | + versions = [ |
| 77 | + (version["slug"], f"/{language}/{version['slug']}/") |
| 78 | + for version in response_versions["results"] |
| 79 | + ] |
| 80 | + except Exception: |
| 81 | + logger.warning( |
| 82 | + "An error ocurred when hitting API to fetch active versions. Defaulting to an empty list.", |
| 83 | + exc_info=True, |
| 84 | + ) |
| 85 | + versions = [] |
| 86 | + |
| 87 | + try: |
| 88 | + downloads = [] |
| 89 | + for version in response_versions["results"]: |
| 90 | + if version["slug"] != version_slug: |
| 91 | + continue |
| 92 | + |
| 93 | + for key, value in version["downloads"]: |
| 94 | + downloads.append( |
| 95 | + ( |
| 96 | + key, |
| 97 | + value, |
| 98 | + ), |
| 99 | + ) |
| 100 | + except Exception: |
| 101 | + logger.warning( |
| 102 | + "An error ocurred when generating the list of downloads. Defaulting to an empty list.", |
| 103 | + exc_info=True, |
| 104 | + ) |
| 105 | + downloads = [] |
| 106 | + |
| 107 | + try: |
| 108 | + subprojects = [] |
| 109 | + response_project = requests.get( |
| 110 | + f"{scheme}://{production_domain}/api/v2/project/?slug={project_slug}", |
| 111 | + timeout=2, |
| 112 | + ).json() |
| 113 | + project_id = response_project["results"][0]["id"] |
| 114 | + |
| 115 | + response_subprojects = requests.get( |
| 116 | + f"{scheme}://readthedocs.org/api/v2/project/{project_id}/subprojects/", |
| 117 | + timeout=2, |
| 118 | + ).json() |
| 119 | + for subproject in response_subprojects["subprojects"]: |
| 120 | + subprojects.append( |
| 121 | + ( |
| 122 | + subproject["slug"], |
| 123 | + subproject["canonical_url"], |
| 124 | + ), |
| 125 | + ) |
| 126 | + except Exception: |
| 127 | + logger.warning( |
| 128 | + "An error ocurred when hitting API to fetch project/subprojects. Defaulting to an empty list.", |
| 129 | + exc_info=True, |
| 130 | + ) |
| 131 | + subprojects = [] |
| 132 | + |
| 133 | + # Add project information to the template context. |
| 134 | + context = { |
| 135 | + 'html_theme': config.html_theme, |
| 136 | + 'current_version': os.environ.get("READTHEDOCS_VERSION_NAME"), |
| 137 | + 'version_slug': version_slug, |
| 138 | + |
| 139 | + # NOTE: these are used to dump them in some JS files and to build the URLs in flyout. |
| 140 | + # However, we are replacing them with the new Addons. |
| 141 | + # I wouldn't include them in the first version of the extension. |
| 142 | + # We could hardcode them if we want, tho. |
| 143 | + # |
| 144 | + # 'MEDIA_URL': "{{ settings.MEDIA_URL }}", |
| 145 | + # 'STATIC_URL': "{{ settings.STATIC_URL }}", |
| 146 | + # 'proxied_static_path': "{{ proxied_static_path }}", |
| 147 | + |
| 148 | + 'PRODUCTION_DOMAIN': production_domain, |
| 149 | + 'versions': versions, |
| 150 | + "downloads": downloads, |
| 151 | + "subprojects": subprojects, |
| 152 | + |
| 153 | + 'slug': project_slug, |
| 154 | + 'rtd_language': os.environ.get("READTHEDOCS_LANGUAGE"), |
| 155 | + 'canonical_url': os.environ.get("READTHEDOCS_CANONICAL_URL"), |
| 156 | + |
| 157 | + # NOTE: these seem to not be used. |
| 158 | + # 'name': u'{{ project.name }}', |
| 159 | + # 'analytics_code': '{{ project.analytics_code }}', |
| 160 | + # 'single_version': {{ project.is_single_version }}, |
| 161 | + # 'programming_language': u'{{ project.programming_language }}', |
| 162 | + |
| 163 | + 'conf_py_path': conf_py_path, |
| 164 | + # Used only for "readthedocs-sphinx-ext" which we are not installing anymore. |
| 165 | + # 'api_host': '{{ api_host }}', |
| 166 | + # 'proxied_api_host': '{{ project.proxied_api_host }}', |
| 167 | + |
| 168 | + 'github_user': github_user, |
| 169 | + 'github_repo': github_repo, |
| 170 | + 'github_version': os.environ.get("READTHEDOCS_GIT_IDENTIFIER"), |
| 171 | + 'display_github': github_user is not None, |
| 172 | + 'bitbucket_user': bitbucket_user, |
| 173 | + 'bitbucket_repo': bitbucket_repo, |
| 174 | + 'bitbucket_version': os.environ.get("READTHEDOCS_GIT_IDENTIFIER"), |
| 175 | + 'display_bitbucket': bitbucket_user is not None, |
| 176 | + 'gitlab_user': gitlab_user, |
| 177 | + 'gitlab_repo': gitlab_repo, |
| 178 | + 'gitlab_version': os.environ.get("READTHEDOCS_GIT_IDENTIFIER"), |
| 179 | + 'display_gitlab': gitlab_user is not None, |
| 180 | + 'READTHEDOCS': True, |
| 181 | + 'using_theme': (config.html_theme == "default"), |
| 182 | + 'new_theme': (config.html_theme == "sphinx_rtd_theme"), |
| 183 | + 'source_suffix': ".rst", |
| 184 | + 'ad_free': False, |
| 185 | + 'docsearch_disabled': False, |
| 186 | + |
| 187 | + # We don't support Google analytics anymore. |
| 188 | + # See https://github.com/readthedocs/readthedocs.org/issues/9530 |
| 189 | + 'user_analytics_code': "", |
| 190 | + 'global_analytics_code': None, |
| 191 | + |
| 192 | + 'commit': os.environ.get("READTHEDOCS_GIT_COMMIT_HASH")[:8], |
| 193 | + } |
| 194 | + |
| 195 | + # For sphinx >=1.8 we can use html_baseurl to set the canonical URL. |
| 196 | + # https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_baseurl |
| 197 | + if version_info >= (1, 8): |
| 198 | + if not hasattr(config, 'html_baseurl'): |
| 199 | + config.html_baseurl = context['canonical_url'] |
| 200 | + context['canonical_url'] = None |
| 201 | + |
| 202 | + |
| 203 | + if hasattr(config, 'html_context'): |
| 204 | + for key in context: |
| 205 | + if key not in config.html_context: |
| 206 | + config.html_context[key] = context[key] |
| 207 | + else: |
| 208 | + config.html_context = context |
| 209 | + |
| 210 | + project_language = os.environ.get("READTHEDOCS_LANGUAGE") |
| 211 | + |
| 212 | + # User's Sphinx configurations |
| 213 | + language_user = config.language |
| 214 | + latex_engine_user = config.latex_engine |
| 215 | + latex_elements_user = config.latex_elements |
| 216 | + |
| 217 | + # Remove this once xindy gets installed in Docker image and XINDYOPS |
| 218 | + # env variable is supported |
| 219 | + # https://github.com/rtfd/readthedocs-docker-images/pull/98 |
| 220 | + latex_use_xindy = False |
| 221 | + |
| 222 | + chinese = any([ |
| 223 | + language_user in ('zh_CN', 'zh_TW'), |
| 224 | + project_language in ('zh_CN', 'zh_TW'), |
| 225 | + ]) |
| 226 | + |
| 227 | + japanese = any([ |
| 228 | + language_user == 'ja', |
| 229 | + project_language == 'ja', |
| 230 | + ]) |
| 231 | + |
| 232 | + if chinese: |
| 233 | + config.latex_engine = latex_engine_user or 'xelatex' |
| 234 | + |
| 235 | + latex_elements_rtd = { |
| 236 | + 'preamble': '\\usepackage[UTF8]{ctex}\n', |
| 237 | + } |
| 238 | + config.latex_elements = latex_elements_user or latex_elements_rtd |
| 239 | + elif japanese: |
| 240 | + config.latex_engine = latex_engine_user or 'platex' |
| 241 | + |
| 242 | + # Make sure our build directory is always excluded |
| 243 | + if not hasattr(config, "exclude_patterns"): |
| 244 | + config.exclude_patterns = [] |
| 245 | + config.exclude_patterns.extend(['_build']) |
| 246 | + |
| 247 | + |
| 248 | +def setup(app): |
| 249 | + app.connect('config-inited', manipulate_config) |
| 250 | + |
| 251 | + return { |
| 252 | + 'version': "0.0.0", |
| 253 | + 'parallel_read_safe': True, |
| 254 | + 'parallel_write_safe': True, |
| 255 | + } |
0 commit comments