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="
39 move_new_entry_back
() {
40 test -f source-
$now.txt
&&
41 mv source-
$now.txt
$NEW &&
42 git
rm --cached -f source-
$now.txt
52 # add illogical suffix
54 *1?|
*[04-9]) echo "$1th";;
61 make_chinese_hour
() {
92 make_roman_number
() {
95 ?
) digit_to_roman
$1 I V X
;;
96 ??
) echo $
(digit_to_roman
${1%?} X L C
)$
(make_roman_number
${1#?});;
97 ???
) echo $
(digit_to_roman
${1%??} C D M
)$
(make_roman_number
${1#?});;
98 ????
) echo $
(digit_to_roman
${1%???} M
)$
(make_roman_number
${1#?});;
103 printf "%s, %s of %s, Anno Domini %s, at the hour of the %s\n" \
105 $
(nth $
(date +%e
-d @
$1)) \
107 $
(make_roman_number $
(date +%Y
-d @
$1)) \
108 $
(make_chinese_hour $
(date +%H
-d @
$1))
111 # make an argument for sed, to replace $1..$1 by <$2>..</$2>
112 markup_substitution
() {
114 ?
) echo "s/$1\\([^$1]*\\)$1/<$2>\\\\1<\/$2>/g";;
117 tmp2
="\\|${1%?}[^${1#?}]$tmp"
118 tmp3
="\\($tmp\\($tmp2\\($tmp2\\($tmp2\\)\\)\\)\\)"
119 echo "s/$1$tmp3$1/<$2>\\\\1<\/$2>/g"
124 # transform markup in stdin to HTML
126 image_pattern
="\\[\\[Image:\([^]]*\)"
127 image_pattern2
="$image_pattern\(\\|[^\]]*\)\?\]\]"
128 sed -e 's!^$!</p><p>!' \
129 -e 's!IMHO!in my humble opinion!g' \
130 -e 's!BTW!By the way,!g' \
131 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
132 -e 's!:-)!\☺!g' \
133 -e "s!$image_pattern2!<center><img src=$URL\1></center>!g" \
135 border=1 bgcolor=black>\
136 <tr><td bgcolor=lightblue colspan=3>\
140 <table cellspacing=5 border=0\
141 style="color:#ffffff;">\
144 -e 's!</bash>! </pre>\
149 -e "$(markup_substitution "''" i)" \
150 -e "$(markup_substitution "_
" u)"
155 body_style
="width:800px"
156 body_style
="$body_style;background-image:url(${URL}paper.jpg)"
157 body_style
="$body_style;background-repeat:repeat-y"
158 body_style
="$body_style;background-attachment:scroll"
159 body_style
="$body_style;padding:0px;"
160 text_style
="width:610px"
161 text_style
="$text_style;margin-left:120px"
162 text_style
="$text_style;margin-top:50px"
163 text_style
="$text_style;align:left"
164 text_style
="$text_style;vertical-align:top;"
168 <title>$TITLE</title>
169 <meta http-equiv="Content-Type"
170 content="text/html; charset=UTF-8"/>
172 <body style="$body_style">
173 <div style="$text_style">
180 toc_style
="position:absolute;top:50px;left:810px;width=$toc_width"
182 echo "<div style=\"$toc_style\">"
183 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
184 echo "<tr><th>Table of contents:</th></tr>"
187 for file in $
(ls -r source-
*.txt
)
189 basename=${file%.txt}
190 timestamp
=${basename#source-}
191 date="$(date +"%d
%b
%Y
" -d @$timestamp)"
192 title
="$(sed 1q < $file | markup)"
193 echo "<li><a href=#$timestamp>$date $title</a>"
196 echo '</td></tr></table>'
199 test -f links.html
&& {
201 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
202 echo "<tr><th>Links:</th></tr>"
205 echo "</td></tr></table>"
209 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
210 echo "<tr><td align=center>"
212 <script type="text/javascript"><!--
213 google_ad_client = "pub-5106407705643819";
214 /* 300x250, created 1/22/09 */
215 google_ad_slot = "6468207338";
216 google_ad_width = 300;
217 google_ad_height = 250;
220 <script type="text/javascript"
221 src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
224 echo "</td></tr></table>"
227 } |
sed -s "s/^/$indent/"
230 # timestamps will not need padding to sort correctly, for some time...
231 for file in $
(ls -r source-
*.txt
)
233 basename=${file%.txt}
234 timestamp
=${basename#source-}
235 echo "<h6>$(make_date $timestamp)</h6>"
236 echo "<a name=$timestamp>"
237 echo "<h2>$(sed 1q < $file | markup)</h2>"
240 sed 1d
< $file | markup
243 sed -e "s/^./$indent&/" \
244 -e "/<pre>/,/<\/pre>/s/^$indent//"
253 # never, ever have spaces in the file names
254 commit_new_images
() {
256 for image
in $
(cat source-
* |
258 sed -n 's/.*\[\[Image://p' |
262 git add
$image || die
"Could not git add image $image"
263 images
="$images $image"
266 git update-index
--refresh &&
267 git diff-files
--quiet -- $images &&
268 git
diff --cached --quiet HEAD
-- $images ||
269 git commit
-s -m "Commit some images on $(make_date $now)" $images
273 # parse command line option
275 *dry
*) DRYRUN
=1; shift;;
276 *show
*) firefox
"$(pwd)"/$TEST; exit;;
277 *remote
*) firefox
$URLPREFIX$URL$OUTPUT; exit;;
281 die
"Usage: $0 [--dry-run]"
283 # make sure we're on the correct branch
284 test refs
/heads
/$BRANCH = $
(git symbolic-ref HEAD
) ||
285 die
"Not on branch $BRANCH"
287 # make sure there are no uncommitted changes
288 git update-index
--refresh &&
289 git diff-files
--quiet ||
290 die
"Have unstaged changes!"
292 # rename the new blog entry if it exists
295 mv -i $NEW source-
$now.txt
&&
296 git add source-
$now.txt
298 die
"Could not rename source.txt"
300 # commit the images that are referenced and not yet committed
301 test ! -z "$DRYRUN" ||
303 die
"Could not commit new images"
305 # to find the images reliably, we have to use the commit name, not the branch
306 # we use the latest commit touching an image file.
307 IMAGEFILES
="$(git ls-files |
308 grep -v '\.\(html\|gitignore\|in\|sh\|txt\)$')"
309 REV
=$
(git rev-list
-1 HEAD
-- $IMAGEFILES)
310 test -z "$REV" && REV
=$BRANCH
311 URL
="$REMOTEREPOSITORY?a=blob_plain;hb=$REV;f="
313 if test ! -z "$DRYRUN"
315 # Output to test.html and have local links into the current directory
320 make_html
> $OUTPUT || die
"Could not write $OUTPUT"
322 test ! -z "$DRYRUN" && {
328 git commit
-s -m "Update $(make_date $now)" &&
329 git push origin
+$BRANCH