-
Notifications
You must be signed in to change notification settings - Fork 2
/
08_rename_anchors.sh
executable file
·48 lines (40 loc) · 1.73 KB
/
08_rename_anchors.sh
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
#!/usr/bin/env bash
shopt -s globstar
function replace_anchor () {
match=$1
delim=$2
# 1. Convert anchor to lowercase
# 2. Filter out MediaWiki's escape sequences like ".22"
# 3. Filter out ".", ":", '&', '<', and '>'
MATCHSUB=`echo $match | tr '[:upper:]' '[:lower:]' | sed 's/\.[23][0-9a-fA-F]//g' | sed 's/[\.:&<>]//g'`
# If the match contains both "-" and "_", convert all underscores to "-" as a heuristic
if [[ $match =~ "-" && $match =~ "_" ]]
then
MATCHSUB=`echo $MATCHSUB | sed 's/_/-'/g`
fi
# Hardcode some special cases
if [[ "$MATCHSUB" == "pdn-lgr-cpu-cnt0-3" ]]
then
MATCHSUB="pdn_lgr_cpu_cnt0-3"
fi
if [[ "$MATCHSUB" == "file-12-char-id" ]]
then
MATCHSUB="file-_12-char-id"
fi
echo -e " Replacing " $match "\t=>\t" $MATCHSUB
# Replace all mentions of this particular anchor
sed -i "s/#$match$delim/#$MATCHSUB$delim/g" "$i"
}
for i in content/**/*.md
do
echo "Processing $i"
# Find all markdown anchor links, select only the actual anchor text, filter out duplicates, then run the body with the anchor id stored in $match
# NOTE: Some links are broken across lines, so instead of "[" allow the start of line (^) as the left delimiter, too
cat $i | grep -oP '[\]^](.*?)\((.*?)#.*?(?="wikilink"\))' | grep -oP '(?<=\#).*' | sort | uniq | while read -r match; do
replace_anchor $match " "
done
# Find all HTML anchor links (excluding external ones, starting with http), select only the actual anchor text, filter out duplicates, then run the body with the anchor id stored in $match
cat $i | grep -oP 'href="(?!http)(.*?)#.*?(?=")' | grep -oP '(?<=\#).*' | sort | uniq | while read -r match; do
replace_anchor $match "\""
done
done