-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserial-editor.sh
executable file
·409 lines (349 loc) · 10.5 KB
/
serial-editor.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/bin/bash
#
# A script to perform mass edit on articles' YAML
# (and maybe other useful things)
#
# Author: Piero Grandesso
# https://github.com/piero-g/markdown-workflow
#
#####
# 0. events log and other checks
#####
# also: am I in the right place? (is there z-lib folder?)
if . ./z-lib/events-logger.sh ; then
echo "Starting events registration in $eventslog"
else
echo "Something went wrong with event logger, aborting! (is ./z-lib/ in its place?)"
exit 1
fi
printf '%b\n' "[$(date +"%Y-%m-%d %H:%M:%S")] serial-editor.sh started running, logging events" >> "$eventslog"
# trap for exiting while in subshell
set -E
trap '[ "$?" -ne 77 ] || exit 77' ERR
# set the current working directory for future cd
workingDir=$PWD
# temporary file for storing variables
tempvar=`mktemp $workingDir/tmp-values.XXXXXXXXX.sh`
# reading options with getopt...
getopt --test > /dev/null
if [[ $? -ne 4 ]]; then
echo "I’m sorry, `getopt --test` failed in this environment."
exit 1
fi
OPTIONS=up:cs:rh
LONGOPTIONS=undraft,publication:,countpages,pagesequence:,references,help
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTIONS --name "$0" -- "$@")
printf '%b\n' "[$(date +"%Y-%m-%d %H:%M:%S")] current command options: $PARSED\n" >> "$eventslog"
if [[ $? -ne 0 ]]; then
# e.g. $? == 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# help
function printHelp() {
cat <<EOF
This script performs some serial edits to the YAML part of markdown files.
Each option should be launched separately.
The following options are supported:
-h, --help display this message and exit
-u, --undraft change "draft: true" to "false"
-p, --publication set the given publication date
(specified in YYYY-MM-DD format)
-c, --countpages count the pages for each PDF in 2-publication/
the output can be copied and pasted as a TSV
-s, --pagesequence reads a TSV with id/filename, starting page, ending page
and writes those data to page.start and page.end
-r, --references extract from HTMLs in "2-publication" the reference list
EOF
}
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-h|--help)
printHelp
exit 1
;;
-u|--undraft)
u=y
shift
;;
-p|--publication)
publicationDate="$2"
shift 2
;;
-c|--countpages)
pageCount=y
shift
;;
-s|--pagesequence)
pageSequence="$2"
shift 2
;;
-r|--references)
extractReferences=y
shift
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
# countpages and pageSequence are exclusive options (and countpages prevails)
if ([ $u ] || [ $publicationDate ] || [ $pageSequence ]) && [ $pageCount ]; then
echo -e "WARNING: Page count is requested, any other option will be ignored!\n"
else
if ([ $u ] || [ $publicationDate ]) && [ $pageSequence ]; then
echo -e "WARNING: A pageSequence is selected: any other option will be ignored!\n"
else
:
fi
fi
######
# 1. create directory structure for working and archiving, if not already there
######
mkdir -p ./archive/layout-versions
# creating only the directories pertaining this part of the workflow
printf '%b\n' "[$(date +"%Y-%m-%d %H:%M:%S")] Preparing the directory structure, if not ready" >> "$eventslog"
######
# 2. conversion, change extension, not filename; then archive manuscript
######
# prepare daily subdirectory for layout-versions archiving
mkdir -p ./archive/layout-versions/$today
# undraft
undraft() {
sed -r -i.undraft.bak '0,/^(draft:)\s+true *#?(.*)$/s//\1 false #\2/' "${manuscript}"
diff "${manuscript}" "${manuscript}.undraft.bak"
}
# set publication date
setpubdate() {
echo "$publicationDate"
#echo sed -r -i.pub.bak "0,/^\s+(published:)\s+\d\d\d\d\-\d\d\-\d\d/s//\1 $publicationDate/" "${manuscript}"
sed -r -i.pub.bak -e '0,/^(\s+published:)\s+[0-9]{4}-[0-9]{2}-[0-9]{2} *#?(.*)$/s//\1 '$publicationDate' #\2/' "${manuscript}"
diff "${manuscript}" "${manuscript}.pub.bak"
}
# editing function
edityaml() {
echo -e "\n\tediting YAML in ${manuscript%.md}..."
# archive a copy before editing the manuscript
cp "$manuscript" "$workingDir/archive/layout-versions/$today/${manuscript%.md}-$(date +"%Y-%m-%dT%H-%M-%S").md"
printf '%b\n' "[$(date +"%Y-%m-%d %H:%M:%S")] copy of ${manuscript%.md} archived" >> "$workingDir/$eventslog"
if [ $u ]; then
undraft
fi
if [ $publicationDate ]; then
setpubdate
fi
}
# Do you want to run editing on a specific article?
if [ -z ${@+x} ]; then
# no file specified, run on each file within the directory
printf '%b\n' "[$(date +"%Y-%m-%d %H:%M:%S")] Starting editing of manuscripts in ./1-layout..." >> "$eventslog"
# also store a flag
echo ALL=true >> $tempvar
( # start subshell
if cd ./1-layout ; then
echo "Starting editing..."
else
echo "WARNING: ./1-layout directory not found!"
printf '%b\n' "[$(date +"%Y-%m-%d %H:%M:%S")] WARNING: ./1-layout directory not found! Aborting." >> "$eventslog"
exit 77
fi
# check if there are valid files
EXT=(`find ./ -maxdepth 1 -regextype posix-extended -regex '.*\.(md)$'`)
if [ ${#EXT[@]} -gt 0 ]; then
: # valid files, ok
else
printf '%b\n' "[$(date +"%Y-%m-%d %H:%M:%S")] [WARN] No valid files found in ./1-layout, exiting now" >> "../$eventslog"
echo "WARNING: no valid files!"
exit 77
fi
if [ $pageCount ] || [ $pageSequence ] || [ $extractReferences ]; then
: # skip edityaml
else
# convert valid files
for markdown in ./*.md; do
manuscript="${markdown#.\/}"
# launch editing
edityaml
done
fi
) # end subshell
else # we have a parameter: convert only specified file
if [ $pageCount ] || [ $pageSequence ] || [ $extractReferences ]; then
echo "WARNING: the option selected won't run on specific files, aborting!"
printHelp
exit 1
fi
for parameter in "$@"; do
manuscript="$( echo "$parameter" | sed -r 's/^\.?\/?1\-layout\///' )"
if [[ $manuscript == *.md ]]; then
: # valid files, ok
else
printf '%b\n' "[$(date +"%Y-%m-%d %H:%M:%S")] [WARN] The specified $manuscript has not a valid extension, exiting now" >> "$eventslog"
echo "WARNING: $manuscript is not valid!"
exit 1
fi
( # start subshell
if cd ./1-layout ; then
echo "Starting editing..."
else
echo "WARNING: ./1-layout directory not found!"
printf '%b\n' "[$(date +"%Y-%m-%d %H:%M:%S")] WARNING: ./1-layout directory not found! Aborting." >> "$eventslog"
exit 77
fi
edityaml
) # end subshell
done
fi
###
# COUNTING FUNCTION
# page counter, it will only count pages of PDF on the entire "2-publication" folder
###
countpages() {
pagespdf=$(pdfinfo "${manuscript}" | grep "Pages:" | sed 's/Pages: //')
echo -e "${manuscript%.pdf}\t${pagespdf}"
}
if [ $pageCount ]; then
# do not run setpage on a single file (variable check)
. $tempvar
if [ $ALL ]; then
# no file specified, proceed
( # start subshell
if cd ./2-publication ; then
for manuscript in *pdf ; do
# counter function
countpages
done
else
echo "WARNING: ./2-publication directory not found!"
printf '%b\n' "[$(date +"%Y-%m-%d %H:%M:%S")] WARNING: ./2-publication directory not found! Aborting." >> "$eventslog"
exit 77
fi
) # end subshell
else
echo "WARNING: the page sequence can only be applied to the full issue, I will exit"
printHelp
exit 1
fi
# countpages has priority, so we have to exit now
# remove working files
rm $tempvar
exit 1
else
:
fi
###
# PAGE SEQUENCE
# page sequence, it will run on the entire "1-layout" folder
###
setstartpage() {
sed -r -i.start.bak -e '0,/^(\s+start:)\s+[0-9] *#?(.*)$/s//\1 '$startPage' #\2/' "$filename"
diff "$filename" "$filename.start.bak"
}
setendpage() {
sed -r -i.end.bak -e '0,/^(\s+end:)\s+[0-9] *#?(.*)$/s//\1 '$endPage' #\2/' "$filename"
diff "$filename" "$filename.end.bak"
}
# parse TSV and take care for correct paring of file name and values
parsepages() {
echo "set page ${pageSequence}!"
# parse TSV
sed 1d ${pageSequence} | while IFS=$'\t' read -r -a arry
do
fileid="${arry[0]}"
startPage="${arry[1]}"
endPage="${arry[2]}"
echo -e "\n"$fileid" is the file ID..."
filenamepath=$(find "${workingDir}/1-layout/" -maxdepth 1 -type f -name "$fileid*")
filename="${filenamepath##*/}"
echo -e "\n"$filename" is the filename..."
echo "..." $startPage "is its startPage"
echo "..." $endPage "is its endPage"
( # start subshell
if cd ./1-layout ; then
:
else
echo "WARNING: ./1-layout directory not found!"
printf '%b\n' "[$(date +"%Y-%m-%d %H:%M:%S")] WARNING: ./1-layout directory not found! Aborting." >> "$eventslog"
exit 77
fi
if [[ -f $filename ]] && [[ $filename == *.md ]]; then
# we have the file, proceed
setstartpage
setendpage
else
echo "Warning:" $filename "not found, skipping!"
fi
) # end subshell
sleep 2
done
}
# check input source if pageSequence
if [ $pageSequence ]; then
# do not run setpage on a single file (variable check)
. $tempvar
if [ $ALL ]; then
# no file specified, proceed
# check that the input is a TSV
if [[ (-f ${pageSequence}) && (${pageSequence} == *.tsv) ]]; then
echo "ok, it's a TSV!"
parsepages
else
echo "I can't find a file named ${pageSequence} or its not a TSV, abort!"
printHelp
exit 1
fi
else
echo "WARNING: the page sequence can only be applied to the full issue, I will exit"
printHelp
exit 1
fi
else
:
fi
###
# EXTRACT REFERENCES
# extract references from HTML files in "2-publication"
###
referencesExtraction() {
xmllint --html --xpath '//section[@id = "references"]/p' "$article" > "references/${article%\.html}"-references.txt 2>/dev/null
}
cleanReferences() {
# replace any newline with a space
tr '\n' ' ' < "${refslist}" > "${refslist}-tmp" && mv "${refslist}-tmp" "${refslist}"
# remove <p> tags and add newlines
sed -i -e 's/<p>//g' -e 's#</p> #\n\n#g' "${refslist}"
# clean URI
sed -i -E 's#<a href=".+" class="uri">(.+)</a>#\1#g' "${refslist}"
}
if [ $extractReferences ]; then
# do not run setpage on a single file (variable check)
. $tempvar
if [ $ALL ]; then
# no file specified, proceed
( # start subshell
if cd ./2-publication ; then
mkdir -p references
for article in *.html; do
referencesExtraction
done
if cd ./references ; then
for refslist in *-references.txt ; do
cleanReferences
done
fi
fi
) # end subshell
fi
fi
# remove working files
rm $tempvar
# we should remove also .bak files
echo "We are done here!"