-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk
executable file
·193 lines (160 loc) · 3.67 KB
/
mk
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/sh
site_title='Eivind Uggedal'
tmpl_head() {
local title="$1"
cat <<EOF
<!doctype html>
<html>
<head>
<title>$title</title>
<meta name="viewport" content="width=device-width">
<link rel=alternate
type=application/atom+xml
href=/journal/index.atom
title="Feed of the latest journal entries">
<link href=/static/style.css rel="stylesheet">
</head>
<body>
<h1>
<a href=/>$site_title</a>
</h1>
EOF
}
tmpl_foot() {
cat <<EOF
<footer>
Generated with
<a href=http://github.com/uggedal/uggedal.com>POSIX shell</a>.
</footer>
</body>
</html>
EOF
}
header() {
sed "${2}q;d" "$1" | sed 's/^% //'
}
htmlext() {
printf '%s' "${1%*.md}"
}
article() {
local target title date
input=$1
target=$2
title=$(header "$1" 1)
date=$(header "$1" 2)
tmpl_head "$title" >"$target"
cat <<EOF >>"$target"
<article>
<header>
<h1>$title</h1>
<p class="byline">
An entry from <strong>$date</strong> in
the <a href=/journal>Journal</a>.
</p>
</header>
$(sed '1,2d' "$input" | ./md)
</article>
EOF
tmpl_foot >>"$target"
}
reverse_chronological() {
local limit=9999
local date
[ "$1" = '--limit' ] && {
limit=$2
shift 2
}
for f; do
date=$(header "$f" 2)
[ "$date" = draft ] || printf '%s %s\n' "$date" "$f"
done | sort -r | head -n"$limit" | cut -d' ' -f2
}
index() {
local ar_title ar_date ar_href
local target="$1"
local head_title="$2"
local index_title="$3"
shift 3
tmpl_head "$head_title" >"$target"
[ "$1" = '--limit' ] && {
cat <<EOF >>"$target"
<section class=about>
I'm a technical janitor at <a href=https://vipps.no>Vipps</a>.
I share <a href=http://github.com/uggedal>open source code</a>
written in my pastime.
I created <a href=http://mediaqueri.es>mediaqueri.es</a>, <em>the</em>
collection of responsively designed web sites.
You can contact me at
<a href=mailto:[email protected]>[email protected]</a>.
</section>
EOF
}
cat <<EOF >>"$target"
<section class=entries>
<header>
<h1>$index_title</h1>
</header>
<ol>
EOF
for ar in $(reverse_chronological "$@"); do
ar_title=$(header "$ar" 1)
ar_date=$(header "$ar" 2)
ar_href=/$(htmlext "$ar")
cat <<EOF >>"$target"
<li>
$ar_date<br>
<a href=$ar_href>$ar_title</a>
</li>
EOF
done
printf ' </ol>\n' >>"$target"
[ "$1" = '--limit' ] && {
cat <<EOF >>"$target"
<p>
<a href=/journal>
<em>All journal entries</em>
</a>
<p>
EOF
}
printf ' </section>\n' >>"$target"
tmpl_foot >>"$target"
}
feed() {
local target
target=$1
shift
cat <<EOF >"$target"
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">Journal of Eivind Uggedal</title>
<id>http://uggedal.com/journal/index.atom</id>
<updated>$(date -u +"%Y-%m-%dT%H:%M:%SZ")</updated>
<link href="http://uggedal.com/journal" />
<link href="http://uggedal.com/journal/index.atom" rel="self" />
<author>
<name>Eivind Uggedal</name>
</author>
<generator>POSIX shell</generator>
EOF
for f in $(reverse_chronological "$@"); do
f_title=$(header "$f" 1)
f_date=$(header "$f" 2)
f_url=http://uggedal.com/$(htmlext "$f")
cat <<EOF >>"$target"
<entry xml:base="http://uggedal.com/journal/index.atom">
<title type="text">$f_title</title>
<id>$f_url</id>
<updated>${f_date}T00:00:00Z</updated>
<link href="$f_url" />
<content type="html">
<![CDATA[$(sed '1,2d' "$f" | ./md)]]>
</content>
</entry>
EOF
done
printf '</feed>\n' >>"$target"
}
action=$1
shift
$action "$@"