# -*- tcl -*-
# $Id: script,v 1.1 1999/05/12 10:38:19 tobotras Exp $
#
# DocBook 3.0 to HTML convertor
#
require TEItools.tcl
require textutil.tcl
require Simple.tcl

TEItoolsSetup "html"

require TEItoolsHTML.tcl

# Save root element of document (could be BOOK, CHAPTER, PART, ARTICLE ...)
set rootElement [query docroot child el gi]

debug "rootElement $rootElement"

specification translate {
    {elements "SCREENSHOT"} {
	#Those are ignored
	prefix		{}
	suffix		{}
	cdataFilter	null
	sdataFilter	null
    }
    {element BOOK} {
	#BOOK is always root element, right?
	prefix		[openBook]
	suffix		[closeBook]
    }
    {element CHAPTER} {
	prefix		"[ifRoot \"[openRootChapter]\" \"[openChapter]\"]"
	suffix		"[ifRoot \"[closeRootChapter]\" \"[closeChapter]\"]"
    }
    {element PART} {
	prefix		"[ifRoot \"[openRootPart]\" \"[openPart]\"]"
	suffix		"[ifRoot \"[closeRootPart]\" \"[closePart]\"]"
    }
    {element ARTICLE} {
	prefix		"[ifRoot \"[openRootArticle]\" \"[openArticle]\"]"
	suffix		"[ifRoot \"[closeRootArticle]\" \"[closeArticle]\"]"
    }
    {element TITLE in CHAPTER} {
	prefix		{\\renewcommand{\\ChapterTitle}\{\\Large }
	suffix		{\}\n\\doChapterTitle\n}
    }
    {element TITLE in BOOK} {
	prefix		{\\renewcommand{\\BookTitle}\{\\Large }
	suffix		{\}\n}
    }
    {element TITLE in PART} {
	prefix		{\\renewcommand{\\PartTitle}\{\\Large }
	suffix		{\}\n\\doPartTitle\n}
    }
    {element TITLE in ARTHEADER} {
	prefix		{\\renewcommand{\\ArtTitle}\{\\Large }
	suffix		{\}\n}
    }
    {element TITLE in BOOKINFO} {
	prefix		{\\renewcommand{\\BookTitle}\{\\Large }
	suffix		{\}\n}
    }
    {element SUBTITLE in BOOKINFO} {
	prefix		{\\renewcommand{\\BookSubTitle}\{\\large }
	suffix		{\}\n}
    }
    {element LEGALNOTICE in BOOKINFO} {
	prefix		{}
	cdataFilter	null
	sdataFilter	null
    }
    {element AUTHORBLURB in AUTHOR} {
	prefix		{}
	cdataFilter	null
	sdataFilter	null
    }
    {element TITLE in FORMALPARA} {
	prefix		{\\textsf\{\\textbf\{}
	suffix		{\}\}}
    }
    {element FIRSTNAME within AUTHOR} {
	prefix		{\\renewcommand{\\FirstName}\{}
	suffix		{\}\n}
    }
    {element SURNAME within AUTHOR} {
	prefix		{\\renewcommand{\\SurName}\{}
	suffix		{\}\n}
    }
    {element DATE in BOOKINFO} {
	prefix		{\\date\{}
	suffix		{\}\n}
    }
    {element AUTHOR within ARTHEADER} {
	# Need to deal with multiple authors!
	prefix		{}
	suffix		{\n\\author{\\FirstName{} \\SurName{}}}
    }
    {element AUTHOR within BOOKINFO} {
	# Need to deal with multiple authors!
	prefix		{}
	suffix		{\n\\author{\\FirstName{} \\SurName{}}}
    }
    {elements "ARTHEADER"} {
	prefix		{}
	suffix		{\n\\doArtTitle\n}
	cdataFilter	null
	sdataFilter	null
    }
    {elements "BOOKINFO"} {
	prefix		{}
	suffix		{\n\\doBookTitle\n}
	cdataFilter	null
	sdataFilter	null
    }
    {element TITLE in FIGURE} {
	prefix		{\\caption\{}
	suffix		{\}\n}
    }
    {element TITLE} {
	prefix		{\\renewcommand{\\SectTitle}\{}
	suffix		"\}\n[doSectTitle [query parent gi]]"
    }
    {element ABSTRACT} {
	prefix		{\n\\begin{abstract}\n}
	suffix		{\n\\end{abstract}\n}
    }
    {elements "SCREEN PROGRAMLISTING"} {
	prefix		{\n\\begin{verbatim}\n}
	suffix		{\\end{verbatim}\n}
	sdataFilter	verbatimFilter
    }
    {elements "SECT1 SECT2 SECT3 SECT4 SECT"} {
	prefix		{}
	startAction {
	    puts stderr "." nonewline
	}
    }
    {elements "GLOSSLIST VARIABLELIST"} {
	prefix		{\n\\begin{description}\n}
	suffix		{\n\\end{description}\n}
    }
    {element ORDEREDLIST} {
	prefix		{\n\\begin{enumerate}\n}
	suffix		{\n\\end{enumerate}\n}
    }
    {element ITEMIZEDLIST} {
	prefix		{\n\\begin{itemize}\n}
	suffix		{\n\\end{itemize}\n}
    }
    {element GLOSSENTRY in GLOSSLIST} {
	prefix		{\n\\item}
    }
    {element VARLISTENTRY in VARIABLELIST} {
	prefix		{\n\\item}
    }
    {element GLOSSTERM in GLOSSENTRY} {
	prefix		{\[}
	suffix		{\]}
    }
    {element TERM in VARLISTENTRY} {
	prefix		{\[}
	suffix		{\]}
    }
    {element GLOSSDEF in GLOSSENTRY} {
	prefix		{ }
    }

    {element LISTITEM} {
	prefix		{\n\\item }
    }
    {element PARA in LISTITEM} {
	prefix		{}
    }
    {elements "PARA SIMPARA FORMALPARA"} {
	prefix		{\n\\par\n}
    }
    {element COMMAND} {
	prefix		{\\texttt\{}
	suffix		{\}}
	cdataFilter	typewriterCdataFilter
	sdataFilter	typewriterSdataFilter
    }
    {element TRADEMARK} {
	prefix		{\\textsc\{}
	suffix		{\}}
    }
    {element ULINK} {
	prefix		{\\textit\{}
	suffix		"\}\\\\footnote{[textCdataFilter [query attval URL]]}"
    }
    {element FILENAME} {
	prefix		{\\texttt\{}
	suffix		{\}}
    }
    {element SYSTEMITEM} {
	prefix		{\\textbf\{\\textsc\{}
	suffix		{\}\}}
    }
    {element USERINPUT} {
	prefix		{\\textsl\{}
	suffix		{\}}
    }
    {element QUOTE} {
	prefix		"[localize lq]"
	suffix		"[localize rq]"
    }
    {element EMPHASIS} {
	prefix		{\\emph\{}
	suffix		{\}}
    }
    {element FIRSTTERM} {
	prefix		{\\textbf\{}
	suffix		{\}}
    }
    {elements "FIGURE GRAPHIC"} {
	prefix		"\n\\\\begin{figure*}\\[[figPlacement]\\]\n[doFigure]"
	suffix		"\\\\end{figure*}\n"
    }
    {element PRODUCTNAME} {
	prefix		{\\textsc\{}
	suffix		{\}}
    }
    {element LITERAL} {
	prefix		{\\texttt\{}
	suffix		{\}}
    }
    {element GUIBUTTON} {
	prefix		{\\textsc\{}
	suffix		{\}}
    }
    {element KEYCAP} {
	prefix		{\\textsc\{\\textbf\{}
	suffix		{\}\}}
    }
    {el} {
	prefix		"[openUnknown]"
	suffix		{}
	cdataFilter	textCdataFilter
	sdataFilter	textSdataFilter
    }
}

proc ifRoot { whatIfRoot whatIfNotRoot } {
    global rootElement
    if { "$rootElement" == "[query gi]" } {
	return $whatIfRoot
    } else {
	return $whatIfNotRoot
    }
}

proc openBook {} {
    return [HTMLpreamble]
}

proc closeBook {} {
    return [HTMLpostamble]
}

proc openChapter {} {
}

proc closeChapter {} {
}

proc openRootChapter {} {
    return [HTMLpreamble]
}

proc closeRootChapter {} {
    return [HTMLpostamble]
}

proc openPart {} {
}

proc closePart {} {
}

proc openRootPart {} {
    return [HTMLpreamble]
}

proc closeRootPart {} {
    return [HTMLpostamble]
}

proc openArticle {} {
}

proc closeArticle {} {
}

proc openRootArticle {} {
    return [HTMLpreamble]
}

proc closeRootArticle {} {
    return [HTMLpostamble]
}

proc doFigure {} {
    set file [query attval FILEREF]
    if { "$file" == "" } {
	set entityref [query attval ENTITYREF]
	withNode entity $entityref {
	    set file [query sysid]
	}
    }
    return [insertGraphicFile $file]
}

proc doSectTitle { gi } {
    array set sectNames { CHAPTER 0 SECT 1 SECT1 1 SECT2 2 SECT3 3 SECT4 4 SECT5 5 }
    if { ![info exists sectNames($gi)] } {
	puts "***INTERNAL ERROR: no sectName for $gi"
	set gi SECT
    }
    global splitNames
    set sectCommand $splitNames($sectNames($gi))
    return "
\\${sectCommand}{\\SectTitle}
"
}

proc HTMLpreamble {} {
    global TEItools_documentclass_value
    if { ![info exist TEItools_documentclass_value] } {
	global rootElement
	switch $rootElement {
	    "BOOK" {
		set TEItools_documentclass_value "book"
	    }
	    default {
		set TEItools_documentclass_value "article"
	    }
	}
    }

    return "% Generated from DocBook 3.0 DTD by TEItools
%See http://xtalk.price.ru/SGML/TEItools

\\documentclass\[[documentClassOptions]\]{$TEItools_documentclass_value}
[localize LaTeX_preamble]
\\usepackage{graphicx,geometry,textcomp}
\\newcommand{\\doChapterTitle}{
    \\chapter{\\ChapterTitle}
}
\\newcommand{\\doPartTitle}{
    \\part{\\PartTitle}
}
\\newcommand{\\doBookTitle}{
    \\title{\\BookTitle\\BookSubTitle}
    \\maketitle
}
\\newcommand{\\doArtTitle}{
    \\title{\\ArtTitle}
    \\maketitle
}
\\newcommand{\\PartTitle}{}
\\newcommand{\\SectTitle}{}
\\newcommand{\\ChapterTitle}{}
\\newcommand{\\BookTitle}{}
\\newcommand{\\BookSubTitle}{}
\\newcommand{\\ArtTitle}{}
\\newcommand{\\FirstName}{}
\\newcommand{\\SurName}{}
[extraPreamble]
[TEItools_Style]
\\begin{document}
\\ifx\\abstract\\undefined%
\\newenvironment{abstract}{Abstract ... }%
{ ... Abstract}%
\\fi
[definePageStyle]
"
}

proc HTMLpostamble {} {
    return "
<!-- Created by TEItools script (http://xtalk.price.ru/SGML/TEItools). -->
"
}
