Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👷 Upgrade build docs configs #180

Merged
merged 2 commits into from
Aug 9, 2024
Merged

👷 Upgrade build docs configs #180

merged 2 commits into from
Aug 9, 2024

Conversation

tiangolo
Copy link
Member

@tiangolo tiangolo commented Aug 9, 2024

👷 Upgrade build docs configs

Update docs

Tabs

To update the tabs I ran this script update_tabs.py in an interactive window:

import os
import re
from dataclasses import dataclass, field
from pathlib import Path

base_dir = Path(__file__).parent

@dataclass
class Tab:
    title: str
    content: list[str] = field(default_factory=list)

    def __str__(self) -> str:
        content = "\n".join(self.content).strip()
        return f"//// tab | {self.title}\n\n{content}\n\n////\n"


def generate_new_content(content: str) -> str:
    new_content_blocks = []
    open_tab: Tab | None = None
    lines = content.splitlines()
    for line in lines:
        if open_tab:
            if line.startswith("    "):
                open_tab.content.append(line[4:])
                continue
            elif line == "":
                open_tab.content.append(line)
                continue
            else:
                new_content_blocks.append(str(open_tab))
                open_tab = None
        if line.startswith("=== "):
            match = re.match(r'=== "(.*)"', line)
            assert match
            title = match.group(1)
            open_tab = Tab(title=title)
            continue
        new_content_blocks.append(line)
    if open_tab:
        new_content_blocks.append(str(open_tab))
    new_content = "\n".join(new_content_blocks)
    return new_content.strip() + "\n"


def update_md_files_tabs() -> None:
    os.chdir(base_dir)
    md_files = list(Path("docs").glob("**/*.md"))
    for md_file in md_files:
        content = md_file.read_text()
        new_content = generate_new_content(content)
        md_file.write_text(new_content)


if __name__ == "__main__":
    update_md_files_tabs()

Admonitions

To update the admonitions I ran this script update_admonitions.py in an interactive window:

import os
import re
from dataclasses import dataclass, field
from pathlib import Path

base_dir = Path(__file__).parent

@dataclass
class Admonition:
    type: str
    title: str = ""
    content: list[str] = field(default_factory=list)

    def __str__(self) -> str:
        content = "\n".join(self.content).strip()
        if self.title:
            return f"/// {self.type} | {self.title}\n\n{content}\n\n///\n"
        return f"/// {self.type}\n\n{content}\n\n///\n"


def generate_new_content(content: str) -> str:
    new_content_blocks = []
    open_admonition: Admonition | None = None
    lines = content.splitlines()
    for line in lines:
        if open_admonition:
            if line.startswith("    "):
                open_admonition.content.append(line[4:])
                continue
            elif line == "":
                open_admonition.content.append(line)
                continue
            else:
                new_content_blocks.append(str(open_admonition))
                open_admonition = None
        if line.startswith("!!! "):
            no_title_match = re.match(r'!!! (\S*)$', line)
            title_match = re.match(r'!!! (\S*) (.*)$', line)
            title_match_quotes = re.match(r'!!! (\S*) "(.*)"$', line)
            if no_title_match:
                type = no_title_match.group(1).lower()
                open_admonition = Admonition(type=type)
                continue
            elif title_match:
                type = title_match.group(1).lower()
                title = title_match.group(2)
                open_admonition = Admonition(type=type, title=title)
                continue
            elif title_match_quotes:
                type = title_match_quotes.group(1).lower()
                title = title_match_quotes.group(2)
                open_admonition = Admonition(type=type, title=title)
                continue
            raise RuntimeError("Should not reach here")
        new_content_blocks.append(line)
    if open_admonition:
        new_content_blocks.append(str(open_admonition))
    new_content = "\n".join(new_content_blocks)
    return new_content.strip() + "\n"


def update_md_files_tabs() -> None:
    os.chdir(base_dir)
    md_files = list(Path("docs").glob("**/*.md"))
    for md_file in md_files:
        content = md_file.read_text()
        new_content = generate_new_content(content)
        md_file.write_text(new_content)


if __name__ == "__main__":
    update_md_files_tabs()

Copy link

github-actions bot commented Aug 9, 2024

@tiangolo tiangolo marked this pull request as ready for review August 9, 2024 01:29
@tiangolo tiangolo merged commit b5f4394 into main Aug 9, 2024
21 checks passed
@tiangolo tiangolo deleted the upgrade-mkdocs branch August 9, 2024 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant