summary refs log tree commit diff stats
path: root/ll
blob: 6ce90db1724b20610dc2c282e0f0e057b98575fc (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
#!/bin/sh

# Config
BASE_URL=https://www.acdw.net/lists
SITE_TITLE="a listlog"
SITE_COPYRIGHT="Case Duckworth"
BACK_LINK=index.html,back

wrap() {
    title="$(sed 1q "$1")"
    cat <<HEAD
<!DOCTYPE html>
<title>$title</title>
<style>
body{max-width:48em;font:20px/1.4 serif;margin:auto;padding:1em;}
footer{position:absolute;bottom:0;left:0;padding:1em;background:white;}
dt{font-weight:bold;}
</style>
<body>
<!-- LIST_START -->
<h1>$title</h1>
HEAD
    cat
    cat <<FOOT
<!-- LIST_END -->
<footer>
$(test "$2" && printf '<a href="%s">%s</a>' "${2%%,*}" "${2##*,}")
$(case "$1" in (*index*) printf '<a href="%s">%s</a>' \
       "$BASE_URL/feed.xml" feed ;; esac)
</footer>
</body></html>
FOOT
}

# Library
list()(tag="$1";shift;echo "<$tag>"; sed 1d|awk "$@"; echo "</$tag>")
# unordered list
ul()(list ul '/^$/{next}{printf "<li>%s</li>\n",$0}')
# ordered list
ol()(list ol '/^$/{next}{printf "<li>%s</li>\n",$0}')
# anchor (link) list
al()(list ul -F"\n" -vRS= '{printf "<li><a href=\"%s\">%s</a></li>\n",$1,$2}')
# definition list
dl()(list dl -F"\n" -vRS= '{printf "<dt>%s</dt>\n<dd>%s</dd>\n",$1,$2}')

rss() {
    cat <<HEAD
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>$(sed 1q index.al)</title>
<link href="$BASE_URL/feed.xml" rel="self" />
<link href="$BASE_URL" />
<id>$BASE_URL</id>
<rights>$SITE_COPYRIGHT</rights>
<updated>$(date +%FT%TZ)</updated>
HEAD
    # entries
    awk -F"\n" -vRS= \
	-vbase="$BASE_URL" \
	-vauthor="$SITE_COPYRIGHT" \
        'NR==1{next}
{   url=$1; title=$2;
    file=url; sub(base "/", "", file)
    printf "<entry>\n<id>%s</id>\n", url
    printf "<link rel=\"alternate\" href=\"%s\" />\n", url
    printf "<title>%s</title>\n", title
    printf "<author><name>%s</name></author>\n", author
    printf "<updated>%s</updated>\n", updated(file)
    printf "<content type=\"html\"><![CDATA[%s]]></content>\n",
	slurp(file)
    printf "</entry>\n"
}
function updated (file) {
    cmd = "stat -c %y " file " 2>/dev/null"
    cmd = cmd " || stat -f %Sm -t %FT%TZ " file " 2>/dev/null"
    cmd | getline upd
    close(cmd)
    return upd
}
function slurp (file,    out, bodyp) {
    oldRS=RS; RS="\n"
    while ((getline < file) > 0) {
	if ($0 ~ /LIST_END/) bodyp = 0
	if (bodyp) out = out (out?"\n":"") $0
	if ($0 ~ /LIST_START/) bodyp = 1
    }
    RS=oldRS
    return out
}'
    cat <<FOOT
</feed>
FOOT
}

# Main
main() {
    printf "%s\n\n" "$SITE_TITLE" > index.al
    ls -t *.ul *.dl *.ol *.al 2>/dev/null |
	sed '/index.al/d' |
	while read -r lst
	do
	    printf >&2 "Processing %s..." "$lst"
	    "${lst##*.}" < "$lst" |
		wrap "$lst" "$BACK_LINK" > "${lst%%.*}.html"
	    echo "$BASE_URL/${lst%%.*}.html" >> index.al
	    echo "$(sed 1q "$lst")" >> index.al
	    echo >> index.al
	    echo >&2 ok
	done
    printf >&2 "Processing index..."
    al < index.al | wrap index.al > index.html &&
	echo >&2 ok
    printf >&2 "Processing rss..."
    rss < index.al > feed.xml &&
	echo >&2 ok
}

main "$@"