StencilSwiftKit
StencilSwiftKit copied to clipboard
Replace filter: unescape backslashes
In a template, if I try:
value|replace:"\n","\\n"
The search and replace strings become "\\n" and "\\\\n" respectively. I think we should un-escape these backslashes, so that users correctly can search for newlines (for example). Or is this something that we should handle in Stencil itself?
Mmmh good question. Is it:
- The job of the
replacefilter itself - The job of Stencil itself (which would allow to fix that issue for all tags, but might be a breaking change too)
- The job of SwiftGenKit (during parsing of the YAML/JSON/PLIST files)?
Note: SGK correctly parses the files (the string value is as it should be, unchanged).
Yeah the parsing itself shouldn't be changed, but should we or should we not convert newlines to \n when we build the context from those parsed content, or is that the job for Stencil or the template, that's the real question :wink:
Keep in mind that if we do that in SGK, those strings may be somewhere quite deep in some array/dictionary structure, and we'd need to check all strings. Seems quite the overhead on the context builder, where it's honestly an issue in Stencil(SwiftKit).
Note: we also have a contains filter where this applies as well.
Note 2: and hasPrefix and hasSuffix also.
Yeah which is why I'm starting to wonder if it shouldn't be Stencil's job to do that un-escape, instead of each filter individually…
Right, so the culprit would be this line: https://github.com/stencilproject/Stencil/blob/521a599a60206b61b1ac76dd0f01d20f95d3d588/Sources/Variable.swift#L53
Anyone know what's the best way to unescape something like "\\n" or "\\u5404\\u500b\\u90fd" (we may as well do this for unicode sequences)?