summary refs log tree commit diff
path: root/www/photo.causal.agency/generate.sh
blob: 6d6255d4ac84af27929fa179b0179d229fc2b391 (plain) (blame)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/sh
set -eu

mkdir -p static/preview static/thumbnail

resize() {
	local photo=$1 size=$2 output=$3
	if ! test -f $output; then
		# FIXME: convert complains about not understanding XML
		echo $output >&2
		convert $photo -auto-orient -thumbnail $size $output 2>/dev/null ||:
	fi
}

preview() {
	local photo=$1
	local preview=preview/${photo##*/}
	resize $photo 25% static/$preview
	echo $preview
}

thumbnail() {
	local photo=$1
	local thumbnail=thumbnail/${photo##*/}
	resize $photo 5% static/$thumbnail
	echo $thumbnail
}

encode() {
	sed '
		s/&/\&/g
		s/</\&lt;/g
		s/"/\&quot;/g
	' "$@"
}

page_title() {
	date -j -f '%F' $1 '+%B %e, %Y'
}

page_head() {
	local date=$1
	local title=$(page_title $date)
	local lens film

	if test -f $date/lens; then
		lens=$(encode $date/lens)
	else
		lens=$(
			identify -format '%[EXIF:LensModel]' \
				$date/$(ls -1 $date | head -n 1) 2>/dev/null |
			sed '
				s/\([A-Z]\)\([0-9]\)/\1 \2/
				s,f/,ƒ/,
				s/\([0-9]\)-\([0-9]\)/\1–\2/g
			' |
			encode
		)
	fi
	if test -f $date/film; then
		film=$(encode $date/film)
	fi

	cat <<-EOF
	<!DOCTYPE html>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="alternate" type="application/atom+xml" href="../feed.atom">
	<title>${title}</title>
	<style>
	html { color: #bbb; background-color: black; font-family: monospace; }
	p { text-align: center; }
	figure { margin: 1em; padding-top: 0.5em; text-align: center; }
	img { max-width: calc(100vw - 2.5em); max-height: calc(100vh - 2.5em); }
	details { max-width: 78ch; margin: 0.5em auto; }
	</style>
	<h1>${title}</h1>
	<p>📷 ${lens}${film:+ 🎞️ }${film:-}</p>
	EOF
}

photo_info() {
	local photo=$1
	ExposureTime=
	FNumber=
	FocalLength=
	PhotographicSensitivity=
	eval $(
		identify -format '%[EXIF:*]' $photo 2>/dev/null |
		grep -E 'ExposureTime|FNumber|FocalLength|PhotographicSensitivity' |
		sed 's/^exif://'
	)
}

photo_id() {
	local photo=$1
	photo=${photo##*/}
	photo=${photo%%.*}
	echo $photo
}

page_photo() {
	local photo=$1 preview=$2 description=$3
	if ! test -f $description; then
		description=/dev/null
	fi
	photo_info $photo
	cat <<-EOF
	<figure id="$(photo_id $photo)">
		<a href="${photo##*/}">
			<img src="../${preview}" alt="$(encode $description)">
		</a>
		<figcaption>
			${ExposureTime} ·
			ƒ/$(bc -S 1 -e ${FNumber}) ·
			$(bc -e ${FocalLength}) mm ·
			${PhotographicSensitivity} ISO
			<details>
				<summary>description</summary>
				$(encode $description)
			</details>
		</figcaption>
	</figure>
	EOF
}

index_head() {
	cat <<-EOF
	<!DOCTYPE html>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="alternate" type="application/atom+xml" href="feed.atom">
	<title>Photos</title>
	<style>
	html { color: #bbb; background-color: black; font-family: sans-serif; }
	a { text-decoration: none; color: inherit; }
	</style>
	EOF
}

index_page() {
	local date=$1 root=${2:-}
	cat <<-EOF
	<h1><a href="${root}${root:+/}${date}/">$(page_title $date)</a></h1>
	EOF
}

index_photo() {
	local date=$1 photo=$2 thumbnail=$3 root=${4:-}
	cat <<-EOF
	<a href="${root}${root:+/}${date}/#$(photo_id $photo)">
		<img src="${root}${root:+/}${thumbnail}">
	</a>
	EOF
}

Root=https://photo.causal.agency

atom_head() {
	local updated=$(date -u '+%FT%TZ')
	cat <<-EOF
	<?xml version="1.0" encoding="utf-8"?>
	<feed xmlns="http://www.w3.org/2005/Atom">
	<title>Photos</title>
	<author><name>june</name><email>june@causal.agency</email></author>
	<link href="${Root}"/>
	<link rel="self" href="${Root}/feed.atom"/>
	<id>${Root}/</id>
	<updated>${updated}</updated>
	EOF
}

atom_entry_head() {
	local date=$1
	local updated=$(
		date -ju -f '%s' $(stat -f '%m' static/${date}/index.html) '+%FT%TZ'
	)
	cat <<-EOF
	<entry>
	<title>$(page_title $date)</title>
	<link href="${Root}/${date}/"/>
	<id>${Root}/${date}/</id>
	<updated>${updated}</updated>
	<content type="html">
	EOF
}

atom_entry_tail() {
	cat <<-EOF
	</content>
	</entry>
	EOF
}

atom_tail() {
	cat <<-EOF
	</feed>
	EOF
}

set --
for date in 20*; do
	mkdir -p static/${date}
	page=static/${date}/index.html
	if ! test -f $page; then
		echo $page >&2
		page_head $date >$page
		for photo in ${date}/*.JPG; do
			preview=$(preview $photo)
			if ! test -f static/${photo}; then
				ln $photo static/${photo}
			fi
			page_photo $photo $preview ${photo%.JPG}.txt >>$page
		done
	fi
	set -- $date "$@"
done

echo static/index.html >&2
index_head >static/index.html
echo static/feed.atom >&2
atom_head >static/feed.atom
for date; do
	index_page $date >>static/index.html
	atom_entry_head $date >>static/feed.atom
	for photo in ${date}/*.JPG; do
		thumbnail=$(thumbnail $photo)
		index_photo $date $photo $thumbnail >>static/index.html
		index_photo $date $photo $thumbnail $Root | encode >>static/feed.atom
	done
	atom_entry_tail >>static/feed.atom
done
atom_tail >>static/feed.atom