<html>
<body BGCOLOR="#ffffff" LINK="#0000ff" ALINK="#ff0000" TEXT="#000000">

<tcl>

# ----------------------------------------
# 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	{<blockquote>}	{</blockquote>} }
{NAME	{<center><h2>}	{</h2></center>} }
{NOTE	{<!--}	{-->} }
{INGREDIENT-LIST	{<p><h3>Ingredients</h3><ul>}	{</ul>} }
{INGREDIENT	{<li>} "" }
{PREPARATION	{<p><h3>Preparation</h3><ol>}	{</ol>} }
{STEP	{<li>}	"" }
}

# ----------------------------------------
# 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 "<font color=gray>$content</font>"
	    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 "<font color=red>$tag</font> $start_end"
    	if {$attr != ""} {
            puts -nonewline ": <font size=-1>$attr</font>" 
    	}
    	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

</tcl>
</body>
</html>
