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 "source.txt" exists, it is
12 # automatically renamed using the current timestamp.
14 # TODO: have a configurable maximum number of entries per page, and links
17 # make sure we're in the correct working directory
20 GITWEBURL
="$(git config gitweb.url)"
21 test -z "$GITWEBURL" && {
22 echo "Please set gitweb.url in the Git config first!" >&2
26 URLPREFIX
="$(dirname "$GITWEBURL")"/
27 REMOTEREPOSITORY
="$(basename "$GITWEBURL")"
29 URL
="$REMOTEREPOSITORY?a=blob_plain;hb=$BRANCH;f="
40 move_new_entry_back
() {
41 test -f source-
$now.txt
&&
42 mv source-
$now.txt
$NEW &&
43 git
rm --cached -f source-
$now.txt
53 # add illogical suffix
55 *1?|
*[04-9]) echo "$1th";;
62 make_chinese_hour
() {
93 make_roman_number
() {
96 ?
) digit_to_roman
$1 I V X
;;
97 ??
) echo $
(digit_to_roman
${1%?} X L C
)$
(make_roman_number
${1#?});;
98 ???
) echo $
(digit_to_roman
${1%??} C D M
)$
(make_roman_number
${1#?});;
99 ????
) echo $
(digit_to_roman
${1%???} M
)$
(make_roman_number
${1#?});;
104 printf "%s, %s of %s, Anno Domini %s, at the hour of the %s\n" \
106 $
(nth $
(date +%e
-d @
$1)) \
108 $
(make_roman_number $
(date +%Y
-d @
$1)) \
109 $
(make_chinese_hour $
(date +%H
-d @
$1))
112 # make an argument for sed, to replace $1..$1 by <$2>..</$2>
113 markup_substitution
() {
115 ?
) echo "s/$1\\([^$1]*\\)$1/<$2>\\\\1<\/$2>/g";;
118 tmp2
="\\|${1%?}[^${1#?}]$tmp"
119 tmp3
="\\($tmp\\($tmp2\\($tmp2\\($tmp2\\)\\)\\)\\)"
120 echo "s/$1$tmp3$1/<$2>\\\\1<\/$2>/g"
125 # transform markup in stdin to HTML
127 image_pattern
="\\[\\[Image:\([^]]*\)"
128 image_pattern2
="$image_pattern\(\\|[^\]]*\)\?\]\]"
130 *invert-bash
*) bash_bg
=white
; bash_fg
=black
;;
131 *) bash_bg
=black
; bash_fg
=white
;;
133 sed -e 's!^$!</p><p>!' \
134 -e 's!IMHO!in my humble opinion!g' \
135 -e 's!BTW!By the way,!g' \
136 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
137 -e 's!:-)!\☺!g' \
138 -e "s!$image_pattern2!<center><img src=$URL\1></center>!g" \
140 border=1 bgcolor='$bash_bg'>\
141 <tr><td bgcolor=lightblue colspan=3>\
145 <table cellspacing=5 border=0\
146 style="color:'$bash_fg';">\
149 -e 's!</bash>! </pre>\
154 -e "$(markup_substitution "''" i)" \
155 -e "$(markup_substitution "_
" u)"
158 # output lines containing <timestamp> <filename> <title>
159 get_blog_entries
() {
160 for file in $
(ls -r source-
*.txt
)
162 basename=${file%.txt}
163 timestamp
=${basename#source-}
164 title
="$(sed 1q < $file | markup)"
165 echo "$timestamp $file $title"
171 body_style
="width:800px"
172 body_style
="$body_style;background-image:url(${URL}paper.jpg)"
173 body_style
="$body_style;background-repeat:repeat-y"
174 body_style
="$body_style;background-attachment:scroll"
175 body_style
="$body_style;padding:0px;"
176 text_style
="width:610px"
177 text_style
="$text_style;margin-left:120px"
178 text_style
="$text_style;margin-top:50px"
179 text_style
="$text_style;align:left"
180 text_style
="$text_style;vertical-align:top;"
184 <title>$TITLE</title>
185 <meta http-equiv="Content-Type"
186 content="text/html; charset=UTF-8"/>
188 <body style="$body_style">
189 <div style="$text_style">
196 toc_style
="position:absolute;top:50px;left:810px;width=$toc_width"
198 echo "<div style=\"$toc_style\">"
199 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
200 echo "<tr><th>Table of contents:</th></tr>"
204 while read timestamp filename title
206 date="$(date +"%d
%b
%Y
" -d @$timestamp)"
207 echo "<li><a href=#$timestamp>$date $title</a>"
210 echo '</td></tr></table>'
213 test -f links.html
&& {
215 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
216 echo "<tr><th>Links:</th></tr>"
219 echo "</td></tr></table>"
223 test -z "$DRYRUN" && {
225 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
226 echo "<tr><td align=center>"
228 <script type="text/javascript"><!--
229 google_ad_client = "pub-5106407705643819";
230 /* 300x250, created 1/22/09 */
231 google_ad_slot = "6468207338";
232 google_ad_width = 300;
233 google_ad_height = 250;
236 <script type="text/javascript"
237 src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
240 echo "</td></tr></table>"
244 rss_style
="background-color:orange;text-decoration:none"
245 rss_style
="$rss_style;color:white;"
247 echo '<div style="text-align:right;">'
248 echo "<a href=\"$ORIGURL$RSS\""
249 echo ' title="Subscribe to my RSS feed"'
250 echo ' class="rss" rel="nofollow"'
251 echo " style=\"$rss_style\">RSS</a>"
255 } |
sed -s "s/^/$indent/"
258 # timestamps will not need padding to sort correctly, for some time...
260 while read timestamp filename title
262 echo "<h6>$(make_date $timestamp)</h6>"
263 echo "<a name=$timestamp>"
264 echo "<h2>$title</h2>"
267 sed 1d
< $filename | markup
270 sed -e "s/^./$indent&/" \
271 -e "/<pre>/,/<\/pre>/s/^$indent//"
281 echo '<?xml version="1.0" encoding="utf-8"?>'
282 echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
284 echo "<title>Dscho's blog</title>"
285 echo "<link>$URLPREFIX${URL}index.html</link>"
286 self
="$URLPREFIX$ORIGURL$RSS"
287 selfattribs
='rel="self" type="application/rss+xml"'
288 echo "<atom:link href=\"$self\" $selfattribs/>"
289 echo '<description>A few stories told by Dscho</description>'
290 echo "<lastBuildDate>$(date --rfc-2822)</lastBuildDate>"
291 echo '<language>en-us</language>'
294 while read timestamp filename title
297 echo "<title>$title</title>"
298 echo "<link>$URLPREFIX${URL}index.html#$timestamp</link>"
299 echo "<guid>$URLPREFIX${URL}index.html#$timestamp</guid>"
300 echo "<pubDate>$(date --rfc-2822 -d @$timestamp)</pubDate>"
301 description
="$(cat < $filename | markup invert-bash)"
302 echo "<description><![CDATA[$description]]></description>"
310 # never, ever have spaces in the file names
311 commit_new_images
() {
312 generate_rss
> $RSS &&
314 die
"Could not generate $RSS"
317 for image
in $
(cat source-
* |
319 sed -n 's/.*\[\[Image://p' |
323 git add
$image || die
"Could not git add image $image"
324 images
="$images $image"
327 git update-index
--refresh &&
328 git diff-files
--quiet -- $images &&
329 git
diff --cached --quiet HEAD
-- $images ||
330 git commit
-s -m "Commit some images on $(make_date $now)" $images
335 # parse command line option
337 *dry
*) DRYRUN
=1; shift;;
338 *show
*) firefox
"$(pwd)"/$TEST; exit;;
339 *remote
*) firefox
$URLPREFIX$URL$OUTPUT; exit;;
343 die
"Usage: $0 [--dry-run]"
345 # make sure we're on the correct branch
346 test refs
/heads
/$BRANCH = $
(git symbolic-ref HEAD
) ||
347 die
"Not on branch $BRANCH"
349 # make sure there are no uncommitted changes
350 git update-index
--refresh &&
351 git diff-files
--quiet ||
352 die
"Have unstaged changes!"
354 # rename the new blog entry if it exists
357 mv -i $NEW source-
$now.txt
&&
358 git add source-
$now.txt
360 die
"Could not rename source.txt"
362 # commit the images that are referenced and not yet committed
363 test ! -z "$DRYRUN" ||
365 die
"Could not commit new images"
367 # to find the images reliably, we have to use the commit name, not the branch
368 # we use the latest commit touching an image file.
369 IMAGEFILES
="$(git ls-files |
370 grep -v '\.\(rss\|html\|gitignore\|in\|sh\|txt\)$')"
371 REV
=$
(git rev-list
-1 HEAD
-- $IMAGEFILES)
372 test -z "$REV" && REV
=$BRANCH
373 URL
="$REMOTEREPOSITORY?a=blob_plain;hb=$REV;f="
375 if test ! -z "$DRYRUN"
377 # Output to test.html and have local links into the current directory
382 make_html
> $OUTPUT || die
"Could not write $OUTPUT"
384 test ! -z "$DRYRUN" && {
390 git commit
-s -m "Update $(make_date $now)" &&
391 git push origin
+$BRANCH