From 9ef168a03b75aeca0c2f7dda9ce87d4014c703a9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 16 Mar 2018 10:46:23 +0300 Subject: merge common projects --- .../com/juick/formatters/PlainTextFormatter.java | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java (limited to 'juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java') diff --git a/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java b/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java new file mode 100644 index 00000000..c599d33b --- /dev/null +++ b/juick-common/src/main/java/com/juick/formatters/PlainTextFormatter.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2008-2017, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.formatters; + +import com.juick.Message; +import com.juick.util.MessageUtils; +import org.apache.commons.lang3.StringUtils; +import org.ocpsoft.prettytime.PrettyTime; + +import java.util.Date; +import java.util.Locale; + +/** + * Created by vitalyster on 12.10.2016. + */ +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 ? markdown ? MessageUtils.escapeMarkdown(StringUtils.defaultString(jmsg.getReplyQuote())) + : jmsg.getReplyQuote() + : markdown ? MessageUtils.getMarkdownTags(jmsg) : MessageUtils.getTagsString(jmsg); + sb.append(title).append(markdown ? MessageUtils.getMarkdownUser(jmsg) : jmsg.getUser().getName()).append(":\n") + .append(subtitle).append("\n"); + if (markdown) { + sb.append(MessageUtils.escapeMarkdown(StringUtils.defaultString(jmsg.getText()))); + } else { + sb.append(StringUtils.defaultString(jmsg.getText())); + } + sb.append("\n"); + if (!markdown && StringUtils.isNotEmpty(jmsg.getAttachmentType())) { + sb.append(MessageUtils.attachmentUrl(jmsg)); + } + return sb.toString(); + } + + public static String formatPostSummary(Message m) { + int cropLength = 384; + String timeAgo = pt.format(Date.from(m.getTimestamp())); + String repliesCount = m.getReplies() == 1 ? "; 1 reply" : m.getReplies() == 0 ? "" + : String.format("; %d replies", m.getReplies()); + StringBuilder sb = new StringBuilder(); + String txt = StringUtils.defaultString(m.getText()); + String attachmentUrl = MessageUtils.attachmentUrl(m); + if (StringUtils.isNotEmpty(attachmentUrl)) { + sb.append(attachmentUrl).append("\n"); + } + if (txt.length() >= cropLength) { + sb.append(StringUtils.substring(txt, 0, cropLength)).append(" [...]"); + } else { + sb.append(txt); + } + return String.format("@%s:%s\n%s\n#%s (%s%s) %s", + m.getUser().getName(), MessageUtils.getTagsString(m), sb.toString(), formatPostNumber(m), timeAgo, repliesCount, formatUrl(m)); + } + + public static String formatUrl(com.juick.Message jmsg) { + if (jmsg.getRid() > 0) { + return String.format("https://juick.com/%d#%d", jmsg.getMid(), jmsg.getRid()); + } + return "https://juick.com/" + jmsg.getMid(); + } + + public static String formatPostNumber(com.juick.Message jmsg) { + if (jmsg.getRid() > 0) { + return String.format("%d/%d", jmsg.getMid(), jmsg.getRid()); + } + return String.format("%d", jmsg.getMid()); + } + + public static String formatTwitterCard(Message jmsg) { + return MessageUtils.getMessageHashTags(jmsg) + StringUtils.defaultString(jmsg.getText()); + } +} -- cgit v1.2.3