]> git.nbdom.net Git - nb.git/commitdiff
lib/lua/nb.lua
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 14 Jun 2018 01:51:19 +0000 (02:51 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 14 Jun 2018 01:51:19 +0000 (02:51 +0100)
lib/lua/nb.lua

index 62fea668d949d657a3e547c658eb8360160d64ff..7bf304a458cba36893bb508246385fbc22fc9a5b 100644 (file)
@@ -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('(<h1[^>]*>)<[^>]+>','%1'),'<h1[^>]*>([^<>]+)',1)
+       end
+
+       -- Extract title from h2
+       if not vars.title then
+               vars.title = string.match(content:gsub('(<h2[^>]*>)<[^>]+>','%1'),'<h2[^>]*>([^<>]+)',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('(<h1[^>]*>)<[^>]+>','%1'),'<h1[^>]*>([^<>]+)',1)
-       end
-
-       -- Extract title from h2
-       if not vars.title then
-               vars.title = string.match(content:gsub('(<h2[^>]*>)<[^>]+>','%1'),'<h2[^>]*>([^<>]+)',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