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

require TEItools.tcl
require Simple.tcl

TEItoolsSetup "tei"

specification translate {
    {element TRANSDOC} {
	prefix		{<!doctype tei.2 public '-//TEI//DTD TEI Lite 1.5 //EN' \[
<!entity oquote starttag \"q\">
<!entity cquote endtag   \"q\">
<!shortref openqmap '\"' oquote>
<!shortref closeqmap '\"' cquote>
<!usemap openqmap tei.2>
<!usemap closeqmap q>
	   ]>
<tei.2><teiheader></teiheader><text><body>
}
	suffix		"\n</body></text></tei.2>\n"
    }
    {element P} {
	prefix		"[beginPara]"
	suffix		"[endPara]"
    }
    {elements "TAB STRING FIELD FLDRSLT"} {
	prefix		{}
    }
    {element "TABLE"} {
	prefix		"\n<table rend='jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj'>"
	suffix		"</table>\n"
    }
    {element "CELL"} {
	prefix		"\n<cell>"
	suffix		"</cell>\n"
    }
    {element "ROW"} {
	prefix		"<row[rowRole]>\n"
	suffix		"</row>\n"
    } 
    {elements "HEXSTRING FLDINST PNTEXT"} {
	prefix		{}
	cdataFilter	null
    }
    {element "ESCAPE"} {
	prefix		"&[query attval ENTVAL];"
    }
    {el} {
	prefix		"[openUnknown]"
    }
}

proc rowRole {} {
    if { "[query attval TYPE]" == "THEAD" } {
	return " role='label'"
    }
    return
}


proc beginPara {} {
    set style [string toupper [query attval STYLENAME]]
    set closing {}
    switch -regexp $style {
	"HEADER" {
	    set opening "title"
	}
	"(HEADING 1)|( 1)" {
	    set opening "\n\n<div1 type='Section'><head>"
	    set closing "</head>\n"
	}
	"(HEADING 2)|( 2)" {
	    set opening "\n\n<div2 type='Subsection'><head>"
	    set closing "</head>\n"
	}
	"(HEADING 3)|( 3)" {
	    set opening "\n\n<div3 type='Subsubsection'><head>"
	    set closing "</head>\n"
	}
	"(HEADING 4)|( 4)" {
	    set opening "\n\n<div4 type='Paragraph'><head>"
	    set closing "</head>\n"
	}
	"NORMAL" {
	    set opening "p"
	}
	"(BULLET)|(NUMERIC)" {
	    if { "$style" == "NUMERIC" } {
		set listType "Ordered"
	    } else {
		set listType "Bullets"
	    }
	    set opening "<list type='${listType}'>\n<item>"
	    withNode prev withattval STYLENAME $style {
		set opening "<item>"
	    } 
	    set closing "</item>\n</list>\n"
	    withNode next withattval STYLENAME $style {
		set closing "</item>\n"
	    }
	}
	default {
	    Warning "unknown style $style"
	    set opening "p"
	}
    }
    if { "$closing" == "" } {
	setprop closing "</${opening}>\n"
	return "<${opening}>"
    } else {
	setprop closing $closing
	return $opening
    }
}

proc endPara {} {
    return [query propval closing]
}
