-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblurt
More file actions
executable file
·336 lines (279 loc) · 11.8 KB
/
blurt
File metadata and controls
executable file
·336 lines (279 loc) · 11.8 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/sh
set -e
VERSION="2.0.0"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
DIST="$SCRIPT_DIR/dist"
# Get current time in milliseconds (portable across GNU, BusyBox, macOS)
now_ms() {
if ts=$(date +%s%N 2>/dev/null) && [ ${#ts} -gt 10 ]; then
echo $((ts / 1000000))
elif [ -f /proc/uptime ]; then
awk '{printf "%.0f", $1 * 1000}' /proc/uptime
else
echo $(($(date +%s) * 1000))
fi
}
slugify() {
echo "$1" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/--*/-/g' | sed 's/^-//' | sed 's/-$//'
}
xml_escape() {
sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'
}
generate_favicon() {
title="$1"
highlight="$2"
output="$3"
# Extract initials from title (first letter of each word, max 2)
initials=$(echo "$title" | awk '{
for (i=1; i<=NF && i<=2; i++) {
printf toupper(substr($i, 1, 1))
}
}')
cat >"$output" <<EOF
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<rect width="100" height="100" rx="12" fill="$highlight"/>
<text x="51" y="51" font-family="'Open Sans', sans-serif" font-size="42" font-weight="bold" fill="rgba(0,0,0,0.2)" text-anchor="middle" dominant-baseline="central">$initials</text>
<text x="50" y="50" font-family="'Open Sans', sans-serif" font-size="42" font-weight="bold" fill="#fff" text-anchor="middle" dominant-baseline="central">$initials</text>
</svg>
EOF
}
generate_og_image() {
title="$1"
site_url="$2"
date="$3"
highlight="$4"
output="$5"
command -v magick >/dev/null 2>&1 || return
# Strip protocol from URL for display
display_url=$(echo "$site_url" | sed 's|https://||; s|http://||')
# Calculate a very slightly darker shade for almost invisible gradient
darker=$(magick xc:"$highlight" -modulate 92 -format '%[hex:p{0,0}]' info: 2>/dev/null || echo "${highlight#\#}")
if [ -n "$date" ]; then
# Format date as "15 January, 2026"
formatted_date=$(date -j -f "%Y-%m-%d" "$date" "+%-d %B, %Y" 2>/dev/null || echo "$date")
# Post page: URL top-left, date top-right, title bottom-left
# Use 32-bit depth with subtle noise to prevent gradient banding
magick -size 1200x630 -depth 32 -define gradient:direction=SouthWest gradient:"$highlight-#$darker" \
-attenuate 0.15 +noise Gaussian \
-font "Open-Sans-SemiBold" -pointsize 24 -fill 'rgba(0,0,0,0.15)' \
-gravity NorthWest -annotate +61+51 "$display_url" \
-font "Open-Sans-SemiBold" -pointsize 24 -fill 'rgba(255,255,255,0.9)' \
-gravity NorthWest -annotate +60+50 "$display_url" \
-font "Open-Sans-Regular" -pointsize 24 -fill 'rgba(0,0,0,0.15)' \
-gravity NorthEast -annotate +59+51 "$formatted_date" \
-font "Open-Sans-Regular" -pointsize 24 -fill 'rgba(255,255,255,0.9)' \
-gravity NorthEast -annotate +60+50 "$formatted_date" \
-font "Open-Sans-Bold" -pointsize 56 -fill 'rgba(0,0,0,0.2)' \
-gravity SouthWest -annotate +62+58 "$title" \
-font "Open-Sans-Bold" -pointsize 56 -fill '#ffffff' \
-gravity SouthWest -annotate +60+60 "$title" \
-quality 90 "$output" || true
else
# Front page: URL top-left, title bottom-left (no date)
# Use 32-bit depth with subtle noise to prevent gradient banding
magick -size 1200x630 -depth 32 -define gradient:direction=SouthWest gradient:"$highlight-#$darker" \
-attenuate 0.15 +noise Gaussian \
-font "Open-Sans-SemiBold" -pointsize 24 -fill 'rgba(0,0,0,0.15)' \
-gravity NorthWest -annotate +61+51 "$display_url" \
-font "Open-Sans-SemiBold" -pointsize 24 -fill 'rgba(255,255,255,0.9)' \
-gravity NorthWest -annotate +60+50 "$display_url" \
-font "Open-Sans-Bold" -pointsize 56 -fill 'rgba(0,0,0,0.2)' \
-gravity SouthWest -annotate +62+58 "$title" \
-font "Open-Sans-Bold" -pointsize 56 -fill '#ffffff' \
-gravity SouthWest -annotate +60+60 "$title" \
-quality 90 "$output" || true
fi
}
cmd_new() {
open_editor=false
[ "$1" = "-e" ] && open_editor=true && shift
title="$1"
if [ -z "$title" ]; then
echo "Usage: blurt new [-e] \"Post Title\""
exit 1
fi
slug=$(slugify "$title")
file="$SCRIPT_DIR/posts/$slug.md"
if [ -f "$file" ]; then
echo "File already exists: $file"
exit 1
fi
cat >"$file" <<EOF
---
title: $title
date: $(date -u +%Y-%m-%d)
---
EOF
echo "Created $file"
if $open_editor; then ${EDITOR:-vi} "$file"; fi
}
cmd_build() {
if ! command -v pandoc >/dev/null 2>&1; then
echo "pandoc is required but not installed"
exit 1
fi
build_start=$(now_ms)
commit_sha="${GITHUB_SHA:-$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")}"
post_template="$SCRIPT_DIR/templates/post.html"
index_template="$SCRIPT_DIR/templates/index.html"
data_dir="--data-dir=$SCRIPT_DIR"
config_flag=""
[ -f "$SCRIPT_DIR/config.yaml" ] && config_flag="--metadata-file=$SCRIPT_DIR/config.yaml"
# Read config values for OG image generation
site_url=$(sed -n 's/^site_url: *"\{0,1\}\([^"]*\)"\{0,1\} *$/\1/p' "$SCRIPT_DIR/config.yaml" 2>/dev/null | head -1)
social_highlight=$(sed -n 's/^social_highlight: *"\{0,1\}\([^"]*\)"\{0,1\} *$/\1/p' "$SCRIPT_DIR/config.yaml" 2>/dev/null | head -1)
[ -z "$social_highlight" ] && social_highlight="#6246ea"
rm -rf "$DIST"
mkdir -p "$DIST"
if [ -d "$SCRIPT_DIR/static" ] && [ "$(ls -A "$SCRIPT_DIR/static" 2>/dev/null)" ]; then
cp -r "$SCRIPT_DIR/static"/* "$DIST/"
fi
posts_data=""
for post in "$SCRIPT_DIR/posts"/*.md; do
[ -f "$post" ] || continue
slug=$(basename "$post" .md)
title=$(sed -n 's/^title: *//p' "$post" | head -1)
date=$(sed -n 's/^date: *//p' "$post" | head -1)
description=$(sed -n 's/^description: *//p' "$post" | head -1)
mkdir -p "$DIST/$slug"
page_start=$(now_ms)
pandoc "$post" --template="$post_template" $data_dir $config_flag -M slug="$slug" -M pagetitle="$title" -M pagedescription="$description" -M is_post=true -o "$DIST/$slug/index.html"
page_time="$(($(now_ms) - page_start))ms"
sed -i.bak "s/__BUILD_TIME__/$page_time/g" "$DIST/$slug/index.html"
rm -f "$DIST/$slug/index.html.bak"
generate_og_image "$title" "$site_url" "$date" "$social_highlight" "$DIST/$slug/og.jpg"
posts_data="$posts_data$date $slug $title
"
done
sorted=$(printf '%s' "$posts_data" | sort -r)
index_list=$(echo "$sorted" | while IFS=' ' read -r date slug title; do
[ -z "$date" ] && continue
printf '<li><a href="%s/">%s</a> <time datetime="%s">%s</time></li>' "$slug" "$title" "$date" "$date"
done)
page_start=$(now_ms)
site_title=$(sed -n 's/^site_title: *"\{0,1\}\([^"]*\)"\{0,1\} *$/\1/p' "$SCRIPT_DIR/config.yaml" 2>/dev/null | head -1)
site_description=$(sed -n 's/^site_description: *"\{0,1\}\([^"]*\)"\{0,1\} *$/\1/p' "$SCRIPT_DIR/config.yaml" 2>/dev/null | head -1)
echo "$index_list" | pandoc --template="$index_template" $data_dir $config_flag -M pagetitle="$site_title" -M pagedescription="$site_description" -o "$DIST/index.html"
page_time="$(($(now_ms) - page_start))ms"
sed -i.bak "s/__BUILD_TIME__/$page_time/g" "$DIST/index.html"
rm -f "$DIST/index.html.bak"
# Generate OG image for homepage (no date)
generate_og_image "$site_title" "$site_url" "" "$social_highlight" "$DIST/og.jpg"
generate_favicon "$site_title" "$social_highlight" "$DIST/favicon.svg"
# Generate Atom feed
site_url=$(sed -n 's/^site_url: *"\{0,1\}\([^"]*\)"\{0,1\} *$/\1/p' "$SCRIPT_DIR/config.yaml" 2>/dev/null | head -1)
site_title=$(sed -n 's/^site_title: *"\{0,1\}\([^"]*\)"\{0,1\} *$/\1/p' "$SCRIPT_DIR/config.yaml" 2>/dev/null | head -1)
site_author=$(sed -n 's/^site_author: *"\{0,1\}\([^"]*\)"\{0,1\} *$/\1/p' "$SCRIPT_DIR/config.yaml" 2>/dev/null | head -1)
if [ -n "$site_url" ]; then
feed_updated=$(echo "$sorted" | head -1 | cut -f1)
[ -z "$feed_updated" ] && feed_updated=$(date -u +%Y-%m-%d)
site_url_escaped=$(echo "$site_url" | xml_escape)
cat >"$DIST/feed.xml" <<'FEED_HEADER'
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
FEED_HEADER
printf ' <title>%s</title>\n' "$(echo "$site_title" | xml_escape)" >>"$DIST/feed.xml"
printf ' <link href="%s/feed.xml" rel="self"/>\n' "$site_url_escaped" >>"$DIST/feed.xml"
printf ' <link href="%s/"/>\n' "$site_url_escaped" >>"$DIST/feed.xml"
printf ' <id>%s/</id>\n' "$site_url_escaped" >>"$DIST/feed.xml"
printf ' <updated>%sT00:00:00Z</updated>\n' "$feed_updated" >>"$DIST/feed.xml"
[ -n "$site_author" ] && printf ' <author><name>%s</name></author>\n' "$(echo "$site_author" | xml_escape)" >>"$DIST/feed.xml"
echo "$sorted" | while IFS=' ' read -r date slug title; do
[ -z "$date" ] && continue
article=$(sed -n '/<article>/,/<\/article>/p' "$DIST/$slug/index.html")
title_escaped=$(echo "$title" | xml_escape)
content_escaped=$(printf '%s' "$article" | xml_escape)
printf ' <entry>\n' >>"$DIST/feed.xml"
printf ' <title>%s</title>\n' "$title_escaped" >>"$DIST/feed.xml"
printf ' <link href="%s/%s/"/>\n' "$site_url_escaped" "$slug" >>"$DIST/feed.xml"
printf ' <id>%s/%s/</id>\n' "$site_url_escaped" "$slug" >>"$DIST/feed.xml"
printf ' <updated>%sT00:00:00Z</updated>\n' "$date" >>"$DIST/feed.xml"
printf ' <content type="html">%s</content>\n' "$content_escaped" >>"$DIST/feed.xml"
printf ' </entry>\n' >>"$DIST/feed.xml"
done
echo "</feed>" >>"$DIST/feed.xml"
fi
# Replace variables in all pages
find "$DIST" -name '*.html' -exec sed -i.bak "s/__COMMIT_SHA__/$commit_sha/g; s/__BLURT_VERSION__/$VERSION/g" {} \;
find "$DIST" -name '*.bak' -delete
echo "Built site in $DIST ($(($(now_ms) - build_start))ms)"
}
cmd_update() {
remote_url="https://raw.githubusercontent.com/shkm/blurt/refs/heads/main/blurt"
tmp_file=$(mktemp)
trap "rm -f '$tmp_file'" EXIT
echo "Fetching latest version..."
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$remote_url" -o "$tmp_file"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$tmp_file" "$remote_url"
else
echo "curl or wget is required for update"
exit 1
fi
if [ ! -s "$tmp_file" ] || ! head -1 "$tmp_file" | grep -q '^#!/bin/sh'; then
echo "Failed to download valid script"
exit 1
fi
remote_version=$(grep '^VERSION=' "$tmp_file" | head -1 | cut -d'"' -f2)
[ -z "$remote_version" ] && remote_version="unknown"
if [ "$VERSION" = "$remote_version" ]; then
echo "Already at latest version ($VERSION)"
exit 0
fi
cp "$tmp_file" "$0"
chmod +x "$0"
echo "Updated blurt from $VERSION to $remote_version"
}
cmd_start() {
if ! command -v python3 >/dev/null 2>&1; then
echo "python3 is required for the dev server"
exit 1
fi
port="${1:-8000}"
cmd_build
python3 -m http.server "$port" -d "$DIST" &
server_pid=$!
trap "kill $server_pid 2>/dev/null" EXIT
echo "Serving at http://localhost:$port"
if command -v fswatch >/dev/null 2>&1; then
fswatch -o "$SCRIPT_DIR/posts" "$SCRIPT_DIR/templates" "$SCRIPT_DIR/static" "$SCRIPT_DIR/config.yaml" | while read; do
echo "Rebuilding..."
cmd_build
done
else
echo "Note: install fswatch for instant rebuilds (currently polling)"
while true; do
sleep 1
changed=$(find "$SCRIPT_DIR/posts" "$SCRIPT_DIR/templates" "$SCRIPT_DIR/static" "$SCRIPT_DIR/config.yaml" -newer "$DIST/index.html" 2>/dev/null | head -1)
[ -n "$changed" ] && echo "Rebuilding..." && cmd_build
done
fi
}
case "$1" in
new)
shift
cmd_new "$@"
;;
build)
cmd_build
;;
start)
cmd_start "$2"
;;
update)
cmd_update
;;
version | -v | --version)
echo "blurt $VERSION"
;;
*)
echo "Usage: blurt <new|build|start|update|version>"
echo " new [-e] \"Title\" - Create a new post"
echo " build - Build the site"
echo " start [port] - Watch, rebuild, and serve (default: 8000)"
echo " update - Update blurt to latest version"
echo " version - Show version number"
exit 1
;;
esac