From be8d139d8ea58396107cf1a2b12ad16ac8bda6d6 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 17 Jan 2018 14:38:32 +0300 Subject: api: markdown tags for telegram --- .../src/main/java/com/juick/formatters/PlainTextFormatter.java | 6 +++++- juick-core/src/main/java/com/juick/util/MessageUtils.java | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'juick-core/src/main') diff --git a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java index 459384fb..0f5e0756 100644 --- a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java +++ b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java @@ -32,10 +32,14 @@ public class PlainTextFormatter { static PrettyTime pt = new PrettyTime(new Locale("ru")); public static String formatPost(Message jmsg) { + return formatPost(jmsg, false); + } + + public static String formatPost(Message jmsg, boolean markdown) { StringBuilder sb = new StringBuilder(); boolean isReply = jmsg.getRid() > 0; String title = isReply ? "Reply by @" : "@"; - String subtitle = isReply ? jmsg.getReplyQuote() : jmsg.getTagsString(); + String subtitle = isReply ? jmsg.getReplyQuote() : markdown ? MessageUtils.getMarkdownTags(jmsg) : jmsg.getTagsString(); sb.append(title).append(jmsg.getUser().getName()).append(":\n") .append(subtitle).append("\n").append(StringUtils.defaultString(jmsg.getText())).append("\n"); if (StringUtils.isNotEmpty(jmsg.getAttachmentType())) { diff --git a/juick-core/src/main/java/com/juick/util/MessageUtils.java b/juick-core/src/main/java/com/juick/util/MessageUtils.java index aba718a5..6e0b47c0 100644 --- a/juick-core/src/main/java/com/juick/util/MessageUtils.java +++ b/juick-core/src/main/java/com/juick/util/MessageUtils.java @@ -26,6 +26,7 @@ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * Created by aalexeev on 11/13/16. @@ -178,6 +179,10 @@ public class MessageUtils { } return hashtags.toString(); } + public static String getMarkdownTags(final Message jmsg) { + return jmsg.getTags().stream().map(t -> String.format("[%s](http://juick.com/tag/%s)", t.getName(), percentEncode(t.getName()))) + .collect(Collectors.joining(", ")); + } // TODO: check if it is really needed public static String percentEncode(final String s) { -- cgit v1.2.3