-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10_fix_href_colon.sh
executable file
·37 lines (31 loc) · 1.06 KB
/
10_fix_href_colon.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
#!/usr/bin/env bash
shopt -s globstar
CONTENTS=$@
if [ -z "$CONTENTS" ]
then
CONTENTS=`find content/**/*.md`
fi
for i in $CONTENTS
do
echo "Processing $i"
# Replace "<a href=..." with the href template, to make them more consistent with links processed from markdown.
# Notably, links to non-existing targets get formatted accordingly.
cat $i | grep -oP 'href="(?!https?:)(?!#)(.*?)"' | grep -oP '(?<=").*(?=")' | sort | uniq | while read -r match; do
BROKEN=""
WITHOUTHASH=`echo -n $match | sed 's/#.*//'`
# Perform a case-insensitive lookup and fix the link if needed
DIR=`dirname $WITHOUTHASH`
NAME=`basename $WITHOUTHASH`
LOOKUP=`(cd content/News && find $DIR -maxdepth 1 -iname $NAME.md)`
if [ ! -n "$LOOKUP" ]
then
LOOKUP=$match.md
BROKEN='"broken" '
echo `dirname "$i"`/News/$WITHOUTHASH".md"
fi
MATCHSUB="{{% href \"${LOOKUP::-3}\" $BROKEN%}}"
echo -e " Replacing " $match "\t=>\t" $MATCHSUB
# Replace all mentions of this particular link
sed -i "s<href=\"$match\"<$MATCHSUB<g" "$i"
done
done