From b11a395c46aa92179a4a6019d2ebef15877b3946 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 16 Nov 2016 12:10:32 +0300 Subject: formatMessage moved to core, remove copy-paste from juick-rss --- juick-rss/src/main/java/com/juick/rss/Main.java | 80 ++----------------------- 1 file changed, 6 insertions(+), 74 deletions(-) (limited to 'juick-rss') diff --git a/juick-rss/src/main/java/com/juick/rss/Main.java b/juick-rss/src/main/java/com/juick/rss/Main.java index bf8348fb..029057e3 100644 --- a/juick-rss/src/main/java/com/juick/rss/Main.java +++ b/juick-rss/src/main/java/com/juick/rss/Main.java @@ -20,7 +20,9 @@ package com.juick.rss; import com.juick.Message; import com.juick.server.MessagesQueries; import com.juick.server.UserQueries; +import com.juick.util.MessageUtils; import org.apache.commons.dbcp2.BasicDataSource; +import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.jdbc.core.JdbcTemplate; @@ -35,8 +37,6 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Properties; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * @author ugnich @@ -136,14 +136,14 @@ public class Main extends HttpServlet { } } out.println("]]>"); - out.println(""); + out.println(""); out.println("" + sdfRSS.format(msg.getDate()) + ""); out.println("http://juick.com/" + msg.getUser().getName() + "/" + msg.getMid() + ""); if (!msg.getTags().isEmpty()) { for (int n = 0; n < msg.getTags().size(); n++) { - out.println("" + escapeHtml(msg.getTags().get(n).getName()) + ""); + out.println("" + StringEscapeUtils.escapeHtml4(msg.getTags().get(n).getName()) + ""); } } if (msg.AttachmentType != null) { @@ -201,14 +201,14 @@ public class Main extends HttpServlet { reply.pubDate = rs.getTimestamp(6); reply.attachmentType = rs.getString(7); return reply; - }, -hours).stream().forEach(r -> { + }, -hours).forEach(r -> { out.println(""); out.println("http://juick.com/" + r.muname + "/" + r.mid + ""); out.println("http://juick.com/" + r.muname + "/" + r.mid + "#" + r.rid + ""); out.println("http://juick.com/" + r.muname + "/" + r.mid + "#" + r.rid + ""); out.println("http://juick.com/" + r.uname + "/"); out.println("@" + r.uname + ":"); - out.println(""); + out.println(""); out.println("" + sdfRSS.format(r.pubDate) + ""); String attachment = r.attachmentType; if (attachment != null && !attachment.isEmpty()) { @@ -230,72 +230,4 @@ public class Main extends HttpServlet { return sql.queryForList("SELECT message_id FROM messages WHERE messages.ts>TIMESTAMPADD(HOUR,?,NOW())", Integer.class, -hours); } - - private static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|<)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?", ">"); - } - - public static String formatMessage(String msg) { - msg = escapeHtml(msg); - - // -- - // — - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\-\\-?((?=\\s)|(?=\\Z))", "$1—$2"); - - // http://juick.com/last?page=2 - // juick.com - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\s\\n\\\"]*)", "$1$3"); - - // #12345 - // #12345 - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1#$2$3"); - - // #12345/65 - // #12345/65 - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1#$2/$3$4"); - - // *bold* - // bold - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))\\*([^\\*\\n<>]+)\\*((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); - - // /italic/ - // italic - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))/([^\\/\\n<>]+)/((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); - - // _underline_ - // underline - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))_([^\\_\\n<>]+)_((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); - - // /12 - // /12 - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1/$2$3"); - - // @username@jabber.org - // @username@jabber.org - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3"); - - // @username - // @username - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3"); - - // (http://juick.com/last?page=2) - // (juick.com) - Matcher m = regexLinks2.matcher(msg); - StringBuffer sb = new StringBuffer(); - while (m.find()) { - String url = m.group(3).replace(" ", "%20").replaceAll("\\s+", ""); - m.appendReplacement(sb, "$1$2$4$5"); - } - m.appendTail(sb); - msg = sb.toString(); - - // > citate - msg = msg.replaceAll("(?:(?<=\\n)|(?<=\\A))> *(.*)?(\\n|(?=\\Z))", "
$1
"); - msg = msg.replaceAll("
", "\n"); - - msg = msg.replaceAll("\n", "
\n"); - return msg; - } } -- cgit v1.2.3