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

dog() {
	while IFS= read -r line; do
		printf '%s\n' "$line"
	done
}

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

body() {
	# because the metadata is in a comment, we can just
	dog
	# the whole input.
}

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

meta() {
	tmp="/tmp/$file.meta"
	echo "$tmp" >>/tmp/shite
	trap 'rm /tmp/shite' EXIT INT
	test -f "$tmp" || sed -n '/<!--/,/-->/p' >"$tmp"
	sed -n "s/^$1:[ \t]*//p" "$tmp"
}

title() {
	meta title
}

pubdate() {
	meta pubdate
}

filters() {
	body | phtml
}

pages() {
	for file; do
		echo >&2 "$file"
		mkdir -p "$OUT"/"${file%.htm}"
		filters <"$file" |
			expand .template.html >"$OUT"/"${file%.htm}"/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%.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() {
	dog <<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}
}

main() {
	DOMAIN="${SHITE_DOMAIN:-${PWD##*/}}"
	SOURCE="$PWD"
	OUT=out
	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"
	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 "$@"