forked from HandBrake/HandBrake-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marsh
executable file
·2418 lines (2313 loc) · 114 KB
/
marsh
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# marsh - build static websites using markdown and bash
#
# Copyright 2022 Bradley Sepos
# Released under the MIT License. See LICENSE for details.
# https://github.com/bradleysepos/marsh
NAME="marsh"
VERSION="0.8.2"
SELF="${BASH_SOURCE[0]}"
SELF_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P)
SELF_DIR="${SELF_DIR:-$(pwd)}"
SELF_NAME=$(basename "${SELF}")
SYS_NAME=$(uname | awk '{ print tolower($0)}')
MARKDOWN_OPTIONS_DEFAULT=(-1.0 +alphalist -autolink -cdata +definitionlist -divquote -dldiscount +dlextra -emphasis +ext +fencedcode +footnote +githubtags -header +html +image -latex +links -safelink +smarty +strikethrough +style +superscript +tables -tabstop -toc)
HELP="\
usage: ${SELF_NAME} [-h | --help]
${SELF_NAME} [-v | --version]
${SELF_NAME} build [--fetch] [--force] [-j # | --jobs #]
[--markdown markdown] [--markdown-options opt1,opt2] [config_file]
where:
-h, --help
display this help text
-v, --version
display version information
--fetch
download remote resources
--force
remove and replace existing targets (overwrite)
-j, --jobs
number of concurrent jobs to run using GNU Parallel
default: automatically chosen by GNU Parallel, if available
--markdown
path to markdown application
--markdown-options
markdown translation options (comma delimited)
default: $(echo ${MARKDOWN_OPTIONS_DEFAULT[@]} | sed 's/ /,/g')"
# creates bash variables from yaml records
# extended to support arrays, empty values, and more
# https://gist.github.com/DinoChiesa/3e3c3866b51290f31243
# https://gist.github.com/epiloque/8cf512c6d64641bde388
# https://gist.github.com/pkuczynski/8665367
function parse_yaml { # parse_yaml yaml_string_or_file variables_prefix spaces_per_indentation
local INPUT PREFIX INDENT S W FS
INPUT="${1:-}"
[[ "${INPUT}" == "" ]] && return 0
if [[ -f "${INPUT}" ]] && [[ -r "${INPUT}" ]]; then
INPUT=$(<"${INPUT}")
fi
PREFIX="${2:-}"
INDENT="${3:-}"
if [[ ! "${INDENT}" =~ '^[0-9]+$' ]] || [[ ! "${INDENT}" -eq 0 ]]; then
INDENT=2
fi
S='[[:space:]]*'
W='[a-zA-Z0-9_]*'
FS=$(echo @|tr @ '\034')
echo "${INPUT}" |
sed -n \
-e "s|^\(\(${S}\)\(-\) \)\{1\}\(${S}\)\(${W}\)${S}:${S}\(\".*\"\)\${S}\$|\3${FS}\2 \4${FS}\5${FS}\6|p" \
-e "s|^\(\(${S}\)\(-\) \)\{1\}\(${S}\)\(${W}\)${S}:${S}\(.*\)${S}\$|\3${FS}\2 \4${FS}\5${FS}\6|p" \
-e "s|^\(\(${S}\)\(-\) \)\{1\}\(${S}\)\(\".*\"\)${S}\$|\3${FS}\2 \4${FS}${FS}\5|p" \
-e "s|^\(\(${S}\)\(-\) \)\{1\}\(${S}\)\([^:].*\)${S}\$|\3${FS}\2 \4${FS}${FS}\5|p" \
-e "s|^\(${S}\)\(${W}\)${S}:${S}\(\".*\"\)${S}\$|${FS}\1${FS}\2${FS}\3|p" \
-e "s|^\(${S}\)\(${W}\)${S}:${S}\(.*\)${S}\$|${FS}\1${FS}\2${FS}\3|p" |
awk -F"${FS}" \
'#
# parse_csv, public domain, http://lorance.freeshell.org/csv/
#**************************************************************************
#
# This file is in the public domain.
#
# For more information email [email protected].
# Or see http://lorance.freeshell.org/csv/
#
# Parse a CSV string into an array.
# The number of fields found is returned.
# In the event of an error a negative value is returned and csverr is set to
# the error. See below for the error values.
#
# Parameters:
# string = The string to parse.
# csv = The array to parse the fields into.
# sep = The field separator character. Normally ,
# quote = The string quote character. Normally "
# escape = The quote escape character. Normally "
# newline = Handle embedded newlines. Provide either a newline or the
# string to use in place of a newline. If left empty embedded
# newlines cause an error.
# trim = When true spaces around the separator are removed.
# This affects parsing. Without this a space between the
# separator and quote result in the quote being ignored.
#
# These variables are private:
# fields = The number of fields found thus far.
# pos = Where to pull a field from the string.
# strtrim = True when a string is found so we know to remove the quotes.
#
# Error conditions:
# -1 = Unable to read the next line.
# -2 = Missing end quote.
# -3 = Missing separator.
#
# Notes:
# The code assumes that every field is preceded by a separator, even the
# first field. This makes the logic much simpler, but also requires a
# separator be prepended to the string before parsing.
function parse_csv(string,csv,sep,quote,escape,newline,trim, fields,pos,strtrim) {
# Make sure there is something to parse.
if (length(string) == 0) return 0;
string = sep string; # The code below assumes ,FIELD.
fields = 0; # The number of fields found thus far.
while (length(string) > 0) {
# Remove spaces after the separator if requested.
if (trim && substr(string, 2, 1) == " ") {
if (length(string) == 1) return fields;
string = substr(string, 2);
continue;
}
strtrim = 0; # Used to trim quotes off strings.
# Handle a quoted field.
if (substr(string, 2, 1) == quote) {
pos = 2;
do {
pos++
if (pos != length(string) &&
substr(string, pos, 1) == escape &&
(substr(string, pos + 1, 1) == quote ||
substr(string, pos + 1, 1) == escape)) {
# Remove escaped quote characters.
string = substr(string, 1, pos - 1) substr(string, pos + 1);
} else if (substr(string, pos, 1) == quote) {
# Found the end of the string.
strtrim = 1;
} else if (newline && pos >= length(string)) {
# Handle embedded newlines if requested.
if (getline == -1) {
csverr = "Unable to read the next line.";
return -1;
}
string = string newline $0;
}
} while (pos < length(string) && strtrim == 0)
if (strtrim == 0) {
csverr = "Missing end quote.";
return -2;
}
} else {
# Handle an empty field.
if (length(string) == 1 || substr(string, 2, 1) == sep) {
csv[fields] = "";
fields++;
if (length(string) == 1)
return fields;
string = substr(string, 2);
continue;
}
# Search for a separator.
pos = index(substr(string, 2), sep);
# If there is no separator the rest of the string is a field.
if (pos == 0) {
csv[fields] = substr(string, 2);
fields++;
return fields;
}
}
# Remove spaces after the separator if requested.
if (trim && pos != length(string) && substr(string, pos + strtrim, 1) == " ") {
trim = strtrim
# Count the number fo spaces found.
while (pos < length(string) && substr(string, pos + trim, 1) == " ") {
trim++
}
# Remove them from the string.
string = substr(string, 1, pos + strtrim - 1) substr(string, pos + trim);
# Adjust pos with the trimmed spaces if a quotes string was not found.
if (!strtrim) {
pos -= trim;
}
}
# Make sure we are at the end of the string or there is a separator.
if ((pos != length(string) && substr(string, pos + 1, 1) != sep)) {
csverr = "Missing separator.";
return -3;
}
# Gather the field.
csv[fields] = substr(string, 2 + strtrim, pos - (1 + strtrim * 2));
fields++;
# Remove the field from the string for the next pass.
string = substr(string, pos + 1);
}
return fields;
}
BEGIN { prefix = "'"${PREFIX}"'"; indent = '"${INDENT}"'; prev_level = 0; }
{
type = $1;
level = length($2)/indent;
key = $3;
val = $4;
out = "";
array = 0;
if (level < prev_level) { adepth[prev_level] = -1; }
if (adepth[level] == "") { adepth[level] = -1; }
if (type == "-") { adepth[level]++; }
vname[level] = key;
for (i in vname) {
if (i > (level)) { vname[i] = ""; }
}
for (i = 0; i < level; i++) {
if (vname[i] != "") {
out = out "_" vname[i];
if (adepth[i] > -1) { out = out "_" adepth[i+1]; }
} else {
out = out "_" adepth[i+1];
}
}
if (substr(val,1,1) == "[" && substr(val,length(val),1) == "]") {
array = 1;
gsub(/^\[[ ]*/, "", val);
gsub(/[ ]*\]$/, "", val);
num_vals = parse_csv(val, vals, ",", "\"", "\"", "\\n", 1);
if (num_vals < 0) {
# Error parsing inline array
exit 1;
}
} else {
if (substr(val,1,1) == "\"" && substr(val,length(val),1) == "\"") {
num_vals = parse_csv(val, vals, "'"${FS}"'", "\"", "\"", "\\n", 0);
if (num_vals < 0) {
# Error parsing
exit 1;
}
} else {
vals[1] = val;
}
}
if (key != "") { out = out "_" key; }
out = prefix out;
gsub(/__/, "_", out);
gsub(/_=/, "", out);
#for (i = 1; i <= length(vals); i++) {
for (i in vals) {
gsub(/"/, "\\\"", vals[i]);
if (vals[i] != "true" && vals[i] != "false") {
vals[i]="\"" vals[i] "\""
}
if (array == 1) {
print out "_" i "=" vals[i];
} else {
print out "=" vals[i];
}
}
delete vals;
prev_level = level;
}'
}
# tests whether a string is an exact match for an array item
function in_array { # in_array needle haystack[@]
local e
for e in "${@:2}"; do
[[ "${e}" == "${1}" ]] && return 0;
done
return 1
}
# creates an array from the contents of flat variables
# TEST_0="foo"; TEST_1="bar"; TEST_2="baz"
# eval $(flat_to_array "TEST" "TEST_") # loops over TEST_{0..2}
# echo "${TEST[*]}" # foo bar baz
function flat_to_array { # flat_to_array array_name variable_prefix
local ARRAY_NAME PREFIX INDEX ITEM
ARRAY_NAME="${1:-}"
[[ "${ARRAY_NAME}" == "" ]] && return 1
PREFIX="${2:-}"
[[ "${PREFIX}" == "" ]] && return 1
echo "${ARRAY_NAME}=()"
INDEX=0
ITEM="${PREFIX}${INDEX}"
ITEM="${!ITEM:-}"
while [[ "${ITEM}" != "" ]]; do
echo "${ARRAY_NAME}+=(\"${ITEM//\"/\\\"}\")"
INDEX=$((INDEX + 1))
ITEM="${PREFIX}${INDEX}"
ITEM="${!ITEM:-}"
done
return 0
}
# prints arguments combined with delimiters
# join_delimited ', ' ', ' ', ' foo bar baz # foo, bar, baz
# join_delimited ', ' ' and ' ', and ' foo bar baz # foo, bar, and baz
function join_delimited { # join_delimited delimiter1 delimiter2 delimiter3 args[@]
local DELIM1 DELIM2 DELIM3
DELIM1="${1:-}"
shift 1
DELIM2="${1:-}"
shift 1
DELIM3="${1:-}"
shift 1
if [[ "${#@}" -gt 0 ]]; then
echo -n "${1}"
if [[ "${#@}" -eq 2 ]]; then
echo "${DELIM2}${2}"
return 0
fi
while [[ "${#@}" -gt 2 ]]; do
echo -n "${DELIM1}${2}"
shift 1
done
echo -n "${2+$DELIM3$2}"
echo
fi
return 0
}
# prints md5 hash for a given string
function md5_string { # md5_string string
local STRING SYS_NAME
STRING="${1:-}"
SYS_NAME=$(uname | awk '{ print tolower($0)}')
if [[ "${SYS_NAME}" == "darwin" ]]; then
echo -n "${STRING}" | md5 -r | awk '{ print $1 }'
else
echo -n "${STRING}" | md5sum | awk '{ print $1 }'
fi
return 0
}
# converts html name or number entities to utf-8 character
function decode_entities { # decode_entities
TEXT=$(</dev/stdin)
echo "${TEXT}" | sed -e 's/"/"/g' -e 's/"/"/g' -e 's/&/\&/g' -e 's/&/\&/g' -e 's/'/'"'"'/g' -e 's/'/'"'"'/g' -e 's/</</g' -e 's/</</g' -e 's/>/>/g' -e 's/>/>/g' -e 's/ / /g' -e 's/ / /g' -e 's/¡/¡/g' -e 's/¡/¡/g' -e 's/¢/¢/g' -e 's/¢/¢/g' -e 's/£/£/g' -e 's/£/£/g' -e 's/¤/¤/g' -e 's/¤/¤/g' -e 's/¥/¥/g' -e 's/¥/¥/g' -e 's/¦/¦/g' -e 's/¦/¦/g' -e 's/§/§/g' -e 's/§/§/g' -e 's/¨/¨/g' -e 's/¨/¨/g' -e 's/©/©/g' -e 's/©/©/g' -e 's/ª/ª/g' -e 's/ª/ª/g' -e 's/«/«/g' -e 's/«/«/g' -e 's/¬/¬/g' -e 's/¬/¬/g' -e 's/­//g' -e 's/­//g' -e 's/®/®/g' -e 's/®/®/g' -e 's/¯/¯/g' -e 's/¯/¯/g' -e 's/°/°/g' -e 's/°/°/g' -e 's/±/±/g' -e 's/±/±/g' -e 's/²/²/g' -e 's/²/²/g' -e 's/³/³/g' -e 's/³/³/g' -e 's/´/´/g' -e 's/´/´/g' -e 's/µ/µ/g' -e 's/µ/µ/g' -e 's/¶/¶/g' -e 's/¶/¶/g' -e 's/·/·/g' -e 's/·/·/g' -e 's/¸/¸/g' -e 's/¸/¸/g' -e 's/¹/¹/g' -e 's/¹/¹/g' -e 's/º/º/g' -e 's/º/º/g' -e 's/»/»/g' -e 's/»/»/g' -e 's/¼/¼/g' -e 's/¼/¼/g' -e 's/½/½/g' -e 's/½/½/g' -e 's/¾/¾/g' -e 's/¾/¾/g' -e 's/¿/¿/g' -e 's/¿/¿/g' -e 's/×/×/g' -e 's/×/×/g' -e 's/÷/÷/g' -e 's/÷/÷/g' -e 's/À/À/g' -e 's/À/À/g' -e 's/Á/Á/g' -e 's/Á/Á/g' -e 's/Â/Â/g' -e 's/Â/Â/g' -e 's/Ã/Ã/g' -e 's/Ã/Ã/g' -e 's/Ä/Ä/g' -e 's/Ä/Ä/g' -e 's/Å/Å/g' -e 's/Å/Å/g' -e 's/Æ/Æ/g' -e 's/Æ/Æ/g' -e 's/Ç/Ç/g' -e 's/Ç/Ç/g' -e 's/È/È/g' -e 's/È/È/g' -e 's/É/É/g' -e 's/É/É/g' -e 's/Ê/Ê/g' -e 's/Ê/Ê/g' -e 's/Ë/Ë/g' -e 's/Ë/Ë/g' -e 's/Ì/Ì/g' -e 's/Ì/Ì/g' -e 's/Í/Í/g' -e 's/Í/Í/g' -e 's/Î/Î/g' -e 's/Î/Î/g' -e 's/Ï/Ï/g' -e 's/Ï/Ï/g' -e 's/Ð/Ð/g' -e 's/Ð/Ð/g' -e 's/Ñ/Ñ/g' -e 's/Ñ/Ñ/g' -e 's/Ò/Ò/g' -e 's/Ò/Ò/g' -e 's/Ó/Ó/g' -e 's/Ó/Ó/g' -e 's/Ô/Ô/g' -e 's/Ô/Ô/g' -e 's/Õ/Õ/g' -e 's/Õ/Õ/g' -e 's/Ö/Ö/g' -e 's/Ö/Ö/g' -e 's/Ø/Ø/g' -e 's/Ø/Ø/g' -e 's/Ù/Ù/g' -e 's/Ù/Ù/g' -e 's/Ú/Ú/g' -e 's/Ú/Ú/g' -e 's/Û/Û/g' -e 's/Û/Û/g' -e 's/Ü/Ü/g' -e 's/Ü/Ü/g' -e 's/Ý/Ý/g' -e 's/Ý/Ý/g' -e 's/Þ/Þ/g' -e 's/Þ/Þ/g' -e 's/ß/ß/g' -e 's/ß/ß/g' -e 's/à/à/g' -e 's/à/à/g' -e 's/á/á/g' -e 's/á/á/g' -e 's/â/â/g' -e 's/â/â/g' -e 's/ã/ã/g' -e 's/ã/ã/g' -e 's/ä/ä/g' -e 's/ä/ä/g' -e 's/å/å/g' -e 's/å/å/g' -e 's/æ/æ/g' -e 's/æ/æ/g' -e 's/ç/ç/g' -e 's/ç/ç/g' -e 's/è/è/g' -e 's/è/è/g' -e 's/é/é/g' -e 's/é/é/g' -e 's/ê/ê/g' -e 's/ê/ê/g' -e 's/ë/ë/g' -e 's/ë/ë/g' -e 's/ì/ì/g' -e 's/ì/ì/g' -e 's/í/í/g' -e 's/í/í/g' -e 's/î/î/g' -e 's/î/î/g' -e 's/ï/ï/g' -e 's/ï/ï/g' -e 's/ð/ð/g' -e 's/ð/ð/g' -e 's/ñ/ñ/g' -e 's/ñ/ñ/g' -e 's/ò/ò/g' -e 's/ò/ò/g' -e 's/ó/ó/g' -e 's/ó/ó/g' -e 's/ô/ô/g' -e 's/ô/ô/g' -e 's/õ/õ/g' -e 's/õ/õ/g' -e 's/ö/ö/g' -e 's/ö/ö/g' -e 's/ø/ø/g' -e 's/ø/ø/g' -e 's/ù/ù/g' -e 's/ù/ù/g' -e 's/ú/ú/g' -e 's/ú/ú/g' -e 's/û/û/g' -e 's/û/û/g' -e 's/ü/ü/g' -e 's/ü/ü/g' -e 's/ý/ý/g' -e 's/ý/ý/g' -e 's/þ/þ/g' -e 's/þ/þ/g' -e 's/ÿ/ÿ/g' -e 's/ÿ/ÿ/g' -e 's/∀/∀/g' -e 's/∀/∀/g' -e 's/∂/∂/g' -e 's/∂/∂/g' -e 's/∃/∃/g' -e 's/∃/∃/g' -e 's/∅/∅/g' -e 's/∅/∅/g' -e 's/∇/∇/g' -e 's/∇/∇/g' -e 's/∈/∈/g' -e 's/∈/∈/g' -e 's/∉/∉/g' -e 's/∉/∉/g' -e 's/∋/∋/g' -e 's/∋/∋/g' -e 's/∏/∏/g' -e 's/∏/∏/g' -e 's/∑/∑/g' -e 's/∑/∑/g' -e 's/−/−/g' -e 's/−/−/g' -e 's/∗/∗/g' -e 's/∗/∗/g' -e 's/√/√/g' -e 's/√/√/g' -e 's/∝/∝/g' -e 's/∝/∝/g' -e 's/∞/∞/g' -e 's/∞/∞/g' -e 's/∠/∠/g' -e 's/∠/∠/g' -e 's/∧/∧/g' -e 's/∧/∧/g' -e 's/∨/∨/g' -e 's/∨/∨/g' -e 's/∩/∩/g' -e 's/∩/∩/g' -e 's/∪/∪/g' -e 's/∪/∪/g' -e 's/∫/∫/g' -e 's/∫/∫/g' -e 's/∴/∴/g' -e 's/∴/∴/g' -e 's/∼/∼/g' -e 's/∼/∼/g' -e 's/≅/≅/g' -e 's/≅/≅/g' -e 's/≈/≈/g' -e 's/≈/≈/g' -e 's/≠/≠/g' -e 's/≠/≠/g' -e 's/≡/≡/g' -e 's/≡/≡/g' -e 's/≤/≤/g' -e 's/≤/≤/g' -e 's/≥/≥/g' -e 's/≥/≥/g' -e 's/⊂/⊂/g' -e 's/⊂/⊂/g' -e 's/⊃/⊃/g' -e 's/⊃/⊃/g' -e 's/⊄/⊄/g' -e 's/⊄/⊄/g' -e 's/⊆/⊆/g' -e 's/⊆/⊆/g' -e 's/⊇/⊇/g' -e 's/⊇/⊇/g' -e 's/⊕/⊕/g' -e 's/⊕/⊕/g' -e 's/⊗/⊗/g' -e 's/⊗/⊗/g' -e 's/⊥/⊥/g' -e 's/⊥/⊥/g' -e 's/⋅/⋅/g' -e 's/⋅/⋅/g' -e 's/Α/Α/g' -e 's/Α/Α/g' -e 's/Β/Β/g' -e 's/Β/Β/g' -e 's/Γ/Γ/g' -e 's/Γ/Γ/g' -e 's/Δ/Δ/g' -e 's/Δ/Δ/g' -e 's/Ε/Ε/g' -e 's/Ε/Ε/g' -e 's/Ζ/Ζ/g' -e 's/Ζ/Ζ/g' -e 's/Η/Η/g' -e 's/Η/Η/g' -e 's/Θ/Θ/g' -e 's/Θ/Θ/g' -e 's/Ι/Ι/g' -e 's/Ι/Ι/g' -e 's/Κ/Κ/g' -e 's/Κ/Κ/g' -e 's/Λ/Λ/g' -e 's/Λ/Λ/g' -e 's/Μ/Μ/g' -e 's/Μ/Μ/g' -e 's/Ν/Ν/g' -e 's/Ν/Ν/g' -e 's/Ξ/Ξ/g' -e 's/Ξ/Ξ/g' -e 's/Ο/Ο/g' -e 's/Ο/Ο/g' -e 's/Π/Π/g' -e 's/Π/Π/g' -e 's/Ρ/Ρ/g' -e 's/Ρ/Ρ/g' -e 's/Σ/Σ/g' -e 's/Σ/Σ/g' -e 's/Τ/Τ/g' -e 's/Τ/Τ/g' -e 's/Υ/Υ/g' -e 's/Υ/Υ/g' -e 's/Φ/Φ/g' -e 's/Φ/Φ/g' -e 's/Χ/Χ/g' -e 's/Χ/Χ/g' -e 's/Ψ/Ψ/g' -e 's/Ψ/Ψ/g' -e 's/Ω/Ω/g' -e 's/Ω/Ω/g' -e 's/α/α/g' -e 's/α/α/g' -e 's/β/β/g' -e 's/β/β/g' -e 's/γ/γ/g' -e 's/γ/γ/g' -e 's/δ/δ/g' -e 's/δ/δ/g' -e 's/ε/ε/g' -e 's/ε/ε/g' -e 's/ζ/ζ/g' -e 's/ζ/ζ/g' -e 's/η/η/g' -e 's/η/η/g' -e 's/θ/θ/g' -e 's/θ/θ/g' -e 's/ι/ι/g' -e 's/ι/ι/g' -e 's/κ/κ/g' -e 's/κ/κ/g' -e 's/λ/λ/g' -e 's/λ/λ/g' -e 's/μ/μ/g' -e 's/μ/μ/g' -e 's/ν/ν/g' -e 's/ν/ν/g' -e 's/ξ/ξ/g' -e 's/ξ/ξ/g' -e 's/ο/ο/g' -e 's/ο/ο/g' -e 's/π/π/g' -e 's/π/π/g' -e 's/ρ/ρ/g' -e 's/ρ/ρ/g' -e 's/ς/ς/g' -e 's/ς/ς/g' -e 's/σ/σ/g' -e 's/σ/σ/g' -e 's/τ/τ/g' -e 's/τ/τ/g' -e 's/υ/υ/g' -e 's/υ/υ/g' -e 's/φ/φ/g' -e 's/φ/φ/g' -e 's/χ/χ/g' -e 's/χ/χ/g' -e 's/ψ/ψ/g' -e 's/ψ/ψ/g' -e 's/ω/ω/g' -e 's/ω/ω/g' -e 's/ϑ/ϑ/g' -e 's/ϑ/ϑ/g' -e 's/ϒ/ϒ/g' -e 's/ϒ/ϒ/g' -e 's/ϖ/ϖ/g' -e 's/ϖ/ϖ/g' -e 's/Œ/Œ/g' -e 's/Œ/Œ/g' -e 's/œ/œ/g' -e 's/œ/œ/g' -e 's/Š/Š/g' -e 's/Š/Š/g' -e 's/š/š/g' -e 's/š/š/g' -e 's/Ÿ/Ÿ/g' -e 's/Ÿ/Ÿ/g' -e 's/ƒ/ƒ/g' -e 's/ƒ/ƒ/g' -e 's/ˆ/ˆ/g' -e 's/ˆ/ˆ/g' -e 's/˜/˜/g' -e 's/˜/˜/g' -e 's/ / /g' -e 's/ / /g' -e 's/ / /g' -e 's/ / /g' -e 's/ / /g' -e 's/ / /g' -e 's/‌//g' -e 's/‌//g' -e 's/‍//g' -e 's/‍//g' -e 's/‎//g' -e 's/‎//g' -e 's/‏//g' -e 's/‏//g' -e 's/–/–/g' -e 's/–/–/g' -e 's/—/—/g' -e 's/—/—/g' -e 's/‘/‘/g' -e 's/‘/‘/g' -e 's/’/’/g' -e 's/’/’/g' -e 's/‚/‚/g' -e 's/‚/‚/g' -e 's/“/“/g' -e 's/“/“/g' -e 's/”/”/g' -e 's/”/”/g' -e 's/„/„/g' -e 's/„/„/g' -e 's/†/†/g' -e 's/†/†/g' -e 's/‡/‡/g' -e 's/‡/‡/g' -e 's/•/•/g' -e 's/•/•/g' -e 's/…/…/g' -e 's/…/…/g' -e 's/‰/‰/g' -e 's/‰/‰/g' -e 's/′/′/g' -e 's/′/′/g' -e 's/″/″/g' -e 's/″/″/g' -e 's/‹/‹/g' -e 's/‹/‹/g' -e 's/›/›/g' -e 's/›/›/g' -e 's/‾/‾/g' -e 's/‾/‾/g' -e 's/€/€/g' -e 's/€/€/g' -e 's/™/™/g' -e 's/™/™/g' -e 's/™/™/g' -e 's/←/←/g' -e 's/←/←/g' -e 's/↑/↑/g' -e 's/↑/↑/g' -e 's/→/→/g' -e 's/→/→/g' -e 's/↓/↓/g' -e 's/↓/↓/g' -e 's/↔/↔/g' -e 's/↔/↔/g' -e 's/↵/↵/g' -e 's/↵/↵/g' -e 's/⌈/⌈/g' -e 's/⌈/⌈/g' -e 's/⌉/⌉/g' -e 's/⌉/⌉/g' -e 's/⌊/⌊/g' -e 's/⌊/⌊/g' -e 's/⌋/⌋/g' -e 's/⌋/⌋/g' -e 's/◊/◊/g' -e 's/◊/◊/g' -e 's/♠/♠/g' -e 's/♠/♠/g' -e 's/♣/♣/g' -e 's/♣/♣/g' -e 's/♥/♥/g' -e 's/♥/♥/g' -e 's/♦/♦/g' -e 's/♦/♦/g'
return 0
}
# transforms a string
function text_filter { # text_filter filters[@]
local FILTERS
local LF TEXT OPTIONS TEXT_TEMP DATE_FORMAT MAX_CHARS MAX_PARAS WRAP
LF=$'\n'
TEXT=$(</dev/stdin)
FILTERS=("${@}")
for FILTER in ${FILTERS[@]}; do
OPTIONS="${FILTER#*:}"
if [[ "${OPTIONS}" == "${FILTER}" ]]; then
OPTIONS=""
fi
OPTIONS=(${OPTIONS//:/ })
case "${FILTER}" in
date:*)
[[ "${TEXT}" == "" ]] && return 1
DATE_FORMAT="${OPTIONS[0]:-}"
if [[ "${DATE_FORMAT}" != "" ]]; then
TEXT_TEMP="${TEXT}"
if [[ "${#TEXT_TEMP}" -eq 10 ]]; then
TEXT_TEMP="${TEXT_TEMP} 00:00:00"
fi
case "${DATE_FORMAT}" in
[Rr][Ff][Cc]3339)
TEXT_TEMP=$(TZ=UTC date -jf '%Y-%m-%d %H:%M:%S' '+%Y-%m-%dT%T%z' "${TEXT_TEMP}" 2>/dev/null | sed -E -e 's/([0-9][0-9])$/:\1/')
[[ $? -ne 0 ]] && return 1
;;
esac
TEXT="${TEXT_TEMP}"
fi
;;
escape:entities)
TEXT=$(echo "${TEXT}" | decode_entities);
;;
escape:html)
TEXT=$(echo "${TEXT}" | decode_entities);
TEXT=$(echo "${TEXT}" | sed -e 's/&/\&/g' -e 's/</\</g' -e 's/>/\>/g' -e 's/"/\"/g' -e 's/'"'"'/\'/g');
;;
escape:json-val)
TEXT=$(echo "${TEXT}" | sed -e 's/"/\\"/g' | awk '(NR==1) { printf "%s", $0 } (NR>1) { printf "\\n%s", $0 }');
;;
excerpt:*)
MAX_CHARS="${OPTIONS[0]:-}"
MAX_PARAS="${OPTIONS[1]:-}"
if [[ "${MAX_CHARS}" != "" ]]; then
[[ "${MAX_CHARS}" -lt 1 ]] && TEXT="" && break
TEXT=$(echo "${TEXT}" | text_filter headings:remove plaintext)
if [[ "${MAX_PARAS}" != "" ]]; then
TEXT=$(echo "${TEXT}" | awk -v RS="\n\n" 'NR<='${MAX_PARAS}'+1 { print $0 }')
fi
TEXT_TEMP=$(echo "${TEXT:0:$MAX_CHARS}")
if [[ "${TEXT}" != "${TEXT_TEMP}" ]]; then
if [[ "${MAX_CHARS}" -gt 3 ]]; then
TEXT=$(echo "${TEXT_TEMP:0:$((MAX_CHARS-3))}")
else
TEXT=""
fi
TEXT="${TEXT}..."
fi
TEXT=$(echo "${TEXT}${LF}")
fi
;;
excerpt)
TEXT=$(echo "${TEXT}" | text_filter excerpt:300:2)
;;
headings:push)
TEXT=$(echo "${TEXT}${LF}" | sed -E -e 'N;s/^(.*)\n([-]+|[=]+)$/# \1/' | sed -E -e 's/^([#]+ )/#\1/' -e 's/^####### (.+)$/**\1**/')
TEXT=$(echo "${TEXT}" | sed -E -e '/<\/?h[0-6][^>]*>/{s/<(\/)?h6/<\1strong/g' -e 's/<(\/)?h5/<\1h6/g' -e 's/<(\/)?h4/<\1h5/g' -e 's/<(\/)?h3/<\1h4/g' -e 's/<(\/)?h2/<\1h3/g' -e 's/<(\/)?h1/<\1h2/g' -e '}')
;;
headings:shift)
TEXT=$(echo "${TEXT}${LF}" | sed -E -e 'N;s/^(.*)\n([-]+|[=]+)$/# \1/' | sed -E -e '/^# .+$/d' -e 's/^#([#]+ )/\1/')
TEXT=$(echo "${TEXT}" | sed -E -e '/<h1[^>]*>.*<\/h1>/s///g' | sed -E -e '/<\/?h[0-6][^>]*>/{s/<(\/)h2/<\1h1/g' -e 's/<(\/)h3/<\1h2/g' -e 's/<(\/)h4/<\1h3/g' -e 's/<(\/)h5/<\1h4/g' -e 's/<(\/)h6/<\1h5/g' -e '}')
# '/./,$!d' deletes blank lines at start
TEXT=$(echo "${TEXT}" | sed -e '/./,$!d')
;;
headings:remove)
TEXT=$(echo "${TEXT}${LF}" | sed -E -e 'N;s/^(.*)\n([-]+|[=]+)$/# \1/' | sed -E -e '/^[#]+ .+$/d')
TEXT=$(echo "${TEXT}" | sed -E -e 's/<(h1|h2|h3|h4|h5|h6)[^>]*>.*<\/(h1|h2|h3|h4|h5|h6)>//g')
;;
plaintext)
# ':a;s/<[^>]*>//g;/</N;//ba' strips html tags and works across multiple lines (better version of 's/<[^>]*>//g')
# '/./,/^$/!d' deletes blank lines at start and collapses consecutive blank lines into one, leaving up to one blank line at end
TEXT=$(echo "${TEXT}" | sed -e ':a' -e 's/<[^>]*>//g' -e '/</N' -e '//ba' | sed '/./,/^$/!d')
TEXT=$(echo "${TEXT}" | decode_entities)
;;
slug)
TEXT=$(echo "${TEXT}" | head -n 1 | sed -E -e 's/[ _]+/-/g' -e 's/[^-a-zA-Z0-9.]//g' -e 's/(^-+)|(-+$)//' | awk '{ print tolower($0) }')
;;
wrap:*)
WRAP="${OPTIONS[0]}"
TEXT=$(echo "<!-- ${WRAP} -->${LF}${TEXT}${LF}<!-- /${WRAP} -->${LF}")
;;
esac
done
echo "${TEXT}"
}
# prints a list of variables given their assignment commands, one per line, e.g. FOO="bar" prints FOO
function get_variables { # get_variables assignments_string
local STRING
STRING=$(</dev/stdin)
echo "${STRING}" | sed -E -e 's/^([^=]+[^+])[+]?=.*/\1/' | sort -u
}
# tests whether existing file or directory is child of parent directory
function is_child { # is_child directory child
local CURRENT_DIR DIRECTORY CHILD CD
CURRENT_DIR=$(pwd)
DIRECTORY="${1:-}"
[[ ! -d "${DIRECTORY}" ]] && return 1
DIRECTORY="${DIRECTORY%/}/"
CHILD="${2:-}"
if [[ -d "${CHILD}" ]]; then
CHILD_DIR="${CHILD%/}/"
elif [[ -f "${CHILD}" ]]; then
CHILD_DIR="${CHILD%/*}/"
else
return 1
fi
# parent dir
cd "${DIRECTORY}" >/dev/null 2>&1 || return 1
DIRECTORY=$(pwd -P . 2>/dev/null || command pwd)
cd "${CURRENT_DIR}"
# child dir
cd "${CHILD_DIR}" >/dev/null 2>&1 || return 1
CHILD_DIR=$(pwd -P . 2>/dev/null || command pwd)
cd "${CURRENT_DIR}"
# compare
if [[ -f "${CHILD}" ]] && [[ "${DIRECTORY}" == "${CHILD_DIR}" ]]; then
# file in directory root
return 0
fi
if [[ "${DIRECTORY}" != "${CHILD_DIR}" ]] && [[ "${CHILD_DIR#$DIRECTORY}" != "${CHILD_DIR}" ]]; then
# subdirectory or file in subdirectory
return 0
fi
return 1
}
# prints an integer transformation of a version string
# useful for simple comparisons, max input 999.999.999
# [[ $(version 2.10.9) -gt $(version 1.0.5) ]] # true
# [[ 2010009 -gt 1000005 ]] # true
function version { # version x(.x)?(.x)?
echo $(printf "%03d%03d%03d\n" $(echo "${1:-}" | sed -E -e 's/^([0-9]+(\.[0-9]+(\.[0-9]+)?)?).*$/\1/' | tr '.' '\n' | head -n 3)) | sed 's/^0*//' | sed 's/^$/0/'
}
# checks for required external tool
function dependency_exists { # dependency_exists dep
local DEP ERROR
DEP="${1:-}"; [[ "${DEP}" == "" ]] && return 1
ERROR=false
if echo "${DEP}" | grep '/' >/dev/null 2>&1 && [[ ! -x "${DEP}" ]]; then
echo "Unable to find command: ${DEP}" >&2
ERROR=true
elif ! hash "${DEP}" >/dev/null 2>&1; then
echo "Unable to find command: ${DEP}" >&2
ERROR=true
fi
[[ "${ERROR}" == true ]] && return 1
return 0
}
# checks for required external tools (plural)
function dependencies_exist { # dependencies_exist deps[@]
local DEPS ERRORS
DEPS=("${@}");
ERRORS=()
for DEP in ${DEPS[@]}; do
dependency_exists "${DEP}" 2>/dev/null || ERRORS+=("${DEP}")
done
if [[ "${#ERRORS[@]}" -ne 0 ]]; then
echo "Unable to find command(s): ${ERRORS[*]}" >&2
return 1
fi
return 0
}
# prints dependency requirements
function print_dependency { # print_dependency name url required_version found_version
local NAME URL VERSION FOUND
NAME="${1:-}"; [[ "${NAME}" == "" ]] && return 1
URL="${2:-}"
VERSION="${3:-}"
FOUND="${4:-}"
echo -n "${NAME}" >&2
if [[ "${VERSION}" != "" ]]; then
echo -n " version ${VERSION} or later" >&2
fi
echo -n " is required" >&2
if [[ "${FOUND}" != "" ]]; then
echo -n " (found version ${FOUND})" >&2
fi
if [[ "${URL}" != "" ]]; then
echo -n ": ${URL}" >&2
fi
echo >&2
}
# prints bash variable assignments for template metadata
function parse_template_metadata { # parse_template_metadata prefix template_dir template_config_extra
local PREFIX TEMPLATE_DIR TEMPLATE_CONFIG_EXTRA
local TEMPLATE_META TEMPLATE_META_VARS
local TEMPLATE_ASSETS_FONTS TEMPLATE_ASSETS_STYLES TEMPLATE_ASSETS_SCRIPTS TEMPLATE_ASSETS_IMAGES TEMPLATE_ASSETS_AUDIO TEMPLATE_ASSETS_VIDEO TEMPLATE_ASSETS_DOCUMENTS TEMPLATE_ASSETS_OTHER
local INDEX ASSET_TYPES ASSET_TYPE_UPPER ASSET FILTER_INDEX REGEX EXTENDED MATCH REPLACE EXTRA ASSETS_INDEX ASSETS ASSETS_TEMP
# init
PREFIX="${1:-}"
TEMPLATE_DIR="${2:-}"
[[ "${TEMPLATE_DIR}" == "" ]] && return 1
TEMPLATE_CONFIG_EXTRA="${3:-}"
# load config
if [[ -f "${TEMPLATE_DIR}/template.yaml" ]] && [[ -r "${TEMPLATE_DIR}/template.yaml" ]]; then
TEMPLATE_META=$(parse_yaml "${TEMPLATE_DIR}/template.yaml" "MARSH_TEMPLATE_")
[[ $? -ne 0 ]] && echo "Unable to parse yaml file: ${TEMPLATE_DIR}/template.yaml" >&2 && return 1
TEMPLATE_META_VARS=$(echo "${TEMPLATE_META}" | get_variables)
eval "${TEMPLATE_META}"
else
echo "Template configuration file not found: ${TEMPLATE_DIR}/template.yaml" >&2
return 1
fi
if [[ "${TEMPLATE_CONFIG_EXTRA}" != "" ]]; then
# load extra config
if [[ -f "${TEMPLATE_CONFIG_EXTRA}" ]] && [[ -r "${TEMPLATE_CONFIG_EXTRA}" ]]; then
TEMPLATE_META=$(parse_yaml "${TEMPLATE_CONFIG_EXTRA}" "MARSH_TEMPLATE_")
[[ $? -ne 0 ]] && echo "Unable to parse yaml file: ${TEMPLATE_CONFIG_EXTRA}" >&2 && return 1
TEMPLATE_META_VARS=$(echo "${TEMPLATE_META_VARS}"$'\n'"${TEMPLATE_META}" | get_variables)
eval "${TEMPLATE_META}"
else
echo "Template configuration file not found: ${TEMPLATE_CONFIG_EXTRA}" >&2
return 1
fi
fi
# print partials paths relative to template dir
[[ "${MARSH_TEMPLATE_Template_Partials_Base:-}" != "" ]] && echo "${PREFIX}Template_Partials_Base=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Base//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Body:-}" != "" ]] && echo "${PREFIX}Template_Partials_Body=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Body//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Document:-}" != "" ]] && echo "${PREFIX}Template_Partials_Document=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Document//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Header:-}" != "" ]] && echo "${PREFIX}Template_Partials_Header=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Header//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Nav:-}" != "" ]] && echo "${PREFIX}Template_Partials_Nav=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Nav//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Notice:-}" != "" ]] && echo "${PREFIX}Template_Partials_Notice=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Notice//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Footer:-}" != "" ]] && echo "${PREFIX}Template_Partials_Footer=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Footer//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Redirect:-}" != "" ]] && echo "${PREFIX}Template_Partials_Redirect=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Redirect//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Archive_HTML_Body:-}" != "" ]] && echo "${PREFIX}Template_Partials_Archive_HTML_Body=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Archive_HTML_Body//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Archive_HTML_Document:-}" != "" ]] && echo "${PREFIX}Template_Partials_Archive_HTML_Document=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Archive_HTML_Document//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Archive_Atom_Body:-}" != "" ]] && echo "${PREFIX}Template_Partials_Archive_Atom_Body=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Archive_Atom_Body//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Archive_Atom_Document:-}" != "" ]] && echo "${PREFIX}Template_Partials_Archive_Atom_Document=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Archive_Atom_Document//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Archive_JSON_Body:-}" != "" ]] && echo "${PREFIX}Template_Partials_Archive_JSON_Body=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Archive_JSON_Body//\"/\\\"}\""
[[ "${MARSH_TEMPLATE_Template_Partials_Archive_JSON_Document:-}" != "" ]] && echo "${PREFIX}Template_Partials_Archive_JSON_Document=\"${TEMPLATE_DIR}/${MARSH_TEMPLATE_Template_Partials_Archive_JSON_Document//\"/\\\"}\""
# assets
ASSET_TYPES=('Fonts' 'Styles' 'Scripts' 'Images' 'Audio' 'Video' 'Documents' 'Binaries' 'Other')
for ASSET_TYPE in ${ASSET_TYPES[@]}; do
eval "TEMPLATE_ASSETS_${ASSET_TYPE}=()"
INDEX=0
ASSET="MARSH_TEMPLATE_Template_Assets_${ASSET_TYPE}_${INDEX}"
ASSET="${!ASSET:-}"
while [[ "${ASSET}" != "" ]]; do
eval "TEMPLATE_ASSETS_${ASSET_TYPE}+=(\"${ASSET//\"/\\\"}\")"
INDEX=$((INDEX+1))
ASSET="MARSH_TEMPLATE_Template_Assets_${ASSET_TYPE}_${INDEX}"
ASSET="${!ASSET:-}"
done
done
# assets filters
FILTER_INDEX=0
REGEX="MARSH_TEMPLATE_Template_Filters_${FILTER_INDEX}_Regex"
REGEX="${!REGEX:-}"
EXTENDED="MARSH_TEMPLATE_Template_Filters_${FILTER_INDEX}_Extended"
EXTENDED="${!EXTENDED:-}"
# loop over assets filters
while [[ "${REGEX}" != "" ]]; do
# escape match
if [[ "$(echo ${REGEX} | grep -E '^s/.+[^\]/.*[^\]/g?$')" == "${REGEX}" ]]; then
MATCH=$(echo "${REGEX}" | sed -E 's/^s\/(.+[^\])\/.*[^\]\/g?/\1/' | sed 's/[[]\.|$(){}?+*^]/\\&/g')
REPLACE=$(echo "${REGEX}" | sed -E 's/^s\/.+[^\]\/(.*[^\])\/g?/\1/')
EXTRA=$(echo "${REGEX}" | sed -E 's/^s\/.+[^\]\/.*[^\]\/(g?)/\1/')
REGEX="s/${MATCH}/${REPLACE}/${EXTRA}"
elif [[ "$(echo ${REGEX} | grep -E '^s\|.+[^\]\|.*[^\]\|g?$')" == "${REGEX}" ]]; then
MATCH=$(echo "${REGEX}" | sed -E 's/^s\|(.+[^\])\|.*[^\]\|g?/\1/' | sed 's/[[]\.|$(){}?+*^]/\\&/g')
REPLACE=$(echo "${REGEX}" | sed -E 's/^s\|.+[^\]\|(.*[^\])\|g?/\1/')
EXTRA=$(echo "${REGEX}" | sed -E 's/^s\|.+[^\]\|.*[^\]\|(g?)/\1/')
REGEX="s|${MATCH}|${REPLACE}|${EXTRA}"
elif [[ "$(echo ${REGEX} | grep -E '^/.+[^\]/d?$')" == "${REGEX}" ]]; then
MATCH=$(echo "${REGEX}" | sed -E 's/^\/(.+[^\])\/d?/\1/' | sed 's/[[]\.|$(){}?+*^]/\\&/g')
REPLACE=""
EXTRA=$(echo "${REGEX}" | sed -E 's/^\/.+[^\]\/(d?)/\1/')
REGEX="/${MATCH}/${EXTRA}"
else
continue
fi
ASSETS_INDEX=0
ASSETS_TYPE="MARSH_TEMPLATE_Template_Filters_${FILTER_INDEX}_Assets_${ASSETS_INDEX}"
ASSETS_TYPE="${!ASSETS_TYPE}"
# loop over assets types for filter
while [[ "${ASSETS_TYPE}" != "" ]]; do
ASSETS="TEMPLATE_ASSETS_${ASSETS_TYPE}[@]"
ASSETS=(${!ASSETS})
ASSETS_TEMP=()
# loop over all individual assets of this type
for ASSET in ${ASSETS[@]}; do
# do regex
if [[ "${EXTENDED}" == true ]]; then
ASSET=$(echo "${ASSET}" | sed -E "${REGEX}" 2>/dev/null)
else
ASSET=$(echo "${ASSET}" | sed "${REGEX}" 2>/dev/null)
fi
# remove empty
if [[ "${ASSET:-}" != "" ]]; then
ASSETS_TEMP+=("${ASSET}")
fi
done
# update original array
eval "TEMPLATE_ASSETS_${ASSETS_TYPE}=()"
for I in "${!ASSETS_TEMP[@]}"; do
eval "TEMPLATE_ASSETS_${ASSETS_TYPE}+=(\"${ASSETS_TEMP[$I]//\"/\\\"}\")"
done
ASSETS_INDEX=$((ASSETS_INDEX+1))
ASSETS_TYPE="MARSH_TEMPLATE_Template_Filters_${FILTER_INDEX}_Assets_${ASSETS_INDEX}"
ASSETS_TYPE="${!ASSETS_TYPE:-}"
done
FILTER_INDEX=$((FILTER_INDEX+1))
REGEX="MARSH_TEMPLATE_Template_Filters_${FILTER_INDEX}_Regex"
REGEX="${!REGEX:-}"
EXTENDED="MARSH_TEMPLATE_Template_Filters_${FILTER_INDEX}_Extended"
EXTENDED="${!EXTENDED:-}"
ASSETS_INDEX=0
ASSETS_TYPE="MARSH_TEMPLATE_Template_Filters_${FILTER_INDEX}_Assets_${ASSETS_INDEX}"
ASSETS_TYPE="${!ASSETS_TYPE:-}"
done
# print assets
for ASSET_TYPE in ${ASSET_TYPES[@]}; do
ASSET_TYPE_UPPER=$(echo "${ASSET_TYPE}" | awk '{ print toupper($0) }')
ASSETS="TEMPLATE_ASSETS_${ASSET_TYPE}[@]"
ASSETS=(${!ASSETS})
echo "${PREFIX}ASSETS_${ASSET_TYPE_UPPER}=()"
for ASSET in ${ASSETS[@]}; do
[[ "${ASSET:-}" != "" ]] && echo "${PREFIX}ASSETS_${ASSET_TYPE_UPPER}+=(\"${ASSET//\"/\\\"}\")"
done
done
# unset temporary global variables
for VAR in ${TEMPLATE_META_VARS[@]}; do unset "${VAR}"; done
}
# loads target metadata into convenience variables
function get_target_metadata { # get_target_metadata source_prefix prefix
local SOURCE_PREFIX PREFIX
local TARGET_PATH TARGET_NAME TEMPLATE_SOURCE TARGET_TYPE TEMPLATE_REMOTE TARGET_FORCE TEMPLATE_FETCH TARGET_NAV
local II TEMPLATE_SOURCE TEMPLATE_REMOTE TEMPLATE_FETCH TEMPLATE_CONFIG
local ARCHIVE ARCHIVE_NAME ARCHIVE_ENCODING ARCHIVE_COUNT ARCHIVE_CONTENT ARCHIVE_TYPES ARCHIVE_EXCLUDED_STATES
# init
SOURCE_PREFIX="${1:-}"
PREFIX="${2:-}"
# root
TARGET_PATH="${SOURCE_PREFIX}Path"
[[ -z ${!TARGET_PATH+isset} ]] && return 1
TARGET_NAME="${SOURCE_PREFIX}Name"
TARGET_SOURCE="${SOURCE_PREFIX}Source"
TARGET_TYPE="${SOURCE_PREFIX}Type"
TARGET_REMOTE="${SOURCE_PREFIX}Remote"
TARGET_FORCE="${SOURCE_PREFIX}Force"
TARGET_FETCH="${SOURCE_PREFIX}Fetch"
TARGET_NAV="${SOURCE_PREFIX}Navigation"
[[ "${!TARGET_PATH:-}" != "" ]] && echo "${PREFIX}PATH=\"${!TARGET_PATH//\"/\\\"}\""
[[ "${!TARGET_NAME:-}" != "" ]] && echo "${PREFIX}NAME=\"${!TARGET_NAME//\"/\\\"}\""
[[ "${!TARGET_SOURCE:-}" != "" ]] && echo "${PREFIX}SOURCE=\"${!TARGET_SOURCE//\"/\\\"}\""
[[ "${!TARGET_TYPE:-}" != "" ]] && echo "${PREFIX}TYPE=\"${!TARGET_TYPE//\"/\\\"}\""
[[ "${!TARGET_REMOTE:-}" != "" ]] && echo "${PREFIX}REMOTE=\"${!TARGET_REMOTE//\"/\\\"}\""
[[ "${!TARGET_NAV:-}" != "" ]] && echo "${PREFIX}NAV=\"${!TARGET_NAV//\"/\\\"}\""
[[ "${!TARGET_FORCE:-}" != "" ]] && echo "${PREFIX}FORCE=\"${!TARGET_FORCE//\"/\\\"}\""
[[ "${!TARGET_FETCH:-}" != "" ]] && echo "${PREFIX}FETCH=\"${!TARGET_FETCH//\"/\\\"}\""
# templates
echo "${PREFIX}TEMPLATES_SOURCE=()"
echo "${PREFIX}TEMPLATES_REMOTE=()"
echo "${PREFIX}TEMPLATES_FETCH=()"
echo "${PREFIX}TEMPLATES_CONFIG=()"
echo "${PREFIX}TEMPLATES=()"
II=0
TEMPLATE_SOURCE="${SOURCE_PREFIX}Templates_${II}_Source"
TEMPLATE_REMOTE="${SOURCE_PREFIX}Templates_${II}_Remote"
TEMPLATE_FETCH="${SOURCE_PREFIX}Templates_${II}_Fetch"
TEMPLATE_CONFIG="${SOURCE_PREFIX}Templates_${II}_Config"
while [[ "${!TEMPLATE_SOURCE:-}" != "" ]]; do
echo "${PREFIX}TEMPLATES_SOURCE+=(\"${!TEMPLATE_SOURCE//\"/\\\"}\")"
echo "${PREFIX}TEMPLATES_REMOTE+=(\"${!TEMPLATE_REMOTE//\"/\\\"}\")"
if [[ "${!TEMPLATE_FETCH:-}" == "true" ]]; then
echo "${PREFIX}TEMPLATES_FETCH+=(true)"
else
echo "${PREFIX}TEMPLATES_FETCH+=(false)"
fi
echo "${PREFIX}TEMPLATES_CONFIG+=(\"${!TEMPLATE_CONFIG//\"/\\\"}\")"
echo "${PREFIX}TEMPLATES+=(\"${!TEMPLATE_SOURCE##*/}-$(md5_string ${!TEMPLATE_REMOTE:-local}:${!TEMPLATE_SOURCE})\")"
II=$((II+1))
TEMPLATE_SOURCE="${SOURCE_PREFIX}Templates_${II}_Source"
TEMPLATE_REMOTE="${SOURCE_PREFIX}Templates_${II}_Remote"
TEMPLATE_FETCH="${SOURCE_PREFIX}Templates_${II}_Fetch"
TEMPLATE_CONFIG="${SOURCE_PREFIX}Templates_${II}_Config"
done
# archives
echo "${PREFIX}ARCHIVES=()"
echo "${PREFIX}ARCHIVES_ENCODING=()"
echo "${PREFIX}ARCHIVES_NAME=()"
echo "${PREFIX}ARCHIVES_COUNT=()"
echo "${PREFIX}ARCHIVES_CONTENT=()"
echo "${PREFIX}ARCHIVES_TYPES=()"
echo "${PREFIX}ARCHIVES_EXCLUDED_STATES=()"
II=0
ARCHIVE="${SOURCE_PREFIX}Archives_${II}_Path"
ARCHIVE_ENCODING="${SOURCE_PREFIX}Archives_${II}_Encoding"
ARCHIVE_NAME="${SOURCE_PREFIX}Archives_${II}_Name"
ARCHIVE_COUNT="${SOURCE_PREFIX}Archives_${II}_Count"
ARCHIVE_CONTENT="${SOURCE_PREFIX}Archives_${II}_Content"
while [[ "${!ARCHIVE:-}" != "" ]]; do
echo "${PREFIX}ARCHIVES+=(\"${!ARCHIVE//\"/\\\"}\")"
echo "${PREFIX}ARCHIVES_ENCODING+=(\"${!ARCHIVE_ENCODING//\"/\\\"}\")"
echo "${PREFIX}ARCHIVES_NAME+=(\"${!ARCHIVE_NAME//\"/\\\"}\")"
echo "${PREFIX}ARCHIVES_COUNT+=(\"${!ARCHIVE_COUNT//\"/\\\"}\")"
echo "${PREFIX}ARCHIVES_CONTENT+=(\"${!ARCHIVE_CONTENT//\"/\\\"}\")"
eval $(flat_to_array "ARCHIVE_TYPES" "${SOURCE_PREFIX}Archives_${II}_Types_")
OIFS="${IFS}"; IFS=$(echo @|tr @ '\034'); echo "${PREFIX}ARCHIVES_TYPES+=(\"${ARCHIVE_TYPES[*]//\"/\\\"}\")"; IFS="${OIFS}"
eval $(flat_to_array "ARCHIVE_EXCLUDED_STATES" "${SOURCE_PREFIX}Archives_${II}_Exclude_")
OIFS="${IFS}"; IFS=$(echo @|tr @ '\034'); echo "${PREFIX}ARCHIVES_EXCLUDED_STATES+=(\"${ARCHIVE_EXCLUDED_STATES[*]//\"/\\\"}\")"; IFS="${OIFS}"
II=$((II+1))
ARCHIVE="${SOURCE_PREFIX}Archives_${II}_Path"
ARCHIVE_ENCODING="${SOURCE_PREFIX}Archives_${II}_Encoding"
ARCHIVE_NAME="${SOURCE_PREFIX}Archives_${II}_Name"
ARCHIVE_COUNT="${SOURCE_PREFIX}Archives_${II}_Count"
ARCHIVE_CONTENT="${SOURCE_PREFIX}Archives_${II}_Content"
done
return 0
}
# copies files, fetching if necessary and authorized
function get_files { # get_files source dest force fetch uri
local SOURCE DEST FORCE FETCH URI
local SOURCE_DIR CACHE TYPE REMOTE FILES
SOURCE="${1:-}"
DEST="${2:-}"
if [[ "${SOURCE}" == "" ]]; then
echo "Internal error copying files: source empty" >&2
return 1
elif [[ "${DEST}" == "" ]]; then
echo "Internal error copying files: destination empty" >&2
return 1
fi
SOURCE_DIR="${SOURCE%/*}"
SOURCE_DIR="${SOURCE_DIR:-.}"
FORCE="${3:-}"
FETCH="${4:-}"
URI="${5:-}"
CACHE=$(md5_string "${URI}")
[[ "${CACHE}" == "" ]] && CACHE=$(md5_string "${SOURCE}")
CACHE="${MARSH_TEMP}/${CACHE}"
TYPE="file"
REMOTE=false
if [[ "${URI}" != "" ]]; then
if [[ "${URI}" =~ ^(git|https?|ssh): ]] || [[ "${URI}" =~ ^.+@.+: ]]; then
# remote git
TYPE="git"
REMOTE=true
elif [[ "${URI}" =~ ^file: ]] || [[ "${URI}" =~ .git$ ]]; then
# local git
TYPE="git"
fi
fi
if [[ "${REMOTE}" != true ]] || [[ "${FETCH}" == true ]]; then
# fetch unnecessary/approved
if [[ "${TYPE}" != "file" ]] && [[ ! -e "${CACHE}" ]]; then
# cache does not exist, create it
mkdir -p "${CACHE}"
if [[ "${TYPE}" == "git" ]]; then
# clone repository
echo "Retrieving source: ${URI}"
if ! "${GIT}" clone --depth 1 --recursive "${URI}" "${CACHE}" >/dev/null 2>&1; then
# unable to clone repository
echo "Unable to retrieve source: ${URI}" >&2
return 1
fi
fi
fi
if [[ "${TYPE}" == "git" ]]; then
# files path relative to cache
SOURCE="${CACHE}/${SOURCE}"
SOURCE_DIR="${SOURCE%/*}"
SOURCE_DIR="${SOURCE_DIR:-.}"
fi
# select files for copy
if [[ -d "${SOURCE}" ]]; then
# entire directory
FILES="${SOURCE#$SOURCE_DIR/}"
DEST="${DEST%/$FILES}"
FILES=("${FILES}")
else
# directory contents
OIFS="${IFS}"
IFS=$'\n'
FILES=($(cd "${SOURCE_DIR}" >/dev/null 2>&1 && find . -maxdepth 1 -name "${SOURCE##*/}" | sed -e 's/^\.\///' -e 's/^\.$//'))
[[ $? -ne 0 ]] && IFS="${OIFS}" && echo "Unable to change into directory: ${SOURCE_DIR}" >&2 && return 1
IFS="${OIFS}"
fi
# loop over files
for FILE in ${FILES[@]}; do
# sanity
if [[ "${SOURCE_DIR}/${FILE}" == "${DEST%/}/${FILE}" ]]; then
# same source and destination
echo "Source and destination paths are equal: ${DEST}" >&2
return 1
fi
if [[ -e "${DEST%/}/${FILE}" ]] && [[ "${FORCE}" != true ]]; then
# path exists
echo "Path exists (--force to overwrite): ${DEST%/}/${FILE}" >&2
return 1
fi
# create path
DEST="${DEST%/.}"
if ! mkdir -p "${DEST}"; then
echo "Unable to create path: ${DEST}" >&2
return 1
fi
# clean destination
if ! rm -rf "${DEST%/}/${FILE}"; then
echo "Unable to replace path: ${DEST}" >&2
return 1
fi
# copy
if ! cp -R "${SOURCE_DIR}/${FILE}" "${DEST%/}/${FILE}"; then
echo "Unable to copy file: ${SOURCE_DIR}/${FILE} -> ${DEST%/}/${FILE}" >&2
return 1
fi
done
return 0
else
# fetch not explicitly authorized
echo "Remote source not authorized (--fetch to override): ${URI}" >&2
return 1
fi
}
# builds an archive of directory contents, sorted by descending date
function build_archive { # build_archive encoding target_dir dest_file dest_title content_length count #types[@] types[@] #excluded_states[@] excluded_states[@]
local ENCODING TARGET_DIR DEST DEST_SUB_PREFIX DEST_TITLE CONTENT COUNT NUM_TYPES TYPES NUM_EXCLUDED_STATES EXCLUDED_STATES
local VALID DOCUMENTS DOCUMENTS_MAX DOCUMENT_META_PREFIX DOCUMENT_META DOCUMENT_META_VARS
local DOCUMENT TITLE DATE DATE_PRETTY LANGCODE STATE EXCLUDE DEST_SUB URI ABSPATH
# init
ENCODING="${1:-html}"
shift 1
TARGET_DIR="${1:-}"; [[ "${TARGET_DIR}" == "" ]] && return 1
DEST="${2:-}"; [[ "${DEST}" == "" ]] && return 1
DEST="${TARGET_DIR}/${DEST}"
DEST_SUB_PREFIX="${DEST}.${MARSH_UID}.subdocuments"
DEST_TITLE="${3:-}"
CONTENT="${4:-}"
CONTENT_VALID=('excerpt' 'none' '')
if ! in_array "${CONTENT%%:*}" "${CONTENT_VALID[@]}"; then
echo "Invalid archive content type: ${CONTENT%%:*}" >&2
return 1
fi
COUNT="${5:-}"
shift 5
NUM_TYPES="${1:-0}"
shift 1
TYPES=()
while [[ "${NUM_TYPES}" -gt 0 ]] && [[ "${#@}" -gt 0 ]]; do
TYPES+=("${1}")
NUM_TYPES=$((NUM_TYPES - 1 ))
shift
done
NUM_EXCLUDED_STATES="${1:-0}"
shift 1
EXCLUDED_STATES=()
while [[ "${NUM_EXCLUDED_STATES}" -gt 0 ]] && [[ "${#@}" -gt 0 ]]; do
EXCLUDED_STATES+=("${1}")
NUM_EXCLUDED_STATES=$((NUM_EXCLUDED_STATES - 1 ))
shift
done
# create directory
mkdir -p "${DEST%/*}" >/dev/null 2>&1 || return 1
# write yaml header
echo "---" > "${DEST}" 2>/dev/null || return 1
if [[ "${ENCODING}" == "html" ]]; then
echo "Type: archive" >> "${DEST}"
else
echo "Type: archive-${ENCODING}" >> "${DEST}"
fi
echo "Date: $(date -u '+%Y-%m-%d %H:%M:%S')" >> "${DEST}"
echo "Title: ${DEST_TITLE}" >> "${DEST}"
echo "Language: English" >> "${DEST}"
echo "Language_Code: en" >> "${DEST}"
echo "---" >> "${DEST}"
echo "" >> "${DEST}"
if [[ "${ENCODING}" == "html" ]]; then
echo "# ${DEST_TITLE}" >> "${DEST}"
echo "" >> "${DEST}"
fi
# loop over documents
VALID=0
OIFS="${IFS}"
IFS=$'\n'
DOCUMENTS=($(cd "${TARGET_DIR}" && find . -name '*.'"${MARSH_UID}"'.document' | sed 's/^\.\///' | sort -nr -t/))
IFS="${OIFS}"
DOCUMENTS_MAX="${#DOCUMENTS[@]}"
DOCUMENTS_MAX=$((DOCUMENTS_MAX - 1))
for DOCUMENT in "${DOCUMENTS[@]}"; do
if [[ "${COUNT}" != "" ]] && [[ "${VALID}" -ge "${COUNT}" ]]; then
break
fi
DOCUMENT_META_PREFIX=$(echo "marsh_meta_${DOCUMENT}_" | sed -E -e 's/[^A-Za-z0-9_]/_/g')
DOCUMENT_META=$(parse_yaml "${TARGET_DIR}/${DOCUMENT%.document}.meta.yaml" "${DOCUMENT_META_PREFIX}")
[[ $? -ne 0 ]] && echo "Unable to parse yaml file: ${TARGET_DIR}/${DOCUMENT%.document}.meta.yaml" >&2 && return 1
DOCUMENT_META_VARS=$(echo "${DOCUMENT_META}" | get_variables)
eval "${DOCUMENT_META}"
TYPE="${DOCUMENT_META_PREFIX}Type"
TYPE="${!TYPE:-}"
if [[ "${TYPE%%-*}" == "archive" ]]; then
# archives are skipped by default
continue
fi
if [[ "${#TYPES[@]}" -gt 0 ]] && ! in_array "${TYPE}" "${TYPES[@]}"; then
# skip type if not in list
continue
fi
if [[ "${#EXCLUDED_STATES[@]}" -gt 0 ]]; then
STATE="${DOCUMENT_META_PREFIX}State"
STATE="${!STATE}"
if [[ "${STATE}" != "" ]]; then
# deprecated (no brackets)
# Key: foo, bar, baz
STATE=(${STATE//, /})
else
# YAML inline array
# Key: [ foo, bar, baz ]
eval $(flat_to_array "STATE" "${DOCUMENT_META_PREFIX}State_")
fi
for E in "${EXCLUDED_STATES[@]}"; do
if in_array "${E}" "${STATE[@]}"; then
# skip excluded state
continue 2
fi
done
fi
# valid subdocument
VALID=$((VALID + 1))
DEST_SUB="${DEST_SUB_PREFIX}.$((VALID-1))"
# add subdocument title
TITLE="${DOCUMENT_META_PREFIX}Title"
TITLE="${!TITLE:-}"
URI="${TARGET_DIR#$MARSH_CONFIG_PATH}"