From: David ‘Bombe’ Roden Date: Fri, 26 Nov 2010 06:02:07 +0000 (+0100) Subject: Parse links from posts and replies and display them accordingly. X-Git-Tag: 0.3.2-RC1~26^2~5 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=74f4317a5515c53c39690c8d75f396c472e20ae9;p=Sone.git Parse links from posts and replies and display them accordingly. --- diff --git a/src/main/java/net/pterodactylus/sone/template/PostAccessor.java b/src/main/java/net/pterodactylus/sone/template/PostAccessor.java index de96780..746ed25 100644 --- a/src/main/java/net/pterodactylus/sone/template/PostAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/PostAccessor.java @@ -17,11 +17,16 @@ package net.pterodactylus.sone.template; +import java.io.IOException; +import java.io.StringReader; + import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.text.FreenetLinkParser; import net.pterodactylus.util.template.DataProvider; import net.pterodactylus.util.template.ReflectionAccessor; +import net.pterodactylus.util.template.TemplateFactory; /** * Accessor for {@link Post} objects that adds additional properties: @@ -34,6 +39,9 @@ import net.pterodactylus.util.template.ReflectionAccessor; */ public class PostAccessor extends ReflectionAccessor { + /** Parser for Freenet links. */ + private final FreenetLinkParser linkParser; + /** The core to get the replies from. */ private final Core core; @@ -42,9 +50,12 @@ public class PostAccessor extends ReflectionAccessor { * * @param core * The core to get the replies from + * @param templateFactory + * The template factory for the text parser */ - public PostAccessor(Core core) { + public PostAccessor(Core core, TemplateFactory templateFactory) { this.core = core; + linkParser = new FreenetLinkParser(templateFactory); } /** @@ -62,6 +73,13 @@ public class PostAccessor extends ReflectionAccessor { return (currentSone != null) && (currentSone.isLikedPostId(post.getId())); } else if (member.equals("new")) { return core.isNewPost(post.getId(), false); + } else if (member.equals("text")) { + String text = post.getText(); + try { + return linkParser.parse(new StringReader(text)); + } catch (IOException ioe1) { + /* ignore. */ + } } return super.get(dataProvider, object, member); } diff --git a/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java b/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java index d664697..1ec4e04 100644 --- a/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java @@ -17,12 +17,17 @@ package net.pterodactylus.sone.template; +import java.io.IOException; +import java.io.StringReader; + import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.text.FreenetLinkParser; import net.pterodactylus.util.template.Accessor; import net.pterodactylus.util.template.DataProvider; import net.pterodactylus.util.template.ReflectionAccessor; +import net.pterodactylus.util.template.TemplateFactory; /** * {@link Accessor} implementation that adds a couple of properties to @@ -32,6 +37,9 @@ import net.pterodactylus.util.template.ReflectionAccessor; */ public class ReplyAccessor extends ReflectionAccessor { + /** Parser for Freenet links. */ + private final FreenetLinkParser linkParser; + /** The core. */ private final Core core; @@ -40,9 +48,12 @@ public class ReplyAccessor extends ReflectionAccessor { * * @param core * The core + * @param templateFactory + * The template factory for the text parser */ - public ReplyAccessor(Core core) { + public ReplyAccessor(Core core, TemplateFactory templateFactory) { this.core = core; + linkParser = new FreenetLinkParser(templateFactory); } /** @@ -58,6 +69,13 @@ public class ReplyAccessor extends ReflectionAccessor { return (currentSone != null) && (currentSone.isLikedReplyId(reply.getId())); } else if (member.equals("new")) { return core.isNewReply(reply.getId(), false); + } else if (member.equals("text")) { + String text = reply.getText(); + try { + return linkParser.parse(new StringReader(text)); + } catch (IOException ioe1) { + /* ignore. */ + } } return super.get(dataProvider, object, member); } diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index f7e6d36..75b218c 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -142,8 +142,8 @@ public class WebInterface implements CoreListener { templateFactory.addAccessor(Object.class, new ReflectionAccessor()); templateFactory.addAccessor(Collection.class, new CollectionAccessor()); templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore())); - templateFactory.addAccessor(Post.class, new PostAccessor(getCore())); - templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore())); + templateFactory.addAccessor(Post.class, new PostAccessor(getCore(), templateFactory)); + templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore(), templateFactory)); templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore())); templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor()); templateFactory.addFilter("date", new DateFilter()); diff --git a/src/main/resources/templates/include/viewPost.html b/src/main/resources/templates/include/viewPost.html index 452513b..5b4525d 100644 --- a/src/main/resources/templates/include/viewPost.html +++ b/src/main/resources/templates/include/viewPost.html @@ -7,7 +7,7 @@
-
<% post.text|html>
+
<% post.text>
diff --git a/src/main/resources/templates/include/viewReply.html b/src/main/resources/templates/include/viewReply.html index d0a7842..8862cf2 100644 --- a/src/main/resources/templates/include/viewReply.html +++ b/src/main/resources/templates/include/viewReply.html @@ -7,7 +7,7 @@
-
<% reply.text|html>
+
<% reply.text>
<% reply.time|date format="MMM d, yyyy, HH:mm:ss">