#!/bin/sh # Typeset a directory of Markdown-formatted files. # Copyright (C) July 2009, Jason LaPorte. PUBLISH_HOST=ftp.agoragames.com PUBLISH_USER=lpe PUBLISH_PATH=public PUBLISH_TOUCHFILE=.last_published recently_modified () { ls -1t *.mkd | grep -v index.mkd | head -n 5 } extract_title () { ruby -e 't = gets; puts t =~ /\[(.+)\](?:\[.*\]|\(.+\))/ ? $1 : t' <$1 } sftp_sync_directory () { ROOT=$1 for PATH in $ROOT/*; do if [ $PATH -nt $PUBLISH_TOUCHFILE ]; then if [ -d $PATH ]; then FILE=`/usr/bin/basename $PATH` echo -MKDIR \"$FILE\" echo CD \"$FILE\" sftp_sync_directory $PATH echo CD .. else echo PUT \"$PATH\" fi fi done } sftp_sync () { if [ -n "$PUBLISH_PATH" ]; then echo CD \"$PUBLISH_PATH\" fi sftp_sync_directory . } # This function takes a file as an argument and prints out its nicely typeset # HTML to STDOUT. typeset () { # The first line of the file is assumed to be the title of the document. We # strip it out and use it. If it's a (Markdown-style) link, we rip out the # formatting and only use the content. TITLE=`extract_title $1` # Documents are formatted as XHTML1.0 Strict. The only added markup (beyond # the HTML header, etc.) is a DIV element containing the entire page, and a # footer image that links back home. cat < $TITLE
END # Which version of Markdown you use is up to you. I favor [Discount][1], # since it's fast and does all the proper formatting by default. Furthermore, # I use its div-class-block syntax to support poetry in my CSS. # # [1]: http://www.pell.portland.or.us/~orc/Code/discount/ markdown $1 # Splat out a footer for the HTML... cat <Lavender, the Lonely Pink Elephant
END } # For each markdown file in the current directory... for SRC in *.mkd; do DEST=`basename $SRC .mkd`.html # If the destination file doesn't exist, or if it's source or the boilerplate # HTML has been modified since it was last typeset, typeset it. if [ ! -e $DEST -o $SRC -nt $DEST -o $0 -nt $DEST ]; then typeset $SRC >$DEST fi done # If we wanted to publish the site, do so. if [ "$1" = "publish" ]; then sftp_sync | sftp -C $PUBLISH_USER@$PUBLISH_HOST >/dev/null && \ /usr/bin/touch $PUBLISH_TOUCHFILE fi