Skip to content

Commit

Permalink
[doc] Created readme2index pre-processor
Browse files Browse the repository at this point in the history
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
HU90m committed Feb 20, 2023
1 parent 910e3f4 commit a8ca357
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ fold = { enable = true, level = 1 }
git-repository-url = "https://github.com/lowrisc/opentitan"
edit-url-template = "https://github.com/lowrisc/opentitan/edit/master/{path}"
curly-quotes = true

[preprocessor.readme2index]
command = "./util/mdbook-readme2index"
50 changes: 50 additions & 0 deletions util/mdbook-readme2index
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()
5 changes: 5 additions & 0 deletions util/mdbook/__init__.py
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
18 changes: 18 additions & 0 deletions util/mdbook/utils.py
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

0 comments on commit a8ca357

Please sign in to comment.