#Source http://wiki.tcl.tk/18175
#Minimal templaTCL implementation

proc parse {txt} {
	set code "set res {}\n"
	while {[set i [string first <% $txt]] != -1} {
		incr i -1
		append code "append res [list [string range $txt 0 $i]]\n"
		set txt [string range $txt [expr {$i + 3}] end]
		if {[string index $txt 0] eq "="} {
	    append code "append res "
		  set txt [string range $txt 1 end]
		}
		if {[set i [string first %> $txt]] == -1} {
		  error "No matching %>"
		}
		incr i -1
		append code "[string range $txt 0 $i] \n"
		set txt [string range $txt [expr {$i + 3}] end]
	}
	if {$txt ne ""} { append code "append res [list $txt]\n" }
	return $code
}
