forked from zeromq/zeromq.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_markdown.sh
executable file
·46 lines (40 loc) · 984 Bytes
/
extract_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
#!/usr/bin/env bash
usage() {
echo Extract a section from a markdown file identified by its header
echo
echo Usage:
echo " ./extract_markdown.sh <input file> <output file> <header1;header2;...>"
}
if [ "$#" -ne 3 ]; then
usage
exit 1
fi
INPUT_FILE=$1
OUTPUT_FILE=$2
HEADERS=$3
parse_markdown() {
HEADER=$1
LEVEL=
while IFS=' ' read -r line
do
parts=( $line )
if [ -z "$LEVEL" ]; then
if [[ "${parts[0]}" =~ ^[#]+$ ]]; then
if [[ $line == *$HEADER* ]]; then
LEVEL=${parts[0]}
echo "$line" >> $OUTPUT_FILE
fi
fi
else
if [[ ! "${parts[0]}" == $LEVEL ]]; then
echo "$line" >> $OUTPUT_FILE
else
break
fi
fi
done < $INPUT_FILE
}
IFS=';' read -ra HEADER_LIST <<< "$HEADERS"
for header in "${HEADER_LIST[@]}"; do
parse_markdown $header
done