-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtemplate.tcl
35 lines (34 loc) · 1.03 KB
/
template.tcl
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
# Templating engine.
# Copyright (c) 2014-2016 D. Bohdan.
# License: MIT.
namespace eval ::template {
variable version 1.0.0
}
# Convert a template into Tcl code.
proc ::template::parse {template} {
set result {}
set regExpr {^(.*?)<%(.*?)%>(.*)$}
set listing "set _output {}\n"
while {[regexp $regExpr $template \
match preceding token template]} {
append listing [list append _output $preceding]\n
switch -exact -- [string index $token 0] {
= {
append listing \
[format {append _output [expr %s]} \
[list [string range $token 1 end]]]
}
! {
append listing \
[format {append _output [%s]} \
[string range $token 1 end]]
}
default {
append listing $token
}
}
append listing \n
}
append listing [list append _output $template]\n
return $listing
}