Skip to content

Instantly share code, notes, and snippets.

@zhanglei
Forked from kindy/a.ltp
Created June 13, 2013 09:24
Show Gist options
  • Save zhanglei/5772402 to your computer and use it in GitHub Desktop.
Save zhanglei/5772402 to your computer and use it in GitHub Desktop.

Revisions

  1. @kindy kindy renamed this gist Jun 24, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @kindy kindy revised this gist Jun 24, 2012. 2 changed files with 68 additions and 0 deletions.
    60 changes: 60 additions & 0 deletions ltp_util
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    -- 在 http://www.savarese.com/software/ltp/ 获取 ltp
    -- lposix 可以在 https://github.com/rrthomas/luaposix.git 获取
    -- 把 ltp 目录和当前文件放到 ngx_lua 可搜索的地方 在希望使用模板的nginx配置中添加以下定义即可
    local ltp = require 'ltp.template'
    local posix = nil
    do
    local ok, ret = pcall(require, 'posix')
    if ok then
    posix = ret
    end
    end
    local ltp_cache = {}

    module(..., package.seeall)

    -- in: filename
    -- in: mode - 1 force cache, 2 nocache, 3 mtime cache
    -- out: render function
    function loadltp(path, mode)
    mode = mode or 1
    if mode == 3 and not posix then
    mode = 2
    end
    if ltp_cache[path] then
    if mode == 1 then
    return ltp_cache[path][2]
    elseif mode == 2 then
    ltp_cache[path] = nil
    elseif mode == 3 then
    if assert(posix.stat(path)).mtime < ltp_cache[1] then
    return ltp_cache[path][2]
    else
    ltp_cache[path] = nil
    end
    end
    end

    local con = assert(io.open(ngx.var.request_filename, 'rb')):read('*a')
    local str = assert(ltp.compile_template_as_function(con, '<?', '?>'))
    local fn = assert(loadstring(str))()

    ltp_cache[path] = {ngx.time(), fn}

    return fn
    end

    function render(path)
    path = path or ngx.var.request_filename

    local fn = assert(loadltp(path))

    local ctx = {}
    setmetatable(ctx, {__index = _G})

    local ret = {}
    setfenv(fn, ctx)
    fn(ret)

    ngx.print(ret)
    end
    8 changes: 8 additions & 0 deletions lua_in_html_v2.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # 注意不要让其他 nginx 的 location 定义覆盖了这里的,那样你的源码就暴露了!!!!!!
    # nginx 很容易出这样的问题

    location ~ \.ltp$ {
    default_type text/html;

    content_by_lua " require 'ltp_util'.render() ";
    }
  3. @kindy kindy revised this gist Jun 24, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lua_in_html.conf
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,6 @@ setmetatable(ctx, {__index = _G})
    setfenv(fn, ctx)
    fn(ret)

    ngx.print(table.concat(ret))
    ngx.print(ret)
    ";
    }
  4. @kindy kindy created this gist Jun 24, 2012.
    9 changes: 9 additions & 0 deletions a.ltp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    "abc"
    <?= 124 ?>
    <?= a ?>
    <? local x = 12
    if math.random(1, 5) > 1 then ?>
    xxx
    <? else ?>
    yyy
    <? end ?>
    23 changes: 23 additions & 0 deletions lua_in_html.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    #在 http://www.savarese.com/software/ltp/ 下载 http://www.savarese.com/downloads/ltp/ltp-1.0.4.tar.bz2
    #把 ltp 目录放到 ngx_lua 可搜索的地方 在希望使用模板的nginx配置中添加以下定义即可
    #模板中可使用任意 lua 模块,如果想设置通用的对象,可在 ctx 变量中指定
    #模板例子见 a.ltp

    location ~ \.ltp$ {
    content_by_lua "
    local ctx = {
    a = 2;
    }

    local ltp = require 'ltp.template'
    local con = assert(io.open(ngx.var.request_filename, 'rb')):read('*a')
    local str = assert(ltp.compile_template_as_function(con, '<?', '?>'))
    local fn = assert(loadstring(str))()
    local ret = {}
    setmetatable(ctx, {__index = _G})
    setfenv(fn, ctx)
    fn(ret)

    ngx.print(table.concat(ret))
    ";
    }