Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix partials to indent as demanded by the mustache spec #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/lustache/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ local Context = require "lustache.context"

local error, ipairs, loadstring, pairs, setmetatable, tostring, type =
error, ipairs, loadstring, pairs, setmetatable, tostring, type
local math_floor, math_max, string_find, string_gsub, string_split, string_sub, table_concat, table_insert, table_remove =
math.floor, math.max, string.find, string.gsub, string.split, string.sub, table.concat, table.insert, table.remove
local math_floor, math_max, string_find, string_gsub, string_split, string_sub, string_rep, table_concat, table_insert, table_remove =
math.floor, math.max, string.find, string.gsub, string.split, string.sub, string.rep, table.concat, table.insert, table.remove

local patterns = {
white = "%s*",
Expand Down Expand Up @@ -64,7 +64,7 @@ local function compile_tokens(tokens, originalTemplate)
t == "^" and rnd:_inverted(
token.value, ctx, subrender(i, token.tokens)
) or
t == ">" and rnd:_partial(token.value, ctx, originalTemplate) or
t == ">" and rnd:_partial(token.value, ctx, originalTemplate, token.startIndex) or
(t == "{" or t == "&") and rnd:_name(token.value, ctx, false) or
t == "name" and rnd:_name(token.value, ctx, true) or
t == "text" and token.value or ""
Expand Down Expand Up @@ -249,7 +249,7 @@ function renderer:_inverted(name, context, callback)
return ""
end

function renderer:_partial(name, context, originalTemplate)
function renderer:_partial(name, context, originalTemplate, startIndex)
local fn = self.partial_cache[name]

-- check if partial cache exists
Expand All @@ -264,7 +264,23 @@ function renderer:_partial(name, context, originalTemplate)
fn = self:compile(partial, nil, originalTemplate)
self.partial_cache[name] = fn
end
return fn and fn(context, self) or ""

local startOfLineIndex = 1
for i = startIndex, 1, -1 do
if string_sub(originalTemplate, i, i) == "\n" then
startOfLineIndex = i + 1
break
end
end
local indent = string_rep(" ", startIndex - startOfLineIndex)

local str = fn and fn(context, self) or ""
local buf = {}
for i,line in ipairs(string_split(str, "\n")) do
buf[#buf+1] = indent .. line .. "\n"
end

return table_concat(buf)
end

function renderer:_name(name, context, escape)
Expand Down