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: handle images (git add them and rewrite the URL dynamically)
15 # TODO: generate an RSS feed, too
17 # TODO: have a configurable maximum number of entries per page, and links
19 # TODO: include the commit name in the URL, so that images will be found
21 # make sure we're in the correct working directory
24 GITWEBURL
="$(git config gitweb.url)"
25 test -z "$GITWEBURL" && {
26 echo "Please set gitweb.url in the Git config first!" >&2
30 URLPREFIX
="$(dirname "$GITWEBURL")"/
31 REMOTEREPOSITORY
="$(basename "$GITWEBURL")"
33 URL
="$REMOTEREPOSITORY?a=blob_plain;hb=$BRANCH;f="
43 move_new_entry_back
() {
44 test -f source-
$now.txt
&&
45 mv source-
$now.txt
$NEW &&
46 git
rm --cached -f source-
$now.txt
56 # add illogical suffix
58 *1?|
*[04-9]) echo "$1th";;
65 make_chinese_hour
() {
96 make_roman_number
() {
99 ?
) digit_to_roman
$1 I V X
;;
100 ??
) echo $
(digit_to_roman
${1%?} X L C
)$
(make_roman_number
${1#?});;
101 ???
) echo $
(digit_to_roman
${1%??} C D M
)$
(make_roman_number
${1#?});;
102 ????
) echo $
(digit_to_roman
${1%???} M
)$
(make_roman_number
${1#?});;
107 printf "%s, %s of %s, Anno Domini %s, at the hour of the %s\n" \
109 $
(nth $
(date +%e
-d @
$1)) \
111 $
(make_roman_number $
(date +%Y
-d @
$1)) \
112 $
(make_chinese_hour $
(date +%H
-d @
$1))
115 # make an argument for sed, to replace $1..$1 by <$2>..</$2>
116 markup_substitution
() {
118 ?
) echo "s/$1\\([^$1]*\\)$1/<$2>\\\\1<\/$2>/g";;
121 tmp2
="\\|${1%?}[^${1#?}]$tmp"
122 tmp3
="\\($tmp\\($tmp2\\($tmp2\\($tmp2\\)\\)\\)\\)"
123 echo "s/$1$tmp3$1/<$2>\\\\1<\/$2>/g"
128 # transform markup in stdin to HTML
130 sed -e 's!^$!</p><p>!' \
131 -e 's!IMHO!in my humble opinion!g' \
132 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
133 -e 's!:-)!\☺!g' \
134 -e "$(markup_substitution "''" i)" \
135 -e "$(markup_substitution "_
" u)"
143 <title>$TITLE</title>
144 <meta http-equiv="Content-Type"
145 content="text/html; charset=UTF-8"/>
146 <link rel="stylesheet" type="text/css" href="$URL$CSS">
153 # timestamps will not need padding to sort correctly, for some time...
154 for file in $
(ls -r source-
*.txt
)
156 basename=${file%.txt}
157 timestamp
=${basename#source-}
158 echo "<h6>$(make_date $timestamp)</h6>"
159 echo "<h2>$(sed 1q < $file | markup)</h2>"
162 sed 1d
< $file | markup
165 sed -e 's/^./\t\t\t&/'
175 # parse command line option
177 *dry
*) DRYRUN
=1; shift;;
178 *show
*) firefox
"$(pwd)"/$TEST; exit;;
179 *remote
*) firefox
$URLPREFIX$URL$OUTPUT; exit;;
183 die
"Usage: $0 [--dry-run]"
185 # make sure we're on the correct branch
186 test refs
/heads
/$BRANCH = $
(git symbolic-ref HEAD
) ||
187 die
"Not on branch $BRANCH"
189 # make sure there are no uncommitted changes
190 git update-index
--refresh &&
191 git diff-files
--quiet &&
192 git diff-index
--quiet --cached HEAD ||
193 die
"Have uncommitted changes!"
195 # rename the new blog entry if it exists
198 mv -i $NEW source-
$now.txt
&&
199 git add source-
$now.txt
201 die
"Could not rename source.txt"
206 sed -e "s/url(/&$URL/g" < $CSS.
in > $CSS &&
208 die
"Rewriting $CSS failed"
215 make_html
> $OUTPUT || die
"Could not write $OUTPUT"
217 test ! -z "$DRYRUN" && {
223 git commit
-s -m "Update $(make_date $now)" &&
224 git push origin
+$BRANCH