diff options
author | Vitaly Takmazov | 2017-08-23 00:47:37 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-08-23 01:08:02 +0300 |
commit | 3e4807157e3c244820dd4d5149997970530e4fcb (patch) | |
tree | 6603098f912a0119f57e37ed94658141ff09e85e /juick-core | |
parent | 007951db3ae9f9ba89a726960b0ef1639d63f3df (diff) |
www: Twitter Cards
Diffstat (limited to 'juick-core')
-rw-r--r-- | juick-core/build.gradle | 1 | ||||
-rw-r--r-- | juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java | 10 | ||||
-rw-r--r-- | juick-core/src/main/java/com/juick/util/MessageUtils.java | 23 |
3 files changed, 32 insertions, 2 deletions
diff --git a/juick-core/build.gradle b/juick-core/build.gradle index ae44f28c..c145a504 100644 --- a/juick-core/build.gradle +++ b/juick-core/build.gradle @@ -3,6 +3,7 @@ apply plugin: 'java' dependencies { compile "com.fasterxml.jackson.core:jackson-annotations:${rootProject.jacksonVersion}" compile 'org.apache.commons:commons-lang3:3.6' + compile "commons-codec:commons-codec:1.10" compile 'org.apache.commons:commons-collections4:4.1' compile "org.apache.commons:commons-text:1.1" compile 'commons-io:commons-io:2.5' 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 b86ebb73..8f9378a8 100644 --- a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java +++ b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java @@ -17,6 +17,8 @@ package com.juick.formatters; +import com.juick.Message; +import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringUtils; import org.ocpsoft.prettytime.PrettyTime; @@ -28,7 +30,7 @@ import java.util.Locale; public class PlainTextFormatter { static PrettyTime pt = new PrettyTime(new Locale("ru")); - public static String formatPost(com.juick.Message jmsg) { + public static String formatPost(Message jmsg) { StringBuilder sb = new StringBuilder(); boolean isReply = jmsg.getRid() > 0; String title = isReply ? "Reply by @" : "@"; @@ -41,7 +43,7 @@ public class PlainTextFormatter { return sb.toString(); } - public static String formatPostSummary(com.juick.Message m) { + public static String formatPostSummary(Message m) { int cropLength = 384; String timeAgo = pt.format(m.getDate()); String repliesCount = m.getReplies() == 1 ? "; 1 reply" : m.getReplies() == 0 ? "" @@ -58,4 +60,8 @@ public class PlainTextFormatter { } return "https://juick.com/" + jmsg.getMid(); } + + public static String formatTwitterCard(Message jmsg) { + return MessageUtils.getMessageHashTags(jmsg) + jmsg.getText(); + } } 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 a901fac1..03b677d4 100644 --- a/juick-core/src/main/java/com/juick/util/MessageUtils.java +++ b/juick-core/src/main/java/com/juick/util/MessageUtils.java @@ -17,8 +17,13 @@ package com.juick.util; +import com.juick.Message; +import com.juick.Tag; +import org.apache.commons.codec.CharEncoding; import org.apache.commons.lang3.StringUtils; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -137,4 +142,22 @@ public class MessageUtils { msg = msg.replaceAll("\n", "<br/>\n"); return msg; } + + public static String getMessageHashTags(final Message jmsg) { + StringBuilder hashtags = new StringBuilder(); + for (Tag tag : jmsg.getTags()) { + hashtags.append("#").append(tag).append(" "); + } + return hashtags.toString(); + } + + // TODO: check if it is really needed + public static String percentEncode(final String s) { + String ret = StringUtils.EMPTY; + try { + ret = URLEncoder.encode(s, CharEncoding.UTF_8).replace("+", "%20").replace("*", "%2A").replace("%7E", "~"); + } catch (UnsupportedEncodingException e) { + } + return ret; + } } |