-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmksnipplets.sh
More file actions
executable file
·61 lines (54 loc) · 1.74 KB
/
Copy pathmksnipplets.sh
File metadata and controls
executable file
·61 lines (54 loc) · 1.74 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
#!/bin/bash
if [[ -z $1 || -z $2 ]]; then
echo "$(basename $0) <source> <output directory>"
exit 1
fi
# Parameters
SOURCE=$1
OUTDIR=$2
if [[ ! -d "$OUTDIR" ]]; then
echo "error: \"$OUTDIR\" no such directory found." > /dev/stderr
exit 1
fi
# Markers enclosing code snippets
BEGIN_MARKER="/*#snipplet: " # /*#snipplet: <filename>
BEGIN_MARKER_SED="\/\*#snipplet: " # sed friendly version
BEGIN_MARKER_LEN=13 # Length of '/*#snipplet: '
END_MARKER="/*#end" # /*#end
END_MARKER_SED="\/\*#end" # sed friendly version
END_MARKER_LEN=6 # Lenght of '/*#end'
FILTER_MARKER="/*#filter:" # /*#filter: <sed expr>
FILTER_MARKER_SED="\/\*#filter:" # sed friendly version
FILTER_MARKER_LEN=10 # Length of '/*#filter:'
# Keep whitespace when reading
IFS=''
# In the beginning there was emptiness
FILE=
OLDFILE=
FILTER=
cat "$SOURCE" |
sed -n "/^$BEGIN_MARKER_SED/,/^$END_MARKER_SED *\$/p" |
while read -r line; do
if [[ ${line:0:$FILTER_MARKER_LEN} = "$FILTER_MARKER" ]]; then
FILTER=$(echo -E "$line" | sed "s?${FILTER_MARKER_SED}[ \t]*??" | \
sed 's@[ \t]*\*/.*$@@' \
)
continue
fi
if [[ ${line:0:$BEGIN_MARKER_LEN} = "$BEGIN_MARKER" ]]; then
FILE=$(echo -E "$line" | sed "s@${BEGIN_MARKER_SED}@@" | \
sed 's@[ \t]*\*/.*$@@' \
)
if [[ $OLDFILE != $FILE ]]; then
rm -f "$OUTDIR/$FILE" && touch -m "$OUTDIR/$FILE"
fi
elif [[ ${line:0:$END_MARKER_LEN} = "$END_MARKER" ]]; then
OLDFILE=$FILE; unset FILE FILTER
elif [[ ! -z "$FILE" ]]; then
if [[ "x$FILTER" != "x" ]]; then
echo -E "$line" | sed "$FILTER" >>"$OUTDIR/$FILE"
else
echo -E "$line" >> "$OUTDIR/$FILE"
fi
fi
done