diff --git a/etlua.lua b/etlua.lua index 425f616..c0d10fb 100644 --- a/etlua.lua +++ b/etlua.lua @@ -58,6 +58,7 @@ local Compiler do local _class_0 local _base_0 = { + html_escape = true, render = function(self) return table.concat(self.buffer) end, @@ -86,6 +87,37 @@ do if ... then return self:push("\n") end + end, + compile_chunks = function(self, chunks) + self:header() + for _index_0 = 1, #chunks do + local chunk = chunks[_index_0] + local t = type(chunk) + if t == "table" then + t = chunk[1] + end + local _exp_0 = t + if "string" == _exp_0 then + self:increment() + self:assign(("%q"):format(chunk)) + elseif "code" == _exp_0 then + self:mark(chunk[3]) + self:push(chunk[2], "\n") + elseif "=" == _exp_0 or "-" == _exp_0 then + self:increment() + self:mark(chunk[3]) + self:assign() + if t == "=" and self.html_escape then + self:push("_escape(_tostring(", chunk[2], "))\n") + else + self:push("_tostring(", chunk[2], ")\n") + end + else + error("unknown type " .. tostring(t)) + end + end + self:footer() + return self:render() end } _base_0.__index = _base_0 @@ -114,7 +146,6 @@ do open_tag = "<%", close_tag = "%>", modifiers = "^[=-]", - html_escape = true, next_tag = function(self) local start, stop = self.str:find(self.open_tag, self.pos, true) if not (start) then @@ -333,37 +364,7 @@ do if compiler_cls == nil then compiler_cls = Compiler end - local r = compiler_cls() - r:header() - local _list_0 = self.chunks - for _index_0 = 1, #_list_0 do - local chunk = _list_0[_index_0] - local t = type(chunk) - if t == "table" then - t = chunk[1] - end - local _exp_0 = t - if "string" == _exp_0 then - r:increment() - r:assign(("%q"):format(chunk)) - elseif "code" == _exp_0 then - r:mark(chunk[3]) - r:push(chunk[2], "\n") - elseif "=" == _exp_0 or "-" == _exp_0 then - r:increment() - r:mark(chunk[3]) - r:assign() - if t == "=" and self.html_escape then - r:push("_escape(_tostring(", chunk[2], "))\n") - else - r:push("_tostring(", chunk[2], ")\n") - end - else - error("unknown type " .. tostring(t)) - end - end - r:footer() - return r:render() + return compiler_cls():compile_chunks(self.chunks) end } _base_0.__index = _base_0 diff --git a/etlua.moon b/etlua.moon index a0966c4..4012da6 100644 --- a/etlua.moon +++ b/etlua.moon @@ -40,6 +40,8 @@ pos_to_line = (str, pos) -> line class Compiler + html_escape: true + new: => @buffer = {} @i = 0 @@ -69,11 +71,38 @@ class Compiler @push "_b[_b_i] = ", ... @push "\n" if ... + compile_chunks: (chunks) => + @header! + + for chunk in *chunks + t = type chunk + t = chunk[1] if t == "table" + switch t + when "string" + @increment! + @assign ("%q")\format(chunk) + when "code" + @mark chunk[3] + @push chunk[2], "\n" + when "=", "-" + @increment! + @mark chunk[3] + @assign! + + if t == "=" and @html_escape + @push "_escape(_tostring(", chunk[2], "))\n" + else + @push "_tostring(", chunk[2], ")\n" + else + error "unknown type #{t}" + + @footer! + @render! + class Parser open_tag: "<%" close_tag: "%>" modifiers: "^[=-]" - html_escape: true next_tag: => start, stop = @str\find @open_tag, @pos, true @@ -242,35 +271,7 @@ class Parser -- generates the code of the template chunks_to_lua: (compiler_cls=Compiler) => - r = compiler_cls! - r\header! - - for chunk in *@chunks - t = type chunk - t = chunk[1] if t == "table" - switch t - when "string" - r\increment! - r\assign ("%q")\format(chunk) - when "code" - r\mark chunk[3] - r\push chunk[2], "\n" - when "=", "-" - r\increment! - r\mark chunk[3] - r\assign! - - if t == "=" and @html_escape - r\push "_escape(_tostring(", chunk[2], "))\n" - else - r\push "_tostring(", chunk[2], ")\n" - else - error "unknown type #{t}" - - r\footer! - r\render! - - + compiler_cls!\compile_chunks @chunks compile = Parser!\compile