<commit_before>-- Copyright (C) 2015 <NAME>

local _M = { _VERSION = '0.1'  }
local mt = {__index = _M}

function _M.new(callback, minify, tags)
    tags = ("table" == type(tags) and 4 == #tags) and tags or {"{{", "}}", "{%%", "%%}"}
    return setmetatable({
        callback = callback or (nil == ngx and io.write or ngx.print),
        minify = minify or false,
        replace_templae = {
            ["\\"] = "\\\\",
            --["\n"] = "\\n",
            --["\r"] = "\\r",
            ["'"] = "\'",
            ['"'] = "\"",
            [tags[1]] = "]=]_(",
            [tags[2]] = ")_[=[",
            [tags[3]] = "]=]  ",
            [tags[4]] = " _[=["
        },
        cache = {}
    }, mt)
end

function _M.parse(self, tmpl, minify)
    for k, v in pairs(self.replace_templae) do
        tmpl = tmpl:gsub(k, v)
    end
    local str = "return function(_) _[=[" .. tmpl .. "]=] end"
    return minify and str:gsub("%s+", " ") or str
end

function _M.compile(self, tmpl, minify)
    self.cache[tmpl] = self.cache[tmpl] or loadstring(_M.parse(self, tmpl, minify))()
    return self.cache[tmpl]
end

-- can render compiled function or template string
function _M.render(self, tmpl, data, callback, minify)
    local compile = type(tmpl) == "function" and tmpl or _M.compile(self, tmpl, minify or self.minify)
    setmetatable(data, {__index = _G})
    setfenv(compile, data)
    return compile(callback or self.callback)
end

return _M
<commit_msg>using ngx.md5 to create cache key for template<commit_after>-- Copyright (C) 2015 <NAME>

local _M = { _VERSION = '0.1'  }
local mt = {__index = _M}

function _M.new(callback, minify, tags)
    tags = ("table" == type(tags) and 4 == #tags) and tags or {"{{", "}}", "{%%", "%%}"}
    return setmetatable({
        callback = callback or (nil == ngx and io.write or ngx.print),
        minify = minify or false,
        replace_templae = {
            ["\\"] = "\\\\",
            --["\n"] = "\\n",
            --["\r"] = "\\r",
            ["'"] = "\'",
            ['"'] = "\"",
            [tags[1]] = "]=]_(",
            [tags[2]] = ")_[=[",
            [tags[3]] = "]=]  ",
            [tags[4]] = " _[=["
        },
        cache = {}
    }, mt)
end

function _M.parse(self, tmpl, minify)
    for k, v in pairs(self.replace_templae) do
        tmpl = tmpl:gsub(k, v)
    end
    local str = "return function(_) _[=[" .. tmpl .. "]=] end"
    return minify and str:gsub("%s+", " ") or str
end

function _M.compile(self, tmpl, minify)
    local key = nil == ngx and tmpl or ngx.md5(tmpl)
    self.cache[tmpl] = self.cache[tmpl] or loadstring(_M.parse(self, tmpl, minify))()
    return self.cache[tmpl]
end

-- can render compiled function or template string
function _M.render(self, tmpl, data, callback, minify)
    local compile = type(tmpl) == "function" and tmpl or _M.compile(self, tmpl, minify or self.minify)
    setmetatable(data, {__index = _G})
    setfenv(compile, data)
    return compile(callback or self.callback)
end

return _M

{'fraction_non_alphanumeric': 0.19394544244843645, 'fraction_numerical': 0.007984031936127744, 'mean_word_length': 2.304395604395604, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2094073', 'n_tokens_mistral': 1094, 'n_tokens_neox': 1079, 'n_words': 275}