From 9c2dfaa34c93ee5bf24093c95cff7d4b77b4ecb5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 4 May 2017 12:10:26 +0300 Subject: juick-www: pebble message formatter extension --- .../pebble/extension/FormatterExtension.java | 18 +++++++++++++ .../extension/filters/FormatMessageFilter.java | 31 ++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 juick-www/src/main/java/com/mitchellbosecke/pebble/extension/FormatterExtension.java create mode 100644 juick-www/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java (limited to 'juick-www/src/main/java/com/mitchellbosecke/pebble') 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 getFilters() { + Map 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 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 getArgumentNames() { + return null; + } +} -- cgit v1.2.3