diff options
Diffstat (limited to 'juick-www/src/main/java/com')
3 files changed, 51 insertions, 0 deletions
diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java index 944fbb80..43ca37db 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java @@ -1,6 +1,7 @@ package com.juick.www.configuration; import com.mitchellbosecke.pebble.PebbleEngine; +import com.mitchellbosecke.pebble.extension.FormatterExtension; import com.mitchellbosecke.pebble.loader.Loader; import com.mitchellbosecke.pebble.loader.ServletLoader; import com.mitchellbosecke.pebble.spring4.PebbleViewResolver; @@ -85,6 +86,7 @@ public class WwwServletConfiguration extends WebMvcConfigurationSupport { return new PebbleEngine.Builder() .loader(this.templateLoader()) .extension(springExtension()) + .extension(new FormatterExtension()) .strictVariables(true) .build(); } diff --git a/juick-www/src/main/java/com/mitchellbosecke/pebble/extension/FormatterExtension.java b/juick-www/src/main/java/com/mitchellbosecke/pebble/extension/FormatterExtension.java new file mode 100644 index 00000000..e26b5c20 --- /dev/null +++ b/juick-www/src/main/java/com/mitchellbosecke/pebble/extension/FormatterExtension.java @@ -0,0 +1,18 @@ +package com.mitchellbosecke.pebble.extension; + +import com.mitchellbosecke.pebble.extension.filters.FormatMessageFilter; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by vitalyster on 04.05.2017. + */ +public class FormatterExtension extends AbstractExtension { + @Override + public Map<String, Filter> getFilters() { + Map<String, Filter> filters = new HashMap<>(); + filters.put("formatMessage", new FormatMessageFilter()); + return filters; + } +} diff --git a/juick-www/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java b/juick-www/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java new file mode 100644 index 00000000..e7446431 --- /dev/null +++ b/juick-www/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java @@ -0,0 +1,31 @@ +package com.mitchellbosecke.pebble.extension.filters; + +import com.juick.Message; +import com.juick.util.MessageUtils; +import com.mitchellbosecke.pebble.extension.Filter; +import com.mitchellbosecke.pebble.extension.escaper.SafeString; + +import java.util.List; +import java.util.Map; + +/** + * Created by vitalyster on 04.05.2017. + */ +public class FormatMessageFilter implements Filter { + @Override + public Object apply(Object input, Map<String, Object> args) { + if (input instanceof Message) { + Message msg = (Message) input; + boolean isCode = msg.getTags().stream().anyMatch(t -> t.getName().equals("code")); + String formattedMessage = isCode ? MessageUtils.formatMessageCode(msg.getText()) + : MessageUtils.formatMessage(msg.getText()); + return new SafeString(formattedMessage); + } + throw new IllegalArgumentException("invalid input"); + } + + @Override + public List<String> getArgumentNames() { + return null; + } +} |