a.lua - lemplate

Functions defined

Source code

  1. --[[
  2.    This Lua code was generated by Lemplate, the Lua
  3.    Template Toolkit. Any changes made to this file will be lost the next
  4.    time the templates are compiled.

  5.    Copyright 2016 - Yichun Zhang (agentzh) - All rights reserved.

  6.    Copyright 2006-2014 - Ingy döt Net - All rights reserved.
  7. ]]

  8. local gsub = ngx.re.gsub
  9. local concat = table.concat
  10. local type = type
  11. local math_floor = math.floor
  12. local table_maxn = table.maxn

  13. local _M = {
  14.     version = '0.01'
  15. }

  16. local template_map = {}

  17. local function tt2_true(v)
  18.     return v and v ~= 0 and v ~= "" and v ~= '0'
  19. end

  20. local function tt2_not(v)
  21.     return not v or v == 0 or v == "" or v == '0'
  22. end

  23. local context_meta = {}

  24. function context_meta.plugin(context, name, args)
  25.     if name == "iterator" then
  26.         local list = args[1]
  27.         local count = table_maxn(list)
  28.         return { list = list, count = 1, max = count - 1, index = 0, size = count, first = true, last = false, prev = "" }
  29.     else
  30.         return error("unknown iterator: " .. name)
  31.     end
  32. end

  33. function context_meta.process(context, file)
  34.     local f = template_map[file]
  35.     if not f then
  36.         return error("file error - " .. file .. ": not found")
  37.     end
  38.     return f(context)
  39. end

  40. function context_meta.include(context, file)
  41.     local f = template_map[file]
  42.     if not f then
  43.         return error("file error - " .. file .. ": not found")
  44.     end
  45.     return f(context)
  46. end

  47. context_meta = { __index = context_meta }

  48. local function stash_get(stash, k)
  49.     local v
  50.     if type(k) == "table" then
  51.         v = stash
  52.         for i = 1, #k, 2 do
  53.             local key = k[i]
  54.             local typ = k[i + 1]
  55.             if type(typ) == "table" then
  56.                 local value = v[key]
  57.                 if type(value) == "function" then
  58.                     return value()
  59.                 end
  60.                 if value then
  61.                     return value
  62.                 end
  63.                 if key == "size" then
  64.                     if type(v) == "table" then
  65.                         return #v
  66.                     else
  67.                         return 1
  68.                     end
  69.                 else
  70.                     return error("virtual method " .. key .. " not supported")
  71.                 end
  72.             end
  73.             if type(key) == "number" and key == math_floor(key) and key >= 0 then
  74.                 key = key + 1
  75.             end
  76.             if type(v) ~= "table" then
  77.                 return nil
  78.             end
  79.             v = v[key]
  80.         end
  81.     else
  82.         v = stash[k]
  83.     end
  84.     if type(v) == "function" then
  85.         return v()
  86.     end
  87.     return v
  88. end

  89. local function stash_set(stash, k, v, default)
  90.     if default then
  91.         local old = stash[k]
  92.         if old == nil then
  93.             stash[k] = v
  94.         end
  95.     else
  96.         stash[k] = v
  97.     end
  98. end

  99. function _M.process(file, params)
  100.     local stash = params
  101.     local context = {
  102.         stash = stash,
  103.         filter = function (bits, name, params)
  104.             local s = concat(bits)
  105.             if name == "html" then
  106.                 s = gsub(s, "&", '&', "jo")
  107.                 s = gsub(s, "<", '&lt;', "jo");
  108.                 s = gsub(s, ">", '&gt;', "jo");
  109.                 s = gsub(s, '"', '&quot;', "jo"); -- " end quote for emacs
  110.                 return s
  111.             end
  112.         end
  113.     }
  114.     context = setmetatable(context, context_meta)
  115.     local f = template_map[file]
  116.     if not f then
  117.         return error("file error - " .. file .. ": not found")
  118.     end
  119.     return f(context)
  120. end
  121. -- tmpQQS_k.tt2
  122. template_map['tmpQQS_k.tt2'] = function (context)
  123.     if not context then
  124.         return error("Lemplate function called without context\n")
  125.     end
  126.     local stash = context.stash
  127.     local output = {}
  128.     local i = 0

  129. i = i + 1 output[i] = 'Hello, '
  130. -- line 1 "tmpQQS_k.tt2"
  131. i = i + 1 output[i] = stash_get(stash, 'world')
  132. i = i + 1 output[i] = '!\n'

  133.     return output
  134. end

  135. return _M