From e377357b00cdb151aa20d96a0ce7a5f1a8bbe9c9 Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Thu, 12 Jan 2023 11:46:40 +0300
Subject: Telegram: parse_mode=html
---
src/main/java/com/juick/util/MessageUtils.java | 43 ++++++++++++++--------
.../juick/util/formatters/PlainTextFormatter.java | 21 ++++-------
2 files changed, 35 insertions(+), 29 deletions(-)
(limited to 'src/main/java/com/juick/util')
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
// juick.com
@@ -186,20 +191,28 @@ public class MessageUtils {
// _underline_
// underline
- msg = msg.replaceAll(underlineRegex, "$1$2$3");
+ msg = msg.replaceAll(underlineRegex, "$1$2$3");
// /12
// /12
msg = msg.replaceAll(replyNumberRegex, "$1/$2$3");
+
// @username@mastodon.social
// @username@mastodon.social
- msg = msg.replaceAll(jidRegex, "$1@$2$3");
+ if (compatibleWithDurov) {
+ msg = msg.replaceAll(jidRegex, "$1@$2$3");
+ } else {
+ msg = msg.replaceAll(jidRegex, "$1@$2$3");
+ }
// @username
// @username
- msg = msg.replaceAll(usernameRegex, "$1@$2$3");
-
+ if (compatibleWithDurov) {
+ msg = msg.replaceAll(usernameRegex, "$1@$2$3");
+ } else {
+ msg = msg.replaceAll(usernameRegex, "$1@$2$3");
+ }
// (http://juick.com/last?page=2)
// (juick.com)
Matcher m = regexLinks2.matcher(msg);
@@ -211,11 +224,13 @@ public class MessageUtils {
m.appendTail(sb);
msg = sb.toString();
- // > citate
- msg = msg.replaceAll(citateRegex, "$1
");
- msg = msg.replaceAll("", "\n");
+ if (!compatibleWithDurov) {
+ // > citate
+ msg = msg.replaceAll(citateRegex, "$1
");
+ msg = msg.replaceAll("
", "\n");
- msg = msg.replaceAll("\n", "
\n");
+ msg = msg.replaceAll("\n", "
\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("%s", user.getUri(), user.getName());
} else {
- return String.format("[%s](https://juick.com/%s/)", user.getName(), user.getName());
+ return String.format("%s", 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();
--
cgit v1.2.3