1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
#############################################################
# HTMLLIB, 0.6
# A library of SGML-to-HTML conversion utilities for Cost
#
# NOTE: Work in progress, I don't recommend using this just yet...
#
# htmllib.tcl,v 1.8 1999/07/26 01:44:14 joe Exp
# 1999/07/26 01:44:14
#############################################################
package provide Cost-HTML 0.6
############################################################
#
# Options:
#
environment HTMLEnv {
doctype "-//W3C//DTD HTML 4.0 Transitional//EN"
outputDir ""
filePrefix "node"
fileExtension ".html"
sourceSpec NoSourceSpec
verbose 1
headScript {}
outfile stdout
filename "-"
}
# List of elements whose end-tags should be omitted:
# These are those with EMPTY declared content (BR, IMG, etc),
# and those with optional end-tags that are frequently
# accidentally terminated (P, LI, etc.)
#
global HTMLOmitEnd; # Array
foreach _omitEnd {
BR AREA LINK IMG PARAM HR INPUT COL META
FRAME ISINDEX BASE
P DT DD LI
} {
set HTMLOmitEnd($_omitEnd) 1
}
# html:configure { -option value ... }
#
# Sets options [%%% document me ]
#
proc html:configure {args} {
if {[llength $args] == 1} { set args [lindex $args 0]}
foreach {key val} $args {
regsub {^-*} $key {} key
HTMLEnv get $key ;# signals an error if $key does not exist
HTMLEnv set $key $val
}
}
############################################################
#
# Low-level utilities:
#
# Text substitution for HTML character data:
#
substitution html:escape {
& &
< <
> >
@ @
}
# Text substitution for HTML attribute values:
#
substitution html:escapeAttval {
& &
< <
> >
\" "
\' &squot;
@ @
}
# html:write text --
# Insert 'text' literally into the output stream
#
#
proc html:write {text} {
puts [HTMLEnv get outfile] $text nonewline
}
# html:text {cdata} --
# Insert character data into the output stream
# after escaping special characters
#
proc html:text {cdata} {
html:write [html:escape $cdata]
}
# html:startTag gi [ attspecs ... ]
#
# Emit a start-tag for element 'gi'.
#
# 'attspecs...' is a paired list of attribute-name/attribute-value pairs.
#
proc html:startTag {gi args} {
html:write "<$gi"
if {[llength $args] == 1} {
set args [lindex $args 0]
}
if {[llength $args] % 2} {
return -code error "Odd number of attribute-value pairs: $gi $args"
}
foreach {attname attval} $args {
if {$attname == $attval} {
# Handle HTML-style 'ATTNAME=ATTNAME' minimization:
html:write " $attname"
} else {
html:write " $attname=\"[html:escapeAttval $attval]\""
}
}
html:write "\n>"
}
# html:endTag gi
#
# Emit an end-tag for element 'gi',
# unless end-tag omission is specified for element type 'gi'.
#
proc html:endTag {gi} {
global HTMLOmitEnd
if {![info exists HTMLOmitEnd([string toupper $gi])]} {
html:write "</$gi>"
}
}
# html:element gi ?attname attval...? script
#
# Convenience function: Emit start tag, evaluate script,
# then emit end tag.
#
proc html:element {gi args} {
set script [lindex $args end]
set atts [lrange $args 0 [expr [llength $args] - 2]]
html:startTag $gi $atts
uplevel 1 $script
html:endTag $gi
}
############################################################
#
# File management routines:
#
# html:createFile filename --
# Creates new file 'filename', and diverts subsequent
# output to that file.
#
# html:closeFile --
# Closes current output file, returns to previous file.
#
proc html:createFile {filename} {
set filename [file join [HTMLEnv get outputDir] $filename]
set fp [open $filename "w"]
fconfigure $fp -buffering full
html:message "Writing $filename..."
HTMLEnv save outfile $fp filename $filename
}
proc html:closeFile {} {
if {[HTMLEnv get output] == "stdout"} {
error "cannot close stdout"
}
close [HTMLEnv get outfile]
HTMLEnv restore
}
#
# html:startDocument [options...]
#
# Start an HTML document: <!DOCTYPE ...> declaration and HEAD element.
#
proc html:startDocument {args} {
array set defaults [list \
head [HTMLEnv get headScript {}] \
title {} \
stylesheet [HTMLEnv get stylesheet ""] \
doctype [HTMLEnv get doctype] \
]
foreach {option value} $args {
regsub {^-*} $option {} opt; # trim leading "-"
if {![info exists defaults($opt)]} {
error "Bad option $opt: legal values are [array names defaults]"
}
set defaults($opt) $value
}
if {$defaults(head) == "" && $defaults(title) == ""} {
warning "html:startDocument: No title specified"
}
fconfigure [HTMLEnv get outfile] -buffering full
HTMLEnv save
html:write "<!DOCTYPE HTML PUBLIC \"$defaults(doctype)\">\n"
html:write "<!-- This file was automatically generated -->\n"
html:startTag HTML
html:startTag HEAD
global COST; html:startTag META NAME Creator CONTENT "Cost $COST(VERSION)"
if {[string length $defaults(title)]} {
html:element TITLE { html:text $defaults(title) }
}
uplevel 1 $defaults(head)
html:endTag HEAD
}
proc html:endDocument {} {
html:endTag HTML
html:write "\n"
HTMLEnv restore
}
############################################################
#
# High-level utilities:
#
# %%% Describe this...
# %%% which specs are used, which props are set.
# %%% allow configuration options (?)
# %%% Check: make sure that if HTMLNode is specified, html == #NODE*
# %%% Check: in processNode, vice versa.
#
proc html:preprocess {} {
set spec [HTMLEnv get sourceSpec]
set prefix [HTMLEnv get filePrefix]
set ext [HTMLEnv get fileExtension]
set counter 0
foreachNode doctree el {
set nodeName [subst [$spec get nodeName #IMPLIED]]
set anchorName [subst [$spec get anchorName #IMPLIED]]
switch -- $nodeName {
#IMPLIED { }
#AUTO { setprop HTMLNode \
"$prefix[format %03d [incr counter]]$ext" }
default { setprop HTMLNode "${nodeName}$ext" }
}
switch -- $anchorName {
#IMPLIED { }
#AUTO { setprop HTMLAnchor \
"[q gi][format %04d [elementNumber]]" }
default { setprop HTMLAnchor $anchorName }
}
}
}
############################################################
#
# Cross-reference management:
#
# html:hrefpos [query ... ]
# Returns a (relative) URL which will link to node specified
# by 'query...', or to the current node if 'query' is omitted.
#
# If the node isn't directly addressable in the HTML output,
# returns a link to the nearest ancestor which is.
#
# %%% Also need: convenient way to generate <A NAME="..."> elements
# %%% at appropriate places.
#
proc html:hrefpos {args} {
set anchor ""
set node ""
withNode ! {
if {![eval selectNode $args]} {
warning "query $args failed"
return ""
}
foreachNode ancestor {
if {[query? hasprop HTMLAnchor] && $anchor == ""} {
set anchor [query propval HTMLAnchor]
}
if {[query? hasprop HTMLNode]} {
set node [query propval HTMLNode]
break;
}
}
if {[string length $anchor]} {
return "$node#$anchor"
} elseif {[string length $node]} {
return $node
} ;# else
warning \
"Cannot find HTML locator for node [q gi].[elementNumber]"
}
return ""
}
#
# html:anchorName --
# %%% Describe
#
proc html:anchorName {} {
return [query ancestor propval HTMLAnchor]
}
############################################################
#
# SGML-to-HTML conversion.
#
# html:processNode --
# Process the current source node, depending on
# options specified in current sourceSpec.
#
# %%% TODO: allow configuration options on processNode, processChildren
#
proc html:processNode {} {
switch -- [query nodetype] {
EL { #see below }
CDATA { html:text [query content]; return }
SDATA { cost:undefined SDATA [query content]
html:text "{SDATA [query content]}";
return }
RE { html:write "\n" ; return; }
PEL -
SD { foreachNode child html:processNode ; return }
DATAENT -
ENTREF -
PI -
default { cost:undefined NODETYPE [query nodetype]; return }
}
# Processing for EL nodes:
#
set spec [HTMLEnv get sourceSpec]
uplevel #0 $spec do startAction
set result [subst [$spec get html #UNDEFINED]]
set gi [lindex $result 0]
set atts [lrange $result 1 end]
switch $gi {
#IGNORE { # no-op }
#NODE { error "NYI" }
#TEMPLATE { error "NYI" }
#UNDEFINED { cost:undefined GI [query gi];
html:processChildren }
#IMPLIED -
default {
set implied [string match "#IMPLIED*" $result]
if {!$implied} {
set atts [concat $atts [subst [$spec get attributes {}]]]
html:startTag $gi $atts
}
html:write [subst [$spec get prefix ""]]
uplevel #0 [list $spec do content {
foreachNode child html:processNode
}]
html:write [subst [$spec get suffix ""]]
if {!$implied} { html:endTag $gi }
}
}
uplevel #0 $spec do endAction
return;
}
proc html:processChildren {} {
foreachNode child html:processNode
}
############################################################
#
# Miscellaneous utilities:
#
proc html:message {msg} {
if {[HTMLEnv get verbose]} {
puts stderr $msg
}
}
############################################################
#
# Default main routine:
#
proc html:main {args} {
if {[HTMLEnv get sourceSpec] == "NoSourceSpec"} {
return -code error \
"No source specification. Use html:configure -sourceSpec ..."
}
html:preprocess
html:startDocument
withNode docroot html:processNode
html:endDocument
}
proc main {args} [info body html:main]
#*EOF*
|