Commits

Taylor Venable committed 078b776

Variables can now be defined by file contents

Comments (0)

Files changed (1)

template/template.tcl

         eval $script
     }
 
-    proc var {name dim val} {
+    proc var {name dim args} {
+        upvar #0 templateFile templateFile
+
+        set grouped no
+
         dict set sandbox::vardict $name dim $dim
+        for {set i 0} {$i < [llength $args]} {incr i} {
+            switch -- [lindex $args $i] {
+                {-grouped} { set grouped yes }
+                {-file} {
+                    set fileName [lindex $args [incr i]]
+
+                    # Non-absolute paths are relative to the template file location.
+
+                    if {[file pathtype $fileName] ne "absolute"} {
+                        set fileName [file join [file dirname $templateFile] $fileName]
+                    }
+
+                    set handle [open $fileName r]
+                    set val [list]
+                    set done false
+                    while {!$done} {
+                        for {set j 0} {$j < $dim} {incr j} {
+                            set line [gets $handle]
+                            if {[eof $handle]} {
+                                if {$j != 0} {
+                                    error "file $fileName ended in the middle of a grouping of $dim after reading $j elements"
+                                }
+                                set done true
+                                break
+                            }
+                            lappend val $line
+                        }
+                        if {$grouped} { gets $handle }
+                    }
+                    close $handle
+                }
+                {default} {
+                    set val [lindex $args $i]
+                }
+            }
+        }
         dict set sandbox::vardict $name val $val
     }
 
     proc template {tmplName script} {
         eval $script
         if {$sandbox::callback ne ""} {
-            {*}$callback [eval [dict get $sandbox::tmpldict $tmplName]]
+            eval [dict get $sandbox::tmpldict $tmplName]
+            apply $sandbox::callback $sandbox::results
         }
     }
 
     }
 }
 
+set templateFile [lindex $argv 0]
+
 if {[llength $argv] == 2} {
     set inputFile [lindex $argv 1]
     file copy -force $inputFile "$inputFile.bak"
     set inputHandle [open "$inputFile.bak" r]
     set outputHandle [open $inputFile w]
     try {
-        ::template::init [lindex $argv 0]
+        ::template::init $templateFile
         ::template::processInput $inputFile $inputHandle $outputHandle
     } on error msg {
         close $inputHandle
     close $inputHandle
     close $outputHandle
 } elseif {[llength $argv] == 1} {
-    ::template::init [lindex $argv 0] ::template::printExpansion
+    ::template::init $templateFile {{results} {::template::printExpansion $results stdout}}
 } else {
     error "invalid usage"
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.