Avoid overly long line
[git/dscho.git] / upload.sh
blobf6dc827ab05ec8090dacda7bd54fce949f720b87
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: generate TOC
16 # TODO: have a configurable maximum number of entries per page, and links
17 # to older pages
19 # make sure we're in the correct working directory
20 cd "$(dirname "$0")"
22 GITWEBURL="$(git config gitweb.url)"
23 test -z "$GITWEBURL" && {
24 echo "Please set gitweb.url in the Git config first!" >&2
25 exit 1
28 URLPREFIX="$(dirname "$GITWEBURL")"/
29 REMOTEREPOSITORY="$(basename "$GITWEBURL")"
30 BRANCH=blog
31 URL="$REMOTEREPOSITORY?a=blob_plain;hb=$BRANCH;f="
32 CSS=blog.css
33 NEW=new
34 OUTPUT=index.html
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!repo.or.cz!<a href=http://&>&</a>!g' \
133 -e 's!:-)!\&#x263a;!g' \
134 -e "s!$image_pattern2!<center><img src=$URL\1></center>!g" \
135 -e "$(markup_substitution "''" i)" \
136 -e "$(markup_substitution "_" u)"
139 # make HTML page
140 make_html () {
141 cat << EOF
142 <html>
143 <head>
144 <title>$TITLE</title>
145 <meta http-equiv="Content-Type"
146 content="text/html; charset=UTF-8"/>
147 <link rel="stylesheet" type="text/css" href="$URL$CSS">
148 </head>
149 <body>
150 <div class=content>
151 <h1>$TITLE</h1>
154 # timestamps will not need padding to sort correctly, for some time...
155 for file in $(ls -r source-*.txt)
157 basename=${file%.txt}
158 timestamp=${basename#source-}
159 echo "<h6>$(make_date $timestamp)</h6>"
160 echo "<h2>$(sed 1q < $file | markup)</h2>"
161 echo ""
162 echo "<p>"
163 sed 1d < $file | markup
164 echo "</p>"
165 done |
166 sed -e 's/^./\t\t\t&/'
168 cat << EOF
169 </div>
170 </body>
171 </html>
175 # never, ever have spaces in the file names
176 commit_new_images () {
177 images=
178 for image in $(cat source-* |
179 tr ' ]|' '\n' |
180 sed -n 's/.*\[\[Image://p' |
181 sort |
182 uniq)
184 git add $image || die "Could not git add image $image"
185 images="$images $image"
186 done
188 git update-index --refresh &&
189 git diff --cached --quiet HEAD ||
190 git commit -s -m "Commit some images on $(make_date $now)" $images
194 # parse command line option
195 case "$1" in
196 *dry*) DRYRUN=1; shift;;
197 *show*) firefox "$(pwd)"/$TEST; exit;;
198 *remote*) firefox $URLPREFIX$URL$OUTPUT; exit;;
199 esac
201 test "$#" = 0 ||
202 die "Usage: $0 [--dry-run]"
204 # make sure we're on the correct branch
205 test refs/heads/$BRANCH = $(git symbolic-ref HEAD) ||
206 die "Not on branch $BRANCH"
208 # make sure there are no uncommitted changes
209 git update-index --refresh &&
210 git diff-files --quiet &&
211 git diff-index --quiet --cached HEAD ||
212 die "Have uncommitted changes!"
214 # rename the new blog entry if it exists
215 now=$(date +%s)
216 test ! -f $NEW || {
217 mv -i $NEW source-$now.txt &&
218 git add source-$now.txt
219 } ||
220 die "Could not rename source.txt"
222 # commit the images that are referenced and not yet committed
223 test ! -z "$DRYRUN" ||
224 commit_new_images ||
225 die "Could not commit new images"
227 # to find the images reliably, we have to use the commit name, not the branch
228 URL="$REMOTEREPOSITORY?a=blob_plain;hb=$(git rev-parse --verify $BRANCH);f="
230 # Rewrite the URL in the .css file if we're not running dry
231 if test -z "$DRYRUN"
232 then
233 # rewrite URLs
234 sed -e "s/url(/&$URL/g" < $CSS.in > $CSS &&
235 git add $CSS ||
236 die "Rewriting $CSS failed"
237 else
238 OUTPUT=$TEST
239 CSS=$CSS.in
240 URL=
243 make_html > $OUTPUT || die "Could not write $OUTPUT"
245 test ! -z "$DRYRUN" && {
246 move_new_entry_back
247 exit
250 git add $OUTPUT &&
251 git commit -s -m "Update $(make_date $now)" &&
252 git push origin +$BRANCH