diff --git a/CHANGELOG.md b/CHANGELOG.md index 49945fce6..87adc998f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Sourcery CHANGELOG +## Master + +## Fixes +-Adds trim option for Stencil template leading / trailing whitespace and replaces newline tag markers with normal newline after that + --- ## 1.4.0 diff --git a/SourceryStencil/Sources/StencilTemplate.swift b/SourceryStencil/Sources/StencilTemplate.swift index 00b545bb3..63e162ee8 100644 --- a/SourceryStencil/Sources/StencilTemplate.swift +++ b/SourceryStencil/Sources/StencilTemplate.swift @@ -6,6 +6,9 @@ import SourceryRuntime public final class StencilTemplate: StencilSwiftKit.StencilSwiftTemplate { private(set) public var sourcePath: Path = "" + + /// Trim leading / trailing whitespaces until content or newline tag appears + public var trimEnabled: Bool = false public convenience init(path: Path) throws { self.init(templateString: try path.read(), environment: StencilTemplate.sourceryEnvironment(templatePath: path)) @@ -15,6 +18,16 @@ public final class StencilTemplate: StencilSwiftKit.StencilSwiftTemplate { public convenience init(templateString: String) { self.init(templateString: templateString, environment: StencilTemplate.sourceryEnvironment()) } + + // swiftlint:disable:next discouraged_optional_collection + override public func render(_ dictionary: [String: Any]? = nil) throws -> String { + var result = try super.render(dictionary) + if trimEnabled { + result = result.trimmed + } + + return result.replacingOccurrences(of: NewLineNode.marker, with: "\n") + } public static func sourceryEnvironment(templatePath: Path? = nil) -> Stencil.Environment { let ext = Stencil.Extension()