|
| 1 | +"""A directive to generate the list of all the built-in components. |
| 2 | +
|
| 3 | +Read the content of the component folder and generate a list of all the components. |
| 4 | +This list will display some informations about the component and a link to the |
| 5 | +GitHub file. |
| 6 | +""" |
| 7 | +import re |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any, Dict, List |
| 10 | + |
| 11 | +from docutils import nodes |
| 12 | +from sphinx.application import Sphinx |
| 13 | +from sphinx.util import logging |
| 14 | +from sphinx.util.docutils import SphinxDirective |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class ComponentListDirective(SphinxDirective): |
| 20 | + """A directive to generate the list of all the built-in components. |
| 21 | +
|
| 22 | + Read the content of the component folder and generate a list of all the components. |
| 23 | + This list will display some informations about the component and a link to the |
| 24 | + GitHub file. |
| 25 | + """ |
| 26 | + |
| 27 | + name = "component-list" |
| 28 | + has_content = True |
| 29 | + required_arguments = 0 |
| 30 | + optional_arguments = 0 |
| 31 | + final_argument_whitespace = True |
| 32 | + |
| 33 | + def run(self) -> List[nodes.Node]: |
| 34 | + """Create the list.""" |
| 35 | + # get the list of all th jinja templates |
| 36 | + # not that to remain compatible with sphinx they are labeled as html files |
| 37 | + root = Path(__file__).parents[2] |
| 38 | + component_dir = ( |
| 39 | + root |
| 40 | + / "src" |
| 41 | + / "pydata_sphinx_theme" |
| 42 | + / "theme" |
| 43 | + / "pydata_sphinx_theme" |
| 44 | + / "components" |
| 45 | + ) |
| 46 | + if not component_dir.is_dir(): |
| 47 | + raise FileNotFoundError( |
| 48 | + f"Could not find component folder at {component_dir}." |
| 49 | + ) |
| 50 | + components = sorted(component_dir.glob("*.html")) |
| 51 | + |
| 52 | + # create the list of all the components description using bs4 |
| 53 | + # at the moment we use dummy information |
| 54 | + docs = [] |
| 55 | + pattern = re.compile(r"(?<={#).*?(?=#})", flags=re.DOTALL) |
| 56 | + for c in components: |
| 57 | + comment = pattern.findall(c.read_text()) |
| 58 | + docs.append(comment[0].strip() if comment else "No description available.") |
| 59 | + |
| 60 | + # get the urls from the github repo latest branch |
| 61 | + github_url = "https://github.com/pydata/pydata-sphinx-theme/blob/main" |
| 62 | + urls = [ |
| 63 | + f"{github_url}/{component.relative_to(root)}" for component in components |
| 64 | + ] |
| 65 | + |
| 66 | + # build the list of all the components |
| 67 | + items = [] |
| 68 | + for component, url, doc in zip(components, urls, docs): |
| 69 | + items.append( |
| 70 | + nodes.list_item( |
| 71 | + "", |
| 72 | + nodes.paragraph( |
| 73 | + "", |
| 74 | + "", |
| 75 | + nodes.reference("", component.stem, internal=False, refuri=url), |
| 76 | + nodes.Text(f": {doc}"), |
| 77 | + ), |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + return [nodes.bullet_list("", *items)] |
| 82 | + |
| 83 | + |
| 84 | +def setup(app: Sphinx) -> Dict[str, Any]: |
| 85 | + """Add custom configuration to sphinx app. |
| 86 | +
|
| 87 | + Args: |
| 88 | + app: the Sphinx application |
| 89 | +
|
| 90 | + Returns: |
| 91 | + the 2 parallel parameters set to ``True``. |
| 92 | + """ |
| 93 | + app.add_directive("component-list", ComponentListDirective) |
| 94 | + |
| 95 | + return { |
| 96 | + "parallel_read_safe": True, |
| 97 | + "parallel_write_safe": True, |
| 98 | + } |
0 commit comments