sphinx.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. import json
  4. import collections
  5. import logging
  6. import os
  7. import posixpath
  8. from sphinx import config as sphinx_config
  9. from sphinx.util import i18n as sphinx_i18n
  10. from sphinx.locale import _
  11. logger = logging.getLogger(__name__)
  12. DATE_FMT = "%Y-%m-%d %H:%M:%S %z"
  13. DEFAULT_TAG_WHITELIST = r"^.*$"
  14. DEFAULT_BRANCH_WHITELIST = r"^.*$"
  15. DEFAULT_REMOTE_WHITELIST = None
  16. DEFAULT_RELEASED_PATTERN = r"^tags/.*$"
  17. DEFAULT_OUTPUTDIR_FORMAT = r"{ref.name}"
  18. Version = collections.namedtuple(
  19. "Version", ["name", "url", "version", "release", "is_released",]
  20. )
  21. class VersionInfo:
  22. def __init__(self, app, context, metadata, current_version_name):
  23. self.app = app
  24. self.context = context
  25. self.metadata = metadata
  26. self.current_version_name = current_version_name
  27. def _dict_to_versionobj(self, v):
  28. return Version(
  29. name=v["name"],
  30. url=self.vpathto(v["name"]),
  31. version=v["version"],
  32. release=v["release"],
  33. is_released=v["is_released"],
  34. )
  35. @property
  36. def tags(self):
  37. return [
  38. self._dict_to_versionobj(v)
  39. for v in self.metadata.values()
  40. if v["source"] == "tags"
  41. ]
  42. @property
  43. def branches(self):
  44. return [
  45. self._dict_to_versionobj(v)
  46. for v in self.metadata.values()
  47. if v["source"] != "tags"
  48. ]
  49. @property
  50. def releases(self):
  51. return [
  52. self._dict_to_versionobj(v)
  53. for v in self.metadata.values()
  54. if v["is_released"]
  55. ]
  56. @property
  57. def in_development(self):
  58. return [
  59. self._dict_to_versionobj(v)
  60. for v in self.metadata.values()
  61. if not v["is_released"]
  62. ]
  63. def __iter__(self):
  64. for item in self.tags:
  65. yield item
  66. for item in self.branches:
  67. yield item
  68. def __getitem__(self, name):
  69. v = self.metadata.get(name)
  70. if v:
  71. return self._dict_to_versionobj(v)
  72. def vhasdoc(self, other_version_name):
  73. if self.current_version_name == other_version_name:
  74. return True
  75. other_version = self.metadata[other_version_name]
  76. return self.context["pagename"] in other_version["docnames"]
  77. def vpathto(self, other_version_name):
  78. if self.current_version_name == other_version_name:
  79. return "{}.html".format(
  80. posixpath.split(self.context["pagename"])[-1]
  81. )
  82. # Find relative outputdir paths from common output root
  83. current_version = self.metadata[self.current_version_name]
  84. other_version = self.metadata[other_version_name]
  85. current_outputroot = os.path.abspath(current_version["outputdir"])
  86. other_outputroot = os.path.abspath(other_version["outputdir"])
  87. outputroot = os.path.commonpath((current_outputroot, other_outputroot))
  88. current_outputroot = os.path.relpath(
  89. current_outputroot, start=outputroot
  90. )
  91. other_outputroot = os.path.relpath(other_outputroot, start=outputroot)
  92. # Ensure that we use POSIX separators in the path (for the HTML code)
  93. if os.sep != posixpath.sep:
  94. current_outputroot = posixpath.join(
  95. *os.path.split(current_outputroot)
  96. )
  97. other_outputroot = posixpath.join(*os.path.split(other_outputroot))
  98. # Find relative path to root of other_version's outputdir
  99. current_outputdir = posixpath.dirname(
  100. posixpath.join(current_outputroot, self.context["pagename"])
  101. )
  102. other_outputdir = posixpath.relpath(
  103. other_outputroot, start=current_outputdir
  104. )
  105. if not self.vhasdoc(other_version_name):
  106. return posixpath.join(other_outputdir, "index.html")
  107. return posixpath.join(
  108. other_outputdir, "{}.html".format(self.context["pagename"])
  109. )
  110. def html_page_context(app, pagename, templatename, context, doctree):
  111. versioninfo = VersionInfo(
  112. app, context, app.config.smv_metadata, app.config.smv_current_version
  113. )
  114. context["versions"] = versioninfo
  115. context["vhasdoc"] = versioninfo.vhasdoc
  116. context["vpathto"] = versioninfo.vpathto
  117. context["current_version"] = versioninfo[app.config.smv_current_version]
  118. context["latest_version"] = versioninfo[app.config.smv_latest_version]
  119. context["html_theme"] = app.config.html_theme
  120. def config_inited(app, config):
  121. """Update the Sphinx builder.
  122. :param sphinx.application.Sphinx app: Sphinx application object.
  123. """
  124. if not config.smv_metadata:
  125. if not config.smv_metadata_path:
  126. return
  127. with open(config.smv_metadata_path, mode="r") as f:
  128. metadata = json.load(f)
  129. config.smv_metadata = metadata
  130. if not config.smv_current_version:
  131. return
  132. try:
  133. data = app.config.smv_metadata[config.smv_current_version]
  134. except KeyError:
  135. return
  136. app.connect("html-page-context", html_page_context)
  137. # Restore config values
  138. old_config = sphinx_config.Config.read(data["confdir"])
  139. old_config.pre_init_values()
  140. old_config.init_values()
  141. config.version = data["version"]
  142. config.release = data["release"]
  143. config.today = old_config.today
  144. if not config.today:
  145. config.today = sphinx_i18n.format_date(
  146. format=config.today_fmt or _("%b %d, %Y"),
  147. date=datetime.datetime.strptime(data["creatordate"], DATE_FMT),
  148. language=config.language,
  149. )
  150. def setup(app):
  151. app.add_config_value("smv_metadata", {}, "html")
  152. app.add_config_value("smv_metadata_path", "", "html")
  153. app.add_config_value("smv_current_version", "", "html")
  154. app.add_config_value("smv_latest_version", "master", "html")
  155. app.add_config_value("smv_tag_whitelist", DEFAULT_TAG_WHITELIST, "html")
  156. app.add_config_value(
  157. "smv_branch_whitelist", DEFAULT_BRANCH_WHITELIST, "html"
  158. )
  159. app.add_config_value(
  160. "smv_remote_whitelist", DEFAULT_REMOTE_WHITELIST, "html"
  161. )
  162. app.add_config_value(
  163. "smv_released_pattern", DEFAULT_RELEASED_PATTERN, "html"
  164. )
  165. app.add_config_value(
  166. "smv_outputdir_format", DEFAULT_OUTPUTDIR_FORMAT, "html"
  167. )
  168. app.connect("config-inited", config_inited)
  169. return {
  170. "version": "0.2",
  171. "parallel_read_safe": True,
  172. "parallel_write_safe": True,
  173. }