Markdown, PlainText, Dropbox, and shell scripts

I am a big fan of Dropbox, the automatic folder syncing service that keeps a document folder synchronized across all of your computers and handheld devices. I became a bigger fan when I found iPhone apps like PlainText, which acts as a Dropbox-synchronized notepad. You can write notes on your desktop, then see and update them on your phone and vice-versa. The files themselves are just text files, not some weird proprietary format, so you can work with them on the desktop, too.

I’ve also started to get intrigued by Gruber’s Markdown “language” (for lack of a better word). It allows you to write text in a simple and readable way that can then be automatically translated to stylized and linked text. For instance, writing something *like this* will make it bold upon publication. Writing [Google](http://google.com) will produce a link to Google. Nested bullet lists are similarly easy.

I have a few notes in PlainText that consist of lists and links. They translate quite easily into Markdown, but I couldn’t find any great iPhone tools for viewing or editing Markdown across a Dropbox share. So I did what any self-respecting Unix admin would do: I wrote a shell script!

Specifically, the shell script looks for files in my Dropbox PlainText folder with the extension “.md.txt”. This indicates it’s a text file in Markdown format. If there’s no corresponding “.html” file or the .md.txt is newer than the .html file, then it generates a new HTML file.

What this means is that I can edit a note on my iPhone or iPad (or from a shortcut in the dock of desktop computer)…

Markdown Source

…and then when the script is run, a corresponding HTML file is available:

Generated HTML (Browser)

Since I cannot (easily) kick off a shell script from my phone, this is set up as a cron job on one of my desktop machines. It runs every 5 minutes, looks for missing or out-of-date HTML files, then generates what it needs to. I guess I could have made it run more frequently, but the sorts of notes I take rarely change and are mainly used as reference every few days or weeks.

For those that are interested, the script is reproduced here:

#!/bin/sh
MARKDOWN=/usr/local/bin/Markdown.pl
TMP=/tmp/

find . -name '*.md.txt' > ${TMP}markdownlist.$$
exec 0<${TMP}markdownlist.$$
while read SRC
do
    DST=`echo $SRC | sed 's/.md.txt/.html/'`
    REBUILD=0
    if [ ! -f "$DST" ]; then			# doesn't exist -> rebuild
        REBUILD=1
    elif [ "$SRC" -nt "$DST" ]; then	# html older than Markdown -> rebuild
        REBUILD=1
    fi
    if [ "$1" == "-force" ]; then
        REBUILD=1
    fi
    if [ $REBUILD -eq 1 ]; then
        echo "$SRC -> $DST"
        echo '<html><head><title>' > $DST
        echo "$DST" | sed 's/.*\///' | sed 's/.html//' >> $DST
        echo '</title><body>' >> $DST
        cat "$SRC" | $MARKDOWN >> $DST
        echo '</body></html>' >> $DST
    fi
done
rm -f ${TMP}markdownlist.$$

I originally tried to do this as a Makefile, but using make to process files with possible spaces in their names proved to be more difficult than expected. Make assumes space-delimited filenames in most places and the tricks to convert spaces to something else, then back, did not mesh so well with wildcards and filenames that are unknown at runtime.

Posted in: Code Dear Diary iPhone

Published by

Brian Enigma

Brian Enigma is a Portlander, manipulator of atoms & bits, minor-league blogger, and all-around great guy. He typically writes about the interesting “maker” projects he's working on, but sometimes veers off into puzzles, software, games, local news, and current events.

2 thoughts on “Markdown, PlainText, Dropbox, and shell scripts”

  1. Thanks, a few days ago made something like it but simpler for Markdown, using Hazel, using your compilation code and today I came back to learn about the -nt operator which is way useful! This time is for automatic LaTeX compilation 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *