Commit some images on Sunday, 25th of January, Anno Domini MMIX, at the hour of the...
[git/dscho.git] / upload.sh
blobef972367fb10427840df1d4d17281fa866791755
1 #!/bin/sh
3 # This is a simple script that will produce my blog on repo.or.cz
5 # The idea is to have source-<timestamp>.txt files as input, having the
6 # stories, and this script turning them into nice HTML, committing
7 # everything, and then pushing it to my repository.
9 # The blog will then be served using gitweb.
11 # To make it easier on me, if a file "new" exists, it is automatically
12 # renamed using the current timestamp.
14 # How to use:
16 # $ mkdir my-blog
17 # $ cd my-blog
18 # $ git init
20 # Then symlink or copy this file (upload.sh); you can track it or add it
21 # to .gitignore, does not matter.
23 # Add a remote "origin" (you might want to track only the appropriate branch
24 # if the repository contains other branches, too), add a background image,
25 # and then set the config variables gitweb.url, blog.title, blog.background
26 # and blog.branch appropriately.
28 # Example:
30 # $ git remote add -t blog repo.or.cz:/srv/git/git/dscho.git/
31 # $ git symbolic-ref HEAD refs/heads/blog
32 # $ cp ~/images/background.jpg ./
33 # $ git config gitweb.url http://repo.or.cz/w/git/dscho.git
34 # $ git config blog.title "Dscho's blog"
35 # $ git config blog.background background.jpg
36 # $ git config blog.branch blog
38 # Now you can start writing posts, by creating a file called "new", and
39 # calling ./upload.sh to commit the post together with the images and
40 # push all.
42 # Note that no file names may contain spaces.
44 # TODO: document the "syntax" of the source-*.txt files
47 # make sure we're in the correct working directory
48 cd "$(dirname "$0")"
50 GITWEBURL="$(git config gitweb.url)"
51 test -z "$GITWEBURL" && {
52 echo "Please set gitweb.url in the Git config first!" >&2
53 exit 1
56 get_config () {
57 value=$(git config blog.$1)
58 test -z "$value" && value=$2
59 echo $value
62 BACKGROUNDIMG=$(get_config background paper.jpg)
63 TITLE=$(get_config title "Dscho's blog")
64 MAXENTRIES=$(get_config maxPostsPerPage 10)
65 BRANCH=$(get_config branch blog)
67 URLPREFIX="$(dirname "$GITWEBURL")"/
68 REMOTEREPOSITORY="$(basename "$GITWEBURL")"
69 URL="$REMOTEREPOSITORY?a=blob_plain;hb=$BRANCH;f="
70 ORIGURL=$URL
71 NEW=new
72 OUTPUT=index.html
73 RSS=blog.rss
74 TEST=test.html
75 THIS=$0
77 LC_ALL=C
78 export LC_ALL
80 move_new_entry_back () {
81 test -f source-$now.txt &&
82 mv source-$now.txt $NEW &&
83 git rm --cached -f source-$now.txt
86 die () {
87 move_new_entry_back
88 echo "$*" >&2
89 exit 1
92 strip_prefix () {
93 echo "${1#$2}"
96 chomp () {
97 strip_prefix "${1%$3}" "$2"
100 nth () {
101 # add illogical suffix
102 case "$1" in
103 *1?|*[04-9]) echo "$1th";;
104 *1) echo "$1st";;
105 *2) echo "$1nd";;
106 *3) echo "$1rd";;
107 esac
110 make_chinese_hour () {
111 case $1 in
112 23|00) echo Rat;;
113 01|02) echo Buffalo;;
114 03|04) echo Tiger;;
115 05|06) echo Rabbit;;
116 07|08) echo Dragon;;
117 09|10) echo Snake;;
118 11|12) echo Horse;;
119 13|14) echo Goat;;
120 15|16) echo Monkey;;
121 17|18) echo Rooster;;
122 19|20) echo Dog;;
123 21|22) echo Pig;;
124 esac
127 digit_to_roman () {
128 case $1 in
129 1) echo $2;;
130 2) echo $2$2;;
131 3) echo $2$2$2;;
132 4) echo $2$3;;
133 5) echo $3;;
134 6) echo $3$2;;
135 7) echo $3$2$2;;
136 8) echo $3$2$2$2;;
137 9) echo $2$4;;
138 esac
141 make_roman_number () {
142 case $1 in
143 '') ;;
144 ?) digit_to_roman $1 I V X;;
145 ??) echo $(digit_to_roman ${1%?} X L C)$(make_roman_number ${1#?});;
146 ???) echo $(digit_to_roman ${1%??} C D M)$(make_roman_number ${1#?});;
147 ????) echo $(digit_to_roman ${1%???} M)$(make_roman_number ${1#?});;
148 esac
151 make_date () {
152 printf "%s, %s of %s, Anno Domini %s, at the hour of the %s\n" \
153 $(date +%A -d @$1) \
154 $(nth $(date +%e -d @$1)) \
155 $(date +%B -d @$1) \
156 $(make_roman_number $(date +%Y -d @$1)) \
157 $(make_chinese_hour $(date +%H -d @$1))
160 # make an argument for sed, to replace $1..$1 by <$2>..</$2>
161 markup_substitution () {
162 case "$1" in
163 ?) echo "s/$1\\([^$1]*\\)$1/<$2>\\\\1<\/$2>/g";;
165 tmp="[^${1%?}]*"
166 tmp2="\\|${1%?}[^${1#?}]$tmp"
167 tmp3="\\($tmp\\($tmp2\\($tmp2\\($tmp2\\)\\)\\)\\)"
168 echo "s/$1$tmp3$1/<$2>\\\\1<\/$2>/g"
170 esac
173 # transform markup in stdin to HTML
174 markup () {
175 case "$*" in
176 *invert-bash*) bash_bg=white; bash_fg=black;;
177 *) bash_bg=black; bash_fg=white;;
178 esac
179 sed -e 's!^$!</p><p>!' \
180 -e "$(markup_substitution "''" i)" \
181 -e "$(markup_substitution "_" u)" \
182 -e 's!IMHO!in my humble opinion!g' \
183 -e 's!BTW!By the way,!g' \
184 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
185 -e 's!:-)!\&#x263a;!g' \
186 -e "s!\\[\\[\(Image\|SVG\):.*!$THIS handle &!e" \
187 -e 's!<bash>!<table\
188 border=1 bgcolor='$bash_bg'>\
189 <tr><td bgcolor=lightblue colspan=3>\
190 \&nbsp;\
191 </td></tr>\
192 <tr><td>\
193 <table cellspacing=5 border=0\
194 style="color:'$bash_fg';">\
195 <tr><td>\
196 <pre>!' \
197 -e 's!</bash>! </pre>\
198 </td></tr>\
199 </table>\
200 </td></tr>\
201 </table>!' \
205 # output lines containing <timestamp> <filename> <title>
206 get_blog_entries () {
207 for file in $(ls -r source-*.txt)
209 timestamp=$(chomp $file source- .txt)
210 title="$(sed 1q < $file | markup)"
211 echo "$timestamp $file $title"
212 done
215 get_last_removed_entry () {
216 git log --pretty=format: --name-only --diff-filter=D HEAD |
217 while read line
219 case "$line" in
220 source-*.txt) file=$line;;
221 '') test -z "$file" || {
222 echo "$file"
223 break
225 esac
226 done
229 # make HTML page
230 make_html () {
231 body_style="width:800px"
232 body_style="$body_style;background-image:url($URL$BACKGROUNDIMG)"
233 body_style="$body_style;background-repeat:repeat-y"
234 body_style="$body_style;background-attachment:scroll"
235 body_style="$body_style;padding:0px;"
236 text_style="width:610px"
237 text_style="$text_style;margin-left:120px"
238 text_style="$text_style;margin-top:50px"
239 text_style="$text_style;align:left"
240 text_style="$text_style;vertical-align:top;"
241 cat << EOF
242 <html>
243 <head>
244 <title>$TITLE</title>
245 <meta http-equiv="Content-Type"
246 content="text/html; charset=UTF-8"/>
247 </head>
248 <body style="$body_style">
249 <div style="$text_style">
250 <h1>$TITLE</h1>
252 indent='\t\t\t'
254 # make toc
255 toc_width=400px
256 toc_style="position:absolute;top:50px;left:810px;width=$toc_width"
258 echo "<div style=\"$toc_style\">"
259 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
260 echo "<tr><th>Table of contents:</th></tr>"
261 echo "<tr><td>"
262 echo '<p><ul>'
263 get_blog_entries |
264 while read timestamp filename title
266 date="$(date +"%d %b %Y" -d @$timestamp)"
267 echo "<li><a href=#$timestamp>$date $title</a>"
268 done
269 echo '</ul></p>'
270 file=
271 last_removed_entry=$(get_last_removed_entry)
272 test -z "$last_removed_entry" || {
273 commit=$(git log --pretty=format:%H --diff-filter=AM \
274 -- $last_removed_entry |
275 head -n 1)
276 previous="$REMOTEREPOSITORY?a=blob_plain;hb=$commit"
277 echo "<a href=$previous;f=index.html>Older posts</a>"
279 echo '</td></tr></table>'
281 # RSS feed
282 rss_style="background-color:orange;text-decoration:none"
283 rss_style="$rss_style;color:white;font-family:sans-serif;"
284 echo '<br>'
285 echo '<div style="text-align:right;">'
286 echo "<a href=\"$ORIGURL$RSS\""
287 echo ' title="Subscribe to my RSS feed"'
288 echo ' class="rss" rel="nofollow"'
289 echo " style=\"$rss_style\">RSS</a>"
290 echo '</div>'
292 # Links
293 test -f links.html && {
294 echo "<br>"
295 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
296 echo "<tr><th>Links:</th></tr>"
297 echo "<tr><td>"
298 cat links.html
299 echo "</td></tr></table>"
302 # Google AdSense
303 test -z "$DRYRUN" && test -f google.adsense && {
304 echo "<br>"
305 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
306 echo "<tr><th>Google Ads:</th></tr>"
307 echo "<tr><td align=center>"
308 cat google.adsense
309 echo "</td></tr></table>"
312 echo '</div>'
313 } | sed -s "s/^/$indent/"
316 # timestamps will not need padding to sort correctly, for some time...
317 get_blog_entries |
318 while read timestamp filename title
320 echo "<h6>$(make_date $timestamp)</h6>"
321 echo "<a name=$timestamp>"
322 echo "<h2>$title</h2>"
323 echo ""
324 echo "<p>"
325 sed 1d < $filename | markup
326 echo "</p>"
327 done |
328 sed -e "s/^./$indent&/" \
329 -e "/<pre>/,/<\/pre>/s/^$indent//"
331 cat << EOF
332 </div>
333 </body>
334 </html>
338 generate_rss () {
339 echo '<?xml version="1.0" encoding="utf-8"?>'
340 echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
341 echo '<channel>'
342 echo "<title>Dscho's blog</title>"
343 echo "<link>$URLPREFIX${URL}index.html</link>"
344 self="$URLPREFIX$ORIGURL$RSS"
345 selfattribs='rel="self" type="application/rss+xml"'
346 echo "<atom:link href=\"$self\" $selfattribs/>"
347 echo '<description>A few stories told by Dscho</description>'
348 echo "<lastBuildDate>$(date --rfc-2822)</lastBuildDate>"
349 echo '<language>en-us</language>'
351 get_blog_entries |
352 while read timestamp filename title
354 echo '<item>'
355 echo "<title>$title</title>"
356 echo "<link>$URLPREFIX${URL}index.html#$timestamp</link>"
357 echo "<guid>$URLPREFIX${URL}index.html#$timestamp</guid>"
358 echo "<pubDate>$(date --rfc-2822 -d @$timestamp)</pubDate>"
359 description="$(cat < $filename | markup invert-bash)"
360 echo "<description><![CDATA[$description]]></description>"
361 echo "</item>"
362 done
364 echo '</channel>'
365 echo '</rss>'
368 get_image_files () {
369 git ls-files |
370 grep -v '\.\(rss\|html\|gitignore\|in\|sh\|txt\|adsense\)$'
373 remove_old_entries () {
374 count=$(ls source-*.txt | wc -l)
375 test $MAXENTRIES -ge $count && return
377 for file in source-*.txt
379 test $MAXENTRIES -lt $count || break
380 git rm $file > /dev/null || return 1
381 count=$(($count-1))
382 echo $file
383 done
385 # remove no-longer referenced images
386 image_files=$(get_image_files)
387 referenced_files="$(cat source-*.txt |
388 tr ']|' '\n' |
389 sed -ne 's/\[\[\(Image\|SVG\)://p') $BACKGROUNDIMG"
390 for file in $(echo $image_files $referenced_files $referenced_files |
391 tr ' ' '\n' | sort | uniq -u)
393 git rm $file > /dev/null || return 1
394 echo $file
395 done
398 # never, ever have spaces in the file names
399 commit_new_images () {
400 files="$(remove_old_entries) $RSS $BACKGROUNDIMG" ||
401 die "Could not remove old entries"
404 generate_rss > $RSS &&
405 git add $RSS ||
406 die "Could not generate $RSS"
408 for image in $(cat source-* |
409 tr ' ]|' '\n' |
410 sed -n 's/.*\[\[\(Image\|SVG\)://p' |
411 sort |
412 uniq)
414 git add $image || die "Could not git add image $image"
415 files="$files $image"
416 done
418 git update-index --refresh &&
419 git diff-files --quiet -- $files &&
420 git diff --cached --quiet HEAD -- $files ||
421 git commit -s -m "Commit some images on $(make_date $now)" $files
424 get_image_url () {
425 test ! -z "$DRYRUN" && echo "$1" && return
426 rev=$(git rev-list -1 HEAD -- $1)
427 test -z "$rev" && die "No revision found for $1"
428 echo "$REMOTEREPOSITORY?a=blob_plain;hb=$rev;f=$1"
431 handle_svg_file () {
432 # for some reason, Firefox adds scrollbars, so nudge the width a bit
433 width=$(sed -ne 's/.* width="\([^"]*\).*/\1/p' -e '/<metadata/q' < "$1")
434 test -z "$width" || width=" width=$(($width+5))"
435 url=$(get_image_url "$1")
436 cat << EOF
437 <center>
438 <table border=0>
439 <tr>
440 <td align=center>
441 <embed type="image/svg+xml"
442 src="$url"$width />
443 </td>
444 </tr>
445 <tr>
446 <td align=center>
447 <a href=$url>$1</a>
448 </td>
449 </tr>
450 </table>
451 </center>
455 handle_image_file () {
456 echo "<center><img src=$(get_image_url "${1%% *}") ${1#* }></center>"
461 # parse command line option
462 case "$1" in
463 *dry*) DRYRUN=1; export DRYRUN; shift;;
464 *show*) firefox "$(pwd)"/$TEST; exit;;
465 *remote*) firefox $URLPREFIX$URL$OUTPUT; exit;;
466 handle)
467 shift
468 case "$1" in
469 "[[SVG:"*) handle_svg_file "$(chomp "$*" '\[\[SVG:' '\]\]')";;
470 "[[Image:"*) handle_image_file "$(chomp "$*" '\[\[Image:' '\]\]')";;
471 esac
472 exit
474 '') ;;
475 *) die "Unknown command: $1";;
476 esac
478 test "$#" = 0 ||
479 die "Usage: $0 [--dry-run]"
481 # make sure we're on the correct branch
482 test refs/heads/$BRANCH = $(git symbolic-ref HEAD) ||
483 die "Not on branch $BRANCH"
485 # make sure there are no uncommitted changes
486 git update-index --refresh &&
487 git diff-files --quiet ||
488 die "Have unstaged changes!"
490 # rename the new blog entry if it exists
491 now=$(date +%s)
492 test ! -f $NEW || {
493 mv -i $NEW source-$now.txt &&
494 git add source-$now.txt
495 } ||
496 die "Could not rename source.txt"
498 # commit the images that are referenced and not yet committed
499 test ! -z "$DRYRUN" ||
500 commit_new_images ||
501 die "Could not commit new images"
503 # to find the images reliably, we have to use the commit name, not the branch
504 # we use the latest commit touching an image file.
505 IMAGEFILES="$(get_image_files)"
506 REV=$(git rev-list -1 HEAD -- $IMAGEFILES)
507 test -z "$REV" && REV=$BRANCH
508 URL="$REMOTEREPOSITORY?a=blob_plain;hb=$REV;f="
510 if test ! -z "$DRYRUN"
511 then
512 # Output to test.html and have local links into the current directory
513 OUTPUT=$TEST
514 URL=
517 make_html > $OUTPUT || die "Could not write $OUTPUT"
519 test ! -z "$DRYRUN" && {
520 move_new_entry_back
521 exit
524 git add $OUTPUT &&
525 git commit -s -m "Update $(make_date $now)" &&
526 git push origin +$BRANCH