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