|
| 1 | +import docutils |
| 2 | +from packaging.version import parse as version_parse |
| 3 | +from sphinx.util.docutils import SphinxDirective |
| 4 | + |
| 5 | + |
| 6 | +DOC_ROOT = "https://lab-cosmo.github.io/torch-pme" |
| 7 | + |
| 8 | + |
| 9 | +class VersionList(SphinxDirective): |
| 10 | + """ |
| 11 | + Directive for a list of previous versions of the documentation. |
| 12 | +
|
| 13 | + This is rendered by manually generating ReST using sphinx-design directives. |
| 14 | + """ |
| 15 | + |
| 16 | + has_content = True |
| 17 | + required_arguments = 0 |
| 18 | + optional_arguments = 0 |
| 19 | + option_spec = { |
| 20 | + "tag-prefix": str, |
| 21 | + "url-suffix": str, |
| 22 | + } |
| 23 | + |
| 24 | + def run(self): |
| 25 | + # group together versions with the same major & minor components |
| 26 | + grouped_versions = {} |
| 27 | + content = self.parse_content_to_nodes() |
| 28 | + for node in content: |
| 29 | + if not isinstance(node, VersionNode): |
| 30 | + raise ValueError( |
| 31 | + "only `.. version::` is allowed inside `.. version-list::`" |
| 32 | + ) |
| 33 | + |
| 34 | + version = version_parse(node.version) |
| 35 | + group = (version.major, version.minor) |
| 36 | + if group not in grouped_versions: |
| 37 | + grouped_versions[group] = [] |
| 38 | + |
| 39 | + grouped_versions[group].append(node) |
| 40 | + |
| 41 | + # generate ReST with the desired markup |
| 42 | + generated_content = """ |
| 43 | +.. grid:: |
| 44 | + :margin: 0 0 0 0\n""" |
| 45 | + |
| 46 | + for group_i, (version_short, group) in enumerate(grouped_versions.items()): |
| 47 | + |
| 48 | + if group_i < 3: |
| 49 | + generated_content += f""" |
| 50 | + .. grid-item:: |
| 51 | + :columns: 12 6 3 3 |
| 52 | + :class: sd-p-1 |
| 53 | +
|
| 54 | + .. dropdown:: Version {version_short[0]}.{version_short[1]} |
| 55 | + :class-body: sd-mb-0 sd-pb-0 |
| 56 | + :class-title: font-size-small\n""" |
| 57 | + elif group_i == 3: |
| 58 | + generated_content += """ |
| 59 | + .. grid-item:: |
| 60 | + :columns: 12 6 3 3 |
| 61 | + :class: sd-p-1 |
| 62 | +
|
| 63 | + .. dropdown:: Older versions |
| 64 | + :class-body: sd-mb-0 sd-pb-0 |
| 65 | + :class-title: font-size-small\n""" |
| 66 | + |
| 67 | + for node in group: |
| 68 | + version = node.version |
| 69 | + tag_prefix = self.options["tag-prefix"] |
| 70 | + |
| 71 | + url_suffix = ( |
| 72 | + node.url_suffix |
| 73 | + if node.url_suffix is not None |
| 74 | + else self.options["url-suffix"] |
| 75 | + ) |
| 76 | + |
| 77 | + generated_content += f""" |
| 78 | + .. card:: {version} |
| 79 | + :class-body: sd-p-2 |
| 80 | + :class-title: sd-mb-0 |
| 81 | + :text-align: center |
| 82 | + :link: {DOC_ROOT}/{tag_prefix}{version}/{url_suffix} |
| 83 | + :link-type: url\n""" |
| 84 | + |
| 85 | + # parse the generated ReST |
| 86 | + return self.parse_text_to_nodes(generated_content) |
| 87 | + |
| 88 | + |
| 89 | +class VersionNode(docutils.nodes.Node): |
| 90 | + """ |
| 91 | + Node for a single version directive. This is only used to transmit information |
| 92 | + between the ``.. version::`` directive and the version list, and never rendered on |
| 93 | + its own. |
| 94 | + """ |
| 95 | + |
| 96 | + def __init__(self, version, url_suffix): |
| 97 | + self.version = version |
| 98 | + self.url_suffix = url_suffix |
| 99 | + |
| 100 | + |
| 101 | +class VersionItem(SphinxDirective): |
| 102 | + """ |
| 103 | + A single item in a version list. This can override the url suffix if a different url |
| 104 | + was used for this version. |
| 105 | + """ |
| 106 | + |
| 107 | + has_content = False |
| 108 | + required_arguments = 1 |
| 109 | + optional_arguments = 0 |
| 110 | + final_argument_whitespace = False |
| 111 | + option_spec = { |
| 112 | + "url-suffix": str, |
| 113 | + } |
| 114 | + |
| 115 | + def run(self): |
| 116 | + return [ |
| 117 | + VersionNode( |
| 118 | + version=self.arguments[0], |
| 119 | + url_suffix=self.options.get("url-suffix"), |
| 120 | + ) |
| 121 | + ] |
| 122 | + |
| 123 | + |
| 124 | +def setup(app): |
| 125 | + app.add_directive("version-list", VersionList) |
| 126 | + app.add_directive("version", VersionItem) |
0 commit comments