Fix issues from Inheritance spec (#36)

* Separate inheritance block and expansion

* Catch top level partial definition, and block newlines

* Add testTrailingNewLines to verify output of trailing newlines in partials

* Remove comment

* If block,partial has indentation add indent for first line

* Re-enable full sections spec

* withBlockExpansion

* Get indentation of blocks correct
This commit is contained in:
Adam Fowler
2024-07-15 09:36:15 +01:00
committed by GitHub
parent cc0eaffa06
commit 7689de0a42
6 changed files with 215 additions and 45 deletions

View File

@@ -28,7 +28,8 @@ extension MustacheTemplate {
if let indentation = context.indentation, indentation != "" {
for token in tokens {
let renderedString = self.renderToken(token, context: &context)
if renderedString != "", string.last == "\n" {
// if rendered string is not empty and we are on a new line
if renderedString.count > 0, string.last == "\n" {
string += indentation
}
string += renderedString
@@ -75,11 +76,11 @@ extension MustacheTemplate {
let child = self.getChild(named: variable, transforms: transforms, context: context)
return self.renderInvertedSection(child, with: template, context: context)
case .inheritedSection(let name, let template):
case .blockExpansion(let name, let defaultTemplate, let indented):
if let override = context.inherited?[name] {
return override.render(context: context)
return override.render(context: context.withBlockExpansion(indented: indented))
} else {
return template.render(context: context)
return defaultTemplate.render(context: context.withBlockExpansion(indented: indented))
}
case .partial(let name, let indentation, let overrides):
@@ -89,6 +90,9 @@ extension MustacheTemplate {
case .contentType(let contentType):
context = context.withContentType(contentType)
case .blockDefinition:
fatalError("Should not be rendering block definitions")
}
return ""
}