#!/bin/sh
#
# Copyright (C) 2001-2008 Graeme Walker <graeme_walker@users.sourceforge.net>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or 
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ===
#
# mu2html.sh
#
# Mark-up to html converter. Converts the output of "txt2mu.sh" 
# into html. (Also see "index.sh".)
#
# Does some in-line formatting independently of the line-based 
# mark-up in the input. For example, it converts quoted words into 
# "<em></em>" tags, and it converts "*foo* [bar]" text into
# hypertext links.
#
# The "-x" flag suppresses the output of html header and
# footer sections. This is useful when the output is to be
# spliced into another html document.
#
# usage: mu2html.sh [-a <awk>] [-x] [<title> [<stylesheet>]]
#
# (If the title is not supplied then the input is copied
# to a temporary file in order to extract the H1 header
# text for the title.)
#

awk="gawk"
if test "${1}" = "-a"
then
	shift
	if test "${1}" != "" ; then awk="${1}" ; fi
	shift
fi

full="1"
if test "${1}" = "-x"
then
	full="0"
	shift
fi

title="${1}"
stylesheet="${2}"


if test "${stylesheet}" != "" -a \! -f "${stylesheet}"
then
	: # echo `basename $0`: warning: missing stylesheet: ${stylesheet} >&2
fi

Main()
{
	${awk} -v prefix="`basename $0`" -v title="${1}" -v stylesheet="${2}" -v full="${3}" '
	BEGIN {
		if( full )
		{
			dtd_name = "-//W3C//DTD HTML 4.01//EN"
			dtd_ref = "http://www.w3.org/TR/html4/strict.dtd"
			printf( "<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">\n" , dtd_name , dtd_ref )
			printf( "<html>\n" )
			printf( " <head>\n" )
			printf( "  <title>%s</title>\n" , title )
			printf( "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n" )
			if( length(stylesheet) )
				printf( "  <link rel=\"stylesheet\" href=\"%s\" type=\"text/css\">\n" , stylesheet )
			printf( " </head>\n" )
			printf( " <body>\n" )
			printf( "  <!-- index:0::::%s -->\n" , title )
			printf( " <div class=\"div-main\">\n" )
		}
	}
	function arg1( type )
	{
		sub( "[^,]*," , "" , type )
		sub( ",.*" , "" , type )
		return type
	}
	function arg2( type )
	{
		sub( "[^,]*," , "" , type )
		sub( "[^,]*," , "" , type )
		sub( ",.*" , "" , type )
		return type
	}
	function escape( line )
	{
		gsub( "&" , "\\&amp;" , line )
		gsub( "<" , "\\&lt;" , line )
		gsub( ">" , "\\&gt;" , line )
		return line
	}
	function dequote( line )
	{
		quote = "\""
		not_quote = "[^" quote "]"
		start_tag="<em class=\"quote\">"
		end_tag="</em>"
		gsub( quote not_quote "*" quote , start_tag "&" end_tag , line )
		gsub( start_tag quote , start_tag , line )
		gsub( quote end_tag , end_tag , line )
		return line
	}
	function fn( line )
	{
		gsub( "[^[:space:]][^[:space:]]*\\(\\)" , "<em class=\"fn\">&</em>" , line )
		return line
	}
	{
		pos = index( $0 , ":" )
		type = substr( $0 , 1 , pos )
		tail = substr( $0 , pos+1 )
		etail_raw = escape(tail)
		etail = fn(dequote(escape(tail)))
	
		# h1
		if( match(type,"^h1[:,]") )
		{
			printf( "  <h1><a class=\"a-header\" name=\"H_%d\">%s</a></h1> " , arg1(type) , etail )
			printf( "<!-- index:1:H:%d::%s -->\n" , arg1(type) , etail )
		}
	
		# h2
		else if( match(type,"^h2[:,]") )
		{
			printf( "   <h2><a class=\"a-header\" name=\"SH_%d_%d\">%s</a></h2> " , arg1(type) , arg2(type) , etail )
			printf( "<!-- index:2:SH:%d:%d:%s -->\n" , arg1(type) , arg2(type) , etail )
		}
	
		# item
		else if( match(type,"^item,1[:,]") )
			printf( "    <ul>\n     <li>%s</li>\n" , etail )
		else if( match(type,"^item[:,]") )
			printf( "     <li>%s</li>\n" , etail )
		else if( match(type,"^item-end[:,]") )
			printf( "    </ul>\n" , etail )
	
		# item-numbered
		else if( match(type,"^item-numbered,1[:,]") )
			printf( "    <ol>\n     <li>%s</li>\n" , etail )
		else if( match(type,"^item-numbered[:,]") )
			printf( "     <li>%s</li>\n" , etail )
		else if( match(type,"^item-numbered-end[:,]") )
			printf( "    </ol>\n" , etail )
	
		# item-outer
		else if( match(type,"^item-outer,1[:,]") )
			printf( "    <ul>\n     <li>%s\n" , etail )
		else if( match(type,"^item-outer[:,]") )
			printf( "     </li>\n     <li>%s\n" , etail )
		else if( match(type,"^item-outer-end[:,]") )
			printf( "     </li>\n    </ul>\n" , etail )
	
		# item-inner
		else if( match(type,"^item-inner,1[:,]") )
			printf( "      <ul>\n       <li>%s</li>\n" , etail )
		else if( match(type,"^item-inner[:,]") )
			printf( "       <li>%s</li>\n" , etail )
		else if( match(type,"^item-inner-end[:,]") )
			printf( "      </ul>\n" , etail )
	
		# item-name
		else if( match(type,"^item-name,1[:,]") )
			printf( "    <dl>\n     <dt>%s</dt>\n" , etail )
		else if( match(type,"^item-name[:,]") )
			printf( "     <dt>%s</dt>\n" , etail )
		else if( match(type,"^item-name-end[:,]") )
			printf( "    </dl>\n" , etail )
	
		# item-detail
		else if( match(type,"^item-detail,1[:,]") )
			printf( "      <dd>\n       %s\n" , etail )
		else if( match(type,"^item-detail[:,]") )
			printf( "       %s\n" , etail )
		else if( match(type,"^item-detail-end[:,]") )
			printf( "      </dd>\n" )
		else if( match(type,"^item-detail-blank[:,]") )
			printf( "       <p class=\"p-break\"></p>\n" )

		# code
		else if( match(type,"^code,1[:,]") )
			printf( "      <div class=\"div-pre\">\n       <pre>%s" , etail_raw )
		else if( match(type,"^code[:,]") )
			printf( "\n%s" , etail_raw )
		else if( match(type,"^code-end[:,]") )
			printf( "</pre>\n      </div><!-- div-pre -->\n" )

		# text
		else if( match(type,"^text,1[:,]") )
			printf( "    <p>\n     %s\n" , etail )
		else if( match(type,"^text[:,]") )
			printf( "     %s\n" , etail )
		else if( match(type,"^text-end[:,]") )
			printf( "    </p>\n" , etail )

		# footer-text
		else if( match(type,"^footer,1[:,]") )
			printf( "    <div class=\"div-footer\">\n     <p>\n      %s\n" , etail )
		else if( match(type,"^footer[:,]") )
			printf( "      %s\n" , etail )
		else if( match(type,"^footer-end[:,]") )
			printf( "     </p>\n    </div><!-- div-footer -->\n" , etail )

		# citation-text
		else if( match(type,"^citation,1[:,]") )
			printf( "    <p class=\"citation\">\n     %s\n" , etail )
		else if( match(type,"^citation[:,]") )
			printf( "     %s\n" , etail )
		else if( match(type,"^citation-end[:,]") )
			printf( "    </p>\n" , etail )

		# author-text
		else if( match(type,"^author,1[:,]") )
			printf( "    <p class=\"author\">\n     %s\n" , etail )
		else if( match(type,"^author[:,]") )
			printf( "     %s\n" , etail )
		else if( match(type,"^author-end[:,]") )
			printf( "    </p>\n" , etail )

		# html
		else if( match(type,"^html[:,]") )
			printf( "%s\n" , tail )

		# image
		else if( match(type,"^image[:,]") )
			printf( "<img src=\"%s\" alt=\"%s\">\n" , tail , "image" )

		# blank
		else if( match(type,"^blank[:,]") )
			printf( "\n" )
	
		# ignore
		else if( match(type,"^ignore") )
			printf( "" )

		else if( match(type,"[^[:space:]]") )
			printf( "%s: unrecognised mark-up tag on line %d: \"%s\"\n" , prefix , NR , type )>"/dev/fd/2"
	}
	END {
		if( full )
		{
			printf( " </div> <!-- div-main -->\n" )
			printf( " </body>\n" )
			printf( "</html>\n" )
		}
	} ' 
}

Anchorise()
{
	sed 's/\*\([^\*]*\)\* \[\([^]]*\)\]/<a class=\"a-href\" href="\2">\1<\/a>/g'
}

if test "${title}" = ""
then
	tmp="`basename $0`.tmp"
	${awk} '{print}' > ${tmp}
	title="`${awk} '/^h1/ { sub(\"[^:]*:\",\"\") ; print ; exit }' ${tmp}`"
	${awk} '{print}' ${tmp} | Main "${title}" "${stylesheet}" "${full}" | Anchorise
	rm -f ${tmp}
else
	Main "${title}" "${stylesheet}" "${full}" | Anchorise
fi

