Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
fur-q committed Sep 17, 2012
1 parent 35ae20f commit 2118f18
Show file tree
Hide file tree
Showing 12 changed files with 602 additions and 579 deletions.
Empty file modified .gitignore 100644 → 100755
Empty file.
Empty file modified .travis.yml 100644 → 100755
Empty file.
Empty file modified LICENSE 100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions README.md 100644 → 100755
Expand Up @@ -31,7 +31,7 @@ using `luarocks install lustache`. On OSX, you can `brew install luarocks`.

Below is quick example how to use lustache:

lustache = require "lustache"
local lustache = require "lustache"

view_model = {
title = "Joe",
Expand All @@ -40,9 +40,9 @@ Below is quick example how to use lustache:
end
}

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

In this example, the `lustache.render` function takes two parameters: 1) the
In this example, the `lustache:render` function takes two parameters: 1) the
[mustache](http://mustache.github.com/) template and 2) a `view_model` object
that contains the data and code needed to render the template.

Expand Down Expand Up @@ -324,7 +324,7 @@ Can be thought of as a single, expanded template:
{{/names}}

In lustache an object of partials may be passed as the third argument to
`lustache.render`. The object should be keyed by the name of the partial, and
`lustache:render`. The object should be keyed by the name of the partial, and
its value should be the partial text.

### Set Delimiter
Expand Down
7 changes: 5 additions & 2 deletions lustache-1.1-1.rockspec 100644 → 100755
Expand Up @@ -21,7 +21,10 @@ dependencies = {
}
build = {
type = "builtin",
modules = {
lustache = "src/lustache.lua"
modules = {
lustache = "src/lustache.lua",
["lustache.context"] = "src/lustache/context.lua",
["lustache.renderer"] = "src/lustache/renderer.lua",
["lustache.scanner"] = "src/lustache/scanner.lua"
}
}
6 changes: 4 additions & 2 deletions spec/context_spec.lua 100644 → 100755
@@ -1,8 +1,10 @@
lustache = require 'lustache'
package.path = "../src/?.lua"

local lustache = require "lustache" -- only needed to set string.split

local topic = function()
local view = { name = 'parent', message = 'hi', a = { b = 'b' } }
local context = lustache.Context(view)
local context = require("lustache.context"):new(view)
return context, view
end

Expand Down
6 changes: 4 additions & 2 deletions spec/parse_spec.lua 100644 → 100755
@@ -1,4 +1,6 @@
lustache = require 'lustache'
package.path = "../src/?.lua"

local lustache = require "lustache"

expectations = {
{ template = "{{hi}}", value = { { type= "name", value= "hi" } }},
Expand Down Expand Up @@ -56,7 +58,7 @@ describe("parsing", function()

for i,v in ipairs(expectations) do
it("Tests template #"..x, function()
local parsed = lustache.parse(v.template)
local parsed = lustache:parse(v.template)
assert.same(parsed, v.value)
end)
x = x + 1
Expand Down
46 changes: 24 additions & 22 deletions spec/render_spec.lua 100644 → 100755
@@ -1,4 +1,6 @@
lustache = require 'lustache'
package.path = "../src/?.lua"

local lustache = require "lustache"

describe("rendering", function()
local template, data, partials, expectation
Expand All @@ -11,132 +13,132 @@ describe("rendering", function()
end

it("RenderNothingTest", function()
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderTextTest", function()
template = "Hi"
expectation = "Hi"
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("Render whitespace", function()
template = "\n"
expectation = "\n"
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderDataTest", function()
template = "{{ message }}"
expectation = "Hi"
data = { message = "Hi" }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderDataAndTextTest", function()
template = "{{ message }} Jack!"
expectation = "Hi Jack!"
data = { message = "Hi" }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderDataAndTextTest", function()
template = "{{ message }} Jack!"
expectation = "Hi Jack!"
data = { message = "Hi" }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderNestedDataTest", function()
template = "{{ message.words }} Jack!"
expectation = "Hi Jack!"
data = { message = { words = "Hi" } }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderNestedDataSectionsTest", function()
template = "{{# message }}{{ words }}{{/ message }} Jack!"
expectation = "Hi Jack!"
data = { message = { words = "Hi" } }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderMoreNestedDataWithBooleanSectionsTest", function()
template = "{{# message }}{{# words }}Yo{{/ words }}{{/ message }} Jack!"
expectation = "Yo Jack!"
data = { message = { words = "Yo" } }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderNegatedSectionsTest", function()
template = "{{# message }}{{^ words }}Yo{{/ words }}{{/ message }} Jack!"
expectation = "Yo Jack!"
data = { message = { 1 } }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderArrayTest", function()
template = "{{# message }}{{ . }}, {{/ message }}"
expectation = "1, 2, 3, "
data = { message = { 1, 2, 3 } }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderEscapedHTMLTest", function()
template = "{{message}}"
expectation = "<h1>HI<&#x2Fh1>"
data = { message = "<h1>HI</h1>" }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderUnescapedHTMLTest", function()
template = "{{{message}}}"
expectation = "<h1>HI</h1>"
data = { message = "<h1>HI</h1>" }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderPartialsTest", function()
template = "{{ title }}{{> message_template }}"
expectation = "Message: Hi, Jack"
data = { title = "Message: " }
partials = { message_template = "Hi, Jack" }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderPartialsInContextTest", function()
template = "{{ title }}{{> message_template }}"
expectation = "Message: Hi, Jack"
data = { title = "Message: ", message = "Hi, Jack" }
partials = { message_template = "{{ message }}" }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderFunctionsTest", function()
template = "{{ message }} Jack!"
expectation = "Yo Jack!"
data = { message = function() return "Yo" end }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderFunctionsWithArgsTest", function()
template = "{{# message }}H{{/ message }} Jack!"
expectation = "Hi Jack!"
data = { message = function(self, text, render) return render(text).."i" end }
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("RenderCommentsTest", function()
template = "{{! comment }}Hi"
expectation = "Hi"
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("ChangeDelimiterTest", function()
template = "{{=| |=}}|text|"
data = { text = "Hi" }
expectation = "Hi"
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("ArrayOfTablesTest", function()
Expand All @@ -150,7 +152,7 @@ describe("rendering", function()
}
}
expectation = "John Lennon Paul McCartney George Harrison Ringo Starr "
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)

it("ArrayOfTablesFunctionTest", function()
Expand All @@ -167,6 +169,6 @@ describe("rendering", function()
end
}
expectation = "* John Lennon\n* Paul McCartney\n* George Harrison\n* Ringo Starr\n"
assert.equal(expectation, lustache.render(template, data, partials))
assert.equal(expectation, lustache:render(template, data, partials))
end)
end)

0 comments on commit 2118f18

Please sign in to comment.