Skip to content

Commit

Permalink
Refactor stencil swift extensions registration for easier use with an…
Browse files Browse the repository at this point in the history
… existing `Extension`.
  • Loading branch information
djbe committed Jan 27, 2017
1 parent 3539629 commit 77f033e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* Renamed `SwiftTemplate` to `StencilSwiftTemplate`.
[David Jennes](https://github.com/djbe)
[#14](https://github.com/SwiftGen/StencilSwiftKit/issues/14)
* Refactor stencil swift extensions registration for easier use with an existing `Extension`.
[David Jennes](https://github.com/djbe)
[#16](https://github.com/SwiftGen/StencilSwiftKit/issues/16)

## Before 5.0.0

Expand Down
4 changes: 4 additions & 0 deletions Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions Sources/Environment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// StencilSwiftKit
// Copyright (c) 2017 SwiftGen
// MIT Licence
//

import Stencil

public extension Extension {
public func registerStencilSwiftExtensions() {
registerTag("set", parser: SetNode.parse)
registerTag("macro", parser: MacroNode.parse)
registerTag("call", parser: CallNode.parse)
registerFilter("swiftIdentifier", filter: StringFilters.stringToSwiftIdentifier)
registerFilter("join", filter: ArrayFilters.join)
registerFilter("lowerFirstWord", filter: StringFilters.lowerFirstWord)
registerFilter("snakeToCamelCase", filter: StringFilters.snakeToCamelCase)
registerFilter("snakeToCamelCaseNoPrefix", filter: StringFilters.snakeToCamelCaseNoPrefix)
registerFilter("titlecase", filter: StringFilters.titlecase)
registerFilter("hexToInt", filter: NumFilters.hexToInt)
registerFilter("int255toFloat", filter: NumFilters.int255toFloat)
registerFilter("percent", filter: NumFilters.percent)
registerFilter("escapeReservedKeywords", filter: StringFilters.escapeReservedKeywords)
}
}

public func stencilSwiftEnvironment() -> Environment {
let ext = Extension()
ext.registerStencilSwiftExtensions()

return Environment(extensions: [ext], templateClass: StencilSwiftTemplate.self)
}
23 changes: 0 additions & 23 deletions Sources/StencilSwiftTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,3 @@ open class StencilSwiftTemplate: Template {
return unmarkedNewlines
}
}

// Create StencilSwiftKit-specific namespace including custom tags & filters
func stencilSwiftExtension() -> Extension {
let namespace = Extension()
namespace.registerTag("set", parser: SetNode.parse)
namespace.registerTag("macro", parser: MacroNode.parse)
namespace.registerTag("call", parser: CallNode.parse)
namespace.registerFilter("swiftIdentifier", filter: StringFilters.stringToSwiftIdentifier)
namespace.registerFilter("join", filter: ArrayFilters.join)
namespace.registerFilter("lowerFirstWord", filter: StringFilters.lowerFirstWord)
namespace.registerFilter("snakeToCamelCase", filter: StringFilters.snakeToCamelCase)
namespace.registerFilter("snakeToCamelCaseNoPrefix", filter: StringFilters.snakeToCamelCaseNoPrefix)
namespace.registerFilter("titlecase", filter: StringFilters.titlecase)
namespace.registerFilter("hexToInt", filter: NumFilters.hexToInt)
namespace.registerFilter("int255toFloat", filter: NumFilters.int255toFloat)
namespace.registerFilter("percent", filter: NumFilters.percent)
namespace.registerFilter("escapeReservedKeywords", filter: StringFilters.escapeReservedKeywords)
return namespace
}

public func stencilSwiftEnvironment() -> Environment {
return Environment(extensions: [stencilSwiftExtension()], templateClass: StencilSwiftTemplate.self)
}

0 comments on commit 77f033e

Please sign in to comment.