aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2023-02-15 23:56:05 +0300
committerGravatar Vitaly Takmazov2023-02-15 23:56:05 +0300
commit1695469b2c2d0f72ef63ad72070a464e42b2086a (patch)
treef0b9df0957b10741bd99d0f939d5cec5c6733489 /src
parent156240c66008d9e05fa4141c623b16feaf5f5350 (diff)
Parse old Pidgin urls
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/juick/util/MessageUtils.java12
-rw-r--r--src/test/java/com/juick/server/tests/ServerTests.java11
-rw-r--r--src/test/resources/message_group_reference.json1
3 files changed, 18 insertions, 6 deletions
diff --git a/src/main/java/com/juick/util/MessageUtils.java b/src/main/java/com/juick/util/MessageUtils.java
index 3651540e..a04dc24f 100644
--- a/src/main/java/com/juick/util/MessageUtils.java
+++ b/src/main/java/com/juick/util/MessageUtils.java
@@ -68,7 +68,7 @@ public class MessageUtils {
private final static String urlWithWhitespacesRegex =
urlWhiteSpacePrefix + urlRegex;
- private final static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|&lt;)((?:ht|f)tps?:\\/\\/(?:www\\.)?([^\\/\\s\\\"\\)\\!&gt;]*)\\/?(?:[^\\]\\}](?<!&gt;))*)([\\]\\}]|&gt;)");
+ private final static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[{])((?:ht|f)tps?://(?:www\\.)?([^/\\s\")!&gt;]*)/?(?:[^]}](?<!&gt;))*)([]}])");
private final static String replyNumberRegex = "((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))";
@@ -82,10 +82,10 @@ public class MessageUtils {
private final static String ridRegex = "((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))";
- private final static String textUrlRegex = "\\[([^\\]]+)\\]\\[((?:ht|f)tps?://[^\\]]+)\\]";
-
- private final static String textUrlRegex2 = "\\[([^\\]]+)\\]\\(((?:ht|f)tps?://[^\\)]+)\\)";
+ private final static String textUrlRegex = "\\[([^]]+)]\\[((?:ht|f)tps?://[^]]+)]";
+ private final static String textUrlRegex2 = "\\[([^]]+)]\\(((?:ht|f)tps?://[^)]+)\\)";
+ private final static String pidginUrlRegex = "(\\S+)\\s&lt;((?:ht|f)tps?://[^&]+)&gt;";
private final static String boldRegex = "((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))\\*([^\\*\\n<>]+)\\*((?=\\s)|(?=\\Z)|(?=\\p{Punct}))";
private final static String italicRegex = "((?<=\\s)|(?<=\\A))/([^\\/\\n<>]+)/((?=\\s)|(?=\\Z)|(?=\\p{Punct}))";
@@ -169,9 +169,11 @@ public class MessageUtils {
// [link text][http://juick.com/last?page=2]
// [link text](http://juick.com/last?page=2)
+ // www.juick.com <http://juick.com/>
// <a href="http://juick.com/last?page=2" rel="nofollow">link text</a>
msg = msg.replaceAll(textUrlRegex, "<a href=\"$2\" rel=\"nofollow\">$1</a>");
msg = msg.replaceAll(textUrlRegex2, "<a href=\"$2\" rel=\"nofollow\">$1</a>");
+ msg = msg.replaceAll(pidginUrlRegex, "<a href=\"$2\" rel=\"nofollow\">$1</a>");
// #12345
// <a href="http://juick.com/12345">#12345</a>
@@ -218,7 +220,7 @@ public class MessageUtils {
Matcher m = regexLinks2.matcher(msg);
StringBuffer sb = new StringBuffer();
while (m.find()) {
- String url = m.group(3).replace(" ", "%20").replaceAll("\\s+", StringUtils.EMPTY);
+ String url = m.group(3).replace(" ", "%20").replace("$", "%36").replaceAll("\\s+", StringUtils.EMPTY);
m.appendReplacement(sb, "$1$2<a href=\"" + url + "\" rel=\"nofollow\">$4</a>$5");
}
m.appendTail(sb);
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java
index f297ee2e..e97f4a20 100644
--- a/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/src/test/java/com/juick/server/tests/ServerTests.java
@@ -122,7 +122,6 @@ import java.nio.file.*;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
-import java.sql.Timestamp;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
@@ -234,6 +233,8 @@ public class ServerTests {
private Resource sapeOutput;
@Value("classpath:friendica_update.json")
private Resource friendicaUpdate;
+ @Value("classpath:message_group_reference.json")
+ private Resource groupReferenceMessage;
@Inject
AppleClientSecretGenerator clientSecretGenerator;
@Inject
@@ -2651,4 +2652,12 @@ public class ServerTests {
assertThat(tagService.getTag(first, true).getName(), is(first));
assertThat(tagService.getTag(second, true).getName(), is(second));
}
+ @Test
+ public void testPidginLinks() throws IOException {
+ String msgData = IOUtils.toString(groupReferenceMessage.getInputStream(), StandardCharsets.UTF_8);
+ Message msg = jsonMapper.readValue(msgData, Message.class);
+ String output = MessageUtils.formatMessage(msg.getText());
+ assertThat(output, is("К вопросу об опросах типа \"За кого бы вы проголосовали\" и \"Согласны ли вы с результатами\"...<br/>\n" +
+ "Напоминает один старый опрос: опрос на сайте <a href=\"http://www.$somesite$.ru\" rel=\"nofollow\">www.$somesite$.ru</a> показал, что 100% Россиян использует интернет."));
+ }
}
diff --git a/src/test/resources/message_group_reference.json b/src/test/resources/message_group_reference.json
new file mode 100644
index 00000000..e3c9693e
--- /dev/null
+++ b/src/test/resources/message_group_reference.json
@@ -0,0 +1 @@
+{"user":{"avatar":"https://juick.com/i/a/287-a38e60ff15cd4d0bf1a9c8dbf81b2c0e.png","uri":"","uid":287,"uname":"datacompboy"},"updated":"2011-12-08T20:52:21Z","replies":1,"to":{"uri":"","uname":"Anonymous"},"replyUri":"","mid":1662739,"body":"К вопросу об опросах типа \"За кого бы вы проголосовали\" и \"Согласны ли вы с результатами\"...\nНапоминает один старый опрос: опрос на сайте www.$somesite$.ru <http://www.$somesite$.ru> показал, что 100% Россиян использует интернет.","timestamp":"2011-12-08 17:29:51","updated_at":"2011-12-08 17:29:51","repliesby":"@alv"}