Skip to content

Commit 8c02d9d

Browse files
HU90mGregAC
authored andcommitted
[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]>
1 parent 934f088 commit 8c02d9d

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

book.toml

+3
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ fold = { enable = true}
1515
git-repository-url = "https://github.com/lowrisc/opentitan"
1616
edit-url-template = "https://github.com/lowrisc/opentitan/edit/master/{path}"
1717
curly-quotes = true
18+
19+
[preprocessor.readme2index]
20+
command = "./util/mdbook_readme2index.py"

util/mdbook/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright lowRISC contributors.
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
"""Common module for mdbook pre-processors."""

util/mdbook/utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright lowRISC contributors.
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
"""Common utilities used by mdbook pre-processors."""
5+
6+
from typing import List, Any, Dict, Generator
7+
8+
9+
def chapters(items: List[Dict[str, Any]]) -> Generator[Dict[str, Any], None, None]:
10+
"""Recursively yields all chapters"""
11+
for chapter in (item.get("Chapter") for item in items):
12+
if not chapter:
13+
continue
14+
15+
for sub_chapter in chapters(chapter["sub_items"]):
16+
yield sub_chapter
17+
18+
yield chapter

util/mdbook_readme2index.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
# Copyright lowRISC contributors.
3+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
4+
# SPDX-License-Identifier: Apache-2.0
5+
"""mdbook preprocessor that converts README.md to index.md
6+
7+
We use the built-in `index` preprocessor to rename our `README.md` files to `index.md`,
8+
but it doesn't currently fixup links: https://github.com/rust-lang/mdBook/issues/984.
9+
This is a temporary preprocessor to regex replace those links until upstream gets fixed.
10+
"""
11+
12+
import json
13+
import sys
14+
import re
15+
16+
import mdbook.utils as md_utils
17+
18+
# Finds all markdown links, `[...](...)`,
19+
# which link to a file called readme.md
20+
# To check it isn't a link no colon, `:`, is allowed before the readme.md .
21+
# `#` and '?' also aren't allowed before the readme.md,
22+
# in case `readme.md` is not the filename but in fact a fragment or selector.
23+
# It matches the link content before and after the readme into groups
24+
# so that it can be substituted back into the file.
25+
RM2IDX_PATTERN_INLINE = re.compile(
26+
r"(\[[^\]]*\]\([^\)|#|:|\?]*)readme(\.md[^\)]*\))",
27+
re.IGNORECASE,
28+
)
29+
30+
# Similar to the pattern above but for `[...]: ...` style links.
31+
RM2IDX_PATTERN_NOT_INLINE = re.compile(
32+
r"^(\[[^\]]*\]:\s+[^\n|#|:|\?]*)readme(\.md[^\n]*$)",
33+
re.IGNORECASE | re.MULTILINE,
34+
)
35+
36+
37+
def main() -> None:
38+
if len(sys.argv) > 2:
39+
if (sys.argv[1], sys.argv[2]) == ("supports", "html"):
40+
sys.exit(0)
41+
else:
42+
sys.exit(1)
43+
44+
# load both the context and the book from stdin
45+
context, book = json.load(sys.stdin)
46+
47+
for chapter in md_utils.chapters(book["sections"]):
48+
chapter["content"] = RM2IDX_PATTERN_INLINE.sub(r"\1index\2", chapter["content"])
49+
chapter["content"] = RM2IDX_PATTERN_NOT_INLINE.sub(r"\1index\2", chapter["content"])
50+
51+
# dump the book into stdout
52+
print(json.dumps(book))
53+
54+
55+
if __name__ == "__main__":
56+
main()

0 commit comments

Comments
 (0)