# $HeadURL: https://tstotts.net/pubvc/bash-snippets/muse.sh $
# $Id: muse.sh 309 2006-12-27 04:24:13Z tas $

# NanoBlogger plugin to render Emacs MUSE format entries as HTML or XHTML.
# MUSE is documented and implemented at
#   <URL:http://www.mwolson.org/projects/EmacsMuse.html>

# Authored by Timothy Stotts in 2006.
#
# This is free software distributed under the GPL.
# It has no warranty. See the GNU General Public License for more
# information. 

MUSE_EMACS="emacs"
MUSE_EMACS_OPTS="--batch -q"

function muse_publish ()
{
    tmpdir=$MUSE_TMPDIR
    [[ -z $tmpdir ]] && tmpdir=$TMPDIR
    [[ -z $tmpdir ]] && tmpdir=/tmp
    style=$MUSE_STYLE
    [[ -z $style ]] && style="html"

    pre=${tmpdir}/muse_publish_${RANDOM}
    infile=${pre}.muse
    outfile=${pre}.html
    
    cat > "${infile}"

    read -d '' lispcode <<EOF
(require 'muse)
(require 'muse-html)
(require 'muse-publish)
(setq muse-html-footer "")
(setq muse-html-header "")
(setq muse-xhtml-footer "")
(setq muse-xhtml-header "")
(muse-publish-file "${infile}" "${style}" "${tmpdir}/" t)
(save-buffers-kill-emacs)
EOF

    ${MUSE_EMACS} ${MUSE_EMACS_OPTS} --load <(echo ${lispcode}) >/dev/null 2>&1

    cat "${outfile}"

    rm -f "${infile}" "${outfile}"
}

# For use with NanoBlogger, create two files:
#    plugins/entry/format/muse.sh
#    plugins/page/format/muse.sh

# The contents of 'plugins/page/format/muse.sh' should be this file, with the
# following two lines uncommented:
#
# nb_msg "$plugins_textformataction `basename $nb_plugin` ..."
# MKPAGE_CONTENT=`echo "$MKPAGE_CONTENT" | muse_publish`

# The contents of 'plugins/entry/format/muse.sh' should be this file, with the
# following one line uncommented:
#
# NB_EntryBody=$(echo "$NB_EntryBody" | muse_publish)

# To use this conversion on the command line, source this file and then:
#
# cat myfile.muse | muse_publish > myfile.html

# The variable MUSE_STYLE may be set as 'html' or 'xhtml' for differing output
# styles.

# The variable MUSE_TMPDIR may be set to specify a temporary work directory.

# Add the following to MUSE_EMACS_OPTS if emacs cannot find your muse
# installation:
#
#     --directory /my/path/to/muse
