about summary refs log tree commit diff stats
path: root/shite
blob: ebd9e1e13c86150f17a127882e5bdde060ec9685 (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
#!/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 "$@"
		echo
		echo "$end"
	)" && count=$((count + 1))
}

body() {
	awk 'NR==1{next;}{print;}'
}

phtml() {
	awk 'BEGIN{RS="";}/^[ \t]*</{print;next;}{print "<p>"$0"</p>";}'
}

title() {
	awk 'BEGIN{FS="	";}NR==1{print $1;quit;}'
}

pubdate() {
	awk 'BEGIN{FS="	";}NR==1{print $2;quit;}'
}

filters() {
	body | phtml
}

pages() {
	for file; do
		echo >&2 "$file"
		filters <"$file" |
			expand .template.html >out/"${1%.html}"/index.html
	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() {
	for file; do
		echo >&2 "feed: $file"
		echo "<item>"
		echo " <title>$(title "$file")</title>"
		echo " <link>$DOMAIN/${file%.html}/</link>"
		echo " <guid>$DOMAIN/${file%.html}/</guid>"
		if test -n "$(pubdate "$file")"; then
			echo " <pubDate>$(pubdate "$file")</pubDate>"
		fi
		echo "</item>"
	done | expand .feed.xml >out/feed.xml
}

main() {
	DOMAIN="${SHITE_DOMAIN:-${PWD##*/}}"
	SOURCE="$PWD"
	while getopts d:C: opt; do
		case "$opt" in
		d) DOMAIN="$OPTARG" ;;
		C) SOURCE="$OPTARG" ;;
		*) exit 1 ;;
		esac
	done
	shift "$((OPTIND - 1))"

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

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