From c513e3329a397d0ebc17fe672f41c7d9d21122c7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 7 Dec 2011 15:03:51 +0100 Subject: [PATCH] Allow the ID of an image as input for the image link filter. --- .../pterodactylus/sone/template/ImageLinkFilter.java | 18 ++++++++++++++++-- .../java/net/pterodactylus/sone/web/WebInterface.java | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java index d7b5eea..1684be7 100644 --- a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java @@ -21,6 +21,7 @@ import java.io.StringReader; import java.io.StringWriter; import java.util.Map; +import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Image; import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.object.Default; @@ -41,16 +42,22 @@ public class ImageLinkFilter implements Filter { /** The template to render for the <img> tag. */ private static final Template linkTemplate = TemplateParser.parse(new StringReader(" class=\"<%class|css>\"<%/if> src=\"<%src|html><%if forceDownload>?forcedownload=true<%/if>\" alt=\"<%alt|html>\" title=\"<%title|html>\" width=\"<%width|html>\" height=\"<%height|html>\" style=\"position: relative;<%ifnull ! top>top: <% top|html>;<%/if><%ifnull ! left>left: <% left|html>;<%/if>\"/>")); + /** The core. */ + private final Core core; + /** The template context factory. */ private final TemplateContextFactory templateContextFactory; /** * Creates a new image link filter. * + * @param core + * The core * @param templateContextFactory * The template context factory */ - public ImageLinkFilter(TemplateContextFactory templateContextFactory) { + public ImageLinkFilter(Core core, TemplateContextFactory templateContextFactory) { + this.core = core; this.templateContextFactory = templateContextFactory; } @@ -59,7 +66,14 @@ public class ImageLinkFilter implements Filter { */ @Override public Object format(TemplateContext templateContext, Object data, Map parameters) { - Image image = (Image) data; + Image image = null; + if (data instanceof String) { + image = core.getImage((String) data, false); + } else if (data instanceof Image) { + image = (Image) data; + } else { + return null; + } String imageClass = parameters.get("class"); int maxWidth = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE); int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE); diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 094d2a4..5887dc9 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -251,7 +251,7 @@ public class WebInterface implements CoreListener { templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate")); templateContextFactory.addFilter("format", new FormatFilter()); templateContextFactory.addFilter("sort", new CollectionSortFilter()); - templateContextFactory.addFilter("image-link", new ImageLinkFilter(templateContextFactory)); + templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory)); templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter()); templateContextFactory.addFilter("in", new ContainsFilter()); templateContextFactory.addFilter("unique", new UniqueElementFilter()); -- 2.7.4