minor code refactoring

This commit is contained in:
Ilya Puchka
2017-12-27 19:29:17 +01:00
parent 662849e968
commit d6766b43da
5 changed files with 58 additions and 60 deletions

View File

@@ -4,11 +4,12 @@ class BlockContext {
// contains mapping of block names to their nodes and templates where they are defined
var blocks: [String: [BlockNode]]
init(blocks: [String: [BlockNode]]) {
self.blocks = blocks
init(blocks: [String: BlockNode]) {
self.blocks = [:]
blocks.forEach { self.blocks[$0.key] = [$0.value] }
}
func pushBlock(_ block: BlockNode, named blockName: String) {
func push(_ block: BlockNode, forKey blockName: String) {
if var blocks = blocks[blockName] {
blocks.append(block)
self.blocks[blockName] = blocks
@@ -17,7 +18,7 @@ class BlockContext {
}
}
func popBlock(named blockName: String) -> BlockNode? {
func pop(_ blockName: String) -> BlockNode? {
if var blocks = blocks[blockName] {
let block = blocks.removeFirst()
if blocks.isEmpty {
@@ -88,14 +89,12 @@ class ExtendsNode : NodeType {
let baseTemplate = try context.environment.loadTemplate(name: templateName)
let blockContext: BlockContext
if let _blockContext = context[BlockContext.contextKey] as? BlockContext {
blockContext = _blockContext
if let currentBlockContext = context[BlockContext.contextKey] as? BlockContext {
blockContext = currentBlockContext
for (name, block) in blocks {
blockContext.pushBlock(block, named: name)
blockContext.push(block, forKey: name)
}
} else {
var blocks = [String: [BlockNode]]()
self.blocks.forEach { blocks[$0.key] = [$0.value] }
blockContext = BlockContext(blocks: blocks)
}
@@ -144,48 +143,47 @@ class BlockNode : NodeType {
}
func render(_ context: Context) throws -> String {
if let blockContext = context[BlockContext.contextKey] as? BlockContext, let child = blockContext.popBlock(named: name) {
// child node is a block node from child template that extends this node (has the same name)
var newContext: [String: Any] = [BlockContext.contextKey: blockContext]
if let blockSuperNode = child.nodes.first(where: {
if case .variable(let variable, _)? = $0.token, variable == "block.super" { return true }
else { return false}
}) {
do {
// render current (base) node so that its content can be used as part of node that extends it
newContext["block"] = ["super": try self.render(context)]
} catch {
if let error = error as? TemplateSyntaxError {
throw TemplateSyntaxError(
reason: error.reason,
token: blockSuperNode.token,
stackTrace: error.allTokens)
} else {
throw TemplateSyntaxError(
reason: "\(error)",
token: blockSuperNode.token,
stackTrace: [])
}
}
}
if let blockContext = context[BlockContext.contextKey] as? BlockContext, let child = blockContext.pop(name) {
let childContext = try self.childContext(child, blockContext: blockContext, context: context)
// render extension node
do {
return try context.push(dictionary: newContext) {
return try context.push(dictionary: childContext) {
return try child.render(context)
}
} catch {
if var error = error as? TemplateSyntaxError {
error.token = error.token ?? child.token
throw error
} else {
throw error
}
throw error.withToken(child.token)
}
}
return try renderNodes(nodes, context)
}
// child node is a block node from child template that extends this node (has the same name)
func childContext(_ child: BlockNode, blockContext: BlockContext, context: Context) throws -> [String: Any?] {
var childContext: [String: Any?] = [BlockContext.contextKey: blockContext]
if let blockSuperNode = child.nodes.first(where: {
if case .variable(let variable, _)? = $0.token, variable == "block.super" { return true }
else { return false}
}) {
do {
// render base node so that its content can be used as part of child node that extends it
childContext["block"] = ["super": try self.render(context)]
} catch {
if let error = error as? TemplateSyntaxError {
throw TemplateSyntaxError(
reason: error.reason,
token: blockSuperNode.token,
stackTrace: error.allTokens)
} else {
throw TemplateSyntaxError(
reason: "\(error)",
token: blockSuperNode.token,
stackTrace: [])
}
}
}
return childContext
}
}