diff options
author | Vitaly Takmazov | 2023-01-12 11:46:40 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-01-12 11:56:11 +0300 |
commit | e377357b00cdb151aa20d96a0ce7a5f1a8bbe9c9 (patch) | |
tree | 8e776a6705e71eb72e8ea48bb3d345bc0e96fa6c /src/main/java/com/juick/util | |
parent | 84f6e27f7e563bcb58815575546c52c944f0cd3e (diff) |
Telegram: parse_mode=html
Diffstat (limited to 'src/main/java/com/juick/util')
-rw-r--r-- | src/main/java/com/juick/util/MessageUtils.java | 43 | ||||
-rw-r--r-- | src/main/java/com/juick/util/formatters/PlainTextFormatter.java | 21 |
2 files changed, 35 insertions, 29 deletions
diff --git a/src/main/java/com/juick/util/MessageUtils.java b/src/main/java/com/juick/util/MessageUtils.java index 0ece7249..3651540e 100644 --- a/src/main/java/com/juick/util/MessageUtils.java +++ b/src/main/java/com/juick/util/MessageUtils.java @@ -149,6 +149,9 @@ public class MessageUtils { } public static String formatMessage(String msg) { + return formatMessage(msg, false); + } + public static String formatMessage(String msg, boolean compatibleWithDurov) { msg = msg.replaceAll("&", "&"); msg = msg.replaceAll("<", "<"); @@ -156,7 +159,9 @@ public class MessageUtils { // -- // — - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\-\\-?((?=\\s)|(?=\\Z))", "$1—$2"); + if (!compatibleWithDurov) { + msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\-\\-?((?=\\s)|(?=\\Z))", "$1—$2"); + } // http://juick.com/last?page=2 // <a href="http://juick.com/last?page=2" rel="nofollow">juick.com</a> @@ -186,20 +191,28 @@ public class MessageUtils { // _underline_ // <span class="u">underline</span> - msg = msg.replaceAll(underlineRegex, "$1<span class=\"u\">$2</span>$3"); + msg = msg.replaceAll(underlineRegex, "$1<u>$2</u>$3"); // /12 // <a href="#12">/12</a> msg = msg.replaceAll(replyNumberRegex, "$1<a href=\"#$2\">/$2</a>$3"); + // @username@mastodon.social // <a href="http://juick.com/mention?username=username@mastodon.social/">@username@mastodon.social</a> - msg = msg.replaceAll(jidRegex, "$1<span class=\"h-card\"><a class=\"u-url\" href=\"http://juick.com/mention?username=$2\">@$2</a></span>$3"); + if (compatibleWithDurov) { + msg = msg.replaceAll(jidRegex, "$1<a href=\"http://juick.com/mention?username=$2\">@$2</a>$3"); + } else { + msg = msg.replaceAll(jidRegex, "$1<span class=\"h-card\"><a class=\"u-url\" href=\"http://juick.com/mention?username=$2\">@$2</a></span>$3"); + } // @username // <a href="http://juick.com/username/">@username</a> - msg = msg.replaceAll(usernameRegex, "$1<span class=\"h-card\"><a class=\"u-url\" href=\"https://juick.com/$2/\">@$2</a></span>$3"); - + if (compatibleWithDurov) { + msg = msg.replaceAll(usernameRegex, "$1<a href=\"https://juick.com/$2/\">@$2</a>$3"); + } else { + msg = msg.replaceAll(usernameRegex, "$1<span class=\"h-card\"><a class=\"u-url\" href=\"https://juick.com/$2/\">@$2</a></span>$3"); + } // (http://juick.com/last?page=2) // (<a href="http://juick.com/last?page=2" rel="nofollow">juick.com</a>) Matcher m = regexLinks2.matcher(msg); @@ -211,11 +224,13 @@ public class MessageUtils { m.appendTail(sb); msg = sb.toString(); - // > citate - msg = msg.replaceAll(citateRegex, "<q>$1</q>"); - msg = msg.replaceAll("</q><q>", "\n"); + if (!compatibleWithDurov) { + // > citate + msg = msg.replaceAll(citateRegex, "<q>$1</q>"); + msg = msg.replaceAll("</q><q>", "\n"); - msg = msg.replaceAll("\n", "<br/>\n"); + msg = msg.replaceAll("\n", "<br/>\n"); + } return msg; } @@ -259,11 +274,11 @@ public class MessageUtils { .collect(Collectors.joining(", ")); } - public static String getMarkdownUser(final User user) { + public static String getUserHtmlLink(final User user, final String webDomain) { if (user.getUri().toASCIIString().length() > 0) { - return String.format("[%s](%s)", user.getName(), user.getUri()); + return String.format("<a href=\"%s\">%s</a>", user.getUri(), user.getName()); } else { - return String.format("[%s](https://juick.com/%s/)", user.getName(), user.getName()); + return String.format("<a href=\"https://%s/%s\">%s</a>", webDomain, user.getName(), user.getName()); } } @@ -276,10 +291,6 @@ public class MessageUtils { return StringUtils.defaultString(msg.getText()) .replaceAll(replyNumberRegex, String.format("$1[/$2](https://juick.com/m/%d#$2)$3", msg.getMid())); } - public static String escapeMarkdown(final String s) { - return s.replace("_", "\\_").replace("*", "\\*") - .replace("`", "\\`"); - } public static String attachmentUrl(final Message jmsg) { if (StringUtils.isEmpty(jmsg.getAttachmentType())) { return StringUtils.EMPTY; diff --git a/src/main/java/com/juick/util/formatters/PlainTextFormatter.java b/src/main/java/com/juick/util/formatters/PlainTextFormatter.java index 83feb8a6..b1b1a44a 100644 --- a/src/main/java/com/juick/util/formatters/PlainTextFormatter.java +++ b/src/main/java/com/juick/util/formatters/PlainTextFormatter.java @@ -31,25 +31,20 @@ import java.util.Locale; public class PlainTextFormatter { static PrettyTime pt = new PrettyTime(new Locale("en")); - public static String formatPost(Message jmsg) { - return formatPost(jmsg, false); + public static String formatPost(Message jmsg, String domain) { + return formatPost(jmsg, false, domain); } - public static String formatPost(Message jmsg, boolean markdown) { + public static String formatPost(Message jmsg, boolean compatibleWithDurov, String domain) { StringBuilder sb = new StringBuilder(); String title = MessageUtils.isReply(jmsg) ? "Reply by @" : MessageUtils.isPM(jmsg) ? "Private message from @" : "@"; - String subtitle = MessageUtils.isReply(jmsg) ? markdown ? MessageUtils.escapeMarkdown(StringUtils.defaultString(jmsg.getReplyQuote())) - : jmsg.getReplyQuote() - : markdown ? MessageUtils.getMessageHashTags(jmsg) : MessageUtils.getTagsString(jmsg); - sb.append(title).append(markdown ? MessageUtils.getMarkdownUser(jmsg.getUser()) : jmsg.getUser().getName()).append(":\n") + String subtitle = MessageUtils.isReply(jmsg) ? jmsg.getReplyQuote() + : compatibleWithDurov ? MessageUtils.getMessageHashTags(jmsg) : MessageUtils.getTagsString(jmsg); + sb.append(title).append(compatibleWithDurov ? MessageUtils.getUserHtmlLink(jmsg.getUser(), domain) : jmsg.getUser().getName()).append(":\n") .append(subtitle).append("\n"); - if (markdown) { - sb.append(MessageUtils.formatMarkdownText(jmsg)); - } else { - sb.append(StringUtils.defaultString(jmsg.getText())); - } + sb.append(compatibleWithDurov? MessageUtils.formatMessage(jmsg.getText(), true) : StringUtils.defaultString(jmsg.getText())); sb.append("\n"); - if (!markdown && StringUtils.isNotEmpty(jmsg.getAttachmentType())) { + if (!compatibleWithDurov && StringUtils.isNotEmpty(jmsg.getAttachmentType())) { sb.append(MessageUtils.attachmentUrl(jmsg)); } return sb.toString(); |