# ----------------------------------------
# The tag map. Mappings from the XML tags to HTML.
# Each item consists of the tag name, the html to output at the beginning
# of processing the tag, and the html to output at the end of processing
# the tag
set tag_map {
{RECIPE "" "" }
{DESCRIPTION {} {
} }
{NAME {} {
} }
{NOTE {} }
{INGREDIENT-LIST {Ingredients
} }
{INGREDIENT {} "" }
{PREPARATION {Preparation
} {
} }
{STEP {} "" }
}
# ----------------------------------------
# Initialize the xml to html mapping arrays
proc init_tag_map {} {
global tag_map xml_html_start xml_html_end
foreach tag $tag_map {
set xml_html_start([lindex $tag 0]) [lindex $tag 1]
set xml_html_end([lindex $tag 0]) [lindex $tag 2]
}
}
# ----------------------------------------
# The main processing function: process XML from the parser
proc process { list } {
set type [lindex $list 0]
set tag [lindex $list 1]
set attr [lindex $list 2]
set content [lindex $list 3]
switch $type {
parse:elem {
process_tag start $tag $attr
# puts "$content"
process $content
process_tag end $tag $attr
}
parse:text {
puts "$tag"
}
}
if {[llength $list] > 4} {
set list [lreplace $list 0 3]
process $list
}
}
# ----------------------------------------
# Process an XML tag
proc process_tag {start_end tag attr} {
global xml_html_start xml_html_end
if {[info exists xml_html_start($tag)]} {
if {$start_end == "start"} {
puts "$xml_html_start($tag)"
} else {
puts "$xml_html_end($tag)"
}
} else {
puts -nonewline "$tag $start_end"
if {$attr != ""} {
puts -nonewline ": $attr"
}
puts ""
}
}
# == main: ===============================
set home "/home/tdarugar/bine/tcl-paper"
source "$home/xml-parse.tcl"
init_tag_map
set fd [open "$home/carrotcake.xml"]
set recipe [XML::parse [read $fd]]
close $fd
process $recipe