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

body() {
	sed 1n
}

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

title() {
	sed '1s/\t.*$//;q'
}

pubdate() {
	sed '1s/^[^\t]*\t//;q'
}

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 "$@"