|
6 | 6 | from pathlib import Path
|
7 | 7 | from typing import Any
|
8 | 8 |
|
9 |
| -from docutils.parsers.rst import directives |
| 9 | +from sphinx.builders.dirhtml import DirectoryHTMLBuilder |
| 10 | +from docutils.parsers.rst import directives, Directive |
10 | 11 | from sphinx.application import Sphinx
|
11 | 12 | from sphinx.application import logger as sphinx_log
|
12 | 13 | from sphinx.config import Config
|
13 |
| -from sphinx_design.dropdown import DropdownDirective |
| 14 | + |
| 15 | +has_sphinx_design = False |
| 16 | +try: |
| 17 | + # Try to import sphinx-design to include directives for HTML pages (e.g. tabs and dropdowns). |
| 18 | + # sphinx-design is not required for building man pages. |
| 19 | + # python-sphinx-design is not currently available on EPEL. The package for EPEL includes man pages. |
| 20 | + from sphinx_design.dropdown import DropdownDirective |
| 21 | + has_sphinx_design = True |
| 22 | +except ImportError: |
| 23 | + print ("Unable to import sphinx_design. Documentation cannot be built as HTML.") |
14 | 24 |
|
15 | 25 | # Ensure we can import "mongoc" extension module.
|
16 | 26 | this_path = os.path.dirname(__file__)
|
|
29 | 39 | # package building.
|
30 | 40 | # "sphinxcontrib.moderncmakedomain",
|
31 | 41 | "cmakerefdomain",
|
32 |
| - "sphinx_design", |
33 | 42 | "sphinx.ext.mathjax",
|
34 | 43 | ]
|
35 | 44 |
|
| 45 | +if has_sphinx_design: |
| 46 | + extensions.append("sphinx_design") |
| 47 | + |
36 | 48 | # General information about the project.
|
37 | 49 | project = "libmongoc"
|
38 | 50 | copyright = "2017-present, MongoDB, Inc"
|
@@ -176,22 +188,36 @@ def add_canonical_link(app: Sphinx, pagename: str, templatename: str, context: d
|
176 | 188 | context["metatags"] = context.get("metatags", "") + link
|
177 | 189 |
|
178 | 190 |
|
179 |
| -class AdDropdown(DropdownDirective): |
180 |
| - """A sphinx-design dropdown that can also be an admonition.""" |
181 |
| - |
182 |
| - option_spec = DropdownDirective.option_spec | {"admonition": directives.unchanged_required} |
183 |
| - |
184 |
| - def run(self): |
185 |
| - adm = self.options.get("admonition") |
186 |
| - if adm is not None: |
187 |
| - self.options.setdefault("class-container", []).extend(("admonition", adm)) |
188 |
| - self.options.setdefault("class-title", []).append(f"admonition-title") |
189 |
| - return super().run() |
190 |
| - |
| 191 | +if has_sphinx_design: |
| 192 | + class AdDropdown(DropdownDirective): |
| 193 | + """A sphinx-design dropdown that can also be an admonition.""" |
| 194 | + |
| 195 | + option_spec = DropdownDirective.option_spec | {"admonition": directives.unchanged_required} |
| 196 | + |
| 197 | + def run(self): |
| 198 | + adm = self.options.get("admonition") |
| 199 | + if adm is not None: |
| 200 | + self.options.setdefault("class-container", []).extend(("admonition", adm)) |
| 201 | + self.options.setdefault("class-title", []).append(f"admonition-title") |
| 202 | + return super().run() |
| 203 | +else: |
| 204 | + class EmptyDirective(Directive): |
| 205 | + has_content = True |
| 206 | + def run(self): |
| 207 | + return [] |
| 208 | + |
| 209 | +def check_html_builder_requirements (app): |
| 210 | + if isinstance(app.builder, DirectoryHTMLBuilder) and not has_sphinx_design: |
| 211 | + raise RuntimeError("The sphinx-design package is required to build HTML documentation but was not detected. Install sphinx-design.") |
191 | 212 |
|
192 | 213 | def setup(app: Sphinx):
|
193 | 214 | mongoc_common_setup(app)
|
194 |
| - app.add_directive("ad-dropdown", AdDropdown) |
| 215 | + app.connect("builder-inited", check_html_builder_requirements) |
| 216 | + if has_sphinx_design: |
| 217 | + app.add_directive("ad-dropdown", AdDropdown) |
| 218 | + else: |
| 219 | + app.add_directive("ad-dropdown", EmptyDirective) |
| 220 | + app.add_directive("tab-set", EmptyDirective) |
195 | 221 | app.connect("html-page-context", add_canonical_link)
|
196 | 222 | app.add_css_file("styles.css")
|
197 | 223 | app.connect("config-inited", _maybe_update_inventories)
|
|
0 commit comments