emailrelay/bin/mu2docbook.sh_
Graeme Walker ed23b3c747 v1.5
2007-04-10 12:00:00 +00:00

316 lines
8.2 KiB
Bash

#!/bin/sh
#
# Copyright (C) 2001-2007 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 2 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, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# ===
#
# mu2docbook.sh
#
# Mark-up to docbook converter. Converts the output of "txt2mu.sh"
# into docbook markup.
#
# 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 the xml header. This is
# useful when the output is to be spliced into another document.
#
# usage: mu2docbook.sh [-a <awk>] [{-x|--head|--tail}] [<title>]
#
# (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
op=""
if test "${1}" = "--head"
then
shift
op="head"
elif test "${1}" = "--tail"
then
shift
op="tail"
fi
title="${1}"
Head()
{
echo '' | ${awk} -v prefix="`basename $0`" -v title="${1}" -v full="${2}" '
BEGIN {
if( full )
{
dtd_name="-//OASIS//DTD DocBook XML V4.1.2//EN"
dtd_ref="http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"
qq="\""
printf( "%s\n" , "<?xml version=\"1.0\"?>" )
printf( "<!DOCTYPE article PUBLIC \"%s\" \"%s\">\n" , dtd_name , dtd_ref )
printf( "<article>\n" )
printf( " <articleinfo>\n" ) ;
printf( " <title>%s</title>\n" , title )
printf( " </articleinfo>\n" )
}
} '
}
Tail()
{
echo '' | ${awk} -v prefix="`basename $0`" -v title="${1}" -v full="${2}" '
BEGIN {
if( full )
{
printf( "</article>\n" )
}
} '
}
Body()
{
${awk} -v prefix="`basename $0`" -v title="${1}" -v full="${2}" '
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="<emphasis>"
end_tag="</emphasis>"
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:]]*\\(\\)" , "<literal>&</literal>" , 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[:,]") )
{
if( done_h2 )
printf( " </section>\n" )
if( done_h1 )
printf( " </section>\n" )
printf( " <section><title>%s</title>\n" , etail )
done_h1 = 1
done_h2 = 0
}
# h2
else if( match(type,"^h2[:,]") )
{
if( done_h2 )
printf( " </section>\n" , etail )
printf( " <section><title>%s</title>\n" , etail )
done_h2 = 1
}
# item
else if( match(type,"^item,1[:,]") )
printf( " <itemizedlist>\n <listitem><para>%s</para></listitem>\n" , etail )
else if( match(type,"^item[:,]") )
printf( " <listitem><para>%s</para></listitem>\n" , etail )
else if( match(type,"^item-end[:,]") )
printf( " </itemizedlist>\n" , etail )
# item-numbered
else if( match(type,"^item-numbered,1[:,]") )
printf( " <orderedlist>\n <listitem><para>%s</para></listitem>\n" , etail )
else if( match(type,"^item-numbered[:,]") )
printf( " <listitem><para>%s</para></listitem>\n" , etail )
else if( match(type,"^item-numbered-end[:,]") )
printf( " </orderedlist>\n" , etail )
# item-outer
else if( match(type,"^item-outer,1[:,]") )
printf( " <itemizedlist>\n <listitem><para>%s\n" , etail )
else if( match(type,"^item-outer[:,]") )
printf( " </para></listitem>\n <listitem><para>%s\n" , etail )
else if( match(type,"^item-outer-end[:,]") )
printf( " </para></listitem>\n </itemizedlist>\n" , etail )
# item-inner
else if( match(type,"^item-inner,1[:,]") )
printf( " <itemizedlist>\n <listitem><para>%s</para></listitem>\n" , etail )
else if( match(type,"^item-inner[:,]") )
printf( " <listitem><para>%s</para></listitem>\n" , etail )
else if( match(type,"^item-inner-end[:,]") )
printf( " </itemizedlist>\n" , etail )
# item-name
else if( match(type,"^item-name,1[:,]") )
printf( " <variablelist>\n <varlistentry><term>%s</term>\n" , etail )
else if( match(type,"^item-name[:,]") )
printf( " </varlistentry>\n <varlistentry><term>%s</term>\n" , etail )
else if( match(type,"^item-name-end[:,]") )
printf( " </varlistentry>\n </variablelist>\n" , etail )
# item-detail
else if( match(type,"^item-detail,1[:,]") )
printf( " <listitem><para>\n %s\n" , etail )
else if( match(type,"^item-detail[:,]") )
printf( " %s\n" , etail )
else if( match(type,"^item-detail-end[:,]") )
printf( " </para></listitem>\n" )
else if( match(type,"^item-detail-blank[:,]") )
printf( " </para><para>\n" )
# code
else if( match(type,"^code,1[:,]") )
printf( " <programlisting>%s" , etail_raw )
else if( match(type,"^code[:,]") )
printf( "\n%s" , etail_raw )
else if( match(type,"^code-end[:,]") )
printf( "</programlisting>\n" )
# text
else if( match(type,"^text,1[:,]") )
printf( " <para>\n %s\n" , etail )
else if( match(type,"^text[:,]") )
printf( " %s\n" , etail )
else if( match(type,"^text-end[:,]") )
printf( " </para>\n" )
# footer-text
else if( match(type,"^footer,1[:,]") )
printf( " <para>\n %s\n" , etail )
else if( match(type,"^footer[:,]") )
printf( " %s\n" , etail )
else if( match(type,"^footer-end[:,]") )
printf( " </para>\n" )
# 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" )
# author-text
else if( match(type,"^author,1[:,]") )
printf( " <para>\n %s\n" , etail )
else if( match(type,"^author[:,]") )
printf( " %s\n" , etail )
else if( match(type,"^author-end[:,]") )
printf( " </para>\n" )
# html
else if( match(type,"^html[:,]") )
printf( "%s\n" , tail )
# image
else if( match(type,"^image[:,]") )
printf( "<graphic fileref=\"%s\"/>\n" , tail )
# 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( done_h2 )
printf( " </section>\n" )
if( done_h1 )
printf( " </section>\n" )
} '
Tail
}
Main()
{
Head "${title}" "${full}"
Body "${title}" "${full}"
Tail "${title}" "${full}"
}
Anchorise()
{
sed 's/\*\([^\*]*\)\* \[\([^]]*\)\]/<ulink url="\2">\1<\/ulink>/g'
}
if test "${op}" = "head"
then
Head "${title}" "${full}"
elif test "${op}" = "tail"
then
Tail "${title}" "${full}"
elif test "${title}" = ""
then
tmp="`basename $0`.tmp"
${awk} '{print}' > ${tmp}
title="`${awk} '/^h1/ { sub(\"[^:]*:\",\"\") ; print ; exit }' ${tmp}`"
${awk} '{print}' ${tmp} | Main "${title}" "${full}" | Anchorise
rm -f ${tmp}
else
Main "${title}" "${full}" | Anchorise
fi