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 test $box_count = 0 ||
echo "<br>"
232 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
233 echo "<tr><th>$1</th></tr>"
238 echo "</td></tr></table>"
239 box_count
=$
(($box_count+1))
244 body_style
="width:800px"
245 body_style
="$body_style;background-image:url($URL$BACKGROUNDIMG)"
246 body_style
="$body_style;background-repeat:repeat-y"
247 body_style
="$body_style;background-attachment:scroll"
248 body_style
="$body_style;padding:0px;"
249 text_style
="width:610px"
250 text_style
="$text_style;margin-left:120px"
251 text_style
="$text_style;margin-top:50px"
252 text_style
="$text_style;align:left"
253 text_style
="$text_style;vertical-align:top;"
257 <title>$TITLE</title>
258 <meta http-equiv="Content-Type"
259 content="text/html; charset=UTF-8"/>
261 <body style="$body_style">
262 <div style="$text_style">
269 toc_style
="position:absolute;top:50px;left:810px;width=$toc_width"
271 echo "<div style=\"$toc_style\">"
272 begin_box
"Table of contents:"
275 while read timestamp filename title
277 date="$(date +"%d
%b
%Y
" -d @$timestamp)"
278 echo "<li><a href=#$timestamp>$date $title</a>"
282 last_removed_entry
=$
(get_last_removed_entry
)
283 test -z "$last_removed_entry" ||
{
284 commit
=$
(git log
--pretty=format
:%H
--diff-filter=AM \
285 -- $last_removed_entry |
287 previous
="$REMOTEREPOSITORY?a=blob_plain;hb=$commit"
288 echo "<a href=$previous;f=index.html>Older posts</a>"
293 rss_style
="background-color:orange;text-decoration:none"
294 rss_style
="$rss_style;color:white;font-family:sans-serif;"
296 echo '<div style="text-align:right;">'
297 echo "<a href=\"$ORIGURL$RSS\""
298 echo ' title="Subscribe to my RSS feed"'
299 echo ' class="rss" rel="nofollow"'
300 echo " style=\"$rss_style\">RSS</a>"
304 test -f about.html
&& {
305 begin_box
"About this blog:"
311 test -f links.html
&& {
318 test -z "$DRYRUN" && test -f google.adsense
&& {
319 begin_box
"Google Ads:"
325 } |
sed -s "s/^/$indent/"
328 # timestamps will not need padding to sort correctly, for some time...
330 while read timestamp filename title
332 echo "<h6>$(make_date $timestamp)</h6>"
333 echo "<a name=$timestamp>"
334 echo "<h2>$title</h2>"
337 sed 1d
< $filename | markup
340 sed -e "s/^./$indent&/" \
341 -e "/<pre>/,/<\/pre>/s/^$indent//"
351 echo '<?xml version="1.0" encoding="utf-8"?>'
352 echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
354 echo "<title>Dscho's blog</title>"
355 echo "<link>$URLPREFIX${URL}index.html</link>"
356 self
="$URLPREFIX$ORIGURL$RSS"
357 selfattribs
='rel="self" type="application/rss+xml"'
358 echo "<atom:link href=\"$self\" $selfattribs/>"
359 echo '<description>A few stories told by Dscho</description>'
360 echo "<lastBuildDate>$(date --rfc-2822)</lastBuildDate>"
361 echo '<language>en-us</language>'
364 while read timestamp filename title
367 title
=$
(echo "$title" |
sed 's/<[^>]*>//g')
369 echo "<title>$title</title>"
370 echo "<link>$URLPREFIX${URL}index.html#$timestamp</link>"
371 echo "<guid>$URLPREFIX${URL}index.html#$timestamp</guid>"
372 echo "<pubDate>$(date --rfc-2822 -d @$timestamp)</pubDate>"
373 description
="$(cat < $filename | markup invert-bash)"
374 echo "<description><![CDATA[$description]]></description>"
384 grep -v '\.\(rss\|html\|gitignore\|in\|sh\|txt\|adsense\)$'
387 remove_old_entries
() {
388 count
=$
(ls source-
*.txt |
wc -l)
389 test $MAXENTRIES -ge $count && return
391 for file in source-
*.txt
393 test $MAXENTRIES -lt $count ||
break
394 git
rm $file > /dev
/null ||
return 1
399 # remove no-longer referenced images
400 image_files
=$
(get_image_files
)
401 referenced_files
="$(cat source-*.txt |
403 sed -ne 's/\[\[\(Image\|SVG\)://p') $BACKGROUNDIMG"
404 for file in $
(echo $image_files $referenced_files $referenced_files |
405 tr ' ' '\n' |
sort |
uniq -u)
407 git
rm $file > /dev
/null ||
return 1
412 # never, ever have spaces in the file names
413 commit_new_images
() {
414 files
="$(remove_old_entries) $RSS $BACKGROUNDIMG" ||
415 die
"Could not remove old entries"
418 generate_rss
> $RSS &&
420 die
"Could not generate $RSS"
422 for image
in $
(cat source-
* |
424 sed -n 's/.*\[\[\(Image\|SVG\)://p' |
428 git add
$image || die
"Could not git add image $image"
429 files
="$files $image"
432 git update-index
--refresh &&
433 git diff-files
--quiet -- $files &&
434 git
diff --cached --quiet HEAD
-- $files ||
435 git commit
-s -m "Commit some images on $(make_date $now)" $files
439 test ! -z "$DRYRUN" && echo "$1" && return
440 rev=$
(git rev-list
-1 HEAD
-- $1)
441 test -z "$rev" && die
"No revision found for $1"
442 echo "$REMOTEREPOSITORY?a=blob_plain;hb=$rev;f=$1"
446 # for some reason, Firefox adds scrollbars, so nudge the width a bit
447 width
=$
(sed -ne 's/.* width="\([^"]*\).*/\1/p' -e '/<metadata/q' < "$1")
448 test -z "$width" || width
=" width=$(($width+5))"
449 url
=$
(get_image_url
"$1")
455 <embed type="image/svg+xml"
469 handle_image_file
() {
470 echo "<center><img src=$(get_image_url "${1%% *}") ${1#* }></center>"
475 # parse command line option
477 *dry
*) DRYRUN
=1; export DRYRUN
; shift;;
478 *show
*) firefox
"$(pwd)"/$TEST; exit;;
479 *remote
*) firefox
$URLPREFIX$URL$OUTPUT; exit;;
483 "[[SVG:"*) handle_svg_file
"$(chomp "$
*" '\[\[SVG:' '\]\]')";;
484 "[[Image:"*) handle_image_file
"$(chomp "$
*" '\[\[Image:' '\]\]')";;
489 *) die
"Unknown command: $1";;
493 die
"Usage: $0 [--dry-run]"
495 # make sure we're on the correct branch
496 test refs
/heads
/$BRANCH = $
(git symbolic-ref HEAD
) ||
497 die
"Not on branch $BRANCH"
499 # make sure there are no uncommitted changes
500 git update-index
--refresh &&
501 git diff-files
--quiet ||
502 die
"Have unstaged changes!"
504 # rename the new blog entry if it exists
507 mv -i $NEW source-
$now.txt
&&
508 git add source-
$now.txt
510 die
"Could not rename source.txt"
512 # commit the images that are referenced and not yet committed
513 test ! -z "$DRYRUN" ||
515 die
"Could not commit new images"
517 # to find the images reliably, we have to use the commit name, not the branch
518 # we use the latest commit touching an image file.
519 IMAGEFILES
="$(get_image_files)"
520 REV
=$
(git rev-list
-1 HEAD
-- $IMAGEFILES)
521 test -z "$REV" && REV
=$BRANCH
522 URL
="$REMOTEREPOSITORY?a=blob_plain;hb=$REV;f="
524 if test ! -z "$DRYRUN"
526 # Output to test.html and have local links into the current directory
531 make_html
> $OUTPUT || die
"Could not write $OUTPUT"
533 test ! -z "$DRYRUN" && {
539 git commit
-s -m "Update $(make_date $now)" &&
540 git push origin
+$BRANCH