-
Notifications
You must be signed in to change notification settings - Fork 58
/
Environment.swift
110 lines (100 loc) · 4.12 KB
/
Environment.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// StencilSwiftKit
// Copyright © 2022 SwiftGen
// MIT Licence
//
import PathKit
import Stencil
public extension Extension {
/// Registers this package's tags and filters
func registerStencilSwiftExtensions() {
registerTags()
registerStringsFilters()
registerNumbersFilters()
}
}
private extension Extension {
func registerFilter(_ name: String, filter: @escaping Filters.BooleanWithArguments) {
typealias GenericFilter = (Any?, [Any?]) throws -> Any?
let inverseFilter: GenericFilter = { value, arguments in
try !filter(value, arguments)
}
registerFilter(name, filter: filter as GenericFilter)
registerFilter("!\(name)", filter: inverseFilter)
}
func registerNumbersFilters() {
registerFilter("hexToInt", filter: Filters.Numbers.hexToInt)
registerFilter("int255toFloat", filter: Filters.Numbers.int255toFloat)
registerFilter("percent", filter: Filters.Numbers.percent)
}
func registerStringsFilters() {
registerFilter("basename", filter: Filters.Strings.basename)
registerFilter("camelToSnakeCase", filter: Filters.Strings.camelToSnakeCase)
registerFilter("dirname", filter: Filters.Strings.dirname)
registerFilter("escapeReservedKeywords", filter: Filters.Strings.escapeReservedKeywords)
registerFilter("lowerFirstLetter", filter: Filters.Strings.lowerFirstLetter)
registerFilter("lowerFirstWord", filter: Filters.Strings.lowerFirstWord)
registerFilter("removeNewlines", filter: Filters.Strings.removeNewlines)
registerFilter("replace", filter: Filters.Strings.replace)
registerFilter("snakeToCamelCase", filter: Filters.Strings.snakeToCamelCase)
registerFilter("swiftIdentifier", filter: Filters.Strings.swiftIdentifier)
registerFilter("titlecase", filter: Filters.Strings.upperFirstLetter)
registerFilter("upperFirstLetter", filter: Filters.Strings.upperFirstLetter)
registerFilter("contains", filter: Filters.Strings.contains)
registerFilter("hasPrefix", filter: Filters.Strings.hasPrefix)
registerFilter("hasSuffix", filter: Filters.Strings.hasSuffix)
}
func registerTags() {
registerTag("set", parser: SetNode.parse)
registerTag("macro", parser: MacroNode.parse)
registerTag("call", parser: CallNode.parse)
registerTag("map", parser: MapNode.parse)
registerTag("import", parser: ImportNode.parse)
}
}
/// Creates an Environment for Stencil to load & render templates
///
/// - Parameters:
/// - templatePaths: Paths where Stencil can search for templates, used for example for `include` tags
/// - extensions: Additional extensions with filters/tags/… you want to provide to Stencil
/// - templateClass: Custom template class to process templates
/// - trimBehaviour: Globally control how whitespace is handled, defaults to `smart`.
/// - Returns: A fully configured `Environment`
public func stencilSwiftEnvironment(
templatePaths: [Path] = [],
extensions: [Extension] = [],
templateClass: Template.Type = Template.self,
trimBehaviour: TrimBehaviour = .smart
) -> Environment {
let ext = Extension()
ext.registerStencilSwiftExtensions()
return Environment(
loader: FileSystemLoader(paths: templatePaths),
extensions: extensions + [ext],
templateClass: templateClass,
trimBehaviour: trimBehaviour
)
}
/// Creates an Environment for Stencil to load & render templates
///
/// - Parameters:
/// - templates: Templates that can be included, imported, etc…
/// - extensions: Additional extensions with filters/tags/… you want to provide to Stencil
/// - templateClass: Custom template class to process templates
/// - trimBehaviour: Globally control how whitespace is handled, defaults to `smart`.
/// - Returns: A fully configured `Environment`
public func stencilSwiftEnvironment(
templates: [String: String],
extensions: [Extension] = [],
templateClass: Template.Type = Template.self,
trimBehaviour: TrimBehaviour = .smart
) -> Environment {
let ext = Extension()
ext.registerStencilSwiftExtensions()
return Environment(
loader: DictionaryLoader(templates: templates),
extensions: extensions + [ext],
templateClass: templateClass,
trimBehaviour: trimBehaviour
)
}