Summary Files Commits Branches @commit @diff @stats
path: root / generate.sh
blob: a9efe8c7f54793b42b9cafc6c94fed98c854c634 (plain) (blame)
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
#!/bin/sh

set -e

: ${SRC:=$PWD/src}
: ${DST:=./website}
: ${URL:=https://syndamia.com}
: ${AWK:="mawk"}

: ${HEADER:=$PWD/meta/header.html}
: ${FOOTER:=$PWD/meta/footer.html}
: ${META:=$PWD/meta/meta.tsv}

export SRC DST URL AWK HEADER FOOTER META

NO_FIX=
NO_META=
NO_TINY=
FORCE_GEN=
HIGHLIGHT=
while [ -n "$1" ]
do
	case "$1" in
		(-x|--no_fix)  NO_FIX=y  ;;
		(-m|--no_meta) NO_META=y ;;
		(-t|--no_tiny) NO_TINY=y ;;
		(-f|--force)   FORCE_GEN=y ;;
		(-hi|--highlight)
			if command -v highlight >/dev/null 2>&1
			then
				HIGHLIGHT="$(command -v highlight)"
			else
				echo 'Requested highlighting but could not find "highlight" command!'
				exit 1
			fi
			;;
		(*) echo "Unknown argument '$1'!" ; exit 1 ;;
	esac
	shift
done

export HIGHLIGHT

#== 0. Check for changes ==#

if [ -z "$FORCE_GEN" ]                                        \
	&& [ -d "$DST" ]                                          \
	&& [ -f "$DST/.commit" ]                                  \
	&& command -v git >/dev/null 2>&1                         \
	&& [ -z "$(git status -s)" ]                              \
	&& [ "$(cat "$DST"/.commit)" = "$(git rev-parse HEAD)" ]
then
	echo "Output '$DST' is up to date with website source '$SRC'!"
	echo "Refusing to generate..."
	exit 0
fi

#== 1. Fix hardlinks ==#

if [ -z "$NO_FIX" ]
then
	while read -r folder
	do
		if [ -f "$folder.du" ] && [ ! "$folder.du" -ef "$folder/index.du" ]
		then
			rm "$folder/index.du" >/dev/null 2>&1
			ln "$folder.du" "$folder/index.du"
		fi
	done << EOF
$(find "$SRC" -maxdepth 1 -type d)
EOF

	if [ ! "src/favicon.ico" -ef "src/img/favicon.ico" ]
	then
		rm "src/favicon.ico" >/dev/null 2>&1
		ln "src/img/favicon.ico" "src/favicon.ico"
	fi

	echo "[1/4] Fixed folder/index.md hardlinks"
else
	echo "[1/4] Skipping..."
fi

#== 2. Dunbar source to HTML ==#

mkdir -p "$DST"
rm -r "$DST"/* 2>/dev/null || true
cp -a "$SRC/." "$DST"

/bin/sh ./dunbar/dunbar_awk/dunbar.sh html         \
	-i                                             \
	-e ./ext/pagenav.awk -e ./ext/presentation.awk \
	-e ./ext/highlight.awk                         \
	./ext/global.du                                \
	$(find "$DST" -type f -name "*.du")

date '+%e.%m.%Y %H:%M:%S %Z' > "$DST"/.build

echo "[2/4] Converted '$SRC' to dunbar in '$DST'"

#== 3. Meta handling ==#

if [ -z "$NO_META" ]
then
	$AWK -f ./meta/meta.awk          \
	     -v date="$(date +%Y-%m-%d)" \
	     $(find "$DST" -type f -name "*.html")

	if command -v git >/dev/null 2>&1
	then
		git rev-parse HEAD > "$DST"/.commit
	fi

	echo "[3/4] Added HTML header/footer, generated sitemap"
else
	echo "[3/4] Skipping"
fi

#== 4. Tiny version, strip away styles and other stuff ==#

if [ -z "$NO_TINY" ]
then
	mkdir -p "$DST.tiny"
	rm -r "$DST.tiny"/* 2>/dev/null || true
	cp -a "$DST/." "$DST.tiny"
	rm -rf "$DST.tiny"/*.css "$DST.tiny"/fonts/

	sed -i                                      \
	   -e '/rel="stylesheet"/d'                 \
	   -e '/<script/d'                          \
	   -e '/id="settingsWrapper"/,/<\/nav>/d'   \
	   -e '/id="scrollBtn"/d'                   \
	$(find "$DST.tiny" -type f -name "*.html")

	echo "[4/4] Created tiny website in '$DST.tiny'"
else
	echo "[4/4] Skipping..."
fi