Unbreak images
[git/dscho.git] / upload.sh
blob92c4a3295f8b0e92fc1041159b95a8b002b9ffcc
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 # transform markup in stdin to HTML
179 markup () {
180 case "$*" in
181 *invert-bash*) bash_bg=white; bash_fg=black;;
182 *) bash_bg=black; bash_fg=white;;
183 esac
184 sed -e 's!^$!</p><p>!' \
185 -e "$(markup_substitution "''" i)" \
186 -e "$(markup_substitution "_" u)" \
187 -e 's!IMHO!in my humble opinion!g' \
188 -e 's!BTW!By the way,!g' \
189 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
190 -e 's!:-)!\&#x263a;!g' \
191 -e "s!\\[\\[\(Image\|SVG\):.*!$THIS handle &!e" \
192 -e 's!<bash>!<table\
193 border=1 bgcolor='$bash_bg'>\
194 <tr><td bgcolor=lightblue colspan=3>\
195 \&nbsp;\
196 </td></tr>\
197 <tr><td>\
198 <table cellspacing=5 border=0\
199 style="color:'$bash_fg';">\
200 <tr><td>\
201 <pre>!' \
202 -e 's!</bash>! </pre>\
203 </td></tr>\
204 </table>\
205 </td></tr>\
206 </table>!' \
210 # output lines containing <timestamp> <filename> <title>
211 get_blog_entries () {
212 for file in $(ls -r source-*.txt)
214 timestamp=$(chomp $file source- .txt)
215 title="$(sed 1q < $file | markup)"
216 echo "$timestamp $file $title"
217 done
220 get_last_removed_entry () {
221 git log --pretty=format: --name-only --diff-filter=D HEAD |
222 while read line
224 case "$line" in
225 source-*.txt) file=$line;;
226 '') test -z "$file" || {
227 echo "$file"
228 break
230 esac
231 done
234 box_count=0
235 begin_box () {
236 test $box_count = 0 || echo "<br>"
237 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
238 echo "<tr><th>$1</th></tr>"
239 echo "<tr><td>"
242 end_box () {
243 echo "</td></tr></table>"
244 box_count=$(($box_count+1))
247 # make HTML page
248 make_html () {
249 body_style="width:800px"
250 body_style="$body_style;background-image:url($URL$BACKGROUNDIMG)"
251 body_style="$body_style;background-repeat:repeat-y"
252 body_style="$body_style;background-attachment:scroll"
253 body_style="$body_style;padding:0px;"
254 text_style="width:610px"
255 text_style="$text_style;margin-left:120px"
256 text_style="$text_style;margin-top:50px"
257 text_style="$text_style;align:left"
258 text_style="$text_style;vertical-align:top;"
259 cat << EOF
260 <html>
261 <head>
262 <title>$TITLE</title>
263 <meta http-equiv="Content-Type"
264 content="text/html; charset=UTF-8"/>
265 </head>
266 <body style="$body_style">
267 <div style="$text_style">
268 <h1>$TITLE</h1>
270 indent='\t\t\t'
272 # make toc
273 toc_width=400px
274 toc_style="position:absolute;top:50px;left:810px;width=$toc_width"
276 echo "<div style=\"$toc_style\">"
277 begin_box "Table of contents:"
278 echo '<p><ul>'
279 get_blog_entries |
280 while read timestamp filename title
282 date="$(date +"%d %b %Y" -d @$timestamp)"
283 echo "<li><a href=#$timestamp>$date $title</a>"
284 done
285 echo '</ul></p>'
286 file=
287 last_removed_entry=$(get_last_removed_entry)
288 test -z "$last_removed_entry" || {
289 commit=$(git log --pretty=format:%H --diff-filter=AM \
290 -- $last_removed_entry |
291 head -n 1)
292 previous="$BLOBPLAIN;hb=$commit"
293 echo "<a href=$previous;f=index.html>Older posts</a>"
295 end_box
297 # RSS feed
298 rss_style="background-color:orange;text-decoration:none"
299 rss_style="$rss_style;color:white;font-family:sans-serif;"
300 echo '<br>'
301 echo '<div style="text-align:right;">'
302 echo "<a href=\"$ORIGURL$RSS\""
303 echo ' title="Subscribe to my RSS feed"'
304 echo ' class="rss" rel="nofollow"'
305 echo " style=\"$rss_style\">RSS</a>"
306 echo '</div>'
308 # About
309 test -f about.html && {
310 begin_box "About this blog:"
311 cat about.html
312 end_box
315 # Links
316 test -f links.html && {
317 begin_box "Links:"
318 cat links.html
319 end_box
322 # Google AdSense
323 test -z "$DRYRUN" && test -f google.adsense && {
324 begin_box "Google Ads:"
325 cat google.adsense
326 end_box
329 echo '</div>'
330 } | sed -s "s/^/$indent/"
333 # timestamps will not need padding to sort correctly, for some time...
334 get_blog_entries |
335 while read timestamp filename title
337 echo "<h6>$(make_date $timestamp)</h6>"
338 echo "<a name=$timestamp>"
339 echo "<h2>$title</h2>"
340 echo ""
341 echo "<p>"
342 sed 1d < $filename | markup
343 echo "</p>"
344 done |
345 sed -e "s/^./$indent&/" \
346 -e "/<pre>/,/<\/pre>/s/^$indent//"
348 cat << EOF
349 </div>
350 </body>
351 </html>
355 generate_rss () {
356 echo '<?xml version="1.0" encoding="utf-8"?>'
357 echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
358 echo '<channel>'
359 echo "<title>Dscho's blog</title>"
360 echo "<link>$URLPREFIX${URL}index.html</link>"
361 self="$URLPREFIX$ORIGURL$RSS"
362 selfattribs='rel="self" type="application/rss+xml"'
363 echo "<atom:link href=\"$self\" $selfattribs/>"
364 echo '<description>A few stories told by Dscho</description>'
365 echo "<lastBuildDate>$(date --rfc-2822)</lastBuildDate>"
366 echo '<language>en-us</language>'
368 get_blog_entries |
369 while read timestamp filename title
371 # remove all tags
372 title=$(echo "$title" | sed 's/<[^>]*>//g')
373 echo '<item>'
374 echo "<title>$title</title>"
375 echo "<link>$URLPREFIX${URL}index.html#$timestamp</link>"
376 echo "<guid>$URLPREFIX${URL}index.html#$timestamp</guid>"
377 echo "<pubDate>$(date --rfc-2822 -d @$timestamp)</pubDate>"
378 description="$(cat < $filename | markup invert-bash)"
379 echo "<description><![CDATA[$description]]></description>"
380 echo "</item>"
381 done
383 echo '</channel>'
384 echo '</rss>'
387 get_image_files () {
388 git ls-files |
389 grep -v '\.\(rss\|html\|gitignore\|in\|sh\|txt\|adsense\)$'
392 remove_old_entries () {
393 count=$(ls source-*.txt | wc -l)
394 test $MAXENTRIES -ge $count && return
396 for file in source-*.txt
398 test $MAXENTRIES -lt $count || break
399 git rm $file > /dev/null || return 1
400 count=$(($count-1))
401 echo $file
402 done
404 # remove no-longer referenced images
405 image_files=$(get_image_files)
406 referenced_files="$(cat source-*.txt |
407 tr ']|' '\n' |
408 sed -ne 's/\[\[\(Image\|SVG\)://p') $BACKGROUNDIMG"
409 for file in $(echo $image_files $referenced_files $referenced_files |
410 tr ' ' '\n' | sort | uniq -u)
412 git rm $file > /dev/null || return 1
413 echo $file
414 done
417 # never, ever have spaces in the file names
418 commit_new_images () {
419 files="$(remove_old_entries) $RSS $BACKGROUNDIMG" ||
420 die "Could not remove old entries"
423 generate_rss > $RSS &&
424 git add $RSS ||
425 die "Could not generate $RSS"
427 for image in $(cat source-* |
428 tr ' ]|' '\n' |
429 sed -n 's/.*\[\[\(Image\|SVG\)://p' |
430 sort |
431 uniq)
433 git add $image || die "Could not git add image $image"
434 files="$files $image"
435 done
437 git update-index --refresh &&
438 git diff-files --quiet -- $files &&
439 git diff --cached --quiet HEAD -- $files ||
440 git commit -s -m "Housekeeping on $(make_date $now)" $files
443 get_image_url () {
444 test ! -z "$DRYRUN" && echo "$1" && return
445 rev=$(git rev-list -1 HEAD -- $1)
446 test -z "$rev" && die "No revision found for $1"
447 echo "$BLOBPLAIN;hb=$rev;f=$1"
450 handle_svg_file () {
451 # for some reason, Firefox adds scrollbars, so nudge the width a bit
452 width=$(sed -ne 's/.* width="\([^"]*\).*/\1/p' -e '/<metadata/q' < "$1")
453 test -z "$width" || width=" width=$(($width+5))"
454 url=$(get_image_url "$1")
455 cat << EOF
456 <center>
457 <table border=0>
458 <tr>
459 <td align=center>
460 <embed type="image/svg+xml"
461 src="$url"$width />
462 </td>
463 </tr>
464 <tr>
465 <td align=center>
466 <a href=$url>$1</a>
467 </td>
468 </tr>
469 </table>
470 </center>
474 handle_image_file () {
475 echo "<center><img src=$(get_image_url "${1%% *}") ${1#* }></center>"
480 # parse command line option
481 case "$1" in
482 *dry*) DRYRUN=1; export DRYRUN; shift;;
483 *show*) firefox "$(pwd)"/$TEST; exit;;
484 *remote*) firefox $URLPREFIX$URL$OUTPUT; exit;;
485 handle)
486 shift
487 case "$1" in
488 "[[SVG:"*) handle_svg_file "$(chomp "$*" '\[\[SVG:' '\]\]')";;
489 "[[Image:"*) handle_image_file "$(chomp "$*" '\[\[Image:' '\]\]')";;
490 esac
491 exit
493 '') ;;
494 *) die "Unknown command: $1";;
495 esac
497 test "$#" = 0 ||
498 die "Usage: $0 [--dry-run]"
500 # make sure we're on the correct branch
501 test refs/heads/$BRANCH = $(git symbolic-ref HEAD) ||
502 die "Not on branch $BRANCH"
504 # make sure there are no uncommitted changes
505 git update-index --refresh &&
506 git diff-files --quiet ||
507 die "Have unstaged changes!"
509 # rename the new blog entry if it exists
510 now=$(date +%s)
511 test ! -f $NEW || {
512 mv -i $NEW source-$now.txt &&
513 git add source-$now.txt
514 } ||
515 die "Could not rename source.txt"
517 # commit the images that are referenced and not yet committed
518 test ! -z "$DRYRUN" ||
519 commit_new_images ||
520 die "Could not commit new images"
522 # to find the images reliably, we have to use the commit name, not the branch
523 # we use the latest commit touching an image file.
524 IMAGEFILES="$(get_image_files)"
525 REV=$(git rev-list -1 HEAD -- $IMAGEFILES)
526 test -z "$REV" && REV=$BRANCH
527 URL="$BLOBPLAIN;hb=$REV;f="
529 if test ! -z "$DRYRUN"
530 then
531 # Output to test.html and have local links into the current directory
532 OUTPUT=$TEST
533 URL=
536 make_html > $OUTPUT || die "Could not write $OUTPUT"
538 test ! -z "$DRYRUN" && {
539 move_new_entry_back
540 exit
543 git add $OUTPUT &&
544 git commit -s -m "Update $(make_date $now)" &&
545 git push origin +$BRANCH