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.
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.
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
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
50 GITWEBURL
="$(git config gitweb.url)"
51 test -z "$GITWEBURL" && {
52 echo "Please set gitweb.url in the Git config first!" >&2
57 value
=$
(git config blog.
$1)
58 test -z "$value" && value
=$2
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="
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
97 strip_prefix
"${1%$3}" "$2"
101 # add illogical suffix
103 *1?|
*[04-9]) echo "$1th";;
110 make_chinese_hour
() {
113 01|
02) echo Buffalo
;;
121 17|
18) echo Rooster
;;
141 make_roman_number
() {
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#?});;
152 printf "%s, %s of %s, Anno Domini %s, at the hour of the %s\n" \
154 $
(nth $
(date +%e
-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
() {
163 ?
) echo "s/$1\\([^$1]*\\)$1/<$2>\\\\1<\/$2>/g";;
166 tmp2
="\\|${1%?}[^${1#?}]$tmp"
167 tmp3
="\\($tmp\\($tmp2\\($tmp2\\($tmp2\\)\\)\\)\\)"
168 echo "s/$1$tmp3$1/<$2>\\\\1<\/$2>/g"
173 # transform markup in stdin to HTML
176 *invert-bash
*) bash_bg
=white
; bash_fg
=black
;;
177 *) bash_bg
=black
; bash_fg
=white
;;
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!:-)!\☺!g' \
186 -e "s!\\[\\[\(Image\|SVG\):.*!$THIS handle &!e" \
188 border=1 bgcolor='$bash_bg'>\
189 <tr><td bgcolor=lightblue colspan=3>\
193 <table cellspacing=5 border=0\
194 style="color:'$bash_fg';">\
197 -e 's!</bash>! </pre>\
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"
215 get_last_removed_entry
() {
216 git log
--pretty=format
: --name-only --diff-filter=D HEAD |
220 source-
*.txt
) file=$line;;
221 '') test -z "$file" ||
{
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;"
244 <title>$TITLE</title>
245 <meta http-equiv="Content-Type"
246 content="text/html; charset=UTF-8"/>
248 <body style="$body_style">
249 <div style="$text_style">
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>"
264 while read timestamp filename title
266 date="$(date +"%d
%b
%Y
" -d @$timestamp)"
267 echo "<li><a href=#$timestamp>$date $title</a>"
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 |
276 previous
="$REMOTEREPOSITORY?a=blob_plain;hb=$commit"
277 echo "<a href=$previous;f=index.html>Older posts</a>"
279 echo '</td></tr></table>'
282 rss_style
="background-color:orange;text-decoration:none"
283 rss_style
="$rss_style;color:white;font-family:sans-serif;"
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>"
293 test -f links.html
&& {
295 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
296 echo "<tr><th>Links:</th></tr>"
299 echo "</td></tr></table>"
303 test -z "$DRYRUN" && test -f google.adsense
&& {
305 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
306 echo "<tr><th>Google Ads:</th></tr>"
307 echo "<tr><td align=center>"
309 echo "</td></tr></table>"
313 } |
sed -s "s/^/$indent/"
316 # timestamps will not need padding to sort correctly, for some time...
318 while read timestamp filename title
320 echo "<h6>$(make_date $timestamp)</h6>"
321 echo "<a name=$timestamp>"
322 echo "<h2>$title</h2>"
325 sed 1d
< $filename | markup
328 sed -e "s/^./$indent&/" \
329 -e "/<pre>/,/<\/pre>/s/^$indent//"
339 echo '<?xml version="1.0" encoding="utf-8"?>'
340 echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
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>'
352 while read timestamp filename title
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>"
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
385 # remove no-longer referenced images
386 image_files
=$
(get_image_files
)
387 referenced_files
="$(cat source-*.txt |
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
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 &&
406 die
"Could not generate $RSS"
408 for image
in $
(cat source-
* |
410 sed -n 's/.*\[\[\(Image\|SVG\)://p' |
414 git add
$image || die
"Could not git add image $image"
415 files
="$files $image"
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
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"
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")
441 <embed type="image/svg+xml"
455 handle_image_file
() {
456 echo "<center><img src=$(get_image_url "${1%% *}") ${1#* }></center>"
461 # parse command line option
463 *dry
*) DRYRUN
=1; export DRYRUN
; shift;;
464 *show
*) firefox
"$(pwd)"/$TEST; exit;;
465 *remote
*) firefox
$URLPREFIX$URL$OUTPUT; exit;;
469 "[[SVG:"*) handle_svg_file
"$(chomp "$
*" '\[\[SVG:' '\]\]')";;
470 "[[Image:"*) handle_image_file
"$(chomp "$
*" '\[\[Image:' '\]\]')";;
475 *) die
"Unknown command: $1";;
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
493 mv -i $NEW source-
$now.txt
&&
494 git add source-
$now.txt
496 die
"Could not rename source.txt"
498 # commit the images that are referenced and not yet committed
499 test ! -z "$DRYRUN" ||
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"
512 # Output to test.html and have local links into the current directory
517 make_html
> $OUTPUT || die
"Could not write $OUTPUT"
519 test ! -z "$DRYRUN" && {
525 git commit
-s -m "Update $(make_date $now)" &&
526 git push origin
+$BRANCH