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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
local T = require 'pl.text'
local utils = require 'pl.utils'
local Template = T.Template
local asserteq = require 'pl.test'.asserteq
local OrderedMap = require 'pl.OrderedMap'
local t1 = Template [[
while true do
$contents
end
]]
assert(t1:substitute {contents = 'print "hello"'},[[
while true do
print "hello"
end
]])
assert(t1:indent_substitute {contents = [[
for i = 1,10 do
gotcha(i)
end
]]},[[
while true do
for i = 1,10 do
gotcha(i)
end
end
]])
asserteq(T.dedent [[
one
two
three
]],[[
one
two
three
]])
asserteq(T.fill ([[
It is often said of Lua that it does not include batteries. That is because the goal of Lua is to produce a lean expressive language that will be used on all sorts of machines, (some of which don't even have hierarchical filesystems). The Lua language is the equivalent of an operating system kernel; the creators of Lua do not see it as their responsibility to create a full software ecosystem around the language. That is the role of the community.
]],20),[[
It is often said of Lua
that it does not include
batteries. That is because
the goal of Lua is to
produce a lean expressive
language that will be
used on all sorts of machines,
(some of which don't
even have hierarchical
filesystems). The Lua
language is the equivalent
of an operating system
kernel; the creators of
Lua do not see it as their
responsibility to create
a full software ecosystem
around the language. That
is the role of the community.
]])
local template = require 'pl.template'
local t = [[
# for i = 1,3 do
print($(i+1))
# end
]]
asserteq(template.substitute(t),[[
print(2)
print(3)
print(4)
]])
t = [[
> for i = 1,3 do
print(${i+1})
> end
]]
asserteq(template.substitute(t,{_brackets='{}',_escape='>'}),[[
print(2)
print(3)
print(4)
]])
t = [[
#@ for i = 1,3 do
print(@{i+1})
#@ end
]]
asserteq(template.substitute(t,{_brackets='{}',_escape='#@',_inline_escape='@'}),[[
print(2)
print(3)
print(4)
]])
--- iteration using pairs is usually unordered. But using OrderedMap
--- we can get the exact original ordering.
t = [[
# for k,v in pairs(T) do
"$(k)", -- $(v)
# end
]]
if utils.lua51 then
-- easy enough to define a general pairs in Lua 5.1
local rawpairs = pairs
function pairs(t)
local mt = getmetatable(t)
local f = mt and mt.__pairs
if f then
return f(t)
else
return rawpairs(t)
end
end
end
local Tee = OrderedMap{{Dog = 'Bonzo'}, {Cat = 'Felix'}, {Lion = 'Leo'}}
-- note that the template will also look up global functions using _parent
asserteq(template.substitute(t,{T=Tee,_parent=_G}),[[
"Dog", -- Bonzo
"Cat", -- Felix
"Lion", -- Leo
]])
-- for those with a fondness for Python-style % formatting...
T.format_operator()
asserteq('[%s]' % 'home', '[home]')
asserteq('%s = %d' % {'fred',42},'fred = 42')
-- mostly works like string.format, except that %s forces use of tostring()
-- rather than throwing an error
local List = require 'pl.List'
asserteq('TBL:%s' % List{1,2,3},'TBL:{1,2,3}')
-- table with keys and format with $
asserteq('<$one>' % {one=1}, '<1>')
-- (second arg may also be a function, like os.getenv)
function subst(k)
if k == 'A' then return 'ay'
elseif k == 'B' then return 'bee'
else return '?'
end
end
asserteq(
'$A & $B' % subst,'ay & bee'
)
t = [[
a whole lot
of love
]]
asserteq(T.indent(t,4),[[
a whole lot
of love
]])
asserteq(T.indent([[
easy
enough!
]],2,'*'),[[
**easy
**
**enough!
]])
|