Installing Textile

(March 10 2006)

Nanoblogger 3.1 (the version delivered by Debian3.1) didn't have any markup for entries or articles. So I added Markdown, because it's used in version 3.3 and was easy to install and retrofit.

But I wasn't very happy with Markdown - especially around code blocks. Multiple times I entered what should have been a code block, only to see it rendered as text. I got to the stage of cut'n'paste'ing working code blocks into my new document, and then replacing their text with what I wanted … sometimes it worked, sometimes it didn't. There was obviously something very brittle going on with invisible characters somewhere.

Seeing as I'm playing with Ruby on Rails at the moment, I thought I'd use Textile markup - it's one of the canonical examples of markup alongside Markdown (via the RedCloth module). But there doesn't seem to be a package in Debian that delivers Textile as a stand-alone command-line filter program, like there is with Markdown …

So let's see just how good Ruby is as a system scripting language. I need a program that sucks in STDIN, processes the whole lot through the RedCloth module to produce HTML, and then spits it all out on STDOUT.

One line.

Well, that's ignoring the shell interpreter declaration, the two “require” directives to actually provide RedCloth to Ruby, and the comments. But really, the code is one line. That's what Ruby is for!

   #!/usr/bin/env ruby
   # Textile'ise things.
   # Take Textile markup on STDIN, and output HTML to STDOUT
   require 'rubygems'
   require 'redcloth'
   print (RedCloth.new STDIN.read).to_html

This gives me a nice command to filter Textile to HTML, the equivalent of the markdown command I'd been using previously.

To get nanoblogger to use Textile markup for both entries and articles, I have to make two changes - one nice and one ugly-ish.

The nice one is for entries - go to /usr/share/nanoblogger/plugins/entries/body and remove markdown.sh, replacing it with textile.sh :-

    # NanoBlogger plugin to render Textile format entries
    # Textile is documented and implemented at
    #   <URL:http://www.textism.com/tools/textile/>

    TEXTILE="/usr/local/bin/textilise.rb"
    TEXTILE_OPTS=""

    nb_msg "$plugins_textformataction `basename $nb_plugin` ..."
    NB_EntryBody=$(echo "$NB_EntryBody" | ${TEXTILE} ${TEXTILE_OPTS})

For articles, we have to add the equivalent code directly into /usr/share/nanoblogger/plugins/zyx_articles.sh :-

    create_article(){
        if [ -f "$BLOG_DIR/$ARTICLE_DIR/$article_srcfile" ]; then
                MKPAGE_OUTFILE="$BLOG_DIR/$ARTICLE_DIR/$article_outfile"
                NB_Entries=`sed 1d "$BLOG_DIR/$ARTICLE_DIR/$article_srcfile"`
                # modify page content used by make_page function
                #MKPAGE_CONTENT="$NB_Entries"
                TEXTILE="/usr/local/bin/textilise.rb"
                TEXTILE_OPTS=""
                MKPAGE_CONTENT=$(echo "$NB_Entries" | ${TEXTILE} ${TEXTILE_OPTS})
                [ "$weblog_update" = articles ] && rm -f "$MKPAGE_OUTFILE"
                make_page "$BLOG_DIR/$ARTICLE_DIR/$article_srcfile" \
                          "$ARTICLE_TEMPLATE" "$MKPAGE_OUTFILE"
        fi
        }

This means that all new entries and articles will be formatted with Textile. Existing stuff that has been rendered with Markdown will be fine - until I ask nb to regenerate everything, with nb -u all. Before then I have to re-markup the articles, or figure out if I want to use the FORMAT metadata I've noticed, but not investigated. I guess I'll just rewrite - there isn't much, and it gives me a chance to stretch my knowledge of Textile :-)