-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added filter to apply dynamic filters #203
Changes from 1 commit
564ccb7
f3d5843
2c411ca
e795f05
0269052
dbb5e14
da6a0cc
019d0cc
08fc21d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ public class TokenParser { | |
} | ||
|
||
if let tag = token.components().first { | ||
let parser = try findTag(name: tag) | ||
let parser = try environment.findTag(name: tag) | ||
nodes.append(try parser(self, token)) | ||
} | ||
case .comment: | ||
|
@@ -71,8 +71,24 @@ public class TokenParser { | |
tokens.insert(token, at: 0) | ||
} | ||
|
||
public func compileFilter(_ token: String) throws -> Resolvable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do these methods ( Note: if they can be internal, don't forget to reset the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So that they can be used by 3rd party extensions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ehm, do they need to be used by 3rd party extensions? Do you have a scenario where that'd be necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cant think of any now but if I needed it in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, a library should only expose the minimal surface necessary for a developer to use it. If at any point, something is missing, it can be added later on. Maybe someone else has some thoughts on this point? @AliSoftware? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd normally agree with limiting the surface of the public API, but given all other stuff like TBH I haven't used Stencil as a library to be extended for a while now (only use case for us is StencilSwiftKit to extend Stencil), not sure there are many people using those features — main uses of this lib is probably just using it as-is to render templates, adding maybe some custom filters and that's all), that shouldn't really bother them. BUT, if we're gonna have so much public API, then we should then definitely add doc-comments to them to explain to end users what they're supposed to be used for (especially to those not completely familiar with the concepts of a parser vs lexer etc, and proper definition of token etc). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea on the docs then. It took me quite a while to understand the codebase a bit, and the documentation in the code is quite sparse. |
||
return try environment.compileFilter(token) | ||
} | ||
|
||
public func compileExpression(components: [String]) throws -> Expression { | ||
return try environment.compileExpression(components: components) | ||
} | ||
|
||
public func compileResolvable(_ token: String) throws -> Resolvable { | ||
return try environment.compileResolvable(token) | ||
} | ||
|
||
} | ||
|
||
extension Environment { | ||
|
||
func findTag(name: String) throws -> Extension.TagParser { | ||
for ext in environment.extensions { | ||
for ext in extensions { | ||
if let filter = ext.tags[name] { | ||
return filter | ||
} | ||
|
@@ -82,7 +98,7 @@ public class TokenParser { | |
} | ||
|
||
func findFilter(_ name: String) throws -> FilterType { | ||
for ext in environment.extensions { | ||
for ext in extensions { | ||
if let filter = ext.filters[name] { | ||
return filter | ||
} | ||
|
@@ -97,7 +113,7 @@ public class TokenParser { | |
} | ||
|
||
private func suggestedFilters(for name: String) -> [String] { | ||
let allFilters = environment.extensions.flatMap({ $0.filters.keys }) | ||
let allFilters = extensions.flatMap({ $0.filters.keys }) | ||
|
||
let filtersWithDistance = allFilters | ||
.map({ (filterName: $0, distance: $0.levenshteinDistance(name)) }) | ||
|
@@ -111,11 +127,15 @@ public class TokenParser { | |
} | ||
|
||
public func compileFilter(_ token: String) throws -> Resolvable { | ||
ilyapuchka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return try FilterExpression(token: token, parser: self) | ||
return try FilterExpression(token: token, environment: self) | ||
} | ||
|
||
public func compileExpression(components: [String]) throws -> Expression { | ||
return try IfExpressionParser(components: components, environment: self).parse() | ||
} | ||
|
||
public func compileResolvable(_ token: String) throws -> Resolvable { | ||
return try RangeVariable(token, parser: self) | ||
return try RangeVariable(token, environment: self) | ||
?? compileFilter(token) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If (when?) #221 gets merged(merged), could you fix the format of this entry? Remove the newline above, add a.
and 2 spaces at the end, and credit yourself + link to this PR.