-
Notifications
You must be signed in to change notification settings - Fork 227
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
Implement the spec to make this a compliant implementation #52
base: master
Are you sure you want to change the base?
Conversation
As a compliant implementation of mustache, this code base should use the specs to ensure that it passes.
Allows more meaningful failure messages.
Add generate target gofmt all the things
"Interpolation tags are used to integrate dynamic content into the template. The tag's content MUST be a non-whitespace character sequence NOT containing the current closing delimiter. This tag's content names the data to replace the tag. A single period (`.`) indicates that the item currently sitting atop the context stack should be used; otherwise, name resolution is as follows: 1) Split the name on periods; the first part is the name to resolve, any remaining parts should be retained. 2) Walk the context stack from top to bottom, finding the first context that is a) a hash containing the name as a key OR b) an object responding to a method with the given name. 3) If the context is a hash, the data is the value associated with the name. 4) If the context is an object, the data is the value returned by the method with the given name. 5) If any name parts were retained in step 1, each should be resolved against a context stack containing only the result from the former resolution. If any part fails resolution, the result should be considered falsey, and should interpolate as the empty string. Data should be coerced into a string (and escaped, if appropriate) before interpolation. The Interpolation tags MUST NOT be treated as standalone."
"Comment tags represent content that should never appear in the resulting output. The tag's content may contain any substring (including newlines) EXCEPT the closing delimiter. Comment tags SHOULD be treated as standalone when appropriate."
"Section tags and End Section tags are used in combination to wrap a section of the template for iteration. These tags' content MUST be a non-whitespace character sequence NOT containing the current closing delimiter; each Section tag MUST be followed by an End Section tag with the same content within the same section. This tag's content names the data to replace the tag. Name resolution is as follows: 1) Split the name on periods; the first part is the name to resolve, any remaining parts should be retained. 2) Walk the context stack from top to bottom, finding the first context that is a) a hash containing the name as a key OR b) an object responding to a method with the given name. 3) If the context is a hash, the data is the value associated with the name. 4) If the context is an object and the method with the given name has an arity of 1, the method SHOULD be called with a String containing the unprocessed contents of the sections; the data is the value returned. 5) Otherwise, the data is the value returned by calling the method with the given name. 6) If any name parts were retained in step 1, each should be resolved against a context stack containing only the result from the former resolution. If any part fails resolution, the result should be considered falsey, and should interpolate as the empty string. If the data is not of a list type, it is coerced into a list as follows: if the data is truthy (e.g. `!!data == true`), use a single-element list containing the data, otherwise use an empty list. For each element in the data list, the element MUST be pushed onto the context stack, the section MUST be rendered, and the element MUST be popped off the context stack. Section and End Section tags SHOULD be treated as standalone when appropriate."
"Set Delimiter tags are used to change the tag delimiters for all content following the tag in the current compilation unit. The tag's content MUST be any two non-whitespace sequences (separated by whitespace) EXCEPT an equals sign ('=') followed by the current closing delimiter. Set Delimiter tags SHOULD be treated as standalone when appropriate."
"Inverted Section tags and End Section tags are used in combination to wrap a section of the template. These tags' content MUST be a non-whitespace character sequence NOT containing the current closing delimiter; each Inverted Section tag MUST be followed by an End Section tag with the same content within the same section. This tag's content names the data to replace the tag. Name resolution is as follows: 1) Split the name on periods; the first part is the name to resolve, any remaining parts should be retained. 2) Walk the context stack from top to bottom, finding the first context that is a) a hash containing the name as a key OR b) an object responding to a method with the given name. 3) If the context is a hash, the data is the value associated with the name. 4) If the context is an object and the method with the given name has an arity of 1, the method SHOULD be called with a String containing the unprocessed contents of the sections; the data is the value returned. 5) Otherwise, the data is the value returned by calling the method with the given name. 6) If any name parts were retained in step 1, each should be resolved against a context stack containing only the result from the former resolution. If any part fails resolution, the result should be considered falsey, and should interpolate as the empty string. If the data is not of a list type, it is coerced into a list as follows: if the data is truthy (e.g. `!!data == true`), use a single-element list containing the data, otherwise use an empty list. This section MUST NOT be rendered unless the data list is empty. Inverted Section and End Section tags SHOULD be treated as standalone when appropriate."
Partials require appropriate indentation, and also handling recursive partials. Added very basic environment as a type of symbol table for dealing with recursive partials. “Partial tags are used to expand an external template into the current template. The tag's content MUST be a non-whitespace character sequence NOT containing the current closing delimiter. This tag's content names the partial to inject. Set Delimiter tags MUST NOT affect the parsing of a partial. The partial MUST be rendered against the context stack local to the tag. If the named partial cannot be found, the empty string SHOULD be used instead, as in interpolations. Partial tags SHOULD be treated as standalone when appropriate. If this tag is used standalone, any whitespace preceding the tag should treated as indentation, and prepended to each line of the partial before rendering.”
I think this makes #43 obsolete. |
👍 |
+1 for this PR. @hoisie - would you please consider to pull this in? |
It would appear @hoisie has gone a little quiet. Thanks for much for this work @jabley - this implementation solves a lot of problems when trying to get Mustache templates working across platforms (Go and NodeJS in my case). If we can't get this merged, then I would suggest that @jabley's fork be considered the new master repo if possible? |
Used https://github.com/mustache/spec generate more tests, then made the tests pass.