-
-
Save zhanglei/5772402 to your computer and use it in GitHub Desktop.
Revisions
-
kindy renamed this gist
Jun 24, 2012 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
kindy revised this gist
Jun 24, 2012 . 2 changed files with 68 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() "; } -
kindy revised this gist
Jun 24, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,6 +18,6 @@ setmetatable(ctx, {__index = _G}) setfenv(fn, ctx) fn(ret) ngx.print(ret) "; } -
kindy created this gist
Jun 24, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)) "; }