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="
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\(\\|[^\]]*\)\?\]\]"
129 sed -e 's!^$!</p><p>!' \
130 -e 's!IMHO!in my humble opinion!g' \
131 -e 's!BTW!By the way,!g' \
132 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
133 -e 's!:-)!\☺!g' \
134 -e "s!$image_pattern2!<center><img src=$URL\1></center>!g" \
136 border=1 bgcolor=black style="color:#ffffff;">\
137 <tr><td bgcolor=lightblue colspan=3>\
141 <table cellspacing=5 border=0>\
144 -e 's!</bash>! </pre>\
149 -e "$(markup_substitution "''" i)" \
150 -e "$(markup_substitution "_
" u)"
158 <title>$TITLE</title>
159 <meta http-equiv="Content-Type"
160 content="text/html; charset=UTF-8"/>
161 <link rel="stylesheet" type="text/css" href="$URL$CSS">
170 toc_style
="position:absolute;top:50px;left:810px;width=$toc_width"
171 echo "<div style=\"$toc_style\">"
172 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1><tr><td>"
174 for file in $
(ls -r source-
*.txt
)
176 basename=${file%.txt}
177 timestamp
=${basename#source-}
178 date="$(date +"%d
%b
%Y
" -d @$timestamp)"
179 title
="$(sed 1q < $file | markup)"
180 echo "<li><a href=#$timestamp>$date $title</a>"
182 echo '</td></tr></table>'
187 # timestamps will not need padding to sort correctly, for some time...
188 for file in $
(ls -r source-
*.txt
)
190 basename=${file%.txt}
191 timestamp
=${basename#source-}
192 echo "<h6>$(make_date $timestamp)</h6>"
193 echo "<a name=$timestamp>"
194 echo "<h2>$(sed 1q < $file | markup)</h2>"
197 sed 1d
< $file | markup
200 sed -e 's/^./\t\t\t&/' \
201 -e '/<pre>/,/<\/pre>/s/^\t\t\t//'
210 # never, ever have spaces in the file names
211 commit_new_images
() {
213 for image
in $
(cat source-
* |
215 sed -n 's/.*\[\[Image://p' |
219 git add
$image || die
"Could not git add image $image"
220 images
="$images $image"
223 git update-index
--refresh &&
224 git diff-files
--quiet -- $images &&
225 git
diff --cached --quiet HEAD
-- $images ||
226 git commit
-s -m "Commit some images on $(make_date $now)" $images
230 # parse command line option
232 *dry
*) DRYRUN
=1; shift;;
233 *show
*) firefox
"$(pwd)"/$TEST; exit;;
234 *remote
*) firefox
$URLPREFIX$URL$OUTPUT; exit;;
238 die
"Usage: $0 [--dry-run]"
240 # make sure we're on the correct branch
241 test refs
/heads
/$BRANCH = $
(git symbolic-ref HEAD
) ||
242 die
"Not on branch $BRANCH"
244 # make sure there are no uncommitted changes
245 git update-index
--refresh &&
246 git diff-files
--quiet ||
247 die
"Have unstaged changes!"
249 # rename the new blog entry if it exists
252 mv -i $NEW source-
$now.txt
&&
253 git add source-
$now.txt
255 die
"Could not rename source.txt"
257 # commit the images that are referenced and not yet committed
258 test ! -z "$DRYRUN" ||
260 die
"Could not commit new images"
262 # to find the images reliably, we have to use the commit name, not the branch
263 # we use the latest commit touching an image file.
264 IMAGEFILES
="$(git ls-files |
265 grep -v '\.\(css\|html\|gitignore\|in\|sh\|txt\)$')"
266 REV
=$
(git rev-list
-1 HEAD
-- $IMAGEFILES)
267 test -z "$REV" && REV
=$BRANCH
268 URL
="$REMOTEREPOSITORY?a=blob_plain;hb=$REV;f="
270 # Rewrite the URL in the .css file if we're not running dry
274 sed -e "s/url(/&$URL/g" < $CSS.
in > $CSS &&
276 die
"Rewriting $CSS failed"
283 make_html
> $OUTPUT || die
"Could not write $OUTPUT"
285 test ! -z "$DRYRUN" && {
291 git commit
-s -m "Update $(make_date $now)" &&
292 git push origin
+$BRANCH