#		-*- Tcl -*-
# $Id: script,v 1.4 1998/10/13 08:11:31 tobotras Exp $

require TEItools.tcl
require Simple.tcl

rename main Simple_main

set split 0
set baseFileName [string range $argv 0 [string wordend $argv 1]]
set argv [string range $argv [string wordend $argv 1] end]

proc main {} {
    collectTOC
    global TEItools_split_value
    if { [info exists TEItools_split_value] } {
	global split
	set split 1
    }
    uplevel \#0 Simple_main
}

proc collectTOC {} {
    global contents
    process grabHeadings
}

set sectN 0
set ssectN 0

specification grabHeadings {
    {element HEADING in SECT} {
	START {
	    global contents sectN ssectN
	    incr sectN
	    set contents($sectN) [content]
	    set ssectN 0
	}
    }
    {element HEADING in SECT1} {
	START {
	    global contents sectN ssectN
	    incr ssectN
	    set contents(${sectN}.${ssectN}) [content]
	}
    }
}

proc mainFileName {} {
    global baseFileName
    return "${baseFileName}.html"
}

environment outFile \
    fileHandle [open [mainFileName] "w"] \
    section 0 \
    section1 0

proc output { text } {
    puts [outFile get fileHandle] $text nonewline
}

specification translate {

    {element LINUXDOC} {
	prefix		""
	suffix		"\\n</HTML>\\n"
    }
    {element TITLEPAG} {
	prefix	""
	suffix	"[generateHeadings]"
    }
    {element ARTICLE} {
	prefix	""
	suffix	"
  <BR>
  <FONT=\"-2\">Created: [exec date]</FONT>
  <BR>
</BODY>"
    }
    {element TITLE in TITLEPAG} {
	cdataFilter	saveTitle
	prefix		{}
    }
    {element DATE in TITLEPAG} {
	cdataFilter	saveDate
	prefix		{}
    }
    {element ABSTRACT in TITLEPAG} {
	cdataFilter	saveAbstract
	prefix		{}
    }
    {element AUTHOR in TITLEPAG} {
	prefix		{}
	cdataFilter	saveAuthorName
    }
    {element NAME in AUTHOR in TITLEPAG} {
	cdataFilter	saveAuthorName
	prefix		{}
    }
    {element TOC} {
	before		"<HR NOSHADE>"
	startAction	generateTOC
	after		"<HR NOSHADE>"
	prefix		{}
    }
    {element HEADING in SECT} {
	startAction	"set theSect {}"
	suffix		"[doSectTitle]"
	cdataFilter	saveSectHeading
	prefix		{}
    }
    {element HEADING in SECT1} {
	startAction	"set theSect1 {}"
	suffix		"[doSect1Title]"
	cdataFilter	saveSect1Heading
	prefix		{}
    }
    {element SECT} {
	endAction	closeSection
	prefix		{}
    }
    {element P} {
	prefix		"\\n<P>"
	suffix		"</P>"
    }
    {element EM} {
	prefix		"<EM>"
	suffix		"</EM>"
    }
    {element VERB} {
	prefix		"<PRE>"
	suffix		"</PRE>"
    }
    {element TSCREEN} {
	prefix		"\\n<BLOCKQUOTE>\\n"
	suffix		"\\n</BLOCKQUOTE>\\n"
    }
    {element ITEMIZE} {
	prefix		"<UL>\\n"
	suffix		"\\n</UL>\\n"
    }
    {element ENUM} {
	prefix		"<OL>\\n"
	suffix		"\\n</OL>\\n"
    }
    {element LIST} {
	prefix		"<DL>\\n"
	suffix		"\\n</DL>\\n"
    }
    {element ITEM in ITEMIZE} {
	prefix		"\\t<LI> "
	suffix		"</LI>\\n"
    }
    {element ITEM in ENUM} {
	prefix		"\\t<LI> "
	suffix		"</LI>\\n"
    }
    {element ITEM in LIST} {
	prefix		"\\t<DD> "
	suffix		"</DD>\\n"
    }
    {elements "BF TAG"} {
	prefix		"<STRONG>"
	suffix		"</STRONG>"
    }
    {element TT} {
	prefix		"<TT>"
	suffix		"</TT>"
    }
    {element FIGURE} {
	prefix		"<CENTER>\\n"
	suffix		"\\n</CENTER>\\n"
    }
    {element EPS in FIGURE} {
	prefix		"<IMG SRC=[query attval FILE].gif>"
    }
    {element LABEL within FIGURE} {
	startAction	{
	    Debug "found label in figure, id: [query attval ID]"
	}
	prefix		{}
    }
    {element REF} {
	startAction	{
	    Debug "found reference to label [query attval ID]"
	}
	prefix		{}
    }
    {element FOOTNOTE} {
	prefix		"<BLOCKQUOTE><SMALL><I>\\n"
	suffix		"\\n</I></SMALL></BLOCKQUOTE>\\n"
    }
    {elements "URL HTMLURL"} {
	prefix		"<A HREF='[query attval URL']>[query attval NAME]</A>"
    }
    {el} {
	prefix		"[openUnknown]"
    }
}


proc htmlFileHeader { title } {
    global env
    set RCSrevision {$Revision: 1.4 $}
    set fqdn {}
    if { [info exist env(HOST) ] } {
	set fqdn $env(HOST)
    } elseif { [info exist env(HOSTNAME)] } {
	set fqdn $env(HOSTNAME)
    } else {
	catch {set fqdn [exec hostname]}
    }
    if { [string first "." $fqdn] == -1 } {
	catch {set fqdn "${fqdn}.[exec domainname]"}
    }
    return "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">

<!-- Generated from LINUXDOC DTD by CoST script, $RCSrevision

	Notes/advices/flames about script are welcomed <URL:mailto:boris@xtalk.msk.su>

	Converted to HTML by $env(USER)@$fqdn at [exec date]
-->

<HTML>
  <HEAD>
    <TITLE>
      ${title}
    </TITLE>
  </HEAD>
  <BODY>
"
}

proc saveSectHeading { text } {
    global theSect
    append theSect $text
    return
}

proc saveSect1Heading { text } {
    global theSect1
    append theSect1 $text
    return
}

proc doSectTitle {} {
    global theSect contents split

    outFile set section [set sectN [expr [outFile get section] + 1]]
    outFile set section1 0
    set contents(${sectN}) ${theSect}
    set ret {}

    if { $split == 1 } {
	outFile save fileHandle [open [sectFileName $sectN] "w"]
	set ret "[htmlFileHeader ${theSect}]\n"
    }
    append ret "<A NAME=\"S${sectN}\">[sectionHeader $sectN]</A>
<H2>${sectN}  ${theSect}</H2>
"
    return $ret
}

proc doSect1Title {} {
    global theSect1 contents
    
    set sectN [outFile get section]
    outFile set section1 [set ssectN [expr [outFile get section1] + 1]]
    set contents(${sectN}.${ssectN}) $theSect1
    return "
<A NAME=\"S${sectN}.${ssectN}\">[section1Header $sectN $ssectN]</A>
<H3>${sectN}.${ssectN}  ${theSect1}</H3>
"
}


proc sectionHeader { N } {
    global split
    if { $split } {
	return "[ThreeButtons $N]\n<HR>\n"
    } else {
	return ""
    }
}

proc sectionFooter { N } {
    global split
    if { $split } {
	return "\n<HR>\n[ThreeButtons $N]\n"
    } else {
	return ""
    }
}

proc ThreeButtons { N } {
    global contents
    set ret {}
    if { $N > 1 } {
	append ret "<A HREF=\"[sectFileName [expr $N - 1]]\#S[expr $N - 1]\">Previous</A>"
    }
    
    if { [info exists contents([expr $N + 1])] } {
	append ret "&nbsp;<A HREF=\"[sectFileName [expr $N + 1]]\#S[expr $N + 1]\">Next</A>"
    }

    append ret "&nbsp;<A HREF=\"[mainFileName]\#STOC\">Contents</A>"
    return $ret
}

proc section1Header { sect ssect } {
    if { $ssect != 1 } {
	return "<HR NOSHADE>\n"
    }
    return
}

proc sectFileName { N } {
    global split
    if { $split == 1 } {
	global baseFileName
	return "${baseFileName}-${N}.html"
    } else {
	return ""
    }
}

proc closeSection {} {
    global split
    output "
[sectionFooter [outFile get section]]
  </BODY>
</HTML>
"

    if { $split == 1 } {
        close [outFile get fileHandle]
        outFile restore
    }
}

proc saveTitle { text } {
    global theTitle
    append theTitle [entFilter $text]
    return
}

#   ,      general.

proc entFilter { text } {
    regsub -all "&trademark;" $text "<SUP>(tm)</SUP>" var
    return $var
}

proc saveDate { text } {
    global theDate
    append theDate $text
    return
}

proc saveAbstract { text } {
    global theAbstract
    append theAbstract $text
    return
}

proc saveAuthorName { text } {
    global theAuthorName
    append theAuthorName $text
    return
}

proc generateHeadings {} {
    global theTitle theDate theAbstract theAuthorName argv

    if { "$argv" == "" && [info exists theTitle]} {
	set argv $theTitle
    }
    set ret "[htmlFileHeader ${argv}]
<H1 ALIGN=\"center\">${theTitle}</H1>
"
    if { [info exists theAuthorName] } {
	append ret "
<P ALIGN=\"center\"><STRONG>
${theAuthorName}</STRONG></P>
"
    }

    if { [info exists theDat] } {
	append ret "<P ALIGN=\"center\">${theDate}</P>"
    }

    if { [info exists theAbstract] } {
	append ret "<P><BLOCKQUOTE><EM><FONT SIZE=\"-1\">
${theAbstract}
</FONT></EM></BLOCKQUOTE></P>
"
    }
    return $ret
}

proc generateTOC {} {
    global contents
    output "
<A NAME=\"STOC\"><H3></H3></A>
"
    for { set sectN 1 } { [info exists contents($sectN)] } { incr sectN } {
	output "
<P>
${sectN} <A HREF=\"[sectFileName $sectN]\#S${sectN}\">
<STRONG>$contents($sectN)</STRONG></A><BR>\n"
	for { set ssectN 1 } { [info exists contents(${sectN}.${ssectN})] } { incr ssectN } {
	    output "&nbsp;&nbsp;&nbsp;&nbsp;${sectN}.${ssectN} <A HREF=\"[sectFileName $sectN]\#S${sectN}.${ssectN}\">$contents(${sectN}.${ssectN})</A><BR>\n"
	}
    }
}

