conf.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Godot Engine documentation build configuration file
  4. import sphinx
  5. import sphinx_rtd_theme
  6. import sys
  7. import os
  8. # -- General configuration ------------------------------------------------
  9. needs_sphinx = "1.3"
  10. # Sphinx extension module names and templates location
  11. sys.path.append(os.path.abspath("_extensions"))
  12. extensions = [
  13. "sphinx_tabs.tabs",
  14. "notfound.extension",
  15. "sphinxext.opengraph",
  16. ]
  17. # Warning when the Sphinx Tabs extension is used with unknown
  18. # builders (like the dummy builder) - as it doesn't cause errors,
  19. # we can ignore this so we still can treat other warnings as errors.
  20. sphinx_tabs_nowarn = True
  21. # Custom 4O4 page HTML template.
  22. # https://github.com/readthedocs/sphinx-notfound-page
  23. notfound_context = {
  24. "title": "Page not found",
  25. "body": """
  26. <h1>Page not found</h1>
  27. <p>
  28. Sorry, we couldn't find that page. It may have been renamed or removed
  29. in the version of the documentation you're currently browsing.
  30. </p>
  31. <p>
  32. If you're currently browsing the
  33. <em>latest</em> version of the documentation, try browsing the
  34. <a href="/en/stable/"><em>stable</em> version of the documentation</a>.
  35. </p>
  36. <p>
  37. Alternatively, use the
  38. <a href="#" onclick="$('#rtd-search-form [name=\\'q\\']').focus()">Search docs</a>
  39. box on the left or <a href="/">go to the homepage</a>.
  40. </p>
  41. """,
  42. }
  43. # on_rtd is whether we are on readthedocs.org, this line of code grabbed from docs.readthedocs.org
  44. on_rtd = os.environ.get("READTHEDOCS", None) == "True"
  45. # Don't add `/en/latest` prefix during local development.
  46. # This makes it easier to test the custom 404 page by loading `/404.html`
  47. # on a local web server.
  48. if not on_rtd:
  49. notfound_urls_prefix = ''
  50. # Specify the site name for the Open Graph extension.
  51. ogp_site_name = "Godot Engine documentation"
  52. if not os.getenv("SPHINX_NO_GDSCRIPT"):
  53. extensions.append("gdscript")
  54. if not os.getenv("SPHINX_NO_DESCRIPTIONS"):
  55. extensions.append("godot_descriptions")
  56. templates_path = ["_templates"]
  57. # You can specify multiple suffix as a list of string: ['.rst', '.md']
  58. source_suffix = ".rst"
  59. source_encoding = "utf-8-sig"
  60. # The master toctree document
  61. master_doc = "index"
  62. # General information about the project
  63. project = "Godot Engine"
  64. copyright = (
  65. "2014-2022, Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0)"
  66. )
  67. author = "Juan Linietsky, Ariel Manzur and the Godot community"
  68. # Version info for the project, acts as replacement for |version| and |release|
  69. # The short X.Y version
  70. version = os.getenv("READTHEDOCS_VERSION", "latest")
  71. # The full version, including alpha/beta/rc tags
  72. release = version
  73. # Parse Sphinx tags passed from RTD via environment
  74. env_tags = os.getenv("SPHINX_TAGS")
  75. if env_tags is not None:
  76. for tag in env_tags.split(","):
  77. print("Adding Sphinx tag: %s" % tag.strip())
  78. tags.add(tag.strip()) # noqa: F821
  79. # Language / i18n
  80. supported_languages = {
  81. "en": "Godot Engine (%s) documentation in English",
  82. "de": "Godot Engine (%s) Dokumentation auf Deutsch",
  83. "es": "Documentación de Godot Engine (%s) en español",
  84. "fr": "Documentation de Godot Engine (%s) en français",
  85. "fi": "Godot Engine (%s) dokumentaatio suomeksi",
  86. "it": "Godot Engine (%s) documentazione in italiano",
  87. "ja": "Godot Engine (%s)の日本語のドキュメント",
  88. "ko": "Godot Engine (%s) 문서 (한국어)",
  89. "pl": "Dokumentacja Godot Engine (%s) w języku polskim",
  90. "pt_BR": "Documentação da Godot Engine (%s) em Português Brasileiro",
  91. "ru": "Документация Godot Engine (%s) на русском языке",
  92. "uk": "Документація до Godot Engine (%s) українською мовою",
  93. "zh_CN": "Godot Engine (%s) 简体中文文档",
  94. "zh_TW": "Godot Engine (%s) 正體中文 (台灣) 文件",
  95. }
  96. language = os.getenv("READTHEDOCS_LANGUAGE", "en")
  97. if not language in supported_languages.keys():
  98. print("Unknown language: " + language)
  99. print("Supported languages: " + ", ".join(supported_languages.keys()))
  100. print(
  101. "The configured language is either wrong, or it should be added to supported_languages in conf.py. Falling back to 'en'."
  102. )
  103. language = "en"
  104. is_i18n = tags.has("i18n") # noqa: F821
  105. exclude_patterns = ["_build"]
  106. # fmt: off
  107. # These imports should *not* be moved to the start of the file,
  108. # they depend on the sys.path.append call registering "_extensions".
  109. # GDScript syntax highlighting
  110. from gdscript import GDScriptLexer
  111. from sphinx.highlighting import lexers
  112. lexers["gdscript"] = GDScriptLexer()
  113. # fmt: on
  114. smartquotes = False
  115. # Pygments (syntax highlighting) style to use
  116. pygments_style = "sphinx"
  117. highlight_language = "gdscript"
  118. # -- Options for HTML output ----------------------------------------------
  119. html_theme = "sphinx_rtd_theme"
  120. html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
  121. if on_rtd:
  122. using_rtd_theme = True
  123. # Theme options
  124. html_theme_options = {
  125. # if we have a html_logo below, this shows /only/ the logo with no title text
  126. "logo_only": True,
  127. # Collapse navigation (False makes it tree-like)
  128. "collapse_navigation": False,
  129. }
  130. html_title = supported_languages[language] % version
  131. # VCS options: https://docs.readthedocs.io/en/latest/vcs.html#github
  132. html_context = {
  133. "display_github": not is_i18n, # Integrate GitHub
  134. "github_user": "godotengine", # Username
  135. "github_repo": "godot-docs", # Repo name
  136. "github_version": "master", # Version
  137. "conf_py_path": "/", # Path in the checkout to the docs root
  138. "godot_inject_language_links": True,
  139. "godot_docs_supported_languages": list(supported_languages.keys()),
  140. "godot_docs_basepath": "https://docs.godotengine.org/",
  141. "godot_docs_suffix": ".html",
  142. "godot_default_lang": "en",
  143. "godot_canonical_version": "stable",
  144. # Distinguish local development website from production website.
  145. # This prevents people from looking for changes on the production website after making local changes :)
  146. "godot_title_prefix": "" if on_rtd else "(DEV) ",
  147. }
  148. html_logo = "img/docs_logo.png"
  149. # These folders are copied to the documentation's HTML output
  150. html_static_path = ["_static"]
  151. html_extra_path = ["robots.txt"]
  152. # These paths are either relative to html_static_path
  153. # or fully qualified paths (e.g. https://...)
  154. html_css_files = [
  155. 'css/algolia.css',
  156. 'https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css',
  157. "css/custom.css",
  158. ]
  159. if not on_rtd:
  160. html_css_files.append("css/dev.css")
  161. html_js_files = [
  162. "js/custom.js",
  163. ('https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js', {'defer': 'defer'}),
  164. ('js/algolia.js', {'defer': 'defer'})
  165. ]
  166. # Output file base name for HTML help builder
  167. htmlhelp_basename = "GodotEnginedoc"
  168. # -- Options for reStructuredText parser ----------------------------------
  169. # Enable directives that insert the contents of external files
  170. file_insertion_enabled = False
  171. # -- Options for LaTeX output ---------------------------------------------
  172. # Grouping the document tree into LaTeX files. List of tuples
  173. # (source start file, target name, title,
  174. # author, documentclass [howto, manual, or own class]).
  175. latex_documents = [
  176. (
  177. master_doc,
  178. "GodotEngine.tex",
  179. "Godot Engine Documentation",
  180. "Juan Linietsky, Ariel Manzur and the Godot community",
  181. "manual",
  182. ),
  183. ]
  184. # -- Options for linkcheck builder ----------------------------------------
  185. # disable checking urls with about.html#this_part_of_page anchors
  186. linkcheck_anchors = False
  187. linkcheck_timeout = 10
  188. # -- I18n settings --------------------------------------------------------
  189. # Godot localization is handled via https://github.com/godotengine/godot-docs-l10n
  190. # where the main docs repo is a submodule. Therefore the translated material is
  191. # actually in the parent folder of this conf.py, hence the "../".
  192. locale_dirs = ["../sphinx/po/"]
  193. gettext_compact = False
  194. # We want to host the localized images in godot-docs-l10n, but Sphinx does not provide
  195. # the necessary feature to do so. `figure_language_filename` has `{root}` and `{path}`,
  196. # but they resolve to (host) absolute paths, so we can't use them as is to access "../".
  197. # However, Python is glorious and lets us redefine Sphinx's internal method that handles
  198. # `figure_language_filename`, so we do our own post-processing to fix the absolute path
  199. # and point to the parallel folder structure in godot-docs-l10n.
  200. # Note: Sphinx's handling of `figure_language_filename` may change in the future, monitor
  201. # https://github.com/sphinx-doc/sphinx/issues/7768 to see what would be relevant for us.
  202. figure_language_filename = "{root}.{language}{ext}"
  203. cwd = os.getcwd()
  204. sphinx_original_get_image_filename_for_language = sphinx.util.i18n.get_image_filename_for_language
  205. def godot_get_image_filename_for_language(filename, env):
  206. """
  207. Hack the absolute path returned by Sphinx based on `figure_language_filename`
  208. to insert our `../images` relative path to godot-docs-l10n's images folder,
  209. which mirrors the folder structure of the docs repository.
  210. The returned string should also be absolute so that `os.path.exists` can properly
  211. resolve it when trying to concatenate with the original doc folder.
  212. """
  213. path = sphinx_original_get_image_filename_for_language(filename, env)
  214. path = os.path.abspath(os.path.join("../images/", os.path.relpath(path, cwd)))
  215. return path
  216. sphinx.util.i18n.get_image_filename_for_language = godot_get_image_filename_for_language
  217. # Similar story for the localized class reference, it's out of tree and there doesn't
  218. # seem to be an easy way for us to tweak the toctree to take this into account.
  219. # So we're deleting the existing class reference and adding a symlink instead...
  220. if is_i18n and os.path.exists("../classes/" + language):
  221. import shutil
  222. if os.path.islink("classes"): # Previously made symlink.
  223. os.unlink("classes")
  224. else:
  225. shutil.rmtree("classes")
  226. os.symlink("../classes/" + language, "classes")
  227. # Couldn't find a way to retrieve variables nor do advanced string
  228. # concat from reST, so had to hardcode this in the "epilog" added to
  229. # all pages. This is used in index.rst to display the Weblate badge.
  230. # On English pages, the badge points to the language-neutral engage page.
  231. rst_epilog = """
  232. .. |weblate_widget| image:: https://hosted.weblate.org/widgets/godot-engine/{image_locale}/godot-docs/287x66-white.png
  233. :alt: Translation status
  234. :target: https://hosted.weblate.org/engage/godot-engine{target_locale}/?utm_source=widget
  235. :width: 287
  236. :height: 66
  237. """.format(
  238. image_locale="-" if language == "en" else language,
  239. target_locale="" if language == "en" else "/" + language,
  240. )