mirror of
https://github.com/TangentFoxy/etlua.git
synced 2025-07-28 11:02:17 +00:00
extract template renderer into own class
This commit is contained in:
93
etlua.lua
93
etlua.lua
@@ -54,6 +54,58 @@ pos_to_line = function(str, pos)
|
|||||||
end
|
end
|
||||||
return line
|
return line
|
||||||
end
|
end
|
||||||
|
local Renderer
|
||||||
|
do
|
||||||
|
local _base_0 = {
|
||||||
|
render = function(self)
|
||||||
|
return table.concat(self.buffer)
|
||||||
|
end,
|
||||||
|
push = function(self, str, ...)
|
||||||
|
local i = self.i + 1
|
||||||
|
self.buffer[i] = str
|
||||||
|
self.i = i
|
||||||
|
if ... then
|
||||||
|
return self:push(...)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
header = function(self)
|
||||||
|
return self:push("local _b, _b_i, _tostring, _concat, _escape = ...\n")
|
||||||
|
end,
|
||||||
|
footer = function(self)
|
||||||
|
return self:push("return _b")
|
||||||
|
end,
|
||||||
|
increment = function(self)
|
||||||
|
return self:push("_b_i = _b_i + 1\n")
|
||||||
|
end,
|
||||||
|
mark = function(self, pos)
|
||||||
|
return self:push("--[[", tostring(pos), "]] ")
|
||||||
|
end,
|
||||||
|
assign = function(self, ...)
|
||||||
|
self:push("_b[_b_i] = ", ...)
|
||||||
|
if ... then
|
||||||
|
return self:push("\n")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
_base_0.__index = _base_0
|
||||||
|
local _class_0 = setmetatable({
|
||||||
|
__init = function(self)
|
||||||
|
self.buffer = { }
|
||||||
|
self.i = 0
|
||||||
|
end,
|
||||||
|
__base = _base_0,
|
||||||
|
__name = "Renderer"
|
||||||
|
}, {
|
||||||
|
__index = _base_0,
|
||||||
|
__call = function(cls, ...)
|
||||||
|
local _self_0 = setmetatable({}, _base_0)
|
||||||
|
cls.__init(_self_0, ...)
|
||||||
|
return _self_0
|
||||||
|
end
|
||||||
|
})
|
||||||
|
_base_0.__class = _class_0
|
||||||
|
Renderer = _class_0
|
||||||
|
end
|
||||||
local Parser
|
local Parser
|
||||||
do
|
do
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
@@ -272,15 +324,8 @@ do
|
|||||||
return self:chunks_to_lua()
|
return self:chunks_to_lua()
|
||||||
end,
|
end,
|
||||||
chunks_to_lua = function(self)
|
chunks_to_lua = function(self)
|
||||||
local buffer = {
|
local r = Renderer()
|
||||||
"local _b, _b_i, _tostring, _concat, _escape = ..."
|
r:header()
|
||||||
}
|
|
||||||
local buffer_i = #buffer
|
|
||||||
local push
|
|
||||||
push = function(str)
|
|
||||||
buffer_i = buffer_i + 1
|
|
||||||
buffer[buffer_i] = str
|
|
||||||
end
|
|
||||||
local _list_0 = self.chunks
|
local _list_0 = self.chunks
|
||||||
for _index_0 = 1, #_list_0 do
|
for _index_0 = 1, #_list_0 do
|
||||||
local chunk = _list_0[_index_0]
|
local chunk = _list_0[_index_0]
|
||||||
@@ -290,24 +335,26 @@ do
|
|||||||
end
|
end
|
||||||
local _exp_0 = t
|
local _exp_0 = t
|
||||||
if "string" == _exp_0 then
|
if "string" == _exp_0 then
|
||||||
push("_b_i = _b_i + 1")
|
r:increment()
|
||||||
push("_b[_b_i] = " .. tostring(("%q"):format(chunk)))
|
r:assign(("%q"):format(chunk))
|
||||||
elseif "code" == _exp_0 then
|
elseif "code" == _exp_0 then
|
||||||
push("--[[" .. tostring(chunk[3]) .. "]] " .. chunk[2])
|
r:mark(chunk[3])
|
||||||
|
r:push(chunk[2], "\n")
|
||||||
elseif "=" == _exp_0 or "-" == _exp_0 then
|
elseif "=" == _exp_0 or "-" == _exp_0 then
|
||||||
local assign = "_tostring(" .. tostring(chunk[2]) .. ")"
|
r:increment()
|
||||||
|
r:mark()
|
||||||
|
r:assign()
|
||||||
if t == "=" and self.html_escape then
|
if t == "=" and self.html_escape then
|
||||||
assign = "_escape(" .. assign .. ")"
|
r:push("_escape(_tostring(", chunk[2], "))\n")
|
||||||
|
else
|
||||||
|
r:push("_tostring(", chunk[2], ")\n")
|
||||||
end
|
end
|
||||||
assign = "_b[_b_i] = " .. assign
|
|
||||||
push("_b_i = _b_i + 1")
|
|
||||||
push("--[[" .. tostring(chunk[3]) .. "]] " .. assign)
|
|
||||||
else
|
else
|
||||||
error("unknown type " .. tostring(t))
|
error("unknown type " .. tostring(t))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
push("return _b")
|
r:footer()
|
||||||
return concat(buffer, "\n")
|
return r:render()
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
@@ -326,13 +373,14 @@ do
|
|||||||
_base_0.__class = _class_0
|
_base_0.__class = _class_0
|
||||||
Parser = _class_0
|
Parser = _class_0
|
||||||
end
|
end
|
||||||
local compile = (function()
|
local compile
|
||||||
|
do
|
||||||
local _base_0 = Parser()
|
local _base_0 = Parser()
|
||||||
local _fn_0 = _base_0.compile
|
local _fn_0 = _base_0.compile
|
||||||
return function(...)
|
compile = function(...)
|
||||||
return _fn_0(_base_0, ...)
|
return _fn_0(_base_0, ...)
|
||||||
end
|
end
|
||||||
end)()
|
end
|
||||||
local render
|
local render
|
||||||
render = function(str, ...)
|
render = function(str, ...)
|
||||||
local fn, err = compile(str)
|
local fn, err = compile(str)
|
||||||
@@ -346,5 +394,6 @@ return {
|
|||||||
compile = compile,
|
compile = compile,
|
||||||
render = render,
|
render = render,
|
||||||
Parser = Parser,
|
Parser = Parser,
|
||||||
|
Renderer = Renderer,
|
||||||
_version = VERSION
|
_version = VERSION
|
||||||
}
|
}
|
||||||
|
70
etlua.moon
70
etlua.moon
@@ -39,6 +39,37 @@ pos_to_line = (str, pos) ->
|
|||||||
line += 1
|
line += 1
|
||||||
line
|
line
|
||||||
|
|
||||||
|
class Renderer
|
||||||
|
new: =>
|
||||||
|
@buffer = {}
|
||||||
|
@i = 0
|
||||||
|
|
||||||
|
render: =>
|
||||||
|
table.concat @buffer
|
||||||
|
|
||||||
|
push: (str, ...) =>
|
||||||
|
i = @i + 1
|
||||||
|
@buffer[i] = str
|
||||||
|
@i = i
|
||||||
|
@push ... if ...
|
||||||
|
|
||||||
|
header: =>
|
||||||
|
@push "local _b, _b_i, _tostring, _concat, _escape = ...\n"
|
||||||
|
|
||||||
|
footer: =>
|
||||||
|
@push "return _b"
|
||||||
|
|
||||||
|
increment: =>
|
||||||
|
@push "_b_i = _b_i + 1\n"
|
||||||
|
|
||||||
|
mark: (pos) =>
|
||||||
|
@push "--[[", tostring(pos), "]] "
|
||||||
|
|
||||||
|
assign: (...) =>
|
||||||
|
@push "_b[_b_i] = ", ...
|
||||||
|
@push "\n" if ...
|
||||||
|
|
||||||
|
|
||||||
class Parser
|
class Parser
|
||||||
open_tag: "<%"
|
open_tag: "<%"
|
||||||
close_tag: "%>"
|
close_tag: "%>"
|
||||||
@@ -206,40 +237,35 @@ class Parser
|
|||||||
|
|
||||||
-- generates the code of the template
|
-- generates the code of the template
|
||||||
chunks_to_lua: =>
|
chunks_to_lua: =>
|
||||||
-- todo: find a no-conflict name for locals
|
r = Renderer!
|
||||||
buffer = {
|
r\header!
|
||||||
"local _b, _b_i, _tostring, _concat, _escape = ..."
|
|
||||||
}
|
|
||||||
buffer_i = #buffer
|
|
||||||
|
|
||||||
push = (str) ->
|
|
||||||
buffer_i += 1
|
|
||||||
buffer[buffer_i] = str
|
|
||||||
|
|
||||||
for chunk in *@chunks
|
for chunk in *@chunks
|
||||||
t = type chunk
|
t = type chunk
|
||||||
t = chunk[1] if t == "table"
|
t = chunk[1] if t == "table"
|
||||||
switch t
|
switch t
|
||||||
when "string"
|
when "string"
|
||||||
push "_b_i = _b_i + 1"
|
r\increment!
|
||||||
push "_b[_b_i] = #{("%q")\format(chunk)}"
|
r\assign ("%q")\format(chunk)
|
||||||
when "code"
|
when "code"
|
||||||
push "--[[#{chunk[3]}]] " .. chunk[2]
|
r\mark chunk[3]
|
||||||
|
r\push chunk[2], "\n"
|
||||||
when "=", "-"
|
when "=", "-"
|
||||||
assign = "_tostring(#{chunk[2]})"
|
r\increment!
|
||||||
|
r\mark!
|
||||||
|
r\assign!
|
||||||
|
|
||||||
if t == "=" and @html_escape
|
if t == "=" and @html_escape
|
||||||
assign = "_escape(" .. assign .. ")"
|
r\push "_escape(_tostring(", chunk[2], "))\n"
|
||||||
|
else
|
||||||
assign = "_b[_b_i] = " .. assign
|
r\push "_tostring(", chunk[2], ")\n"
|
||||||
|
|
||||||
push "_b_i = _b_i + 1"
|
|
||||||
push "--[[#{chunk[3]}]] " .. assign
|
|
||||||
else
|
else
|
||||||
error "unknown type #{t}"
|
error "unknown type #{t}"
|
||||||
|
|
||||||
push "return _b"
|
r\footer!
|
||||||
concat buffer, "\n"
|
r\render!
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
compile = Parser!\compile
|
compile = Parser!\compile
|
||||||
|
|
||||||
@@ -250,5 +276,5 @@ render = (str, ...) ->
|
|||||||
else
|
else
|
||||||
nil, err
|
nil, err
|
||||||
|
|
||||||
{ :compile, :render, :Parser, _version: VERSION }
|
{ :compile, :render, :Parser, :Renderer, _version: VERSION }
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user