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="
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\(\\|[^\]]*\)\?\]\]"
131 *invert-bash
*) bash_bg
=white
; bash_fg
=black
;;
132 *) bash_bg
=black
; bash_fg
=white
;;
134 sed -e 's!^$!</p><p>!' \
135 -e "$(markup_substitution "''" i)" \
136 -e "$(markup_substitution "_
" u)" \
137 -e 's!IMHO!in my humble opinion!g' \
138 -e 's!BTW!By the way,!g' \
139 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
140 -e 's!:-)!\☺!g' \
141 -e "s!$image_pattern2!<center><img src=$URL\1></center>!g" \
142 -e "s!\\[\\[SVG:\\([^]]*\\)\]\]!$THIS handle &!e" \
144 border=1 bgcolor='$bash_bg'>\
145 <tr><td bgcolor=lightblue colspan=3>\
149 <table cellspacing=5 border=0\
150 style="color:'$bash_fg';">\
153 -e 's!</bash>! </pre>\
161 # output lines containing <timestamp> <filename> <title>
162 get_blog_entries
() {
163 for file in $
(ls -r source-
*.txt
)
165 basename=${file%.txt}
166 timestamp
=${basename#source-}
167 title
="$(sed 1q < $file | markup)"
168 echo "$timestamp $file $title"
174 body_style
="width:800px"
175 body_style
="$body_style;background-image:url(${URL}paper.jpg)"
176 body_style
="$body_style;background-repeat:repeat-y"
177 body_style
="$body_style;background-attachment:scroll"
178 body_style
="$body_style;padding:0px;"
179 text_style
="width:610px"
180 text_style
="$text_style;margin-left:120px"
181 text_style
="$text_style;margin-top:50px"
182 text_style
="$text_style;align:left"
183 text_style
="$text_style;vertical-align:top;"
187 <title>$TITLE</title>
188 <meta http-equiv="Content-Type"
189 content="text/html; charset=UTF-8"/>
191 <body style="$body_style">
192 <div style="$text_style">
199 toc_style
="position:absolute;top:50px;left:810px;width=$toc_width"
201 echo "<div style=\"$toc_style\">"
202 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
203 echo "<tr><th>Table of contents:</th></tr>"
207 while read timestamp filename title
209 date="$(date +"%d
%b
%Y
" -d @$timestamp)"
210 echo "<li><a href=#$timestamp>$date $title</a>"
213 echo '</td></tr></table>'
216 rss_style
="background-color:orange;text-decoration:none"
217 rss_style
="$rss_style;color:white;font-family:sans-serif;"
219 echo '<div style="text-align:right;">'
220 echo "<a href=\"$ORIGURL$RSS\""
221 echo ' title="Subscribe to my RSS feed"'
222 echo ' class="rss" rel="nofollow"'
223 echo " style=\"$rss_style\">RSS</a>"
227 test -f links.html
&& {
229 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
230 echo "<tr><th>Links:</th></tr>"
233 echo "</td></tr></table>"
237 test -z "$DRYRUN" && test -f google.adsense
&& {
239 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
240 echo "<tr><th>Google Ads:</th></tr>"
241 echo "<tr><td align=center>"
243 echo "</td></tr></table>"
247 } |
sed -s "s/^/$indent/"
250 # timestamps will not need padding to sort correctly, for some time...
252 while read timestamp filename title
254 echo "<h6>$(make_date $timestamp)</h6>"
255 echo "<a name=$timestamp>"
256 echo "<h2>$title</h2>"
259 sed 1d
< $filename | markup
262 sed -e "s/^./$indent&/" \
263 -e "/<pre>/,/<\/pre>/s/^$indent//"
273 echo '<?xml version="1.0" encoding="utf-8"?>'
274 echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
276 echo "<title>Dscho's blog</title>"
277 echo "<link>$URLPREFIX${URL}index.html</link>"
278 self
="$URLPREFIX$ORIGURL$RSS"
279 selfattribs
='rel="self" type="application/rss+xml"'
280 echo "<atom:link href=\"$self\" $selfattribs/>"
281 echo '<description>A few stories told by Dscho</description>'
282 echo "<lastBuildDate>$(date --rfc-2822)</lastBuildDate>"
283 echo '<language>en-us</language>'
286 while read timestamp filename title
289 echo "<title>$title</title>"
290 echo "<link>$URLPREFIX${URL}index.html#$timestamp</link>"
291 echo "<guid>$URLPREFIX${URL}index.html#$timestamp</guid>"
292 echo "<pubDate>$(date --rfc-2822 -d @$timestamp)</pubDate>"
293 description
="$(cat < $filename | markup invert-bash)"
294 echo "<description><![CDATA[$description]]></description>"
302 # never, ever have spaces in the file names
303 commit_new_images
() {
304 generate_rss
> $RSS &&
306 die
"Could not generate $RSS"
309 for image
in $
(cat source-
* |
311 sed -n 's/.*\[\[\(Image\|SVG\)://p' |
315 git add
$image || die
"Could not git add image $image"
316 images
="$images $image"
319 git update-index
--refresh &&
320 git diff-files
--quiet -- $images &&
321 git
diff --cached --quiet HEAD
-- $images ||
322 git commit
-s -m "Commit some images on $(make_date $now)" $images
326 # for some reason, Firefox adds scrollbars, so nudge the width a bit
327 width
=$
(sed -ne 's/.* width="\([^"]*\).*/\1/p' -e '/<metadata/q' < "$1")
328 test -z "$width" || width
=" width=$(($width+5))"
332 REV
=$
(git rev-list
-1 HEAD
-- $1)
334 URL
="$REMOTEREPOSITORY?a=blob_plain;hb=$REV;f="
341 <embed type="image/svg+xml"
342 src="$URL$1"$width />
347 <a href=$URL$1>$1</a>
357 # parse command line option
359 *dry
*) DRYRUN
=1; export DRYRUN
; shift;;
360 *show
*) firefox
"$(pwd)"/$TEST; exit;;
361 *remote
*) firefox
$URLPREFIX$URL$OUTPUT; exit;;
364 "[[SVG:"*) file=${2#??SVG:}; file=${file%??}; handle_svg_file
"$file";;
371 die
"Usage: $0 [--dry-run]"
373 # make sure we're on the correct branch
374 test refs
/heads
/$BRANCH = $
(git symbolic-ref HEAD
) ||
375 die
"Not on branch $BRANCH"
377 # make sure there are no uncommitted changes
378 git update-index
--refresh &&
379 git diff-files
--quiet ||
380 die
"Have unstaged changes!"
382 # rename the new blog entry if it exists
385 mv -i $NEW source-
$now.txt
&&
386 git add source-
$now.txt
388 die
"Could not rename source.txt"
390 # commit the images that are referenced and not yet committed
391 test ! -z "$DRYRUN" ||
393 die
"Could not commit new images"
395 # to find the images reliably, we have to use the commit name, not the branch
396 # we use the latest commit touching an image file.
397 IMAGEFILES
="$(git ls-files |
398 grep -v '\.\(rss\|html\|gitignore\|in\|sh\|txt\|adsense\)$')"
399 REV
=$
(git rev-list
-1 HEAD
-- $IMAGEFILES)
400 test -z "$REV" && REV
=$BRANCH
401 URL
="$REMOTEREPOSITORY?a=blob_plain;hb=$REV;f="
403 if test ! -z "$DRYRUN"
405 # Output to test.html and have local links into the current directory
410 make_html
> $OUTPUT || die
"Could not write $OUTPUT"
412 test ! -z "$DRYRUN" && {
418 git commit
-s -m "Update $(make_date $now)" &&
419 git push origin
+$BRANCH