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