-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinker.sh
More file actions
executable file
·104 lines (96 loc) · 2.5 KB
/
linker.sh
File metadata and controls
executable file
·104 lines (96 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/bash
set -o nounset
set -o errtrace
set -o pipefail
function CATCH_ERROR { # {{{
local __LEC=$? __i __j
set +x
echo "Traceback (most recent call last):" >&2
for ((__i = ${#FUNCNAME[@]} - 1; __i >= 0; --__i)); do
printf ' File %q line %s in %q\n' >&2 \
"${BASH_SOURCE[__i]}" \
"${BASH_LINENO[__i]}" \
"${FUNCNAME[__i]}"
if ((BASH_LINENO[__i])) && [ -f "${BASH_SOURCE[__i]}" ]; then
for ((__j = 0; __j < BASH_LINENO[__i]; ++__j)); do
read -r REPLY
done < "${BASH_SOURCE[__i]}"
printf ' %s\n' "$REPLY" >&2
fi
done
echo "Error: [ExitCode: ${__LEC}]" >&2
exit "${__LEC}"
}
trap CATCH_ERROR ERR # }}}
hash jq mktemp
shopt -s failglob
license=$(mktemp ./tmp.license.XXXXXXXXXX)
trap 'rm "$license"' exit
readonly license
cat << EOF > "$license"
# License
\`\`\`text
$(<./LICENSE)
\`\`\`
EOF
files=(
./README.md
./pages/README.md
./pages/[0-9][0-9]-*.md
./pages/appendix-*.md
"$license"
)
title_index=$(awk '{sub("^# ", "")}{sub(" ", "-")}
BEGIN{printf"{"}END{printf"}"}
{sub("\\./pages/", "prefix/", FILENAME)}
{sub("\\./", "../", FILENAME)}
{sub("^prefix/", "./", FILENAME)}
i++{printf","}
{printf "\"%s\": \"%s\"", FILENAME, $0}
{nextfile}' "${files[@]}" | jq '.+{"./pages/README.md": .["./README.md"]}')
cat "${files[@]}" | jq -Rrn \
--argjson title "$title_index" \
'
{
NOTE: "注",
TIP: "小技巧",
IMPORTANT: "引入",
WARNING: "警告",
CAUTION: "小心",
} as $alerts |
def page_first:
sub("^# "; "") as $cur | $title | any(.==$cur) and input_line_number > 4;
def alert: $alerts[.] // error("unknown alert \(.)");
def patch:
if . == ">" then
"> "
else
(scan("^> \\[!(\\S+)\\]\\s*$")[0] | "> [!\(.)] \(alert):") // .
end
;
reduce inputs as $line ({};
if $line | page_first then
.pages += [.current]
| .current = [$line]
else .current += [$line | gsub("\\[ (?<desc> [^\\[\\]]+ ) \\] \\s* \\( (?<link> \\. [^()]* ) \\)";
"[\(.desc)](#\(.link|if contains("#") then
gsub(".*#"; "")
else
$title[.]//error | ascii_downcase
end))";
"x")] end
)
| if .current | length != 0 then .pages += [.current] end
| .pages
| map({lines: ., max_ref: (
map(scan("\\[ \\^ (\\d+) \\^? \\]"; "x")[0] | tonumber)|max//0
)})
| foreach .[] as $page (0;
. as $base |
($page.lines[] | gsub("\\[ \\^ (?<n>\\d+) \\^? \\]"; "[^\(.n | tonumber+$base)]"; "x")),
"", "---", "",
$base + $page.max_ref
)
| strings
| patch
'