about summary refs log tree commit diff stats
path: root/shite
blob: 951498b4f557922ca11136647095054d38c33915 (plain)
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
#!/bin/sh
# shite --- shit out a site
# by C. Duckworth <acdw@acdw.net>

expand() {
	end="expand_$(date +%s)_${count:=0}"
	eval "$(
		echo "cat <<$end"
		cat "$1"
		echo
		echo "$end"
	)" && count=$((count + 1))
}

phtml() {
	sed -e '/./{H;1h;$!d;};x;s,^[ \n\t]\+,,;s,^[^<].*,<p>&</p>,'
}

meta() {
	metaf="/tmp/$file.meta"
	echo "$metaf" >>"$RMF"
	test -f "$metaf" ||
		sed '/<!--/n;/-->/q' >"$metaf"
	sed -n "s/^[ \t]*$1:[ \t]*//p" <"$metaf"
}

title() {
	meta title
}

pubdate() {
	meta pubdate
}

filters() {
	phtml
}

pages() {
	for file; do
		echo >&2 "[build] $file"
		outd="$OUT/${file%.htm}"
		outf="$outd/index.html"
		datf="/tmp/$file.dat"
		echo "$datf" >>"$RMF"
		mkdir -p "$outd"
		filters <"$file" >"$datf"
		expand .template.html <"$datf" >"$outf"
	done
}

index() {
	for file; do
		echo >&2 "[index] $file"
		echo "<li><a href=\"$file\">$(title "$file")</a></li>"
	done | expand .index.html >"$OUT"/index.html
}

feed() { # generates RSS 2.0
	for file; do
		echo >&2 "[feed] $file"
		echo "<item>"
		echo " <title>$(title "$file")</title>"
		echo " <link>$DOMAIN/${file%.htm}/</link>"
		echo " <guid>$DOMAIN/${file%.htm}/</guid>"
		if test -n "$(pubdate "$file")"; then
			echo " <pubDate>$(pubdate "$file")</pubDate>"
		fi
		echo "</item>"
	done | expand .feed.xml >"$OUT"/feed.xml
}

usage() {
	cat <<EOF
shite: shit out a site
by Case Duckworth <acdw@acdw.net>
:: USAGE ::
shite [-h]
shite [-d DOMAIN] [-C DIR] [-o DIR]
:: FLAGS ::
-h         Show this help and exit
:: OPTIONS ::
-d DOMAIN    Domain name to use as the base for URLs.
               Default: \$PWD
-C DIR       Directory of source files for website.
               Default: \$PWD
-o DIR       Directory for output files.  Will be created.
               Default: \$PWD/out
EOF
	exit ${1:-0}
}

cleanup() {
	while read f; do
		rm "$f" >/dev/null 2>&1
	done <"$RMF"
	rm "$RMF"
}

main() {
	trap cleanup EXIT INT
	DOMAIN="${SHITE_DOMAIN:-${PWD##*/}}"
	SOURCE="$PWD"
	OUT=out
	RMF=/tmp/shite.rm
	while getopts d:C:o:h opt; do
		case "$opt" in
		d) DOMAIN="$OPTARG" ;;
		C) SOURCE="$OPTARG" ;;
		o) OUT="$OPTARG" ;;
		h) usage ;;
		*) usage 1 ;;
		esac
	done
	shift "$((OPTIND - 1))"

	cd "$SOURCE"
	mkdir -p "$OUT"
	alias body=cat
	test -f ./.shite.sh && . ./.shite.sh
	pages *.htm
	index *.htm
	feed *.htm
	for file in *; do
		test "${file#*.}" = htm && continue
		test "${file#*.}" = xml && continue
		test "$file" = "$OUT" && continue
		cp -r "$file" "$OUT"/
	done
}

test -n "$DEBUG" && set -x
test -n "$SOURCE" || main "$@"