#!/bin/bash function tag { typeset tag="$1" shift printf "<%s>%s\\n" "$tag" \ "$(echo "$*" | sed -e 's/&/\&/g' -e 's/"/\"/g' \ -e 's//\>/g')" "$tag" } function rss_item { typeset item="$1" date="$2" summary="$3" rssdate typeset months="JanFebMarAprJunJulAugSepOctNovDec" # 2006-01-10 12:34 (Sunday) ...* # 0 5 0 5 0 5 typeset month="${date:5:2}" [ ${month:0:1} = "0" ] && month="${month:1:1}" let "month = (month - 1) * 3" month="${months:$month:3}" rssdate="${date:18:3}, ${date:8:2} $month ${date:0:4} ${date:11:5}:00 $(date '+%z')" echo "" tag link "$atomid#L$item" tag guid "$atomid#$item" tag pubDate "$rssdate" tag author "$email ($realname)" tag title "$firstname's journal: $date" tag description "$summary" echo "" } function atom_entry { typeset item="$1" date="$2" summary="$3" tz=$(date '+%z') typeset pubdate="${date:0:10}T${date:11:5}:00${tz:0:3}:${tz:3:2}" echo "" tag updated "$pubdate" tag published "$pubdate" tag title "$firstname's journal: $date" tag id "$atomid#$item" echo "" tag name "$realname" tag uri "$journal" tag email "$email" echo "" tag summary "$summary" echo "" } function rss_head { cat << EOF EOF tag title "$realname's Journal" tag link "$journal" tag description "The latest rumblings from Perette's head" tag language en-us tag webMaster "$email" tag generator "diary $Version$" tag docs "http://www.rssboard.org/rss-specification" tag managingEditor "$email" tag lastBuildDate "$(date '+%a, %d %b %Y %H:%M:%S %z')" } function atom_head { atomid="$journal/$(basename $publish)" cat << EOF $realname's Journal Peri's diary script EOF tag updated "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" tag rights "Copyright $(date '+%Y') Perette Barella" tag id "$atomid" } function generate_feed { typeset aline diary="$1" linecount=0 summary="" date itemid thismonth="$(date '+%Y-%m')" m=${thismonth:5:2} let "m=${m#0}-1" [ ${#m} -eq 1 ] && m="0$m" lastmonth="${thismonth:0:4}-$m" atom_head > $atomfeed rss_head > $rssfeed linenum=0 while IFS="" read aline do let linenum=linenum+1 case "$aline" in $thismonth-[0-3][0-9]?*journal|$lastmonth-[0-3][0-9]?*journal) if [ "$summary" != "" ] then atom_entry $itemid "$date" "$summary" >> $atomfeed rss_item $itemid "$date" "$summary" >> $rssfeed fi itemid=$linenum summary="" linecount=0 date="$aline" ;; [12][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]*) linecount=99 ;; *) [ $linecount -le 10 ] && summary="$summary $aline" let linecount=linecount+1 [ "$aline" = "" ] && linecount=99 [ $linecount -eq 10 ] && linecount=99 && summary="$summary." ;; esac done < "$diary" if [ "$summary" != "" ] then atom_entry $itemid "$date" "$summary" >> $atomfeed rss_item $itemid "$date" "$summary" >> $rssfeed fi echo "" >> $atomfeed echo "" >> $rssfeed } function usage { cat << EOF Diary maintains a plain-text diary and publishes it in HTML form with RSS and Atom feeds. $arg0 [mode] [-f input-file] [-d publication-file] [-R] [-r] Mode defaults to adding a new diary entry, or is one of: -e : edit -j : add new journal entry -m : add new mirror entry -y : add new dreams entry -p : publish -r : read only Options are: -f : specifies the input file for the diary. -d : specifies the destination file for the diary. -R : update the RSS and Atom feeds. EOF } arg0=$(basename $0) year=$(date '+%Y') mode=diary [ "$WEBBASE" = "" ] && WEBBASE="$HOME/Web" diary="$HOME/Documents/Projects/self/diary-$year" journaldir="$HOME/Sites/$USER/journal" publish="$journaldir/journal-$year.html" build="$journaldir/journal-$year.web" rssfeed="$journaldir/journal-rss.xml" atomfeed="$journaldir/journal-atom.xml" journal="http://barella.org/Perette/Journal" email="perette@barella.org" spacetab=" " direction=reverse updatefeed=false while getopts "pyjef:d:rRm?" option do case "$option" in d) publish="$OPTARG" ;; p) mode=publish ;; y) mode=dreams ;; f) diary="$OPTARG" direction=forward year=$(basename "$diary" | sed 's/diary-//') publish="$journaldir/journal-$year.html" build="$journaldir/journal-$year.web" ;; j) mode=journal updatefeed=true ;; e) mode=edit ;; r) mode=read ;; R) updatefeed=true ;; m) mode=mirror ;; ?) usage exit 1 ;; esac done if [ "$mode" = "edit" ] then vi $diary elif [ "$mode" = "read" ] then view $diary exit 0 elif [ "$mode" = "publish" ] then : else echo >> $diary date "+%Y-%m-%d %H:%M (%A) $mode" >> $diary echo >> $diary vi -c '$' $diary fi file=$(basename $diary) if [ "$publish" -ot "$diary" -o "$mode" = "publish" ] then # The plan: Make a forward collection of reversed articles, # then reverse the whole thing to get a reverse collection of # forward articles. articletemp="/var/tmp/$arg0.$$.tmp1" maintemp="/var/tmp/$arg0.$$.tmp2" pre=off copy=off newpara=false linenum=0 (sed -e 's/&/\&/g' -e 's/"/\"/g' -e 's/\\/\\\\/g' \ -e 's//\>/g' < "$diary"; echo "EOF-ETX") | while IFS="" read aline do let linenum=linenum+1 case "$aline" in [12][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]?*journal|EOF-ETX) [ "$pre" = "on" ] && echo "" && pre=off if [ -s "$articletemp" ] then if [ "$direction" = "reverse" ] then tail -r "$articletemp" else cat "$articletemp" fi fi newpara=false echo "

$aline

" > "$articletemp" copy=on ;; [12][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]*) copy=off ;; [$spacetab-]*[^$spacetab]*|[a-zA-Z0-9][.\)]*|[1-9][0-9][.\)]*) [ "$pre" = "off" -a "$copy" = "on" ] && echo "
" >> "$articletemp" &&
				pre=on && newpara=false
			[ "$copy" = "on" ] && echo "$aline" >> "$articletemp"
			;;
		    [Ii]mage:*)
			image=$(echo "$aline" | cut -d: -f2 | awk '{print $1}')
			fullpath=$(locate "$image" | grep "^$WEBBASE" |
				   grep "/Thumbnails/" | head -1)
			if [ "$fullpath" != "" ]
			then
				imagepath=$(echo "$fullpath" | sed 's&/Thumbnails/&/&')
				fullpath=$(echo "$fullpath" |
					   sed "s&^$WEBBASE&DIR_TOP&")
				imagetitle=$(echo "$image" | sed 's/[-_]/ /g')
				echo "_IMAGE($fullpath, ALT=\"$imagetitle\" ALIGN=RIGHT)" >> "$articletemp"
			elif fullpath=$(locate "$image" | grep "^$WEBBASE" |
					head -1); [ "$fullpath" != "" ]
			then
				fullpath=$(echo "$fullpath" |
					   sed "s&^$WEBBASE&DIR_TOP&")
				imagetitle=$(echo "$image" | sed 's/[-_]/ /g')
				echo "_THUMBNAIL($fullpath, 180, 150, ALT=\"$imagetitle\" ALIGN=RIGHT)" >> "$articletemp"
				
			fi
			;;
		    [Mm]ood:*)
			mood=$(echo "$aline" | cut -d: -f2 | tr 'A-Z' 'a-z')
			basemood=$(echo "$mood" |
				   sed -e 's/[^a-zA-Z]*//' -e 's/[^a-zA-Z]*$//')
			if [ "$mood" != "" ]
			then
				echo "
" >> "$articletemp" if [ -f "$WEBBASE/Graphics/Mood/$basemood.gif" ] then echo "_IMAGE(DIR_GFX/Mood/$basemood.gif, ALT=\"$mood\", 50)" >> "$articletemp" fi echo "Mood: $mood" >> "$articletemp" echo "
" >> "$articletemp" fi ;; *) if [ "$copy" = "on" ] then [ "$pre" = "on" ] && echo "
" >> "$articletemp" && pre=off && newpara=false [ "$aline" != "" -a "$newpara" = "true" ] && newpara=false && echo "

" >> "$articletemp" [ "$aline" = "" ] && newpara=true echo "$aline" >> "$articletemp" fi ;; esac done | sed -e 's/^_\([A-Za-z][a-z]\)/\1/' \ -e 's/\([ ]\)_\([a-zA-Z]\)/\1\2/g' \ -e 's&\([a-zA-Z0-9]\)_\([^a-zA-Z0-9]\)&\1\2&g' \ -e 's&\([a-zA-Z0-9]\)_$&\1&' >> "$maintemp" realname=$(finger $USER | head -1 | sed 's/.*Name://') firstname=$(echo "$realname" | awk '{print $1}') echo "m4_include(stddefs.m4) _HEADER(\`\`$firstname's journal: $year'', \`\`\$Id: diary,v 1.11 2006/04/05 02:48:50 perette Exp $'')" > "$build" [ -f "$journaldir/journalmenu.m4" ] && echo "m4_include(journalmenu.m4)" >> "$build" echo "

$firstname's journal: $year

" >> "$build" if [ "$direction" = "reverse" ] then tail -r "$maintemp" >> "$build" else cat "$maintemp" >> "$build" fi rm -f "$articletemp" "$maintemp" if [ "$updatefeed" = "true" -a "$direction" = "reverse" ] then generate_feed "$diary" generate_feed "$diary" fi cd "$journaldir" if make depend && make && [ -x ~/bin/upload ] then ~/bin/upload journal fi fi exit 0