-
Notifications
You must be signed in to change notification settings - Fork 280
More Handlebars helpers #5937
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
Open
bubblobill
wants to merge
19
commits into
RPTools:develop
Choose a base branch
from
bubblobill:handlebarsHelpers
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
More Handlebars helpers #5937
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
59c7367
Made handlebars creation more visible.
bubblobill 581b234
Merge remote-tracking branch 'bb/handlebarsHelpers' into handlebarsHe…
bubblobill 079574f
formatting
bubblobill 42f9233
Added helper dependency
bubblobill cdab108
Merge branch 'RPTools:develop' into handlebarsHelpers
bubblobill 74c5708
Merge branch 'RPTools:develop' into handlebarsHelpers
bubblobill 4542caf
Null/blank check added to Base64EncodeHelper.
bubblobill d5e0927
Formatting
bubblobill fde4085
Removed println from HBLogger.
bubblobill 90c3e03
Changed MathsHelpers.numbers() to enhanced for structure.
bubblobill c249a37
HandlebarsUtil - set preEvaluatePartialBlocks to false (apparently
bubblobill e06cf7e
corrected log level
bubblobill aee27fb
Added missing EmbeddedHelper to helper registration.
bubblobill 90d2c91
formatting
bubblobill 00cb47a
inline partial test
bubblobill b19ee56
Hyphenate stat-sheet.
bubblobill c73cc06
Merge remote-tracking branch 'bb/handlebarsHelpers' into handlebarsHe…
bubblobill dedb69e
Changed logger class.
bubblobill a48d33d
Merge branch 'RPTools:develop' into handlebarsHelpers
bubblobill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
331 changes: 331 additions & 0 deletions
331
src/main/java/net/rptools/maptool/util/HandlebarsHelpers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,331 @@ | ||
| /* | ||
| * This software Copyright by the RPTools.net development team, and | ||
| * licensed under the Affero GPL Version 3 or, at your option, any later | ||
| * version. | ||
| * | ||
| * MapTool Source Code is distributed in the hope that it will be | ||
| * useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
| * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public | ||
| * License * along with this source Code. If not, please visit | ||
| * <http://www.gnu.org/licenses/> and specifically the Affero license | ||
| * text at <http://www.gnu.org/licenses/agpl.html>. | ||
| */ | ||
| package net.rptools.maptool.util; | ||
|
|
||
| import com.github.jknack.handlebars.*; | ||
| import com.github.jknack.handlebars.helper.ConditionalHelpers; | ||
| import com.github.jknack.handlebars.helper.LogHelper; | ||
| import com.github.jknack.handlebars.helper.StringHelpers; | ||
| import com.github.jknack.handlebars.helper.ext.AssignHelper; | ||
| import com.github.jknack.handlebars.helper.ext.IncludeHelper; | ||
| import com.github.jknack.handlebars.helper.ext.NumberHelper; | ||
| import com.vladsch.flexmark.html.HtmlRenderer; | ||
| import com.vladsch.flexmark.parser.Parser; | ||
| import com.vladsch.flexmark.util.ast.Node; | ||
| import com.vladsch.flexmark.util.data.MutableDataSet; | ||
| import java.io.IOException; | ||
| import java.math.BigDecimal; | ||
| import java.math.MathContext; | ||
| import java.math.RoundingMode; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Base64; | ||
| import java.util.List; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class HandlebarsHelpers { | ||
| static Handlebars registerHelpers(Handlebars handlebars) { | ||
| StringHelpers.register(handlebars); | ||
| Arrays.stream(ConditionalHelpers.values()).forEach(h -> handlebars.registerHelper(h.name(), h)); | ||
| handlebars.registerHelper("json", Jackson2Helper.INSTANCE); | ||
| NumberHelper.register(handlebars); | ||
| handlebars.registerHelper(AssignHelper.NAME, AssignHelper.INSTANCE); | ||
| handlebars.registerHelper(IncludeHelper.NAME, IncludeHelper.INSTANCE); | ||
| handlebars.registerHelper(MarkdownHelper.NAME, MarkdownHelper.INSTANCE); | ||
| handlebars.registerHelper(Base64EncodeHelper.NAME, Base64EncodeHelper.INSTANCE); | ||
| Arrays.stream(HandlebarsHelpers.MathsHelpers.values()) | ||
| .forEach(h -> handlebars.registerHelper(h.name(), h)); | ||
| return handlebars; | ||
| } | ||
|
|
||
| static class Base64EncodeHelper implements Helper<Object> { | ||
| /** A singleton instance of this helper. */ | ||
| public static final Helper<Object> INSTANCE = new Base64EncodeHelper(); | ||
|
|
||
| /** The helper's name. */ | ||
| public static final String NAME = "base64Encode"; | ||
|
|
||
| /** | ||
| * Turns the textual form of the value into a base64-encoded string. For example: | ||
| * | ||
| * <pre> | ||
| * <script type="application/json;base64" id="jsonProperty"> | ||
| * {{ base64Encode properties[0].value }} | ||
| * </script> | ||
| * <script type="application/javascript"> | ||
| * const jsonProperty = JSON.parse(atob(document.getElementById("jsonProperty").innerText)); | ||
| * </script> | ||
| * </pre> | ||
| */ | ||
| @Override | ||
| public Object apply(final Object context, final Options options) { | ||
| byte[] message = context.toString().getBytes(StandardCharsets.UTF_8); | ||
| return new Handlebars.SafeString(Base64.getUrlEncoder().encodeToString(message)); | ||
| } | ||
| } | ||
|
|
||
| public enum MathsHelpers implements Helper<Object> { | ||
| add { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| try { | ||
| List<BigDecimal> numbers = numbers(a, options); | ||
| BigDecimal result = numbers.removeLast(); | ||
| while (!numbers.isEmpty()) { | ||
| result = result.add(numbers.removeLast()); | ||
| } | ||
| return String.valueOf(result); | ||
| } catch (Exception ignored) { | ||
| return "NaN"; | ||
| } | ||
| } | ||
| }, | ||
| subtract { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| try { | ||
| List<BigDecimal> numbers = numbers(a, options); | ||
| BigDecimal result = numbers.removeLast(); | ||
| while (!numbers.isEmpty()) { | ||
| result = result.subtract(numbers.removeLast()); | ||
| } | ||
| return String.valueOf(result); | ||
| } catch (Exception ignored) { | ||
| return "NaN"; | ||
| } | ||
| } | ||
| }, | ||
| multiply { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| try { | ||
| List<BigDecimal> numbers = numbers(a, options).reversed(); | ||
| BigDecimal result = numbers.removeLast(); | ||
| while (!numbers.isEmpty()) { | ||
| result = result.multiply(numbers.removeLast()); | ||
| } | ||
| return String.valueOf(result); | ||
| } catch (Exception ignored) { | ||
| return "NaN"; | ||
| } | ||
| } | ||
| }, | ||
| divide { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| try { | ||
| List<BigDecimal> numbers = numbers(a, options).reversed(); | ||
| BigDecimal result = numbers.removeLast(); | ||
| while (!numbers.isEmpty()) { | ||
| result = result.divide(numbers.removeLast(), RoundingMode.HALF_EVEN); | ||
| } | ||
| return String.valueOf(result); | ||
| } catch (Exception ignored) { | ||
| return "NaN"; | ||
| } | ||
| } | ||
| }, | ||
| max { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| try { | ||
| List<BigDecimal> numbers = numbers(a, options); | ||
| BigDecimal result = numbers.removeLast(); | ||
| while (!numbers.isEmpty()) { | ||
| result = result.max(numbers.removeLast()); | ||
| } | ||
| return String.valueOf(result); | ||
| } catch (Exception ignored) { | ||
| return "NaN"; | ||
| } | ||
| } | ||
| }, | ||
| min { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| try { | ||
| List<BigDecimal> numbers = numbers(a, options); | ||
| BigDecimal result = numbers.removeLast(); | ||
| while (!numbers.isEmpty()) { | ||
| result = result.min(numbers.removeLast()); | ||
| } | ||
| return String.valueOf(result); | ||
| } catch (Exception ignored) { | ||
| return "NaN"; | ||
| } | ||
| } | ||
| }, | ||
| mod { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| try { | ||
| List<BigDecimal> numbers = numbers(a, options); | ||
| BigDecimal result = numbers.removeLast(); | ||
| while (!numbers.isEmpty()) { | ||
| result = result.remainder(numbers.removeLast()); | ||
| } | ||
| return String.valueOf(result); | ||
| } catch (Exception ignored) { | ||
| return "NaN"; | ||
| } | ||
| } | ||
| }, | ||
| div { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| try { | ||
| List<BigDecimal> numbers = numbers(a, options); | ||
| BigDecimal result = numbers.removeLast(); | ||
| while (!numbers.isEmpty()) { | ||
| result = result.divideToIntegralValue(numbers.removeLast()); | ||
| } | ||
| return String.valueOf(result); | ||
| } catch (Exception ignored) { | ||
| return "NaN"; | ||
| } | ||
| } | ||
| }, | ||
| pow { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| try { | ||
| List<BigDecimal> numbers = numbers(a, options); | ||
| BigDecimal result = numbers.removeLast(); | ||
| while (!numbers.isEmpty()) { | ||
| result = result.pow(numbers.removeLast().intValue()); | ||
| } | ||
| return String.valueOf(result); | ||
| } catch (Exception ignored) { | ||
| return "NaN"; | ||
| } | ||
| } | ||
| }, | ||
| abs { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| try { | ||
| return new BigDecimal(a.toString()).abs().toString(); | ||
| } catch (Exception ignored) { | ||
| return "NaN"; | ||
| } | ||
| } | ||
| }, | ||
| sqrt { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| try { | ||
| return new BigDecimal(a.toString()).sqrt(MathContext.DECIMAL32).toString(); | ||
| } catch (Exception ignored) { | ||
| return "NaN"; | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| tau { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| return String.valueOf(Math.TAU); | ||
| } | ||
| }, | ||
| pi { | ||
| @Override | ||
| public Object apply(final Object a, final Options options) throws IOException { | ||
| return String.valueOf(Math.PI); | ||
| } | ||
| }, | ||
| ; | ||
|
|
||
| List<BigDecimal> numbers(Object a, final Options options) { | ||
| int count = options.params.length + 1; | ||
|
bubblobill marked this conversation as resolved.
Outdated
|
||
| List<BigDecimal> values = new ArrayList<>(); | ||
| try { | ||
| values.add(new BigDecimal(a.toString())); | ||
| for (int i = 0; i < count; i++) { | ||
| values.add(new BigDecimal((double) options.param(i))); | ||
| } | ||
|
|
||
| } catch (NumberFormatException ignored) { | ||
| } | ||
| return values; | ||
| } | ||
| } | ||
|
|
||
| public static class HBLogger extends LogHelper { | ||
| private static final Logger log = LoggerFactory.getLogger(Handlebars.class); | ||
|
|
||
| /** A singleton instance of this helper. */ | ||
| public static final Helper<Object> INSTANCE = new HBLogger(); | ||
|
|
||
| /** The helper's name. */ | ||
| public static final String NAME = "log"; | ||
|
|
||
| @Override | ||
| public Object apply(Object context, Options options) throws IOException { | ||
| StringBuilder sb = new StringBuilder(); | ||
| String level = options.hash("level", "info"); | ||
| TagType tagType = options.tagType; | ||
| if (tagType.inline()) { | ||
| sb.append(context); | ||
| for (int i = 0; i < options.params.length; i++) { | ||
| sb.append(" ").append((Object) options.param(i)); | ||
| } | ||
| } else { | ||
| sb.append(options.fn()); | ||
| } | ||
| System.out.println("Handlebars(" + level + "): " + sb.toString().trim()); | ||
|
bubblobill marked this conversation as resolved.
Outdated
|
||
| switch (level) { | ||
| case "error": | ||
| log.error(sb.toString().trim()); | ||
| break; | ||
| case "debug": | ||
| log.debug(sb.toString().trim()); | ||
| break; | ||
| case "warn": | ||
| log.warn(sb.toString().trim()); | ||
| break; | ||
| case "trace": | ||
| log.trace(sb.toString().trim()); | ||
| break; | ||
| default: | ||
| log.info(sb.toString().trim()); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public static class MarkdownHelper implements Helper<Object> { | ||
| private static final MutableDataSet MD_OPTIONS = new MutableDataSet(); | ||
| private static final Parser PARSER = Parser.builder(MD_OPTIONS).build(); | ||
| private static final HtmlRenderer HTML_RENDERER = HtmlRenderer.builder(MD_OPTIONS).build(); | ||
|
|
||
| /** A singleton version of {@link MarkdownHelper}. */ | ||
| public static final Helper<Object> INSTANCE = new MarkdownHelper(); | ||
|
|
||
| /** The helper's name. */ | ||
| public static final String NAME = "markdown"; | ||
|
|
||
| @Override | ||
| public Object apply(final Object context, final Options options) throws IOException { | ||
| if (options.isFalsy(context)) { | ||
| return ""; | ||
| } | ||
| String markdown = context.toString(); | ||
| Node document = PARSER.parse(markdown); | ||
| return new Handlebars.SafeString(HTML_RENDERER.render(document)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.