Make the gitweb URL configurable, so that others can use the blog engine
[git/dscho.git] / upload.sh
blob6b9364868bb85e974d5a2edf3f7375a52f1a317c
1 #!/bin/sh
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
16 # TODO: generate TOC
17 # TODO: have a configurable maximum number of entries per page, and links
18 # to older pages
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
22 cd "$(dirname "$0")"
24 GITWEBURL="$(git config gitweb.url)"
25 test -z "$GITWEBURL" && {
26 echo "Please set gitweb.url in the Git config first!" >&2
27 exit 1
30 URLPREFIX="$(dirname "$GITWEBURL")"/
31 REMOTEREPOSITORY="$(basename "$GITWEBURL")"
32 BRANCH=blog
33 URL="$REMOTEREPOSITORY?a=blob_plain;hb=$BRANCH;f="
34 CSS=blog.css
35 NEW=new
36 OUTPUT=index.html
37 TEST=test.html
38 TITLE="Dscho's blog"
40 LC_ALL=C
41 export LC_ALL
43 die () {
44 echo "$*" >&2
45 exit 1
48 nth () {
49 # add illogical suffix
50 case "$1" in
51 *1?|*[04-9]) echo "$1th";;
52 *1) echo "$1st";;
53 *2) echo "$1nd";;
54 *3) echo "$1rd";;
55 esac
58 make_chinese_hour () {
59 case $1 in
60 23|00) echo Rat;;
61 01|02) echo Buffalo;;
62 03|04) echo Tiger;;
63 05|06) echo Rabbit;;
64 07|08) echo Dragon;;
65 09|10) echo Snake;;
66 11|12) echo Horse;;
67 13|14) echo Goat;;
68 15|16) echo Monkey;;
69 17|18) echo Rooster;;
70 19|20) echo Dog;;
71 21|22) echo Pig;;
72 esac
75 digit_to_roman () {
76 case $1 in
77 1) echo $2;;
78 2) echo $2$2;;
79 3) echo $2$2$2;;
80 4) echo $2$3;;
81 5) echo $3;;
82 6) echo $3$2;;
83 7) echo $3$2$2;;
84 8) echo $3$2$2$2;;
85 9) echo $2$4;;
86 esac
89 make_roman_number () {
90 case $1 in
91 '') ;;
92 ?) digit_to_roman $1 I V X;;
93 ??) echo $(digit_to_roman ${1%?} X L C)$(make_roman_number ${1#?});;
94 ???) echo $(digit_to_roman ${1%??} C D M)$(make_roman_number ${1#?});;
95 ????) echo $(digit_to_roman ${1%???} M)$(make_roman_number ${1#?});;
96 esac
99 make_date () {
100 printf "%s, %s of %s, Anno Domini %s, at the hour of the %s\n" \
101 $(date +%A -d @$1) \
102 $(nth $(date +%e -d @$1)) \
103 $(date +%B -d @$1) \
104 $(make_roman_number $(date +%Y -d @$1)) \
105 $(make_chinese_hour $(date +%H -d @$1))
108 # make an argument for sed, to replace $1..$1 by <$2>..</$2>
109 markup_substitution () {
110 case "$1" in
111 ?) echo "s/$1\\([^$1]*\\)$1/<$2>\\\\1<\/$2>/g";;
113 tmp="[^${1%?}]*"
114 tmp2="\\|${1%?}[^${1#?}]$tmp"
115 tmp3="\\($tmp\\($tmp2\\($tmp2\\($tmp2\\)\\)\\)\\)"
116 echo "s/$1$tmp3$1/<$2>\\\\1<\/$2>/g"
118 esac
121 # transform markup in stdin to HTML
122 markup () {
123 sed -e 's!^$!</p><p>!' \
124 -e 's!IMHO!in my humble opinion!g' \
125 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
126 -e 's!:-)!\&#x263a;!g' \
127 -e "$(markup_substitution "''" i)" \
128 -e "$(markup_substitution "_" u)"
131 # make HTML page
132 make_html () {
133 cat << EOF
134 <html>
135 <head>
136 <title>$TITLE</title>
137 <meta http-equiv="Content-Type"
138 content="text/html; charset=UTF-8"/>
139 <link rel="stylesheet" type="text/css" href="$URL$CSS">
140 </head>
141 <body>
142 <div class=content>
143 <h1>$TITLE</h1>
146 # timestamps will not need padding to sort correctly, for some time...
147 for file in $(ls -r source-*.txt)
149 basename=${file%.txt}
150 timestamp=${basename#source-}
151 echo "<h6>$(make_date $timestamp)</h6>"
152 echo "<h2>$(sed 1q < $file | markup)</h2>"
153 echo ""
154 echo "<p>"
155 sed 1d < $file | markup
156 echo "</p>"
157 done |
158 sed -e 's/^./\t\t\t&/'
160 cat << EOF
161 </div>
162 </body>
163 </html>
168 # parse command line option
169 case "$1" in
170 *dry*) DRYRUN=1; shift;;
171 *show*) firefox "$(pwd)"/$TEST; exit;;
172 *remote*) firefox $URLPREFIX$URL$OUTPUT; exit;;
173 esac
175 test "$#" = 0 ||
176 die "Usage: $0 [--dry-run]"
178 # make sure we're on the correct branch
179 test refs/heads/$BRANCH = $(git symbolic-ref HEAD) ||
180 die "Not on branch $BRANCH"
182 # make sure there are no uncommitted changes
183 git update-index --refresh &&
184 git diff-files --quiet &&
185 git diff-index --quiet --cached HEAD ||
186 die "Have uncommitted changes!"
188 # rename the new blog entry if it exists
189 now=$(date +%s)
190 test ! -f $NEW || {
191 mv -i $NEW source-$now.txt &&
192 git add source-$now.txt
193 } ||
194 die "Could not rename source.txt"
196 if test -z "$DRYRUN"
197 then
198 # rewrite URLs
199 sed -e "s/url(/&$URL/g" < $CSS.in > $CSS &&
200 git add $CSS ||
201 die "Rewriting $CSS failed"
202 else
203 OUTPUT=$TEST
204 CSS=$CSS.in
205 URL=
208 make_html > $OUTPUT || die "Could not write $OUTPUT"
210 test ! -z "$DRYRUN" && exit
212 git add $OUTPUT &&
213 git commit -s -m "Update $(make_date $now)" &&
214 git push origin +$BRANCH