-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadjust-markdown.sh
executable file
·87 lines (67 loc) · 2.73 KB
/
adjust-markdown.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
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
#!/bin/bash
just_exit()
{
echo "Error: ($*)" 1>&2
echo "$0 --help # for usage info"
exit 252
}
usage()
{
cat <<EOF
Usage:
$0 ./{markdown-file.md} ./deeper/path/{markdown-file.md}
Copy a markdown file to another place in the repository hierarchy
while also adjusting relative links so that they still work.
The markdown experimental feature of bashctrl.sh exports a markdown
file that summarizes the steps in a bashsteps script. When the file
is viewed from https://github.com/, each steps links to the line in
the script file where the step is defined. The default markdown generated
by bashctrl.sh requires that the file be put at the *top* level of the git
repository, otherwise the relative links do not work.
This script adjusts the markdown file from bashctrl.sh so that the
markdown file works at deeper levels in the repository's file
hierarchy.
EOF
exit 0
}
[[ "$*" == *--help* ]] && usage
orgpath="$1"
newpath="$2"
[ -f "$orgpath" ] || just_exit "File ($orgpath) not found"
[[ "$orgpath" == *.md ]] || just_exit "Filename ($orgpath) does not end in '.md'"
[[ "$(< "$orgpath")" == *../blob/* ]] || just_exit "File ($orgpath) was not generated by bashctrl.sh"
[ -e "$newpath" ] && just_exit "Destination file ($newpath) already exists"
orgdir="$(dirname "$(readlink -f "$orgpath")")" || exit
newdir="$(dirname "$(readlink -f "$newpath")")" || exit
deeperpart="${newdir#$orgdir}"
slashes="${deeperpart//[^\/]}"
if [ "$deeperpart" != "$newdir" ] && [ "$slashes" != "" ]; then # new dir is deeper
frompat='/'; topat='/..'
toadd="${slashes//$frompat/$topat}" # Add an extra "/.." for each level deeper
# so if slashes="//", toadd will now be "/../.."
exec 9> "$newpath" || just_exit "Cannot open output file ($newpath)"
if cat "$orgpath" | sed -e "s,/blob/,$toadd/blob/,g" -e "s,/tree/,$toadd/tree/,g" >&9; then
echo "New file successfully written at $newpath"
exit 0
else
rm "$newpath"
just_exit "Error while processing with with sed"
fi
fi
# For completeness, make the conversion work in the opposite direction:
deeperpart="${orgdir#$newdir}"
slashes="${deeperpart//[^\/]}"
if [ "$deeperpart" != "$orgdir" ] && [ "$slashes" != "" ]; then # new dir is *less* deep
frompat='/'; topat='/..'
toadd="${slashes//$frompat/$topat}" # Remove an extra "/.." for each level deeper
# so if slashes="//", toadd will now be "/../.."
exec 9> "$newpath" || just_exit "Cannot open output file ($newpath)"
if cat "$orgpath" | sed -e "s,$toadd/blob/,/blob/,g" -e "s,$toadd/tree/,/tree/,g" >&9; then
echo "New file successfully written at $newpath"
exit 0
else
rm "$newpath"
just_exit "Error while processing with with sed"
fi
fi
just_exit "Source and destination markdown files are at the same level"