.rss is not an image extension
[git/dscho.git] / upload.sh
blob95d6aec3994ab00bf00fe30936fe11333c6268aa
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: have a configurable maximum number of entries per page, and links
15 # to older pages
17 # make sure we're in the correct working directory
18 cd "$(dirname "$0")"
20 GITWEBURL="$(git config gitweb.url)"
21 test -z "$GITWEBURL" && {
22 echo "Please set gitweb.url in the Git config first!" >&2
23 exit 1
26 URLPREFIX="$(dirname "$GITWEBURL")"/
27 REMOTEREPOSITORY="$(basename "$GITWEBURL")"
28 BRANCH=blog
29 URL="$REMOTEREPOSITORY?a=blob_plain;hb=$BRANCH;f="
30 ORIGURL=$URL
31 NEW=new
32 OUTPUT=index.html
33 RSS=blog.rss
34 TEST=test.html
35 TITLE="Dscho's blog"
37 LC_ALL=C
38 export LC_ALL
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
46 die () {
47 move_new_entry_back
48 echo "$*" >&2
49 exit 1
52 nth () {
53 # add illogical suffix
54 case "$1" in
55 *1?|*[04-9]) echo "$1th";;
56 *1) echo "$1st";;
57 *2) echo "$1nd";;
58 *3) echo "$1rd";;
59 esac
62 make_chinese_hour () {
63 case $1 in
64 23|00) echo Rat;;
65 01|02) echo Buffalo;;
66 03|04) echo Tiger;;
67 05|06) echo Rabbit;;
68 07|08) echo Dragon;;
69 09|10) echo Snake;;
70 11|12) echo Horse;;
71 13|14) echo Goat;;
72 15|16) echo Monkey;;
73 17|18) echo Rooster;;
74 19|20) echo Dog;;
75 21|22) echo Pig;;
76 esac
79 digit_to_roman () {
80 case $1 in
81 1) echo $2;;
82 2) echo $2$2;;
83 3) echo $2$2$2;;
84 4) echo $2$3;;
85 5) echo $3;;
86 6) echo $3$2;;
87 7) echo $3$2$2;;
88 8) echo $3$2$2$2;;
89 9) echo $2$4;;
90 esac
93 make_roman_number () {
94 case $1 in
95 '') ;;
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#?});;
100 esac
103 make_date () {
104 printf "%s, %s of %s, Anno Domini %s, at the hour of the %s\n" \
105 $(date +%A -d @$1) \
106 $(nth $(date +%e -d @$1)) \
107 $(date +%B -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 () {
114 case "$1" in
115 ?) echo "s/$1\\([^$1]*\\)$1/<$2>\\\\1<\/$2>/g";;
117 tmp="[^${1%?}]*"
118 tmp2="\\|${1%?}[^${1#?}]$tmp"
119 tmp3="\\($tmp\\($tmp2\\($tmp2\\($tmp2\\)\\)\\)\\)"
120 echo "s/$1$tmp3$1/<$2>\\\\1<\/$2>/g"
122 esac
125 # transform markup in stdin to HTML
126 markup () {
127 image_pattern="\\[\\[Image:\([^]]*\)"
128 image_pattern2="$image_pattern\(\\|[^\]]*\)\?\]\]"
129 case "$*" in
130 *invert-bash*) bash_bg=white; bash_fg=black;;
131 *) bash_bg=black; bash_fg=white;;
132 esac
133 sed -e 's!^$!</p><p>!' \
134 -e 's!IMHO!in my humble opinion!g' \
135 -e 's!BTW!By the way,!g' \
136 -e 's!repo.or.cz!<a href=http://&>&</a>!g' \
137 -e 's!:-)!\&#x263a;!g' \
138 -e "s!$image_pattern2!<center><img src=$URL\1></center>!g" \
139 -e 's!<bash>!<table\
140 border=1 bgcolor='$bash_bg'>\
141 <tr><td bgcolor=lightblue colspan=3>\
142 \&nbsp;\
143 </td></tr>\
144 <tr><td>\
145 <table cellspacing=5 border=0\
146 style="color:'$bash_fg';">\
147 <tr><td>\
148 <pre>!' \
149 -e 's!</bash>! </pre>\
150 </td></tr>\
151 </table>\
152 </td></tr>\
153 </table>!' \
154 -e "$(markup_substitution "''" i)" \
155 -e "$(markup_substitution "_" u)"
158 # output lines containing <timestamp> <filename> <title>
159 get_blog_entries () {
160 for file in $(ls -r source-*.txt)
162 basename=${file%.txt}
163 timestamp=${basename#source-}
164 title="$(sed 1q < $file | markup)"
165 echo "$timestamp $file $title"
166 done
169 # make HTML page
170 make_html () {
171 body_style="width:800px"
172 body_style="$body_style;background-image:url(${URL}paper.jpg)"
173 body_style="$body_style;background-repeat:repeat-y"
174 body_style="$body_style;background-attachment:scroll"
175 body_style="$body_style;padding:0px;"
176 text_style="width:610px"
177 text_style="$text_style;margin-left:120px"
178 text_style="$text_style;margin-top:50px"
179 text_style="$text_style;align:left"
180 text_style="$text_style;vertical-align:top;"
181 cat << EOF
182 <html>
183 <head>
184 <title>$TITLE</title>
185 <meta http-equiv="Content-Type"
186 content="text/html; charset=UTF-8"/>
187 </head>
188 <body style="$body_style">
189 <div style="$text_style">
190 <h1>$TITLE</h1>
192 indent='\t\t\t'
194 # make toc
195 toc_width=400px
196 toc_style="position:absolute;top:50px;left:810px;width=$toc_width"
198 echo "<div style=\"$toc_style\">"
199 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
200 echo "<tr><th>Table of contents:</th></tr>"
201 echo "<tr><td>"
202 echo '<p><ol>'
203 get_blog_entries |
204 while read timestamp filename title
206 date="$(date +"%d %b %Y" -d @$timestamp)"
207 echo "<li><a href=#$timestamp>$date $title</a>"
208 done
209 echo '</ol></p>'
210 echo '</td></tr></table>'
212 # Links
213 test -f links.html && {
214 echo "<br>"
215 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
216 echo "<tr><th>Links:</th></tr>"
217 echo "<tr><td>"
218 cat links.html
219 echo "</td></tr></table>"
222 # Google AdSense
223 test -z "$DRYRUN" && {
224 echo "<br>"
225 echo "<table width=$toc_width bgcolor=#e0e0e0 border=1>"
226 echo "<tr><td align=center>"
227 cat << EOF
228 <script type="text/javascript"><!--
229 google_ad_client = "pub-5106407705643819";
230 /* 300x250, created 1/22/09 */
231 google_ad_slot = "6468207338";
232 google_ad_width = 300;
233 google_ad_height = 250;
234 //-->
235 </script>
236 <script type="text/javascript"
237 src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
238 </script>
240 echo "</td></tr></table>"
243 # RSS feed
244 rss_style="background-color:orange;text-decoration:none"
245 rss_style="$rss_style;color:white;"
246 echo '<br>'
247 echo '<div style="text-align:right;">'
248 echo "<a href=\"$ORIGURL$RSS\""
249 echo ' title="Subscribe to my RSS feed"'
250 echo ' class="rss" rel="nofollow"'
251 echo " style=\"$rss_style\">RSS</a>"
252 echo '</div>'
254 echo '</div>'
255 } | sed -s "s/^/$indent/"
258 # timestamps will not need padding to sort correctly, for some time...
259 get_blog_entries |
260 while read timestamp filename title
262 echo "<h6>$(make_date $timestamp)</h6>"
263 echo "<a name=$timestamp>"
264 echo "<h2>$title</h2>"
265 echo ""
266 echo "<p>"
267 sed 1d < $filename | markup
268 echo "</p>"
269 done |
270 sed -e "s/^./$indent&/" \
271 -e "/<pre>/,/<\/pre>/s/^$indent//"
273 cat << EOF
274 </div>
275 </body>
276 </html>
280 generate_rss () {
281 echo '<?xml version="1.0" encoding="utf-8"?>'
282 echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
283 echo '<channel>'
284 echo "<title>Dscho's blog</title>"
285 echo "<link>$URLPREFIX${URL}index.html</link>"
286 self="$URLPREFIX$ORIGURL$RSS"
287 selfattribs='rel="self" type="application/rss+xml"'
288 echo "<atom:link href=\"$self\" $selfattribs/>"
289 echo '<description>A few stories told by Dscho</description>'
290 echo "<lastBuildDate>$(date --rfc-2822)</lastBuildDate>"
291 echo '<language>en-us</language>'
293 get_blog_entries |
294 while read timestamp filename title
296 echo '<item>'
297 echo "<title>$title</title>"
298 echo "<link>$URLPREFIX${URL}index.html#$timestamp</link>"
299 echo "<guid>$URLPREFIX${URL}index.html#$timestamp</guid>"
300 echo "<pubDate>$(date --rfc-2822 -d @$timestamp)</pubDate>"
301 description="$(cat < $filename | markup invert-bash)"
302 echo "<description><![CDATA[$description]]></description>"
303 echo "</item>"
304 done
306 echo '</channel>'
307 echo '</rss>'
310 # never, ever have spaces in the file names
311 commit_new_images () {
312 generate_rss > $RSS &&
313 git add $RSS ||
314 die "Could not generate $RSS"
316 images=
317 for image in $(cat source-* |
318 tr ' ]|' '\n' |
319 sed -n 's/.*\[\[Image://p' |
320 sort |
321 uniq)
323 git add $image || die "Could not git add image $image"
324 images="$images $image"
325 done
327 git update-index --refresh &&
328 git diff-files --quiet -- $images &&
329 git diff --cached --quiet HEAD -- $images ||
330 git commit -s -m "Commit some images on $(make_date $now)" $images
335 # parse command line option
336 case "$1" in
337 *dry*) DRYRUN=1; shift;;
338 *show*) firefox "$(pwd)"/$TEST; exit;;
339 *remote*) firefox $URLPREFIX$URL$OUTPUT; exit;;
340 esac
342 test "$#" = 0 ||
343 die "Usage: $0 [--dry-run]"
345 # make sure we're on the correct branch
346 test refs/heads/$BRANCH = $(git symbolic-ref HEAD) ||
347 die "Not on branch $BRANCH"
349 # make sure there are no uncommitted changes
350 git update-index --refresh &&
351 git diff-files --quiet ||
352 die "Have unstaged changes!"
354 # rename the new blog entry if it exists
355 now=$(date +%s)
356 test ! -f $NEW || {
357 mv -i $NEW source-$now.txt &&
358 git add source-$now.txt
359 } ||
360 die "Could not rename source.txt"
362 # commit the images that are referenced and not yet committed
363 test ! -z "$DRYRUN" ||
364 commit_new_images ||
365 die "Could not commit new images"
367 # to find the images reliably, we have to use the commit name, not the branch
368 # we use the latest commit touching an image file.
369 IMAGEFILES="$(git ls-files |
370 grep -v '\.\(rss\|html\|gitignore\|in\|sh\|txt\)$')"
371 REV=$(git rev-list -1 HEAD -- $IMAGEFILES)
372 test -z "$REV" && REV=$BRANCH
373 URL="$REMOTEREPOSITORY?a=blob_plain;hb=$REV;f="
375 if test ! -z "$DRYRUN"
376 then
377 # Output to test.html and have local links into the current directory
378 OUTPUT=$TEST
379 URL=
382 make_html > $OUTPUT || die "Could not write $OUTPUT"
384 test ! -z "$DRYRUN" && {
385 move_new_entry_back
386 exit
389 git add $OUTPUT &&
390 git commit -s -m "Update $(make_date $now)" &&
391 git push origin +$BRANCH