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: generate an RSS feed, too
15 # TODO: have a configurable maximum number of entries per page, and links
18 # make sure we're in the correct working directory
21 GITWEBURL
="$(git config gitweb.url)"
22 test -z "$GITWEBURL" && {
23 echo "Please set gitweb.url in the Git config first!" >&2
27 URLPREFIX
="$(dirname "$GITWEBURL")"/
28 REMOTEREPOSITORY
="$(basename "$GITWEBURL")"
30 URL
="$REMOTEREPOSITORY?a=blob_plain;hb=$BRANCH;f="
41 move_new_entry_back
() {
42 test -f source-
$now.txt
&&
43 mv source-
$now.txt
$NEW &&
44 git
rm --cached -f source-
$now.txt
54 # add illogical suffix
56 *1?|
*[04-9]) echo "$1th";;
63 make_chinese_hour
() {
94 make_roman_number
() {
97 ?
) digit_to_roman
$1 I V X
;;
98 ??
) echo $
(digit_to_roman
${1%?} X L C
)$
(make_roman_number
${1#?});;
99 ???
) echo $
(digit_to_roman
${1%??} C D M
)$
(make_roman_number
${1#?});;
100 ????
) echo $
(digit_to_roman
${1%???} M
)$
(make_roman_number
${1#?});;
105 printf "%s, %s of %s, Anno Domini %s, at the hour of the %s\n" \
107 $
(nth $
(date +%e
-d @
$1)) \
109 $
(make_roman_number $
(date +%Y
-d @
$1)) \
110 $
(make_chinese_hour $
(date +%H
-d @
$1))
113 # make an argument for sed, to replace $1..$1 by <$2>..</$2>
114 markup_substitution
() {
116 ?
) echo "s/$1\\([^$1]*\\)$1/<$2>\\\\1<\/$2>/g";;
119 tmp2
="\\|${1%?}[^${1#?}]$tmp"
120 tmp3
="\\($tmp\\($tmp2\\($tmp2\\($tmp2\\)\\)\\)\\)"
121 echo "s/$1$tmp3$1/<$2>\\\\1<\/$2>/g"
126 # transform markup in stdin to HTML
128 image_pattern
="\\[\\[Image:\([^]]*\)"
129 image_pattern2
="$image_pattern\(\\|[^\]]*\)\?\]\]"
130 sed -e 's!^$!</p><p>!' \
131 -e 's!IMHO!in my humble opinion!g' \
132 -e 's!BTW!By the way,!g' \
133 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
134 -e 's!:-)!\☺!g' \
135 -e "s!$image_pattern2!<center><img src=$URL\1></center>!g" \
137 border=1 bgcolor=black>\
138 <tr><td bgcolor=lightblue colspan=3>\
142 <table cellspacing=5 border=0\
143 style="color:#ffffff;">\
146 -e 's!</bash>! </pre>\
151 -e "$(markup_substitution "''" i)" \
152 -e "$(markup_substitution "_
" u)"
155 # output lines containing <timestamp> <filename> <title>
156 get_blog_entries
() {
157 for file in $
(ls -r source-
*.txt
)
159 basename=${file%.txt}
160 timestamp
=${basename#source-}
161 title
="$(sed 1q < $file | markup)"
162 echo "$timestamp $file $title"
168 body_style
="width:800px"
169 body_style
="$body_style;background-image:url(${URL}paper.jpg)"
170 body_style
="$body_style;background-repeat:repeat-y"
171 body_style
="$body_style;background-attachment:scroll"
172 body_style
="$body_style;padding:0px;"
173 text_style
="width:610px"
174 text_style
="$text_style;margin-left:120px"
175 text_style
="$text_style;margin-top:50px"
176 text_style
="$text_style;align:left"
177 text_style
="$text_style;vertical-align:top;"
181 <title>$TITLE</title>
182 <meta http-equiv="Content-Type"
183 content="text/html; charset=UTF-8"/>
185 <body style="$body_style">
186 <div style="$text_style">
193 toc_style
="position:absolute;top:50px;left:810px;width=$toc_width"
195 echo "<div style=\"$toc_style\">"
196 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
197 echo "<tr><th>Table of contents:</th></tr>"
201 while read timestamp filename title
203 date="$(date +"%d
%b
%Y
" -d @$timestamp)"
204 echo "<li><a href=#$timestamp>$date $title</a>"
207 echo '</td></tr></table>'
210 test -f links.html
&& {
212 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
213 echo "<tr><th>Links:</th></tr>"
216 echo "</td></tr></table>"
220 test -z "$DRYRUN" && {
222 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
223 echo "<tr><td align=center>"
225 <script type="text/javascript"><!--
226 google_ad_client = "pub-5106407705643819";
227 /* 300x250, created 1/22/09 */
228 google_ad_slot = "6468207338";
229 google_ad_width = 300;
230 google_ad_height = 250;
233 <script type="text/javascript"
234 src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
237 echo "</td></tr></table>"
241 rss_style
="background-color:orange;text-decoration:none"
242 rss_style
="$rss_style;color:white;"
244 echo '<div style="text-align:right;">'
245 echo "<a href=\"$ORIGURL$RSS\""
246 echo ' title="Subscribe to my RSS feed"'
247 echo ' class="rss" rel="nofollow"'
248 echo " style=\"$rss_style\">RSS</a>"
252 } |
sed -s "s/^/$indent/"
255 # timestamps will not need padding to sort correctly, for some time...
257 while read timestamp filename title
259 echo "<h6>$(make_date $timestamp)</h6>"
260 echo "<a name=$timestamp>"
261 echo "<h2>$title</h2>"
264 sed 1d
< $filename | markup
267 sed -e "s/^./$indent&/" \
268 -e "/<pre>/,/<\/pre>/s/^$indent//"
278 echo '<?xml version="1.0" encoding="utf-8"?>'
279 echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
281 echo "<title>Dscho's blog</title>"
282 echo "<link>$URLPREFIX${URL}index.html</link>"
283 self
="$URLPREFIX$ORIGURL$RSS"
284 selfattribs
='rel="self" type="application/rss+xml"'
285 echo "<atom:link href=\"$self\" $selfattribs/>"
286 echo '<description>A few stories told by Dscho</description>'
287 echo "<lastBuildDate>$(date --rfc-2822)</lastBuildDate>"
288 echo '<language>en-us</language>'
291 while read timestamp filename title
294 echo "<title>$title</title>"
295 echo "<link>$URLPREFIX${URL}index.html#$timestamp</link>"
296 echo "<guid>$URLPREFIX${URL}index.html#$timestamp</guid>"
297 echo "<pubDate>$(date --rfc-2822 -d @$timestamp)</pubDate>"
298 description
="$(cat < $filename | markup)"
299 echo "<description><![CDATA[$description]]></description>"
307 # never, ever have spaces in the file names
308 commit_new_images
() {
309 generate_rss
> $RSS &&
311 die
"Could not generate $RSS"
314 for image
in $
(cat source-
* |
316 sed -n 's/.*\[\[Image://p' |
320 git add
$image || die
"Could not git add image $image"
321 images
="$images $image"
324 git update-index
--refresh &&
325 git diff-files
--quiet -- $images &&
326 git
diff --cached --quiet HEAD
-- $images ||
327 git commit
-s -m "Commit some images on $(make_date $now)" $images
332 # parse command line option
334 *dry
*) DRYRUN
=1; shift;;
335 *show
*) firefox
"$(pwd)"/$TEST; exit;;
336 *remote
*) firefox
$URLPREFIX$URL$OUTPUT; exit;;
340 die
"Usage: $0 [--dry-run]"
342 # make sure we're on the correct branch
343 test refs
/heads
/$BRANCH = $
(git symbolic-ref HEAD
) ||
344 die
"Not on branch $BRANCH"
346 # make sure there are no uncommitted changes
347 git update-index
--refresh &&
348 git diff-files
--quiet ||
349 die
"Have unstaged changes!"
351 # rename the new blog entry if it exists
354 mv -i $NEW source-
$now.txt
&&
355 git add source-
$now.txt
357 die
"Could not rename source.txt"
359 # commit the images that are referenced and not yet committed
360 test ! -z "$DRYRUN" ||
362 die
"Could not commit new images"
364 # to find the images reliably, we have to use the commit name, not the branch
365 # we use the latest commit touching an image file.
366 IMAGEFILES
="$(git ls-files |
367 grep -v '\.\(html\|gitignore\|in\|sh\|txt\)$')"
368 REV
=$
(git rev-list
-1 HEAD
-- $IMAGEFILES)
369 test -z "$REV" && REV
=$BRANCH
370 URL
="$REMOTEREPOSITORY?a=blob_plain;hb=$REV;f="
372 if test ! -z "$DRYRUN"
374 # Output to test.html and have local links into the current directory
379 make_html
> $OUTPUT || die
"Could not write $OUTPUT"
381 test ! -z "$DRYRUN" && {
387 git commit
-s -m "Update $(make_date $now)" &&
388 git push origin
+$BRANCH