Skip to content

Commit

Permalink
chore: fixup newline node replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofzablocki committed Apr 14, 2021
1 parent dc9e9df commit e1b75db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
13 changes: 13 additions & 0 deletions SourceryStencil/Sources/StencilTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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()
Expand Down

0 comments on commit e1b75db

Please sign in to comment.