Update Tuesday, 27th of January, Anno Domini MMIX, at the hour of the Tiger
[git/dscho.git] / upload.sh
blobedc785404872bafdd454640b2477862e4e9ba75f
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 case "$GITWEBURL" in
70 *'?'*) BLOBPLAIN="$REMOTEREPOSITORY;a=blob_plain";;
71 */) URLPREFIX=$GITWEBURL; BLOBPLAIN="a=blob_plain";;
72 *) BLOBPLAIN="$REMOTEREPOSITORY?a=blob_plain";;
73 esac
74 URL="$BLOBPLAIN;hb=$BRANCH;f="
75 ORIGURL=$URL
76 NEW=new
77 OUTPUT=index.html
78 RSS=blog.rss
79 TEST=test.html
80 THIS=$0
82 LC_ALL=C
83 export LC_ALL
85 move_new_entry_back () {
86 test -f source-$now.txt &&
87 mv source-$now.txt $NEW &&
88 git rm --cached -f source-$now.txt
91 die () {
92 move_new_entry_back
93 echo "$*" >&2
94 exit 1
97 strip_prefix () {
98 echo "${1#$2}"
101 chomp () {
102 strip_prefix "${1%$3}" "$2"
105 nth () {
106 # add illogical suffix
107 case "$1" in
108 *1?|*[04-9]) echo "$1th";;
109 *1) echo "$1st";;
110 *2) echo "$1nd";;
111 *3) echo "$1rd";;
112 esac
115 make_chinese_hour () {
116 case $1 in
117 23|00) echo Rat;;
118 01|02) echo Buffalo;;
119 03|04) echo Tiger;;
120 05|06) echo Rabbit;;
121 07|08) echo Dragon;;
122 09|10) echo Snake;;
123 11|12) echo Horse;;
124 13|14) echo Goat;;
125 15|16) echo Monkey;;
126 17|18) echo Rooster;;
127 19|20) echo Dog;;
128 21|22) echo Pig;;
129 esac
132 digit_to_roman () {
133 case $1 in
134 1) echo $2;;
135 2) echo $2$2;;
136 3) echo $2$2$2;;
137 4) echo $2$3;;
138 5) echo $3;;
139 6) echo $3$2;;
140 7) echo $3$2$2;;
141 8) echo $3$2$2$2;;
142 9) echo $2$4;;
143 esac
146 make_roman_number () {
147 case $1 in
148 '') ;;
149 ?) digit_to_roman $1 I V X;;
150 ??) echo $(digit_to_roman ${1%?} X L C)$(make_roman_number ${1#?});;
151 ???) echo $(digit_to_roman ${1%??} C D M)$(make_roman_number ${1#?});;
152 ????) echo $(digit_to_roman ${1%???} M)$(make_roman_number ${1#?});;
153 esac
156 make_date () {
157 printf "%s, %s of %s, Anno Domini %s, at the hour of the %s\n" \
158 $(date +%A -d @$1) \
159 $(nth $(date +%e -d @$1)) \
160 $(date +%B -d @$1) \
161 $(make_roman_number $(date +%Y -d @$1)) \
162 $(make_chinese_hour $(date +%H -d @$1))
165 # make an argument for sed, to replace $1..$1 by <$2>..</$2>
166 markup_substitution () {
167 case "$1" in
168 ?) echo "s/$1\\([^$1]*\\)$1/<$2>\\\\1<\/$2>/g";;
170 tmp="[^${1%?}]*"
171 tmp2="\\|${1%?}[^${1#?}]$tmp"
172 tmp3="\\($tmp\\($tmp2\\($tmp2\\($tmp2\\)\\)\\)\\)"
173 echo "s/$1$tmp3$1/<$2>\\\\1<\/$2>/g"
175 esac
178 space80=' '
179 space80="$space80$space80$space80$space80$space80$space80$space80$space80"
180 # transform markup in stdin to HTML
181 markup () {
182 case "$*" in
183 *invert-bash*) bash_bg=white; bash_fg=black;;
184 *) bash_bg=black; bash_fg=white;;
185 esac
186 sed -e 's!^$!</p><p>!' \
187 -e "$(markup_substitution "''" i)" \
188 -e "$(markup_substitution "_" u)" \
189 -e 's!IMHO!in my humble opinion!g' \
190 -e 's!BTW!By the way,!g' \
191 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
192 -e 's!:-)!\&#x263a;!g' \
193 -e "s!\\[\\[\(Image\|SVG\):.*!$THIS handle &!e" \
194 -e 's!<bash>!<table\
195 border=1 bgcolor='$bash_bg'>\
196 <tr><td bgcolor=lightblue colspan=3>\
197 <pre>'"$space80"'</pre>\
198 </td></tr>\
199 <tr><td>\
200 <table cellspacing=5 border=0\
201 style="color:'$bash_fg';">\
202 <tr><td>\
203 <pre>!' \
204 -e 's!</bash>!</pre>\
205 </td></tr>\
206 </table>\
207 </td></tr>\
208 </table>!' \
212 # output lines containing <timestamp> <filename> <title>
213 get_blog_entries () {
214 for file in $(ls -r source-*.txt)
216 timestamp=$(chomp $file source- .txt)
217 title="$(sed 1q < $file | markup)"
218 echo "$timestamp $file $title"
219 done
222 get_last_removed_entry () {
223 git log --pretty=format: --name-only --diff-filter=D HEAD |
224 while read line
226 case "$line" in
227 source-*.txt) file=$line;;
228 '') test -z "$file" || {
229 echo "$file"
230 break
232 esac
233 done
236 box_count=0
237 begin_box () {
238 test $box_count = 0 || echo "<br>"
239 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
240 echo "<tr><th>$1</th></tr>"
241 echo "<tr><td>"
244 end_box () {
245 echo "</td></tr></table>"
246 box_count=$(($box_count+1))
249 # make HTML page
250 make_html () {
251 body_style="width:800px"
252 body_style="$body_style;background-image:url($URL$BACKGROUNDIMG)"
253 body_style="$body_style;background-repeat:repeat-y"
254 body_style="$body_style;background-attachment:scroll"
255 body_style="$body_style;padding:0px;"
256 text_style="width:610px"
257 text_style="$text_style;margin-left:120px"
258 text_style="$text_style;margin-top:50px"
259 text_style="$text_style;align:left"
260 text_style="$text_style;vertical-align:top;"
261 cat << EOF
262 <html>
263 <head>
264 <title>$TITLE</title>
265 <meta http-equiv="Content-Type"
266 content="text/html; charset=UTF-8"/>
267 </head>
268 <body style="$body_style">
269 <div style="$text_style">
270 <h1>$TITLE</h1>
272 indent='\t\t\t'
274 # make toc
275 toc_width=400px
276 toc_style="position:absolute;top:50px;left:810px;width=$toc_width"
278 echo "<div style=\"$toc_style\">"
279 begin_box "Table of contents:"
280 echo '<p><ul>'
281 get_blog_entries |
282 while read timestamp filename title
284 date="$(date +"%d %b %Y" -d @$timestamp)"
285 echo "<li><a href=#$timestamp>$date $title</a>"
286 done
287 echo '</ul></p>'
288 file=
289 last_removed_entry=$(get_last_removed_entry)
290 test -z "$last_removed_entry" || {
291 commit=$(git log --pretty=format:%H --diff-filter=AM \
292 -- $last_removed_entry |
293 head -n 1)
294 previous="$BLOBPLAIN;hb=$commit"
295 echo "<a href=$previous;f=index.html>Older posts</a>"
297 end_box
299 # RSS feed
300 rss_style="background-color:orange;text-decoration:none"
301 rss_style="$rss_style;color:white;font-family:sans-serif;"
302 echo '<br>'
303 echo '<div style="text-align:right;">'
304 echo "<a href=\"$ORIGURL$RSS\""
305 echo ' title="Subscribe to my RSS feed"'
306 echo ' class="rss" rel="nofollow"'
307 echo " style=\"$rss_style\">RSS</a>"
308 echo '</div>'
310 # About
311 test -f about.html && {
312 begin_box "About this blog:"
313 cat about.html
314 end_box
317 # Links
318 test -f links.html && {
319 begin_box "Links:"
320 cat links.html
321 end_box
324 # Google AdSense
325 test -z "$DRYRUN" && test -f google.adsense && {
326 begin_box "Google Ads:"
327 cat google.adsense
328 end_box
331 echo '</div>'
332 } | sed -s "s/^/$indent/"
335 # timestamps will not need padding to sort correctly, for some time...
336 get_blog_entries |
337 while read timestamp filename title
339 echo "<h6>$(make_date $timestamp)</h6>"
340 echo "<a name=$timestamp>"
341 echo "<h2>$title</h2>"
342 echo ""
343 echo "<p>"
344 sed 1d < $filename | markup
345 echo "</p>"
346 done |
347 sed -e "s/^./$indent&/" \
348 -e "/<pre>/,/<\/pre>/s/^$indent//"
350 cat << EOF
351 </div>
352 </body>
353 </html>
357 generate_rss () {
358 echo '<?xml version="1.0" encoding="utf-8"?>'
359 echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
360 echo '<channel>'
361 echo "<title>Dscho's blog</title>"
362 echo "<link>$URLPREFIX${URL}index.html</link>"
363 self="$URLPREFIX$ORIGURL$RSS"
364 selfattribs='rel="self" type="application/rss+xml"'
365 echo "<atom:link href=\"$self\" $selfattribs/>"
366 echo '<description>A few stories told by Dscho</description>'
367 echo "<lastBuildDate>$(date --rfc-2822)</lastBuildDate>"
368 echo '<language>en-us</language>'
370 get_blog_entries |
371 while read timestamp filename title
373 # remove all tags
374 title=$(echo "$title" | sed 's/<[^>]*>//g')
375 echo '<item>'
376 echo "<title>$title</title>"
377 echo "<link>$URLPREFIX${URL}index.html#$timestamp</link>"
378 echo "<guid>$URLPREFIX${URL}index.html#$timestamp</guid>"
379 echo "<pubDate>$(date --rfc-2822 -d @$timestamp)</pubDate>"
380 description="$(cat < $filename | markup invert-bash)"
381 echo "<description><![CDATA[$description]]></description>"
382 echo "</item>"
383 done
385 echo '</channel>'
386 echo '</rss>'
389 get_image_files () {
390 git ls-files |
391 grep -v '\.\(rss\|html\|gitignore\|in\|sh\|txt\|adsense\)$'
394 remove_old_entries () {
395 count=$(ls source-*.txt | wc -l)
396 test $MAXENTRIES -ge $count && return
398 for file in source-*.txt
400 test $MAXENTRIES -lt $count || break
401 git rm $file > /dev/null || return 1
402 count=$(($count-1))
403 echo $file
404 done
406 # remove no-longer referenced images
407 image_files=$(get_image_files)
408 referenced_files="$(cat source-*.txt |
409 tr ']|' '\n' |
410 sed -ne 's/\[\[\(Image\|SVG\)://p') $BACKGROUNDIMG"
411 for file in $(echo $image_files $referenced_files $referenced_files |
412 tr ' ' '\n' | sort | uniq -u)
414 git rm $file > /dev/null || return 1
415 echo $file
416 done
419 # never, ever have spaces in the file names
420 commit_new_images () {
421 files="$(remove_old_entries) $RSS $BACKGROUNDIMG" ||
422 die "Could not remove old entries"
425 generate_rss > $RSS &&
426 git add $RSS ||
427 die "Could not generate $RSS"
429 for image in $(cat source-* |
430 tr ' ]|' '\n' |
431 sed -n 's/.*\[\[\(Image\|SVG\)://p' |
432 sort |
433 uniq)
435 git add $image || die "Could not git add image $image"
436 files="$files $image"
437 done
439 git update-index --refresh &&
440 git diff-files --quiet -- $files &&
441 git diff --cached --quiet HEAD -- $files ||
442 git commit -s -m "Housekeeping on $(make_date $now)" $files
445 get_image_url () {
446 test ! -z "$DRYRUN" && echo "$1" && return
447 rev=$(git rev-list -1 HEAD -- $1)
448 test -z "$rev" && die "No revision found for $1"
449 echo "$BLOBPLAIN;hb=$rev;f=$1"
452 handle_svg_file () {
453 # for some reason, Firefox adds scrollbars, so nudge the width a bit
454 width=$(sed -ne 's/.* width="\([^"]*\).*/\1/p' -e '/<metadata/q' < "$1")
455 test -z "$width" || width=" width=$(($width+5))"
456 url=$(get_image_url "$1")
457 cat << EOF
458 <center>
459 <table border=0>
460 <tr>
461 <td align=center>
462 <embed type="image/svg+xml"
463 src="$url"$width />
464 </td>
465 </tr>
466 <tr>
467 <td align=center>
468 <a href=$url>$1</a>
469 </td>
470 </tr>
471 </table>
472 </center>
476 handle_image_file () {
477 echo "<center><img src=$(get_image_url "${1%% *}") ${1#* }></center>"
482 # parse command line option
483 case "$1" in
484 *dry*) DRYRUN=1; export DRYRUN; shift;;
485 *show*) firefox "$(pwd)"/$TEST; exit;;
486 *remote*) firefox $URLPREFIX$URL$OUTPUT; exit;;
487 handle)
488 shift
489 case "$1" in
490 "[[SVG:"*) handle_svg_file "$(chomp "$*" '\[\[SVG:' '\]\]')";;
491 "[[Image:"*) handle_image_file "$(chomp "$*" '\[\[Image:' '\]\]')";;
492 esac
493 exit
495 '') ;;
496 *) die "Unknown command: $1";;
497 esac
499 test "$#" = 0 ||
500 die "Usage: $0 [--dry-run]"
502 # make sure we're on the correct branch
503 test refs/heads/$BRANCH = $(git symbolic-ref HEAD) ||
504 die "Not on branch $BRANCH"
506 # make sure there are no uncommitted changes
507 git update-index --refresh &&
508 git diff-files --quiet ||
509 die "Have unstaged changes!"
511 # rename the new blog entry if it exists
512 now=$(date +%s)
513 test ! -f $NEW || {
514 mv -i $NEW source-$now.txt &&
515 git add source-$now.txt
516 } ||
517 die "Could not rename source.txt"
519 # commit the images that are referenced and not yet committed
520 test ! -z "$DRYRUN" ||
521 commit_new_images ||
522 die "Could not commit new images"
524 # to find the images reliably, we have to use the commit name, not the branch
525 # we use the latest commit touching an image file.
526 IMAGEFILES="$(get_image_files)"
527 REV=$(git rev-list -1 HEAD -- $IMAGEFILES)
528 test -z "$REV" && REV=$BRANCH
529 URL="$BLOBPLAIN;hb=$REV;f="
531 if test ! -z "$DRYRUN"
532 then
533 # Output to test.html and have local links into the current directory
534 OUTPUT=$TEST
535 URL=
538 make_html > $OUTPUT || die "Could not write $OUTPUT"
540 test ! -z "$DRYRUN" && {
541 move_new_entry_back
542 exit
545 git add $OUTPUT &&
546 git commit -s -m "Update $(make_date $now)" &&
547 git push origin +$BRANCH