From 39d571063a3019f6979a09a51f1318014c0b2cf3 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Thu, 14 Jun 2018 02:51:19 +0100 Subject: [PATCH] lib/lua/nb.lua --- lib/lua/nb.lua | 94 ++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/lib/lua/nb.lua b/lib/lua/nb.lua index 62fea668..7bf304a4 100644 --- a/lib/lua/nb.lua +++ b/lib/lua/nb.lua @@ -45,11 +45,7 @@ function nb:ngx_location_tmpl(fheader,ffooter) } --if vars.body_id == '' then vars.body_id='index' end - if not fheader then fheader = root .. "/tmpl/header.tmpl.html" end - if not ffooter then ffooter = root .. "/tmpl/footer.tmpl.html" end - local template = require "resty.template" - local context -- NB 25.05.18 template.print = function(s) -- NB 25.05.18 ngx.print(s) -- NB 25.05.18 end @@ -59,11 +55,6 @@ function nb:ngx_location_tmpl(fheader,ffooter) local content = self:readall(file) if ngx.ctx.tmpl_content then content = ngx.ctx.tmpl_content end if not content then return end - if ngx.var.request_uri:match('^/dev/') then - --content = template.compile(content,vars)(context) - --ngx.print(content) - --return - end local ishtml = true if ngx.ctx.tmpl_content then @@ -73,7 +64,50 @@ function nb:ngx_location_tmpl(fheader,ffooter) else end + -- + -- Get title + -- + --local context + --content = template.compile(content,vars)(context) + --local sav = content + content = template.compile(content)(vars) + --content = sav + + -- Extract title from h1 + if not vars.title then + vars.title = string.match(content:gsub('(]*>)<[^>]+>','%1'),']*>([^<>]+)',1) + end + + -- Extract title from h2 + if not vars.title then + vars.title = string.match(content:gsub('(]*>)<[^>]+>','%1'),']*>([^<>]+)',1) + end + + -- From request_uri + if not vars.title then + local basename = string.match(ngx.var.request_uri,'([^/]+)/?$') + if basename then + local noext = string.match(basename,'([^%.]*)') + vars.title = noext + end + end + + -- From filename +-- if not vars.title then +-- vars.title = string.match(file,'([^/%.]+)%.') -- basename +-- if vars.title == 'index' then +-- vars.title = string.match(file,'/([^/]+)/[^/]+$') +-- end +-- end + + -- First tld domain + -- if not vars.title then vars.title = string.match(ngx.var.http_host,'^([^%.]+)') end + -- Header and footer + if not fheader then fheader = root .. "/tmpl/header.tmpl.html" end + if not ffooter then ffooter = root .. "/tmpl/footer.tmpl.html" end + --local header + --local footer if ishtml and file ~= fheader and file ~= ffooter and not string.match(file,'/tmpl/') then local header = self:readall(fheader) local footer = self:readall(ffooter) @@ -107,44 +141,12 @@ function nb:ngx_location_tmpl(fheader,ffooter) end content = self:html_fix(content) - -- - -- Get title - -- - content = template.compile(content,vars)(context) - - -- Extract title from h1 - if not vars.title then - vars.title = string.match(content:gsub('(]*>)<[^>]+>','%1'),']*>([^<>]+)',1) - end - - -- Extract title from h2 - if not vars.title then - vars.title = string.match(content:gsub('(]*>)<[^>]+>','%1'),']*>([^<>]+)',1) - end - - -- From request_uri - if not vars.title then - local basename = string.match(ngx.var.request_uri,'([^/]+)/?$') - if basename then - local noext = string.match(basename,'([^%.]*)') - vars.title = noext - end - end - - -- From filename --- if not vars.title then --- vars.title = string.match(file,'([^/%.]+)%.') -- basename --- if vars.title == 'index' then --- vars.title = string.match(file,'/([^/]+)/[^/]+$') --- end --- end - - -- Default first tld domain - -- if not vars.title then vars.title = string.match(ngx.var.http_host,'^([^%.]+)') end - -- See: /usr/local/share/lua/5.1/resty/template.lua - -- Same as: template.print(template.compile(content,vars)(context)) - template.render(content,vars) + -- Same as: template.print(template.compile(content)(vars)) + --template.render(content,vars) + template.print(template.compile(content)(vars)) + --template.print(vars.title) + --template.print(content) ngx.exit(0) end -- 2.47.3