Repository: carbon-language/carbon-lang
File: website/prebuild.py (Python)

Code before:
        # has_toc is disabling the list of child files in a separate table of
        # contents.
        content += "has_children: true\nhas_toc: false\n"
    content += "---\n\n" + orig_content
    f.write_text(content)


def label_subdir(
    subdir_str: str,
    top_nav_order: int,
    parent_title: Optional[str] = None,
    grandchild_dirs: bool = False,
) -> None:
    """Automatically adds child information to a subdirectory's markdown files.

    This is in support of navigation.
    """
    assert not (parent_title and grandchild_dirs)

    subdir = Path(subdir_str)
    readme = subdir / "README.md"
    readme_content = readme.read_text()

    def include_child(md_file: Path) -> bool:
        """Determines whether a markdown file should be included in the
        navigation tree as a child of this label.
        """
        # The top-level README.md isn't a child.
        if md_file == readme:
            return False
        # Skip directories that aren't part of the repository.
        if "/node_modules/" in str(md_file):
            return False
        if "/dist/" in str(md_file):
            return False
        return True

    readme_title = get_title(readme, readme_content)
    readme_titles = [readme_title]
    if parent_title:
        readme_titles.insert(0, parent_title)
    children = [x for x in subdir.glob("**/*.md") if include_child(x)]
    add_frontmatter(
        readme, readme_content, readme_titles, top_nav_order, bool(children)
    )

    if grandchild_dirs:
        # When adding grandchildren, we cluster files by child directory.
        child_dirs = {}
        for child in children:
            if child.name == "README.md":

Review comment (suggestion):
```suggestion
        """Returns true for direct children in the navigation tree.
```

(suggesting as the PEP8 single-line-first-line convention)

Code after:
        # has_toc is disabling the list of child files in a separate table of
        # contents.
        content += "has_children: true\nhas_toc: false\n"
    content += "---\n\n" + orig_content
    f.write_text(content)


def label_subdir(
    subdir_str: str,
    top_nav_order: int,
    parent_title: Optional[str] = None,
    grandchild_dirs: bool = False,
) -> None:
    """Automatically adds child information to a subdirectory's markdown files.

    This is in support of navigation.
    """
    assert not (parent_title and grandchild_dirs)

    subdir = Path(subdir_str)
    readme = subdir / "README.md"
    readme_content = readme.read_text()

    def include_child(md_file: Path) -> bool:
        """Returns whether md_file is a child of this label."""
        # The top-level README.md isn't a child.
        if md_file == readme:
            return False
        # Skip directories that aren't part of the repository.
        if "/node_modules/" in str(md_file):
            return False
        if "/dist/" in str(md_file):
            return False
        return True

    readme_title = get_title(readme, readme_content)
    readme_titles = [readme_title]
    if parent_title:
        readme_titles.insert(0, parent_title)
    children = [x for x in subdir.glob("**/*.md") if include_child(x)]
    add_frontmatter(
        readme, readme_content, readme_titles, top_nav_order, bool(children)
    )

    if grandchild_dirs:
        # When adding grandchildren, we cluster files by child directory.
        child_dirs = {}
        for child in children:
            if child.name == "README.md":
                title = get_title(child, child.read_text())
                child_dirs[child.parent] = ChildDir(title)