#!/bin/sh
# Extract all HTML date entries and sort them by date,
# then convert them to an HTML index of these entries.
#
# Usage: chronolog [outfile [r]]
#
FILE=${1:-chronolog.html}
NEWFILE=$FILE+
NEWFILE2=$FILE++
NEWFILE3=$FILE+++
trap "echo ABORTED;rm -f $NEWFILE $NEWFILE2 $NEWFILE3;exit 0" INT
# Output HTML header.
if [ -z "$2" ] ; then
cat >$NEWFILE <<-EOF
Chronological Log Index
EOF
else
cat >$NEWFILE <<-EOF
Reverse-Chronological Log Index
EOF
fi
# Title it.
if [ -z "$2" ] ; then
echo "Chronological Log Index
" >>$NEWFILE
else
echo "Reverse-chronological Log Index
" >>$NEWFILE
fi
# Table header.
cat >>$NEWFILE <<-EOF
EOF
# Get the goodies. Could be tidier, but this works. Maybe later.
#
# Converts all lines in all HTML files like:
#
# Day, Month DD, YYYY...
#
# to lines like (where f is a file name):
#
# | Day, Month DD, YYYY | (f)
#
egrep -H '^.*' *.html */*.html \
| fgrep -v $FILE \
| sed -e 's/:/: /' -e 's/>/> /' \
| sort -s -bd +5n$2 -6 +3M$2 -4 +4n$2 -5 \
| sed 's/\(^.*\): \(.*\)<.*$/\2<\/A> (\1<\/A>)/' \
| sed -e 's@@@' -e 's/^/| /' -e 's/(/ | (/' >> $NEWFILE
# Output HTML trailer.
cat >>$NEWFILE <<-EOF
| |
Return to Site Home
EOF
# Collect (and label) all new year marks. Make each year its own
# table for quicker display in the browser? (No, that didn't help.)
awk <$NEWFILE >$NEWFILE2 '
BEGIN {year=0; first=1}
{pos=match($0, "[1-2][09][0-9][0-9]");
if (pos)
if (year!=substr($0, pos, 4)) {
year=substr($0, pos, 4);
if (first != 1) {
print ""
print "
"
print ""
} else {
print "
|
|
"
}
print "| Dated Entry | From file"
print " |
| "year""
first=1; # Set =0 for separate tables, =1 for one table.
}
print
}'
mv $NEWFILE2 $NEWFILE
# Extract all new year label marks for making an index table.
<$NEWFILE fgrep ' |
| ' | sed 's@.*\([1-2][09][0-9][0-9]\).*@ | \1@' >$NEWFILE2
# Only ten years per row of the index table
awk <$NEWFILE2 >$NEWFILE3 '
BEGIN {line=1}
{
if (line++ > 10) {
print " |
"
line=1
}
print
}'
# Insert index table at front of file.
while read LINE; do
if [[ -r $NEWFILE3 ]]; then
if [[ "$LINE" == '' ]]; then
echo ""
cat $NEWFILE3 2>/dev/null
rm -f $NEWFILE3
echo "
"
fi
fi
echo $LINE
done <$NEWFILE >$NEWFILE2
mv $NEWFILE2 $NEWFILE
# If the new file is different overwrite the old one, else throw it
# away. This keeps the timestamp more valid, and lets the doupdate
# script do its job better.
if cmp -s $NEWFILE $FILE ; then
rm $NEWFILE
else
mv $NEWFILE $FILE
fi
##############################################################################
# The date entries are put into the blogs via these emacs macros:
#
#(defun today-insert (&optional comment-only)
# "Insert today's date tag into the blog"
# (interactive "P") ; "P" == Raw universal prefix into the arg "comment-only".
# (if comment-only
# (progn (shell-command "date '+'|tr -d \\\\012|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t)
# (exchange-point-and-mark))
# (progn (shell-command "date '+
%A, %B %d, %Y
'|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t)
# (exchange-point-and-mark)
# (insert "\n") (open-line 2))))
#
#(defun yesterday-insert (&optional comment-only)
# "Insert yesterday's date tag into the blog"
# (interactive "P") ; "P" == Raw universal prefix into the arg "comment-only".
# (if comment-only
# (progn (shell-command "~/bin/yesterday '+'|tr -d \\\\012|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t)
# (exchange-point-and-mark))
# (progn (shell-command "~/bin/yesterday '+%A, %B %d, %Y
'|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t)
# (exchange-point-and-mark)
# (insert "\n") (open-line 2))))
#
#(defun tomorrow-insert (&optional comment-only)
# "Insert tomorrow's date tag into the blog"
# (interactive "P") ; "P" == Raw universal prefix into the arg "comment-only".
# (if comment-only
# (progn (shell-command "~/bin/tomorrow '+'|tr -d \\\\012|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t)
# (exchange-point-and-mark))
# (progn (shell-command "~/bin/tomorrow '+%A, %B %d, %Y
'|sed 's/\\([= ]\\)0\\([1-9]\\)/\\1\\2/g'" t)
# (exchange-point-and-mark)
# (insert "\n") (open-line 2))))
#
# The ~/bin/tomorrow shell script:
#
#if [ `date +%Z` == "PDT" ] ; then
# TZ=GMT-17 date "$@"
#else # PST
# TZ=GMT-16 date "$@"
#fi
#
# The ~/bin/yesterday shell script:
#
#if [ `date +%Z` == "PDT" ] ; then
# TZ=GMT+31 date "$@"
#else # PST
# TZ=GMT+32 date "$@"
#fi
#
##############################################################################