-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.sh
executable file
·190 lines (155 loc) · 4.85 KB
/
generate.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
# colors
BLUE='\e[0;34m'
RED='\e[0;31m'
NONE='\e[0m'
function usage() {
cat <<-EOF
$0 [-vdp] [-f LIST [-f LIST ...]] [-o PDF] [-y FRONTMATTER] FILE1 [FILE2 FILE3 ...]
Process and pass FILE(s) to pandoc for rendering into a report with the
Eisvogel LaTeX template. FILEs are processed in the order in which they
appear in the list. If FILE is missing or - will read from STDIN.
-v verbose; log debug info to STDERR
-d update FRONTMATTER metadata file with today's date
in YYYY-MM-DD (%F) format
-p preview: renders the markdown that will
be passed to pandoc
-f LIST a file containing the list of files to process
in the order in which they should appear in the
report. multiple files are processed in the order
in which they appear in the invocation. contents of
files in LIST(s) will appear in the report before the
contents of FILE(s)
-o PDF specify output file name with extension, e.g. path/to/report.pdf
default is report.pdf
-y FRONTMATTER specify a custom .yml frontmatter metadata file
EOF
}
# prints only if invoked with -v
# usage: log <string> [color = BLUE]
function log() {
COLOR=${BLUE}
[[ $# -eq 2 ]] && COLOR=$2
[[ -v DEBUG ]] \
&& echo -e "$COLOR[+]$NONE $1" >&2
}
# if no files listd and no pipe detected, print usage and exit
([[ $# -lt 1 ]] && [[ ! -p /dev/stdin ]]) \
&& usage \
&& exit 1
# defaults
WORKING_DIR=${PWD}
TEMPLATES_DIR=${WORKING_DIR}/templates
OUTPUT_FILE="${WORKING_DIR}/report.pdf"
FRONTMATTER="${TEMPLATES_DIR}/frontmatter.yml"
declare -a FILE_LIST=() # empty list
while getopts ':vdpf:o:y:' OPTION; do
case "${OPTION}" in
v)
DEBUG=""
log "debug enabled"
;;
d)
UPDATE_DATE=""
log "UPDATE_DATE set"
;;
p)
PREVIEW=""
log "setting preview flag"
;;
f)
# error checking for options
if [[ ${OPTARG:0:1} == '-' ]]; then
log "-f requires LIST" ${RED}
usage && exit 1
fi
FILE_LIST+=(${OPTARG})
log "added ${OPTARG} to \$FILE_LIST"
;;
o)
OUTPUT_FILE="${OPTARG}"
;;
y)
if [[ ${OPTARG:0:1} == '-' || ${OPTARG} != *.yml ]]; then
log "frontmatter file extension must be .yml" ${RED}
usage && exit 1
fi
FRONTMATTER="${OPTARG}"
;;
\?)
usage
exit 1
;;
esac
done
log "num list files: ${#FILE_LIST[@]}"
log "output file: ${OUTPUT_FILE}"
log "using ${FRONTMATTER} for metadata file"
declare -a INPUT_FILES=()
# get file lists if supplied
if [[ ${#FILE_LIST[@]} -gt 0 ]]; then
for file in ${FILE_LIST[@]}; do
while IFS= read line; do
INPUT_FILES+=("${line}") # quotes preserve newlines
done < ${file}
done
fi
# parse remaining args
# should just be regular-ass files
shift $((OPTIND - 1))
log "remaining args: $*"
# unset IFS so spaces in files are preserved
IFS_BAK="${IFS}"
IFS=
for file in ${@}; do
INPUT_FILES+=("${file}")
log "added ${file} to \$FILE_LIST"
done
# reset IFS
#IFS="${IFS_BAK}"
log "# input files: ${#INPUT_FILES[@]}"
# get lines from input files
declare -a TEXT=()
# read in lines from input files
# in order
# test for pipe from STDIN
if [[ -p /dev/stdin ]]; then
log "pipe detected from STDIN"
PIPED_DATA=$(cat -)
fi
# read files
for file in ${INPUT_FILES[@]}; do
# if piped data, add it to TEXT
if [[ "${file}" == "-" ]]; then
TEXT+=("${PIPED_DATA}")
# else, continue reading files from input files
else
while IFS= read -r line; do
TEXT+=("${line}\n")
done < "${file}" # sub piped data for -
fi
done
# render the output
# remove single leading space for indices >= 1
TEXT=$(echo -e "${TEXT[@]}" | sed 's/^ //g')
if [[ -v UPDATE_DATE ]]; then
today="$(date +%F)"
sed -ri "s/(date: \")([^\"]*)\"/\1${today}\"/" ${FRONTMATTER}
log "replaced date in ${FRONTMATTER} with ${today}"
fi
if [[ -v PREVIEW ]]; then
log "generating markdown preview"
awk -f ${WORKING_DIR}/prepare.awk <(echo -e "${TEXT}")
else
log "generating report"
awk -f ${WORKING_DIR}/prepare.awk <(echo -e "${TEXT}")\
| pandoc -o "${OUTPUT_FILE}" "${FRONTMATTER}" - \
--from markdown+yaml_metadata_block+raw_html \
--template eisvogel \
--table-of-contents \
--toc-depth 6 \
--number-sections \
--top-level-division=chapter \
--highlight-style breezedark
log "file ${OUTPUT_FILE} created"
fi