diff options
Diffstat (limited to 'doc/manual')
-rwxr-xr-x | doc/manual/anchors.jq | 31 | ||||
-rwxr-xr-x | doc/manual/anchors.py | 56 | ||||
-rw-r--r-- | doc/manual/book.toml | 3 | ||||
-rw-r--r-- | doc/manual/local.mk | 2 |
4 files changed, 34 insertions, 58 deletions
diff --git a/doc/manual/anchors.jq b/doc/manual/anchors.jq new file mode 100755 index 000000000..72309779c --- /dev/null +++ b/doc/manual/anchors.jq @@ -0,0 +1,31 @@ +"\\[\\]\\{#(?<anchor>[^\\}]+?)\\}" as $empty_anchor_regex | +"\\[(?<text>[^\\]]+?)\\]\\{#(?<anchor>[^\\}]+?)\\}" as $anchor_regex | + + +def transform_anchors_html: + . | gsub($empty_anchor_regex; "<a name=\"" + .anchor + "\"></a>") + | gsub($anchor_regex; "<a href=\"#" + .anchor + "\" id=\"" + .anchor + "\">" + .text + "</a>"); + + +def transform_anchors_strip: + . | gsub($empty_anchor_regex; "") + | gsub($anchor_regex; .text); + + +def map_contents_recursively(transformer): + . + { + Chapter: (.Chapter + { + content: .Chapter.content | transformer, + sub_items: .Chapter.sub_items | map(map_contents_recursively(transformer)), + }), + }; + + +def process_command: + .[0] as $context | + .[1] as $body | + $body + { + sections: $body.sections | map(map_contents_recursively(if $context.renderer == "html" then transform_anchors_html else transform_anchors_strip end)), + }; + +process_command diff --git a/doc/manual/anchors.py b/doc/manual/anchors.py deleted file mode 100755 index 2a93b2a67..000000000 --- a/doc/manual/anchors.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env python3 - -import argparse -import json -import re -import sys - - -empty_anchor_regex = re.compile(r"\[\]\{#(?P<anchor>[^\}]+?)\}") -anchor_regex = re.compile(r"\[(?P<text>[^\]]+?)\]\{#(?P<anchor>[^\}]+?)\}") - - -def transform_anchors_html(content): - content = empty_anchor_regex.sub(r'<a name="\g<anchor>"></a>', content) - content = anchor_regex.sub(r'<a href="#\g<anchor>" id="\g<anchor>">\g<text></a>', content) - return content - - -def transform_anchors_strip(content): - content = empty_anchor_regex.sub(r'', content) - content = anchor_regex.sub(r'\g<text>', content) - return content - - -def map_contents_recursively(transformer, chapter): - chapter["Chapter"]["content"] = transformer(chapter["Chapter"]["content"]) - for sub_item in chapter["Chapter"]["sub_items"]: - map_contents_recursively(transformer, sub_item) - - -def supports_command(args): - sys.exit(0) - - -def process_command(args): - context, book = json.load(sys.stdin) - transformer = transform_anchors_html if context["renderer"] == "html" else transform_anchors_strip - for section in book["sections"]: - map_contents_recursively(transformer, section) - print(json.dumps(book)) - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description="mdBook preprocessor adding anchors." - ) - parser.set_defaults(command=process_command) - - subparsers = parser.add_subparsers() - - supports_parser = subparsers.add_parser("supports", help="Check if given renderer is supported") - supports_parser.add_argument("renderer", type=str) - supports_parser.set_defaults(command=supports_command) - - args = parser.parse_args() - args.command(args) diff --git a/doc/manual/book.toml b/doc/manual/book.toml index 75554d11f..ff6b79c07 100644 --- a/doc/manual/book.toml +++ b/doc/manual/book.toml @@ -2,4 +2,5 @@ additional-css = ["custom.css"] [preprocessor.anchors] -command = "python3 doc/manual/anchors.py" +renderers = ["html"] +command = "jq --from-file doc/manual/anchors.jq" diff --git a/doc/manual/local.mk b/doc/manual/local.mk index 910d0a03b..371ed6f21 100644 --- a/doc/manual/local.mk +++ b/doc/manual/local.mk @@ -97,7 +97,7 @@ doc/manual/generated/man1/nix3-manpages: $(d)/src/command-ref/new-cli done @touch $@ -$(docdir)/manual/index.html: $(MANUAL_SRCS) $(d)/book.toml $(d)/anchors.py $(d)/custom.css $(d)/src/SUMMARY.md $(d)/src/command-ref/new-cli $(d)/src/command-ref/conf-file.md $(d)/src/expressions/builtins.md $(call rwildcard, $(d)/src, *.md) +$(docdir)/manual/index.html: $(MANUAL_SRCS) $(d)/book.toml $(d)/anchors.jq $(d)/custom.css $(d)/src/SUMMARY.md $(d)/src/command-ref/new-cli $(d)/src/command-ref/conf-file.md $(d)/src/expressions/builtins.md $(call rwildcard, $(d)/src, *.md) $(trace-gen) RUST_LOG=warn mdbook build doc/manual -d $(DESTDIR)$(docdir)/manual endif |