global SaveLine
set SaveLine {}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Interpret a chunk of text as HTML and append its formatted value to the
# window. We normally get just one line at a time here. We strip off chunks
# one at a time, and feed them to various routines for processing. We
# distinguish mainly between three types of text chunks:
# 1. White space.
# 2. HTML tags.
# 3. Other text.
#
proc HTML {w x} {
global D W me Cols Rows LC NL SP in white SaveLine scripts Script Src Txt txtlvl
set Src "$SaveLine$x"
set SaveLine {}
if {$D>2} {puts "$me/HTML: ============================================================="}
if {$D>4} {puts "$me/HTML: Src=\"$Src\""}
if [info exists Txt($txtlvl)] {
set w [set W $Txt($txtlvl)]
if {$D>4} {puts "$me/HTML: w=W=\"$w\" taken from Txt($txtlvl)"}
}
if {!$in(HTML)} {
if {$D>3} {puts "$me/HTML: Not in HTML."}
if [regexp -nocase {^} $Src] {
if {$D>4} {puts "$me/HTML: Found tag"}
set in(HTML) 1
} elseif [regexp -nocase {^
} $Src] {
if {$D>4} {puts "$me/HTML: Found tag"}
set in(HTML) 1
} elseif [regexp -nocase {^} $Src] {
if {$D>4} {puts "$me/HTML: Found tag"}
set in(HTML) 1
} else {
if {$D>4} {puts "$me/HTML: Insert plain text."}
$w insert end $Src
return
}
}
if {$in(except)} {
if {$in(comment)} {
if [regsub {.*-->} $Src {} Src] {
incr in(comment) -1
incr in(except) -1
} else {
set Src {}
}
} elseif {$in(SCRIPT)} {
if [regexp -nocase {(.*)} $Src {} i Src] {
if {$i != {}} {append Script($scripts) $i}
incr in(SCRIPT) -1
incr in(except) -1
} else {
append Script($scripts) $Src
set Src {}
}
}
}
while {$Src != {}} {
if {$D>4} {puts "$me/HTML: ==== Src=\"$Src\""}
if [regexp {^([
]+)} $Src i] {
if {$D>4} {puts "$me/HTML: SPACES {$i} LC={$LC} NL=$NL SP=$SP"}
if {$in(PRE)} {
if {$D>4} {puts "$me/HTML: SPACES preformatted."}
Htxt $w $i
set LC [string range $i end end]
} elseif {$in(SCRIPT)} {
if {$D>4} {puts "$me/HTML: SCRIPT line \"$Src\""}
append Script($scripts) $Src
} elseif {$NL || $SP} {
if {$D>4} {puts "$me/HTML: SPACES ignored because NL=$NL SP=$SP."}
} elseif {$LC == { }} {
if {$D>4} {puts "$me/HTML: SPACES ignored because LC={$LC}."}
} else {
if {$D>4} {puts "$me/HTML: SPACES converted to single space."}
Htxt $w [set LC { }]
}
set Src [string trimleft $Src]
continue
}
if {$D>4} {puts "$me/HTML: Not white space."}
if [regexp {^([^<&]+)(.*)} $Src {} i Src] {
if {$D>4} {puts "$me/HTML: TEXT i=\"$i\" Src=\"$Src\""}
if {$in(PRE)} {
} elseif {$in(SCRIPT)} {
} else {
regsub -all {[
]+} $i { } i
}
if {$D>4} {puts "$me/HTML: TEXT i=\"$i\""}
Htxt $w $i
continue
}
if {$D>4} {puts "$me/HTML: Not text."}
if [regexp {^} $Src {} Src] {
if {$D>4} {puts "$me/HTML: COMMENT deleted."}
incr in(comment) -1
incr in(except) -1
} else {
if {$D>4} {puts "$me/HTML: COMMENT not complete on this line ..."}
set Src {}
}
continue
}
if {$D>4} {puts "$me/HTML: Not comment."}
if [regexp {^<[^>]*$} $Src SaveLine] {
if {$D>4} {puts "$me/HTML: CONT incomplete HTML tag \"$SaveLine\""}
set Src {}
continue
}
if {$D>4} {puts "$me/HTML: Not incomplete tag."}
if [regexp {^([0-9]+);(.*)$} $Src {} i Src] {
if {$D>4} {puts "$me/HTML: CHR i=\"$i\" Src=\"$Src\""}
Htxt $w [Chr $i]
continue
}
if {$D>4} {puts "$me/HTML: Not &XX; escape."}
if [regexp {^&([A-Za-z0-9]+);(.*)$} $Src {} i Src] {
if {$D>4} {puts "$me/HTML: CHR i=\"$i\" Src=\"$Src\""}
Htxt $w [Chr $i]
continue
}
if {$D>4} {puts "$me/HTML: Not &symb; escape."}
if [regexp {^<([^>]+)>(.*)$} $Src {} i Src] {
if {$D>4} {puts "$me/HTML: TAG i=\"$i\" Src=\"$Src\""}
Htag $w [string trim $i]
continue
}
if {$D>4} {puts "$me/HTML: Not complete tag."}
if [regexp {^([^&]*) (.*)$} $Src {} i s Src] {
if {$D>4} {puts "$me/HTML: CHR i=\"$i\" s=\"$s\" Src=\"$Src\""}
if {$i != {}} {Htxt $w $i}
$w insert end { }
set LC { }
set NL 0
continue
}
if {$D>4} {puts "$me/HTML: Not escape."}
if [regexp {^(&[^;]+;)(.*)$} $Src {} i Src] {
if {$D>4} {puts "$me/HTML: chr i=\"$i\" Src=\"$Src\""}
continue
}
if {$D>4} {puts "$me/HTML: Not &...; escape."}
if [regexp {^(.)(.*)$} $Src {} i Src] {
if {$D>4} {puts "$me/HTML: one i=\"$i\" Src=\"$Src\""}
continue
}
if {$D>4} {puts "$me/HTML: Not single character."}
if {$D>0} {puts "$me/HTML: NO PATTERN MATCHED!!!"}
if {$D>0} {puts "$me/HTML: Discard Src=\"$Src\""}
Src = {}
}
}