#!/bin/ksh function tag { typeset tag="$1" attr="$3" [ "$attr" != "" ] && attr=" $attr" printf "<%s>%s\\n" "$tag$attr" \ "$(echo "$2" | sed -e 's/&/\&/g' -e 's/"/\"/g' \ -e 's//\>/g')" "$tag" } function rss_item { typeset item="$1" date="$2" title="$3" summary="$4" rssdate typeset months="JanFebMarAprMayJunJulAugSepOctNovDec" # 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:17:3}, ${date:8:2} $month ${date:0:4} ${date:11:5}:00 $(date '+%z')" echo "" tag link "$atomid#L$item" tag guid "$atomid#L$item" tag pubDate "$rssdate" tag author "$email ($realname)" tag title "$title" tag description "$summary" echo "" } function atom_entry { typeset item="$1" date="$2" title="$3" summary="$4" 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 "$title" tag id "$atomid#$item" echo "" tag name "$realname" tag uri "$journal" tag email "$email" echo "" tag summary "$summary" "type='html'" 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 title diary="$1" keep=false date date1 date2 typeset linecount startline lasttitle summary 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 (egrep -n '^$' "$diary" && echo "9999999:end of file") | sed 's/^\([0-9][0-9]*\):$/\1:\2/' | while IFS=":" read linenum date1 date2 title do date="$date1:$date2" if $keep then itemid="${lastdate:0:4}${lastdate:5:2}${lastdate:8:2}${lastdate:11:2}${lastdate:14:2}" let startline=startline+2 let linecount=linenum-startline summary="$(tail +$startline "$diary" | head -$linecount | egrep -v "^(<.*>)?_[A-Z][A-Z]*")" atom_entry $itemid "$lastdate" "$lasttitle" "$summary" >> $atomfeed rss_item $itemid "$lastdate" "$lasttitle" "$summary" >> $rssfeed fi keep=false case "$date" in $thismonth*|$lastmonth*) keep=true ;; esac typeset startline="$linenum" typeset lasttitle="$title" typeset lastdate="$date" done 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" references="$journaldir/references" friends="$journaldir/friends" 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 sedreference=$(sed 's&^\([^ ][^ ]*\) *\([^ ]*\)$&-e \"s|[[:<:]]\1[[:>:]]|\1|\"&' $references) sedfriends=$(sed 's&^\([^ ][^ ]*\) *\([^ ]*\) *\([^ ]*\)$&-e \"s|[[:<:]]\1[[:>:]]|\2|\"&' $friends) [ "$friends" -nt "$friends.m4" ] && sed 's&^\([^ ][^ ]*\) *\([^ ]*\) *\([^ ]*\)$&_FRIEND(\2, \3)&' $friends > $friends.m4 (sed -e 's/&/\&/g' -e 's/"/\"/g' -e 's/\\/\\\\/g' \ -e 's//\>/g' \ -e 's&\[\[\([a-zA-Z0-9]*\)\]\]&\1&g' \ -e 's&\[\[\([a-zA-Z0-9]*\) \([a-zA-Z0-9]*\)\]\]&\1 \2&g' \ -e 's&\[\[\([a-zA-Z0-9]*\) \([a-zA-Z0-9]*\) \([a-zA-Z0-9]*\)\]\]&\1 \2 \3&g' \ -e 's&\[\[\([a-zA-Z0-9]*\) \([a-zA-Z0-9]*\) \([a-zA-Z0-9]*\) \([a-zA-Z0-9]*\)\]\]&\1 \2 \3 \4&g' \ -e 's&\[http://\([-_a-zA-Z0-9]*\)\.\([-_a-zA-Z0-9.]*\)]&\1&g' \ -e 's&\[\([^|]*\)|\(http://[^]]*\)]&\1&g' \ -e 's&^http://\([-_a-zA-Z0-9.]*\)\(/[-_a-zA-Z0-9./]*\)* *&\1&' \ -e 's& http://\([-_a-zA-Z0-9.]*\)\(/[-_a-zA-Z0-9./]*\)* *& \1 &' < "$diary" | eval sed $sedreference $sedfriends; 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 aline=$(echo "$aline" | sed 's/<[^>]*>//g') if [[ $aline == [12][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]\ [012][0-9]:[0-5][0-9]?*journal* ]] then title=$(echo "$aline" | cut -s -d: -f3-) date=$(echo "$aline" | cut -c1-17,19-21) bylinefield=1-2 else title=$(echo "$aline" | cut -s -d: -f2-) date=$(echo "$aline" | cut -c1-10) bylinefield=1 fi if [ "$title" = "" ] then title="$aline" byline="" else byline="$(echo "$aline" | cut -d: -f$bylinefield)" fi echo "" > "$articletemp" sequence="${date:0:4}${date:5:2}${date:8:2}${date:11:2}${date:14:2}" expr "$(echo "$sequence" | cut -c9-12)" : "[012][0-9][0-5][0-9]\$" >/dev/null || sequence="$(echo "$sequence" | cut -c1-8)" echo "_CHAPTER(\`\`${title:-$aline}'', \`\`L$sequence'')$byline" >> "$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=$(mdfind -onlyin $HOME/Sites "kMDItemFSName = *$image*" | grep /Thumbnails/ | head -1)
			imagetitle=$(echo "$image" | sed 's/[-_]/ /g')
			if [ "$fullpath" != "" ]
			then
				imagepath=$(echo "$fullpath" | sed 's&/Thumbnails/&/&')
				base=$(basename "$imagepath")
				dir=$(dirname "$imagepath")
				[ ! -f "$dir/index.html" ] &&
					dir="$(dirname "$dir")"
				if [ -f "$dir/index.html" ] &&
				   [ $(grep -c "$base" "$dir/index.html") -gt 0 ]
				then
					imagepath="$dir/index.html"
				fi
				fullpath=$(echo "$fullpath" |
					   sed "s&^$WEBBASE&DIR_TOP&")
				imagepath=$(echo "$imagepath" |
					   sed "s&^$WEBBASE&DIR_TOP&")
				echo "_IMAGE($fullpath, ALT=\"$imagetitle\" ALIGN=RIGHT)" >> "$articletemp"
			elif fullpath=$(mdfind -onlyin $HOME/Sites "kMDItemFSName = *$image*" | head -1) ; [ "$fullpath" != "" ]
			then
				fullpath=$(echo "$fullpath" |
					   sed "s&^$WEBBASE&DIR_TOP&")
				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) m4_include(DIR_INCLUDE/toc3.m4) _HEADER(\`\`$firstname's journal: $year'', \`\`\$Id: diary,v 1.17 2007/10/25 20:54:01 perette Exp $'')" > "$build" [ -f "$journaldir/journalmenu.m4" ] && echo "m4_include(journalmenu.m4)" >> "$build" echo "_DOCTITLE(\`\`$firstname's journal: $year'')" >> "$build" echo "_STYLE(\`\`H2 { clear: left; margin-bottom: 0.1ex; }'')" >> "$build" if [ "$direction" = "reverse" ] then echo "m4_define(\`\`_TOC_HEADER'', \`\`'')" >> "$build" echo "m4_define(\`\`_CHAPTER_TOC'', \`\`'')" >> "$build" echo "m4_define(\`\`_TOC_END'', \`\`'')" >> "$build" echo "m4_define(\`\`_CHAPTER_HEADER'', \`\`

__CHAPTER_NAME

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