aboutsummaryrefslogtreecommitdiff
path: root/doc/manual/anchors.jq
diff options
context:
space:
mode:
authorJan Tojnar <jtojnar@gmail.com>2022-05-26 16:47:40 +0200
committerJan Tojnar <jtojnar@gmail.com>2022-05-26 18:17:21 +0200
commit3272afa17b68d25c8070e58819f2e56f075c764d (patch)
treef68d7fd444cf9f1501f8d31f3548d9b60b8974a9 /doc/manual/anchors.jq
parent4de84e095d0f1fa7f3b5db8904496ffd2752d73e (diff)
doc: Port anchors preprocessor to jq script
Python is only pulled into the build closure by Mercurial, which might end up being removed. Let’s port the script to jq, which is more likely to stay.
Diffstat (limited to 'doc/manual/anchors.jq')
-rwxr-xr-xdoc/manual/anchors.jq31
1 files changed, 31 insertions, 0 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