Commit Graph

66 Commits

Author SHA1 Message Date
Adam Gent 96aac1a41f Improve escaping performance 2023-12-19 12:57:38 -05:00
Adam Gent a065543327 Inheritance support #120
Turn on inheritance tests. Some whitespace issues.

Recursion working but multi standalone broken

Make inheritance pass spec tests
2023-11-30 12:11:52 -05:00
Michael Bayne 92c1222d80 Minor formatting tweaks made while reading diffs. 2023-11-29 08:35:28 -08:00
Adam Gent 388406eab5 Fix identation with section segments 2023-11-27 13:27:58 -05:00
Adam Gent 8a85c64522 Fix nested partial indentation 2023-11-21 13:11:57 -05:00
Adam Gent afbf37e028 Partial indentation support for v1.3 mustache 2023-11-17 16:48:38 -05:00
Ralfs Gūtmanis d1d72031f2 Add ability to pass CharSequence instead of forcing String for context variable.
java.lang.String is immutable and cannot be overwritten by user.
When passing sensitive data as context variable String remains in memory until GC removes it.
CharSequence allows user to provide his own implementation and overwrite data as needed.
2023-09-18 14:05:37 +03:00
Michael Bayne 4533567303 Fix bugs relating to . and this.
First, I missed some reference checks when I stopped interning names. Fixed
those.

Second, when iterating over an array which contained null elements, the wrong
thing was happening.
2018-06-04 16:10:35 -07:00
Michael Bayne b823ed87c5 Remove use of String.intern().
Me, on the blackboard:

"I will not optimize code without using a profiler."
"I will not optimize code without using a profiler."
"I will not optimize code without using a profiler."
"I will not optimize code without using a profiler."
"I will not optimize code without using a profiler."
...
2018-04-30 08:58:01 -07:00
Michael Bayne 37e31cbeac Add mechism to visit template tags.
This is useful when you want to inspect the structure of the template without
executing it.
2017-12-17 15:16:09 -08:00
Michael Bayne 326f04cb1e Brace fiddling. 2017-12-17 14:46:42 -08:00
Michael Bayne 02f917e2f7 Add Fragment.executeTemplate().
Allows a lambda to execute a template in the lambda's own context. This enables
'late bound' template inclusion where the lambda can select a template based on
dynamic data in the context at the time it is executed.

Closes #92.
2017-12-03 18:29:15 -08:00
Michael Bayne 172f35fb64 When nesting for non-list contexts, inherit list info.
This ensures that if you enter a conditional block while inside a list
block, the list "state" from the outer list will still be visible. For
example (somewhat contrived, but gets the point across):

{{#list}}
{{#name}}{{-index}}. {{.}}{{/name}}
{{^name}}{{-index}}. No name!{{/name}}
{{/list}}

Yields something like:

1. Bob
2. Mary
3. No name!
4. Jim

Fixes #90.
2017-09-20 09:43:14 -07:00
Michael Bayne 1170d4500e Some toString impls to make debugging easier. 2016-06-15 11:51:44 -07:00
Michael Bayne 115f496947 Add means to obtain parent context(s) in Fragment.
Closes #77.
2016-05-15 11:07:22 -07:00
Michael Bayne 546f78318e Simple support for decompiling fragments.
There are limitations! See the javadoc for Template.Fragment.decompile.

Closes #75.
2016-04-14 08:32:07 -07:00
Michael Bayne 2b376d3189 Align "compact" Javadocs more sanely. 2015-03-06 12:24:13 -08:00
Michael Bayne 8618f89f7e Added strictSections configuration.
Section behavior used to be somewhat hackily determined from nullValue and
defaultValue, but it was messy. Now it has its own configuration and things are
more explicit.

Previously missing/null sections were treated as false, but if you set
nullValue to something, then missing sections became an error, but null
sections remained false. If you set defaultValue to something, missing sections
went back to being omitted. Kooky.

Now missing sections are omitted, unless you set strictSections to true, in
which case they become an error. nullValue and defaultValue have no impact on
section behavior.

There is no way to make null sections an error, but I don't see any demand for
that, so I'm going to leave that as a TODO.

Closes #60.
2014-12-20 11:06:46 -08:00
Michael Bayne f838c91f99 Attempt to resolve compound keys "whole" first.
This allows one to have a map with keys like "foo.bar.baz" even when not in
standards mode. If a whole key is missing, then we attempt to decompose it and
look it up piecewise.

Closes #55.
2014-05-08 09:49:33 -07:00
Michael Bayne 2f267b7199 Make NO_FETCHER_FOUND available to external parties.
Closes #54.
2014-05-02 11:30:31 -07:00
Michael Bayne 20abde5b1a Expose the context to a lambda fragment.
This allows the lambda to take the context and turn around and render it using
a totally different template, if desired.
2014-04-16 14:26:22 -07:00
Michael Bayne 9861b54aa0 Formatting, and small javadoc improvement. 2014-03-18 09:26:38 -07:00
robbytx 75609b42df Add ability for lambda to change the context for fragment execution 2014-03-14 09:50:19 -05:00
Michael Bayne 9372c7009e Cache a NOT_FOUND_FETCHER when no fetcher is found.
Closes #45.

This avoids the expensive process of repeatedly looking for a fetcher in a
context where one doesn't exist.
2014-01-02 11:24:36 -08:00
Michael Bayne 6cfc376e8d Rewrote array/list fetch-by-index using collectors.
That's what collectors are for. This also avoids the needless expense of doing
Integer.parseInt on every single key (the vast majority of which will not
actually be numbers), every single time we do a context lookup.
2013-10-07 14:25:58 -07:00
Mickael Jeanroy 98c7a5d38f Access Array item by index in template 2013-10-06 10:06:48 +02:00
Michael Bayne 9d77d492b0 Expose NO_FETCHER_FOUND as custom fetchers may wish to return it. 2013-04-05 11:01:37 -07:00
Yoryos Valotasios 2889ee8692 Added standard Mustache specs tests suite.
The groups with failing tests are currently commented out (everything except
interpolation). The main reason for failures are due to the requirement that
standalone lines that contain nothing but whitespace be stripped from the
output.
2013-04-02 12:54:20 -07:00
Michael Bayne 3a5da915e9 These days I always cuddle method braces. 2013-03-27 10:37:41 -07:00
Michael Bayne 43db03a26a Changed MustacheContextException -> MustacheException.Context.
Also made data into final fields. I don't like unnecessary getters.
2013-03-05 20:55:01 -08:00
pmenhart c61650734d Added MustacheContextException
Solving need to distinguish context errors from environment errors.
  The exception has fields for the offending key and line number. This
  is a first step towards internationalization of messages.
2013-03-05 23:33:48 -05:00
Michael Bayne acbddd07a2 Added basic lambda support. 2013-02-15 16:38:36 -08:00
Michael Bayne 8f87bf374d Some tweaks to the 'substitute name into null value' patch. 2013-01-23 17:43:46 -08:00
pmenhart 8b0d063ffd Show the name of variable that is null or missing
If Compliler.nullValue contains a substring "{{name}}", then this substring
will be replaced with the name of variable.
For example, if nullValue="?{{name}}?" and variable is resolved to null,
then string "?foo?" will be used.
2013-01-23 15:59:52 -05:00
Michael Bayne 6e71dad5f2 Simplify logic now that null is always returned. 2012-12-04 16:48:23 -08:00
Michael Bayne 65f8d12d62 Merge pull request #22 from tominsam/master
allow inner null values to mask parent context values
2012-12-04 16:38:13 -08:00
Michael Bayne 5ec24e947c Use ConcurrentHashMap to cache fetchers, on platforms that support it. 2012-10-01 09:27:36 -07:00
Tom Insam 2b337b8770 null values can shadow parent context now. Improve test a little. 2012-08-22 15:19:00 +01:00
Michael Bayne 718639127e Added comments and squeezed into 100 columns. 2011-11-22 11:40:09 -08:00
Jacob Lewallen 1a06a0233a Add a Template.execute that takes a parent context. 2011-11-22 11:20:59 -08:00
Michael Bayne f6c6f4916c Pass the context instance itself instead of its class.
In GWT we can't use Class.isAssignableFrom. We need the original instance so
that we can do (ctx instanceof Map<?,?>).
2011-10-29 15:29:18 -07:00
Michael Bayne adf116992c Modified compound variables to honor null/default configuration. Closes #10.
Kali/Bertrand convinced me that being consistent with other Mustache
implementations is more important than whatever wacky reasons I was harboring
for keeping compound variables different than singleton sections.
2011-10-29 14:38:56 -07:00
Michael Bayne 76e9d54683 Extracted the iteration over collections and creation of variable fetchers into
a pluggable component.

This allows scaling down to the GWT environment by hiding the reflection code
via GWT's super source mechanism.

And it allows scaling up to non-Java languages (like Scala) by allowing the
library client to extend the DefaultCollector and teach it about Scala
Iterators, Iterables and Maps.

I also just realized that GWT has no facilities for Reader and Writer, which is
going to complicate making things work in GWT even further. But for now, I'll
just leave things as is.
2011-10-13 20:35:46 -07:00
Michael Bayne a3eb9e22e1 Fixed bug where -last was not correctly handled on lists of a single element,
where said element is both -first and -last.
2011-08-29 13:51:11 -07:00
Michael Bayne 28978548c5 Wrapped some long lines. 2011-08-29 13:42:11 -07:00
Michael Bayne 7255cb7c09 We keep the @Override on the same line round these parts. 2011-07-17 08:38:20 -07:00
David Hoover b0737e3f2b Add missing @Overrides 2011-07-01 11:25:52 -07:00
Michael Bayne 9fae13e008 Added Compiler.nullValue() for situations where one wants to provide a value
for use when variables resolve to null, but still wishes for exceptions to be
thrown when values are not resolvable (i.e. are typos). Pre-existing behavior
is preserved if one uses defaultValue() or does not change the default
configuration.

Nota bene: there are complexities, so be sure to read the documentation.
2011-05-28 10:42:45 -07:00
Michael Bayne ef971015bd Resolves issue 7 regarding proper handling of nested contexts when processing a
nested context.
2011-05-26 14:52:45 -07:00
Michael Bayne 15ff48eabb Some tidying up, name changes and clarifications. I went with "defaultValue"
rather than "missingVariableValue" because the value is used any time a
variable resolves to null, not necessarily just when it is missing. If you
have a context like:

new Object() {
   String foo = null;
}

and reference {{foo}} in your template, foo is not missing, per se, but the
default value will still be used for it.
2011-04-15 12:35:33 -07:00