diff options
author | Vitaly Takmazov | 2016-11-16 12:10:32 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-11-16 16:21:43 +0300 |
commit | b11a395c46aa92179a4a6019d2ebef15877b3946 (patch) | |
tree | 7ab510a4d8a134c17e80795b76b7e153e74b4536 /juick-rss/src/main/java/com/juick/rss | |
parent | 96dc5157a2a0979dbe2511366cb3bec6d16c4e7e (diff) |
formatMessage moved to core, remove copy-paste from juick-rss
Diffstat (limited to 'juick-rss/src/main/java/com/juick/rss')
-rw-r--r-- | juick-rss/src/main/java/com/juick/rss/Main.java | 80 |
1 files changed, 6 insertions, 74 deletions
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("]]></title>"); - out.println("<description><![CDATA[" + formatMessage(msg.getText()) + "]]></description>"); + out.println("<description><![CDATA[" + MessageUtils.formatMessage(msg.getText()) + "]]></description>"); out.println("<pubDate>" + sdfRSS.format(msg.getDate()) + "</pubDate>"); out.println("<comments>http://juick.com/" + msg.getUser().getName() + "/" + msg.getMid() + "</comments>"); if (!msg.getTags().isEmpty()) { for (int n = 0; n < msg.getTags().size(); n++) { - out.println("<category>" + escapeHtml(msg.getTags().get(n).getName()) + "</category>"); + out.println("<category>" + StringEscapeUtils.escapeHtml4(msg.getTags().get(n).getName()) + "</category>"); } } 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("<item>"); out.println("<ya:post>http://juick.com/" + r.muname + "/" + r.mid + "</ya:post>"); out.println("<link>http://juick.com/" + r.muname + "/" + r.mid + "#" + r.rid + "</link>"); out.println("<guid>http://juick.com/" + r.muname + "/" + r.mid + "#" + r.rid + "</guid>"); out.println("<author>http://juick.com/" + r.uname + "/</author>"); out.println("<title>@" + r.uname + ":</title>"); - out.println("<description><![CDATA[" + formatMessage(r.description) + "]]></description>"); + out.println("<description><![CDATA[" + MessageUtils.formatMessage(r.description) + "]]></description>"); out.println("<pubDate>" + sdfRSS.format(r.pubDate) + "</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 escapeHtml(String input) { - return input.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">"); - } - - public static String formatMessage(String msg) { - msg = escapeHtml(msg); - - // -- - // — - 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> - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\s\\n\\\"]*)", "$1<a href=\"$2\" rel=\"nofollow\">$3</a>"); - - // #12345 - // <a href="http://juick.com/12345">#12345</a> - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1<a href=\"http://juick.com/$2\">#$2</a>$3"); - - // #12345/65 - // <a href="http://juick.com/12345#65">#12345/65</a> - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"http://juick.com/$2#$3\">#$2/$3</a>$4"); - - // *bold* - // <b>bold</b> - msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))\\*([^\\*\\n<>]+)\\*((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<b>$2</b>$3"); - - // /italic/ - // <i>italic</i> - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))/([^\\/\\n<>]+)/((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<i>$2</i>$3"); - - // _underline_ - // <span class="u">underline</span> - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))_([^\\_\\n<>]+)_((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<span class=\"u\">$2</span>$3"); - - // /12 - // <a href="#12">/12</a> - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"#$2\">/$2</a>$3"); - - // @username@jabber.org - // <a href="http://juick.com/username@jabber.org/">@username@jabber.org</a> - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"http://juick.com/$2/\">@$2</a>$3"); - - // @username - // <a href="http://juick.com/username/">@username</a> - msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"http://juick.com/$2/\">@$2</a>$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); - StringBuffer sb = new StringBuffer(); - while (m.find()) { - String url = m.group(3).replace(" ", "%20").replaceAll("\\s+", ""); - m.appendReplacement(sb, "$1$2<a href=\"" + url + "\" rel=\"nofollow\">$4</a>$5"); - } - m.appendTail(sb); - msg = sb.toString(); - - // > citate - msg = msg.replaceAll("(?:(?<=\\n)|(?<=\\A))> *(.*)?(\\n|(?=\\Z))", "<blockquote>$1</blockquote>"); - msg = msg.replaceAll("</blockquote><blockquote>", "\n"); - - msg = msg.replaceAll("\n", "<br/>\n"); - return msg; - } } |