#!/bin/tcsh -f # Run this when you add or change entries and want them # to be incorporated into the formatted journal. # What format should dates be displayed in? (see "man date") set dateFmt="+%A, %B %d, %Y" # Do you want the index in forward or reverse chronological order? #set order=forward set order=reverse # NOTE Neither of these dirs should be a child of Journal: # Where are you going to put images (relative to Journal/)? set imsDir="../ims" # Where do you want to keep scripts (relative to Journal/)? set scriptsDir="/home/simon/scripts" # The rest of this script is generic. # (I.e., you shouldn't need to change anything below here.) if (! -d entries) then echo "'entries' source directory doesn't exist." exit 1 endif if (! -d parts) then echo "'parts' source directory doesn't exist." exit 1 endif if (! -d Journal) then echo "Journal target directory doesn't exist. Make it." exit 1 endif echo "Searching for new entries..." # For now, we'll just use files containing a ".h" in their name # to signify highlights: # cd entries set entries=* set entriesH=*.h* cd .. echo "There are $#entries raw entries to date ($#entriesH highlights)..." # It's easiest just to rebuild the index from scratch... # We'll throw the index entries into a temp file so we # can stick it together with header and footer later. # (This also lets us reverse the order at that time if # we choose...): echo -n "" > Journal/index.tmp # Let's also make a Highlights index: echo -n "" > Journal/indexh.tmp # Backing up from the begining drops you back in the index: set prevFile="index.html" set prevFileH="indexh.html" while ($#entries > 0) # Pull the next entry off the list: set entry="$entries[1]" shift entries # And if it's the same as the next highlight entry, # make a note of it and pull it off the highlight list too: set highlight="0" if ($#entriesH > 0) then if ("$entry" == "$entriesH[1]") then set highlight="1" shift entriesH endif endif # Check and make sure the file's name has a reasonable format: set dateString=`date -d $entry:r:r "$dateFmt"` if ($status != 0) then echo "WARNING: entry '$entry:r:r' ($entry) cannot be parsed as a date. Please fix." continue endif # Project the name of the Next entry: if ($#entries > 0) then set nextFile="$entries[1].html" else set nextFile="index.html" endif # Project the names of the next Highlight entry: if ($#entriesH > 0) then set nextFileH="$entriesH[1].html" else set nextFileH="indexh.html" endif # Source and destination files: set entryFile="entries/$entry" set target="Journal/$entry.html" # Extract the title of the entry: set title=`head -1 $entryFile | sed -f NameMappings` # Count the images in the entry: set numims=`grep " 0) then set imnote="($numims)" else set imnote="" endif # Count the mp3's in the entry: set nummps=`grep ".mp3" $entryFile | wc -l` if ($nummps > 0) then set mpnote="($nummps)" else set mpnote="" endif # Add the entry to the index: echo -n "${dateString}" >> Journal/index.tmp echo "$title $imnote $mpnote" >> Journal/index.tmp # If the entry's tagged as a highlight, add it to the highlights index too: if ("$highlight") then echo -n "${dateString}" >> Journal/indexh.tmp echo "$title $imnote $mpnote" >> Journal/indexh.tmp endif # If the target doesn't exist: if (! -e "$target") then echo "New on ${dateString}: $title" goto needsUpdate endif # If the source has been changed since we made the target... if ( -M "$target" < -M "$entryFile" ) then echo "Modified for ${dateString}: $title" goto needsUpdate endif # If this target's Next or Previous links aren't right: if ( `cat $target-next` != "$nextFile" || \ `cat $target-prev` != "$prevFile" || \ `cat $target-nextH` != "$nextFileH" || \ `cat $target-prevH` != "$prevFileH" ) then echo "Next/Prev changed for ${dateString}: $title" goto needsUpdate endif if ( -M "$target" < -M "$entryFile" ) then echo "Modified for ${dateString}: $title" goto needsUpdate endif goto skipUpdate needsUpdate: # Here's where we actually put the pieces together into a target entry: echo "" > "$target" echo "$title" >> "$target" sed -f NameMappings < parts/header >> "$target" echo "
" >> "$target" echo "
" >> "$target" echo "
" >> "$target" echo '' >> "$target" echo '' >> "$target" echo "

$dateString

" >> "$target" echo "

$title

" >> "$target" echo '' >> "$target" echo "
" >> "$target" echo "
" >> "$target" echo "
" >> "$target" tail +2 "$entryFile" | sed -f NameMappings >> "$target" echo -n "

[" >> "$target" echo "<< | " >> "$target" echo "Prev | " >> "$target" echo "Index | " >> "$target" echo "Next | " >> "$target" echo ">>]" >> "$target" echo "

" >> "$target" sed -f NameMappings < parts/footer >> "$target" # Keep notes on what we did: echo -n "$nextFile" > "$target-next" echo -n "$prevFile" > "$target-prev" echo -n "$nextFileH" > "$target-nextH" echo -n "$prevFileH" > "$target-prevH" skipUpdate: set prevFile="$entry.html" if ("$highlight") set prevFileH="$entry.html" end # Now let's build the index.html file: /bin/rm -f Journal/index.html sed -f NameMappings < parts/indexHeader > Journal/index.html if ("$order" == "reverse") then tac Journal/index.tmp >> Journal/index.html else cat Journal/index.tmp >> Journal/index.html endif sed -f NameMappings < parts/indexFooter >> Journal/index.html /bin/rm -f Journal/index.tmp echo "Index updated." # And an indexh.html file (this block is a clone of the index.hmtl block above): /bin/rm -f Journal/indexh.html sed -f NameMappings < parts/indexhHeader > Journal/indexh.html if ("$order" == "reverse") then tac Journal/indexh.tmp >> Journal/indexh.html else cat Journal/indexh.tmp >> Journal/indexh.html endif sed -f NameMappings < parts/indexhFooter >> Journal/indexh.html /bin/rm -f Journal/indexh.tmp echo "Highlights Index updated." if (! -e Journal/scripts && ! -l Journal/scripts) then ln -s "$scriptsDir" Journal/scripts endif if (! -e Journal/ims && ! -l Journal/ims) then ln -s "$imsDir" Journal/ims endif