Giter Site home page Giter Site logo

lustache's People

Contributors

ajacksified avatar andsve avatar brimworks avatar catwell avatar denisphillips avatar fur-q avatar tst2005 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lustache's Issues

\r breaks things

require("lustache").render("\r")
/usr/local/share/lua/5.1/lustache.lua:333: attempt to call a nil value

invalid 'tags' escaping

Hi,
I tried use Lustache to pre-process Lua script in order to do minifying.
I thought I have to change the 'tags', with a intuitive choice like '{{=--[[< >]]=}}'.

But it cause error when parsing.
It seems it caused by invalid escaping the 'tags'.
When I change escape_tags function in renderer module to:

local function escapeRE(s)return (s:gsub("([^%w])", "%%%1"))end

local function escape_tags(tags)
    return {escapeRE(tags[1])..'%s*','%s*'..escapeRE(tags[2]) }
end

, the parsing not causing error anymore, and correctly parse the script now.

Thank you.

Recursion gives stack overflow

The following code gives me stack overflow. Any idea why? Thanks.

local lustache = require'lustache'

local tree = [[
{{#files}}
	{{path}}
	{{>tree}}
{{/files}}
]]

local root = {
	path = '',
	files = {
		path = 'files',
		files = false,
	}
}

print(lustache:render(tree, root, {tree = tree}))

JSON output support

JSON does not allow trailing commas after the last element of a list. Thus the following is legal:

{
  "a": "1", 
  "b": "2"
}

While this is not:

{
  "a": "1", 
  "b": "2",
}

Does lustache support this somehow? Would it be possible to write a function that adds a comma to it's output for all elements of a list but the last?

String value breaks inverted section

This simple sample:

local lustache = require "lustache"
local data = {test = "Hello world"}

local tpl = "{{#test}}Test is set to: {{test}}{{/test}}{{^test}}Test is empty{{/test}}"
print(lustache:render(tpl, data))

produces error:

.\lustache\renderer.lua:29: bad argument #1 to 'pairs' (table expected, got string)

This is caused by error in function renderer:_inverted, function is_array is called here without test whether the value is table. Test for table value could be added to is_array function to fix this issue:

local function is_array(array)
  if type(array) ~= "table" then return false end
  local max, n = 0, 0
  for k, _ in pairs(array) do
    if not (type(k) == "number" and k > 0 and math_floor(k) == k) then
      return false
    end
    max = math_max(max, k)
    n = n + 1
  end
  return n == max
end

Installation fails via luarocks dependency

When installing perimeterx-nginx-plugin the process fails while fetching lustache as it's dependency.

❯ luarocks install perimeterx-nginx-plugin
...
perimeterx-nginx-plugin 7.1.3-1 depends on lustache (not installed)
Installing https://luarocks.org/lustache-1.3.1-0.rockspec

Error: Failed installing dependency: https://luarocks.org/lustache-1.3.1-0.rockspec - Error fetching file: Failed downloading https://github.com/Olivine-Labs/lustache/archive/v1.3.1-0.tar.gz - v1.3.1-0.tar.gz
❯ curl -v https://github.com/Olivine-Labs/lustache/archive/v1.3.1-0.tar.gz
...
HTTP/2 302 
< server: GitHub.com
< date: Thu, 08 Sep 2022 09:12:12 GMT
< content-type: text/html; charset=utf-8
< vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, Accept-Encoding, Accept, X-Requested-With
< permissions-policy: interest-cohort=()
< location: https://codeload.github.com/Olivine-Labs/lustache/tar.gz/refs/tags/v1.3.1-0

A clue might be the 302 redirect. Perhaps the package url needs to be changed

package = "lustache"
version = "1.3.1-0"
source = {
  url = "https://github.com/Olivine-Labs/lustache/archive/v1.3.1-0.tar.gz",
  dir = "lustache-1.3.1-0"
}

to url = "https://codeload.github.com/Olivine-Labs/lustache/tar.gz/refs/tags/v1.3.1-0", ?

Allow for more delimiters

like so ( renderer.lua ):

`local escape_tags_repl = {
["%"] = "%%",
["^"] = "%^",
["$"] = "%$",
["("] = "%(",
[")"] = "%)",
["."] = "%.",
["["] = "%[",
["]"] = "%]",
[""] = "%",
["+"] = "%+",
["-"] = "%-",
["?"] = "%?"
}

local function escape_tags(tags)

--[[ instead of
local result = {
string_gsub(tags[1], "%%", "%%%%").."%s*",
"%s*"..string_gsub(tags[2], "%%", "%%%%"),
}
]]

local delim1 = string_gsub( tags[1], "[%%%^%$%(%)%.%[%]%%+%-%?]", escape_tags_repl ).."%s"
local delim2 = "%s*"..string_gsub( tags[2], "[%%%^%$%(%)%.%[%]%*%+%-%?]", escape_tags_repl )

return { delim1, delim2 }
end
`

bails in nginx-lua

lua handler aborted:
runtime error:
/usr/local/share/lua/5.1/lustache.lua:201: attempt to call upvalue 'fn' (a nil value)

Fail to render sections: attempt to call method 'lookup' (a nil value).

Tested on Windows 10 with lustache 1.3.1-0 and Lua 5.1.5.

While trying to render some of the examples from the readme I got the following error:

lua: .\lustache.lua:404: attempt to call method 'lookup' (a nil value)
stack traceback:
        .\lustache.lua:404: in function '_name'
        .\lustache.lua:202: in function <.\lustache.lua:188>
        (tail call): ?
        .\lustache.lua:347: in function '_section'
        .\lustache.lua:194: in function <.\lustache.lua:188>
        (tail call): ?
        (tail call): ?
        test-lustache-sections.lua:18: in main chunk
        [C]: ?

The test file was

local lustache = require "lustache"
local view_model, template

view_model = {
  stooges = {
    { name = "Moe" },
    { name = "Larry" },
    { name = "Curly" },
  }
}

template = [[
{{#stooges}}
* {{name}}
{{/stooges}}
]]

print(lustache:render(template, view_model))

The first bug I found was in the function Context:lookup(name), where the call to string_find interprets "." as a pattern (search any character). The line

if string_find(name, ".") > 0 then

should be changed to

if string_find(name, ".", 1, true) then

The second bug is when the function Context:new(view, parent) sets the wrong metatable when called from Renderer:_section(token, context, callback, originalTemplate). My workaround was to change the two callback(context:push(v), self) calls into callback(Context:push(v), self).

lustache-sections.zip

how to use without luarocks or brew

my isp is a little... ancient. no luarocks or brew

so how to use this code without luarocks/brew install?

i downloaded the souce. i wrote a file src/dem1.lua

lustache = require "lustache"

view_model = {
  title = "Joe",
  calc = function ()
    return 2 + 4;
  end
}

output = lustache.render("{{title}} spends {{calc}}", view)

then i ran it an got an error:

> lua demo1.lua
lua: ./lustache/renderer.lua:183: Call mustache:render, not mustache.render!
stack traceback:
        [C]: in function 'error'
        ./lustache/renderer.lua:183: in function 'render'
        demo1.lua:10: in main chunk
        [C]: in ?

can u help?

Do not wish to have HTML escape sequences in the output

I do not wish to have HTML escape sequences in my output. To achieve my goal, I commented out the following lines in the file "renderer.lua":

if escape then
  return string_gsub(str, '[&<>"\'/]', function(s) return html_escape_characters[s] end)
end

If you have a more elegant way to achieve my goal, let me know.

Problem with conditional presence in inverted section

The inverted section handling code implicitly assumes the value is a list, so it breaks when the value is e.g. a string:

model = {
person = {
{ name = "steve",  }, -- i.e. job = nil
{ name = "john", job="carpenter" },
},
}
{{#person}}
{{#job}} {{name}} is a {{job}} {{/job}}
{{^job}} {{name}} has no job {{/job}}
{{/person}}

/lustache/renderer.lua:29: bad argument #1 to 'pairs' (table expected, got string)
stack traceback:
[C]: in function 'pairs'
./modules/lustache/renderer.lua:29: in function 'is_array'
./modules/lustache/renderer.lua:242: in function '_inverted'
./modules/lustache/renderer.lua:63: in function 'callback'
./modules/lustache/renderer.lua:211: in function '_section'
./modules/lustache/renderer.lua:60: in function 'render'

The problem is that is_array() assumes the input is a table; this needs to be verified before calling it. Modifying line 242 as follows fixes the issue:

- if value == nil or value == false or (is_array(value) and #value == 0) then
+ if value == nil or value == false or (type(value) == "table" and is_array(value) and #value == 0) then

Line break

Hi there,

I'm a Lua newbie and I think I'm doing something wrong.
Check my code (from lustache's README):

lustache = require "lustache"

view_model = {
  stooges = {
    { name = "Moe" },
    { name = "Larry" },
    { name = "Curly" }
  }
}

template = [[
{{#stooges}}
<b>{{name}}</b>
{{/stooges}}]]

output = lustache:render(template, view_model)

print(output)

Output:


<b>Moe</b>

<b>Larry</b>

<b>Curly</b>

My first guess was related to mustache, so I tried it in http://trymustache.com and the output was the following.

Output:

<b>Moe</b>
<b>Larry</b>
<b>Curly</b>

After, I tried doing the same using Ruby, because I know it better. Check it out:

require 'mustache'

view_model = {
  :stooges => [
    { :name => "Moe" },
    { :name => "Larry" },
    { :name => "Curly" }
  ]
}

template = <<-EOS
{{#stooges}}
<b>{{name}}</b>
{{/stooges}}
EOS

output = Mustache.render(template, view_model)

puts output

Output:

<b>Moe</b>
<b>Larry</b>
<b>Curly</b>

Am I doing something wrong with lustache?

Test against the specs

Mustache has an extensive spec which doubles as a test-suite. Each test comes with sample data, a template and the expected output, all formatted in standard YAML (even the strange {key: value} syntax is standard YAML). The procedure for conformance testing is also described in the specs. Thus this should be a matter of loading the YAML files, iterating over all tests, setting up context and partials appropriately, pushing the template through the render function and comparing the result with the sample output.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.