# -*- tcl -*-
# $Id: script,v 1.8 1998/11/11 15:06:04 tobotras Exp $

require TEItools.tcl
require Simple.tcl
TEItoolsSetup "tei.2"
require indent.tcl

if { ![info exists TEItools_indent_value] } {
    set TEItools_indent_value 2
}

specification translate {
    {element TITLE in TITLESTMT} {
	prefix		[blockOpen]
	suffix		[blockClose]
    }
    {element Q} {
	startAction {
	    global doCdataOffset
	    set doCdataOffset 0
	}
	prefix		[qOpen]
	suffix		[qClose]
    }
    {element NOTE} {
	prefix		[noteOpen]
	suffix		[noteClose]
    }
    {element HEAD} {
	startAction {
	    global doCdataOffset
	    set doCdataOffset 0
	}
	prefix		[headOpen]
	suffix		[headClose]
    }
    {elements "P ITEM LABEL TITLEPART DIV DIV0 DIV1 DIV2 DIV3 DIV4 DIV5 DIV6 DIV7"} {
	startAction {
	    global doCdataOffset
	    set doCdataOffset 0
	}
	prefix		[itemOpen]
	suffix		[blockClose]
    }
    {elements "TEI.2 TEIHEADER FILEDESC TITLESTMT PUBLICATIONSTMT SOURCEDESC TEXT FRONT TITLEPAGE DOCAUTHOR DOCTITLE DIVGEN BODY BACK FIGURE BIBL LIST ROW CELL TABLE LISTBIBL AUTHOR PUBLISHER DATE PROFILEDESC LANGUSAGE REVISIONDESC CHANGE RESPSTMT RESP LANGUAGE DOCIMPRINT DOCDATE"} {
	#Block elements
	prefix		[blockOpen]
	suffix		[blockClose]
    }
    {elements "EG FORMULA"} {
	#Block with linespecific
	startAction {
	    global doCdataOffset
	    set doCdataOffset 0
	}
	prefix		"[blockOpen]<!\[CDATA\["
	suffix		"\]\]> [blockClose]"
    }
    {elements "EMPH ABBR HI PTR BIBLSCOPE LB PB TERM CODE XPTR XREF PTR REF MENTIONED IDENT NAME SOCALLED GI KW GAP TITLE SEG ANCHOR"} {
	#Inline elements
	startAction {
	    global doCdataOffset
	    set doCdataOffset 0
	}
	prefix		[inlineOpen]
	suffix		[inlineClose]
    }
    {el} {
	startAction {
	    global doCdataOffset
	    set doCdataOffset 1
	}
	prefix		[openUnknown]
	cdataFilter	indentLine
	sdataFilter	sdataFilter
    }
}

proc qOpen {} {
    set rend [attVal REND]
    if { "$rend" == "" || "$rend" == "inline" || "$rend" == "\#IMPLIED" } {
	return [inlineOpen]
    } else {
	return [blockOpen]
    }
}
	
proc qClose {} {
    switch -regexp [attVal PLACE] {
	"" {
	    return [inlineClose]
	}
	"(inline)|(\#IMPLIED)" {
	    return [inlineClose]
	}
	default {
	    return [blockClose]
	}
    }
}

proc noteOpen {} {
    switch -regexp [attVal PLACE] {
	"" {
	    return [inlineOpen]
	}
	"(inline)|(\#IMPLIED)" {
	    return [inlineOpen]
	}
	default {
	    return [blockOpen]
	}
    }
}
	
proc noteClose {} {
    switch -regexp [attVal PLACE] {
	"" {
	    return [inlineClose]
	}
	"(inline)|(\#IMPLIED)" {
	    return [inlineClose]
	}
	default {
	    return [blockClose]
	}
    }
}

proc headOpen {} {
    withNode parent {
	set parentGi [query gi]
    }
    switch -regexp $parentGi {
	"(DIV*)|(FIGURE)|(TABLE)|(LIST)" {
	    setprop closing inlineClose
	    return [inlineOpen]
	}
	default {
	    setprop closing blockClose
	    return [blockOpen]
	}
    }
}

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

proc inlineOpen {} {
    return "<[query gi][elementAttributes]>"
}

proc inlineClose {} {
    return "</[query gi]>"
}

proc blockOpen {} {
    return "[blockIndent open]<[query gi][elementAttributes]>\n"
}

proc blockClose {} {
    return "\n[blockIndent close]</[query gi]>\n"
}

proc itemOpen {} {
    return "[blockIndent open]<[query gi][elementAttributes]> "
}

proc itemClose {} {
    return "</[query gi]>\n"
}

set TEItools_current_indent 0
proc blockIndent { openclose } {
    global TEItools_current_indent TEItools_indent_value
    switch $openclose {
	"open" {
	    set offset $TEItools_current_indent
	    incr TEItools_current_indent $TEItools_indent_value
	}
	"close" {
	    incr TEItools_current_indent -$TEItools_indent_value
	    set offset $TEItools_current_indent
	}
	default {
	    set offset $TEItools_current_indent
	}
    }
    return [repeat " " $offset]
}

proc indentLine { text } {
    if { "$text" != "\n" } {
	global doCdataOffset
	if { $doCdataOffset } {
	    set ret [blockIndent none]
	    withNode prev {
		if { "[query nodetype]" == "PEL" } {
		    set ret ""
		}
	    }
	} else {
	    set ret ""
	}
	return "${ret}${text}"
    }
    return "\n[blockIndent none]"
}

proc elementAttributes {} {
    set ret ""
    set gi [query gi]
    foreachNode attlist {
	set attName [query attname]
	set value [content]
	
	if { "$value" != "" && ![globalBlacklisted $attName] \
		 && ![localBlacklisted $gi $attName] } {
	    append ret " [query attname]='$value'"
	}
    }
    return $ret
}

proc globalBlacklisted { attribute } {
    if { "$attribute" == "TEIFORM" } {
	return 1
    }
    return 0
}

array set blacklist {
    BIBL	"DEFAULT"
    DIV		"ORG SAMPLE PART"
    DIV0	"ORG SAMPLE PART"
    DIV1	"ORG SAMPLE PART"
    DIV2	"ORG SAMPLE PART"
    DIV3	"ORG SAMPLE PART"
    DIV4	"ORG SAMPLE PART"
    DIV5	"ORG SAMPLE PART"
    DIV6	"ORG SAMPLE PART"
    DIV7	"ORG SAMPLE PART"
    GI		"TEI"
    LANGUSAGE	"DEFAULT"
    PTR		"TAGORDER"
    Q		"DIRECT TYPE"
    SOURCEDESC	"DEFAULT"
    TEIHEADER	"STATUS TYPE"
    XPTR	"TO TARGORDER"
    XREF	"TO TARGORDER"
}

proc localBlacklisted { gi attribute } {
    global blacklist
    foreach element [array names blacklist] {
	if { "$gi" == "$element" } {
	    if { ![info exists blacklist($element)] } {
		return 0
	    }
	    foreach attr $blacklist($element) {
		if { "$attr" == "$attribute" } {
		    return 1
		}
	    }
	}
    }
    return 0
}

proc sdataFilter { text } {
    return "&[string trim $text { []}];"
}

output "<!doctype tei.2 public '-//TEI//DTD TEI Lite 1.6//EN'>\n\n"
