<commit_before>#!/usr/bin/env lua
-- -*- lua -*-
-- copyright: 2012 Appwill Inc.
-- author : KDr2
--

module('response',package.seeall)

require('mch.mchutil')

ltp=require("ltp.template")

Response={ltp=ltp}

function Response:new()
    local ret={
        headers=ngx.header
    }
    setmetatable(ret,self)
    self.__index=self
    return ret
end

function Response:write(content)
    ngx.print(content)
end

function Response:writeln(content)
    ngx.say(content)
end

function Response:redirect(url, status)
    ngx.redirect(url, status)
end

function Response:set_cookie(key, value, decrypt, duration, path)
    if not key or key=="" or not value then
        return
    end

    if not duration or duration<=0 then
        duration=86400
    end

    if not path or path=="" then
        path = "/"
    end

    if value and value~="" and decrypt==true then
        value=ndk.set_var.set_encrypt_session(value)
        value=ndk.set_var.set_encode_base64(value)
    end

    local expiretime=ngx.time()+duration
    expiretime = ngx.cookie_time(expiretime)
    ngx.header["Set-Cookie"]={key.."="..value.."; expires="..expiretime.."; path="..path}
end

--[[
LTP Template Support
--]]

ltp_templates_cache={}

function ltp_function(template)
    ret=ltp_templates_cache[template]
    if ret then return ret end
    local tdata=mchutil.read_all(MOOCHINE_APP_PATH .. "/templates/" .. template)
    local rfun = ltp.load_template(tdata, '<?lua','?>')
    ltp_templates_cache[template]=rfun
    return rfun
end

function Response:ltp(template,data)
    local rfun=ltp_function(template)
    local output = {}
    local mt={__index=_G}
    setmetatable(data,mt)
    ltp.execute_template(rfun, data, output)
    ngx.say(output)
end

<commit_msg>used table.concat to build string<commit_after>#!/usr/bin/env lua
-- -*- lua -*-
-- copyright: 2012 Appwill Inc.
-- author : KDr2
--

module('response',package.seeall)

require('mch.mchutil')

ltp=require("ltp.template")

Response={ltp=ltp}

function Response:new()
    local ret={
        headers=ngx.header
    }
    setmetatable(ret,self)
    self.__index=self
    return ret
end

function Response:write(content)
    ngx.print(content)
end

function Response:writeln(content)
    ngx.say(content)
end

function Response:redirect(url, status)
    ngx.redirect(url, status)
end

function Response:set_cookie(key, value, decrypt, duration, path)
    if not key or key=="" or not value then
        return
    end

    if not duration or duration<=0 then
        duration=86400
    end

    if not path or path=="" then
        path = "/"
    end

    if value and value~="" and decrypt==true then
        value=ndk.set_var.set_encrypt_session(value)
        value=ndk.set_var.set_encode_base64(value)
    end

    local expiretime=ngx.time()+duration
    expiretime = ngx.cookie_time(expiretime)
    ngx.header["Set-Cookie"]={
        table.concat({key, "=", value, "; expires=", expiretime, "; path=", path})
    }
end

--[[
LTP Template Support
--]]

ltp_templates_cache={}

function ltp_function(template)
    ret=ltp_templates_cache[template]
    if ret then return ret end
    local tdata=mchutil.read_all(MOOCHINE_APP_PATH .. "/templates/" .. template)
    local rfun = ltp.load_template(tdata, '<?lua','?>')
    ltp_templates_cache[template]=rfun
    return rfun
end

function Response:ltp(template,data)
    local rfun=ltp_function(template)
    local output = {}
    local mt={__index=_G}
    setmetatable(data,mt)
    ltp.execute_template(rfun, data, output)
    ngx.say(output)
end


{'fraction_non_alphanumeric': 0.1288135593220339, 'fraction_numerical': 0.007344632768361582, 'mean_word_length': 3.4097135740971356, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6308730', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1208, 'n_words': 262}