|
| 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