about summary refs log tree commit diff stats
path: root/scripts/compile.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/compile.sh')
-rw-r--r--scripts/compile.sh85
1 files changed, 85 insertions, 0 deletions
diff --git a/scripts/compile.sh b/scripts/compile.sh new file mode 100644 index 0000000..5478410 --- /dev/null +++ b/scripts/compile.sh
@@ -0,0 +1,85 @@
1#!/bin/bash
2# vim: fdm=marker
3
4getMeta() { # getMeta <metafield> <file> #{{{
5 local field="$1" file="$2";
6 sed -n '/^---$/,/^\.\.\.$/p' $file | \
7 grep "^${field}:" | \
8 cut -d' ' -f2- \
9 || return 1;
10} #}}}
11firstLineOf() { # firstLineOf <file> #{{{
12 local endOfYaml=$(sed -n '/^\.\.\.$/=' "$1")
13 local tryLine="" tryNum=$((endOfYaml + 1))
14 while [[ -z $tryLine ]]; do
15 tryLine=$(sed -n -e "${tryNum}p" "$1" | \
16 sed -e 's/^[|>] //' \
17 -e 's/[][]//g' \
18 -e 's/^#.*//' \
19 -e 's/^--.*//');
20 (( tryNum ++ ));
21 done
22 echo "$tryLine";
23} #}}}
24backLinksOf() { # backLinksOf <file> <in files> #{{{
25 local search="${1}"; shift; local glob="$@";
26 grep -ql "$search" $glob || return 1;
27} #}}}
28
29FILE="$1"; shift;
30case "$FILE" in
31 *.back) # backlinks: compile.sh a.back a.txt #{{{
32 found=( $(backLinksOf "$(basename ${FILE%.*}).html" ${1%/*}/*.${1##*.}) );
33 if [[ $? -ne 0 ]]; then
34 echo "__ISLAND__"; exit;
35 fi
36 for f in "${found[@]}"; do
37 echo -n "- [$(getMeta title "$f")](../$(basename ${f%.*}).html)";
38 echo -n '<span class="daisy">'
39 echo -n "[&phi;]($(basename ${f%.*}).html)";
40 echo '</span>'
41 done
42 ;; #}}}
43 *island*) # islands: compile.sh islands.txt *.txt #{{{
44 for f in $(grep -l "__ISLAND__" "$@"); do
45 echo "- [$(getMeta title "$f")]($(basename ${f%.*}).html)";
46 done
47 ;; #}}}
48 *hapax*) # hapax: compile.sh hapax.txt *.hapax #{{{
49 for word in $(sort $FILE); do
50 f=$(grep -liwq "^$word$" "$@");
51 grep -qv "^[^0-9A-Za-z]" <<< $word && \
52 echo "[$word]($(basename ${f%.*}).html)";
53 done
54 ;; #}}}
55 *first-*) # first-lines: compile.sh first-lines.txt *.txt #{{{
56 for f in "$@"; do
57 echo "[$(firstLineOf "$f")]($(basename ${f%.*}).html)";
58 done
59 ;; #}}}
60 *common-*) # common-titles: compile.sh common-titles.txt *.txt #{{{
61 for f in "$@"; do
62 echo "[$(getMeta title "$f")]($(basename ${f%.*}).html)";
63 done
64 ;; #}}}
65 *toc*) # table of contents: compile.sh toc.txt *.txt #{{{
66 for f in "$@"; do
67 echo "#. [$(getMeta toc "$f")]($(basename ${f%.*}).html)" >> $FILE;
68 done
69 ;; #}}}
70 *random*) # randomize.js: compile.sh randomize.js *.html #{{{
71 rlist=$(echo "$@" | sed -e 's/\(\S\+.html\) \?/"\1",/g' \
72 -e 's/^\(.*\),$/var files=[\1]/')
73 sed -i "s/var files=.*/$rlist/" $FILE;
74 ;; #}}}
75 --fix-head) # fix backlink head: compile.sh --fix-head a.back a.txt #{{{
76 title="$(getMeta title "$2")";
77 id="$(getMeta id "$2")";
78 sed -i "s/__TITLE__/$title/" "$1";
79 sed -i "s/__ID__/$id/" "$1";
80 ;; #}}}
81 *) # bad argument {{{
82 echo "Bad argument";
83 exit 1;
84 ;; #}}}
85esac