|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import ast |
| 4 | +import re |
| 5 | +from functools import partial |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from griffe import Extension, Inspector, ObjectNode, Visitor, get_logger |
| 10 | +from griffe import Object as GriffeObject |
| 11 | +from pymdownx.slugs import slugify |
| 12 | + |
| 13 | +DOCS_PATH = Path(__file__).parent.parent |
| 14 | +slugifier = slugify(case="lower") |
| 15 | +logger = get_logger("griffe_docklinks") |
| 16 | + |
| 17 | + |
| 18 | +def find_heading(content: str, slug: str, file_path: Path) -> tuple[str, int]: |
| 19 | + for m in re.finditer("^#+ (.+)", content, flags=re.M): |
| 20 | + heading = m.group(1) |
| 21 | + h_slug = slugifier(heading, "-") |
| 22 | + if h_slug == slug: |
| 23 | + return heading, m.end() |
| 24 | + raise ValueError(f"heading with slug {slug!r} not found in {file_path}") |
| 25 | + |
| 26 | + |
| 27 | +def insert_at_top(path: str, api_link: str) -> str: |
| 28 | + rel_file = path.rstrip("/") + ".md" |
| 29 | + file_path = DOCS_PATH / rel_file |
| 30 | + content = file_path.read_text() |
| 31 | + second_heading = re.search("^#+ ", content, flags=re.M) |
| 32 | + assert second_heading, "unable to find second heading in file" |
| 33 | + first_section = content[: second_heading.start()] |
| 34 | + |
| 35 | + if f"[{api_link}]" not in first_section: |
| 36 | + logger.debug( |
| 37 | + 'inserting API link "%s" at the top of %s', |
| 38 | + api_link, |
| 39 | + file_path.relative_to(DOCS_PATH), |
| 40 | + ) |
| 41 | + file_path.write_text( |
| 42 | + '??? api "API Documentation"\n' |
| 43 | + f" [`{api_link}`][{api_link}]<br>\n\n" |
| 44 | + f"{content}" |
| 45 | + ) |
| 46 | + |
| 47 | + heading = file_path.stem.replace("_", " ").title() |
| 48 | + return f'!!! abstract "Usage Documentation"\n [{heading}](../{rel_file})\n' |
| 49 | + |
| 50 | + |
| 51 | +def replace_links(m: re.Match[str], *, api_link: str) -> str: |
| 52 | + path_group = m.group(1) |
| 53 | + if "#" not in path_group: |
| 54 | + # no heading id, put the content at the top of the page |
| 55 | + return insert_at_top(path_group, api_link) |
| 56 | + |
| 57 | + usage_path, slug = path_group.split("#", 1) |
| 58 | + rel_file = usage_path.rstrip("/") + ".md" |
| 59 | + file_path = DOCS_PATH / rel_file |
| 60 | + content = file_path.read_text() |
| 61 | + heading, heading_end = find_heading(content, slug, file_path) |
| 62 | + |
| 63 | + next_heading = re.search("^#+ ", content[heading_end:], flags=re.M) |
| 64 | + if next_heading: |
| 65 | + next_section = content[heading_end : heading_end + next_heading.start()] |
| 66 | + else: |
| 67 | + next_section = content[heading_end:] |
| 68 | + |
| 69 | + if f"[{api_link}]" not in next_section: |
| 70 | + logger.debug( |
| 71 | + 'inserting API link "%s" into %s', |
| 72 | + api_link, |
| 73 | + file_path.relative_to(DOCS_PATH), |
| 74 | + ) |
| 75 | + file_path.write_text( |
| 76 | + f"{content[:heading_end]}\n\n" |
| 77 | + '??? api "API Documentation"\n' |
| 78 | + f" [`{api_link}`][{api_link}]<br>" |
| 79 | + f"{content[heading_end:]}" |
| 80 | + ) |
| 81 | + |
| 82 | + return ( |
| 83 | + f'!!! abstract "Usage Documentation"\n [{heading}](../{rel_file}#{slug})\n' |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +def update_docstring(obj: GriffeObject) -> str: |
| 88 | + return re.sub( |
| 89 | + r"usage[\- ]docs: ?https://docs\.pydantic\.dev/.+?/(\S+)", |
| 90 | + partial(replace_links, api_link=obj.path), |
| 91 | + obj.docstring.value, |
| 92 | + flags=re.I, |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +class UpdateDocstringsExtension(Extension): |
| 97 | + def on_instance( |
| 98 | + self, |
| 99 | + *, |
| 100 | + node: ast.AST | ObjectNode, |
| 101 | + obj: GriffeObject, |
| 102 | + agent: Visitor | Inspector, |
| 103 | + **kwargs: Any, |
| 104 | + ) -> None: |
| 105 | + if not obj.is_alias and obj.docstring is not None: |
| 106 | + obj.docstring.value = update_docstring(obj) |
0 commit comments