-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrender-knitting-charts.sh
More file actions
executable file
·94 lines (85 loc) · 1.8 KB
/
Copy pathrender-knitting-charts.sh
File metadata and controls
executable file
·94 lines (85 loc) · 1.8 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
#!/bin/bash
set -Eeufo pipefail
trap 'echo >&2 "$0:$LINENO [$?]: $BASH_COMMAND"' ERR
usage() {
cat <<EOS
$0 [-hne] FILE
Render <!-- knitting chart: --> comments to SVG.
Options:
-n Dry run (print rather than overwriting)
-e Watch for changes and rerun with entr
EOS
}
grid_script=$PROJECTS/mk12-scripts/make-svg-grid.py
flags='-i inplace'
entr=false
while getopts "hne" opt; do
case $opt in
h) usage; exit 0;;
n) flags= ;;
e) entr=true ;;
*) exit 1 ;;
esac
done
shift $((OPTIND - 1))
replace() {
files=("${@:2}")
if [[ "${files[0]}" == "$grid_script" ]]; then
files=()
if ! [[ -f /tmp/render-knitting-charts-last.txt ]]; then
return
fi
while read -r line; do
files+=("$line")
done < /tmp/render-knitting-charts-last.txt
else
printf "%s\n" "${files[@]}" > /tmp/render-knitting-charts-last.txt
fi
gawk $1 -v grid_script="$grid_script" '
BEGINFILE { s = 0; }
/^<!--$/ { s = 1; print; next; }
s == 1 {
if (match($0, /^knitting-chart: (.*)$/, m)) {
args = m[1];
s = 2;
chart = "";
} else {
s = 0;
}
print; next;
}
s == 2 {
if (/^-->$/) {
s = 3;
} else {
chart = chart $0 "\n";
}
print; next;
}
s == 3 {
if (/^<figure>$/) {
s = 4;
}
print;
next;
}
s == 4 {
if (/^<\/figure>$/) {
print chart > "/tmp/knitting-chart.txt";
system(grid_script " " args " --chart /tmp/knitting-chart.txt");
print;
s = 0;
}
next;
}
{ print; }
' "${files[@]}"
}
if [[ $entr = true ]]; then
export -f replace
export grid_script
printf "%s\n" "$grid_script" "$@" \
| SHELL=/bin/bash entr -s "replace '$flags' \$0"
else
replace "$flags" "$@"
fi