Tuesday, July 3, 2007

Blog tools

So, this is a bash script that makes blogging easy.

#!/bin/sh

#prepares blog post using pandoc and xclip.

#path to programs used
SED=sed
PANDOC=pandoc
XCLIP=xclip
TEE=tee

if(( $# < 1 )); then
    echo "Usage: $0 <input file>"
    exit 1
fi

input="$1"

{
    echo '<div class="pandoc_wrap"><div class="pandoc_html">'
    $PANDOC -w html $input
    echo '</div><div class="pandoc_txt">'
    $SED 's/^/    /' $input | $PANDOC -w html
    echo '</div></div>'
} | tee >($XCLIP)

It creates a <div class="pandoc_wrap">, so you can use DOM to add a button to that div and make its children toggle. Here is example using JQuery:

function show_pandoc_html(pandoc_button) {
    pandoc_button.siblings('.pandoc_html').css('display', 'inline');
    pandoc_button.siblings('.pandoc_txt').css('display', 'none');
    pandoc_button.val('Markdown');
}

function show_pandoc_txt(pandoc_button) {
    pandoc_button.siblings('.pandoc_html').css('display', 'none');
    pandoc_button.siblings('.pandoc_txt').css('display', 'inline');
    pandoc_button.val('Html');
}

function attach_pandoc_button(pandoc_txt) {
    $('<input type="button" class="pandoc_button" value="Click"/><br/>')
        .toggle(function() { show_pandoc_html($(this)); }
                , function() { show_pandoc_txt($(this)); })
        .insertBefore(pandoc_txt.siblings('.pandoc_html'));
}

$(document).ready(function(){
    $('.pandoc_txt').each(function() { attach_pandoc_button($(this)); })
        .css('display', 'none');
});

No comments:

Post a Comment