forked from lowRISC/opentitan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[doc] Created readme2index pre-processor
We use the built-in `index` preprocessor to rename our `README.md` files to `index.md`, but it doesn't currently fixup links: <rust-lang/mdBook#984>. This is a temporary preprocessor to regex replace those links until upstream gets fixed. Signed-off-by: Hugo McNally <[email protected]>
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright lowRISC contributors. | ||
# Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
"""mdbook preprocessor that converts README.md to index.md | ||
We use the built-in `index` preprocessor to rename our `README.md` files to `index.md`, | ||
but it doesn't currently fixup links: https://github.com/rust-lang/mdBook/issues/984. | ||
This is a temporary preprocessor to regex replace those links until upstream gets fixed. | ||
""" | ||
|
||
import json | ||
import sys | ||
import re | ||
|
||
import mdbook | ||
|
||
# Finds all markdown links, `[...](...)`, | ||
# which link to a file called readme.md | ||
# To check it isn't a link no colon, `:`, is allowed before the readme.md . | ||
# `#` and '?' also aren't allowed before the readme.md, | ||
# in case `readme.md` is not the filename but in fact a fragment or selector. | ||
# It matches the link content before and after the readme into groups | ||
# so that it can be substituted back into the file. | ||
RM2IDX_PATTERN = re.compile( | ||
r"(\[[^\]]*\]\([^\)|#|:|\?]*)readme(\.md[^\)]*\))", | ||
re.IGNORECASE, | ||
) | ||
|
||
|
||
def main() -> None: | ||
if len(sys.argv) > 2: | ||
if (sys.argv[1], sys.argv[2]) == ("supports", "html"): | ||
sys.exit(0) | ||
else: | ||
sys.exit(1) | ||
|
||
# load both the context and the book from stdin | ||
context, book = json.load(sys.stdin) | ||
|
||
chapters_gen = mdbook.chapters(book["sections"]) | ||
for chapter in chapters_gen: | ||
chapter["content"] = RM2IDX_PATTERN.sub(r"\1index\2", chapter["content"]) | ||
|
||
# dump the book into stdout | ||
print(json.dumps(book)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright lowRISC contributors. | ||
# Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
"""Common module for mdbook pre-processors.""" | ||
from .utils import chapters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright lowRISC contributors. | ||
# Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
"""Common utilities used by mdbook pre-processors.""" | ||
|
||
from typing import List, Any, Dict, Generator | ||
|
||
|
||
def chapters(items: List[Dict[str, Any]]) -> Generator[Dict[str, Any], None, None]: | ||
"""Recursively yields all chapters""" | ||
for chapter in (item.get("Chapter") for item in items): | ||
if not chapter: | ||
continue | ||
|
||
for sub_chapter in chapters(chapter["sub_items"]): | ||
yield sub_chapter | ||
|
||
yield chapter |