-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move all context variables into HBMustacheContext * Add support for reading inherited sections * Render inherited tokens * Test inheritance spec, fix two minor issues * fix warning * swift format
- Loading branch information
1 parent
af345e9
commit 35d5260
Showing
9 changed files
with
232 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
struct HBMustacheContext { | ||
let stack: [Any] | ||
let sequenceContext: HBMustacheSequenceContext? | ||
let indentation: String? | ||
let inherited: [String: HBMustacheTemplate]? | ||
|
||
init(_ object: Any) { | ||
self.stack = [object] | ||
self.sequenceContext = nil | ||
self.indentation = nil | ||
self.inherited = nil | ||
} | ||
|
||
private init( | ||
stack: [Any], | ||
sequenceContext: HBMustacheSequenceContext?, | ||
indentation: String?, | ||
inherited: [String: HBMustacheTemplate]? | ||
) { | ||
self.stack = stack | ||
self.sequenceContext = sequenceContext | ||
self.indentation = indentation | ||
self.inherited = inherited | ||
} | ||
|
||
func withObject(_ object: Any) -> HBMustacheContext { | ||
var stack = self.stack | ||
stack.append(object) | ||
return .init(stack: stack, sequenceContext: nil, indentation: self.indentation, inherited: self.inherited) | ||
} | ||
|
||
func withPartial(indented: String?, inheriting: [String: HBMustacheTemplate]?) -> HBMustacheContext { | ||
let indentation: String? | ||
if let indented = indented { | ||
indentation = (self.indentation ?? "") + indented | ||
} else { | ||
indentation = self.indentation | ||
} | ||
let inherits: [String: HBMustacheTemplate]? | ||
if let inheriting = inheriting { | ||
if let originalInherits = self.inherited { | ||
inherits = originalInherits.merging(inheriting) { value, _ in value } | ||
} else { | ||
inherits = inheriting | ||
} | ||
} else { | ||
inherits = self.inherited | ||
} | ||
return .init(stack: self.stack, sequenceContext: nil, indentation: indentation, inherited: inherits) | ||
} | ||
|
||
func withSequence(_ object: Any, sequenceContext: HBMustacheSequenceContext) -> HBMustacheContext { | ||
var stack = self.stack | ||
stack.append(object) | ||
return .init(stack: stack, sequenceContext: sequenceContext, indentation: self.indentation, inherited: self.inherited) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.