From 23d984bcc2f874e46807a9d7c3e5c036ad9701fa Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 10 Apr 2011 18:56:36 +0200 Subject: [PATCH] Add filter that turns an Image into an HTML tag. --- .../sone/template/ImageLinkFilter.java | 89 ++++++++++++++++++++++ .../net/pterodactylus/sone/web/WebInterface.java | 4 +- 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java diff --git a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java new file mode 100644 index 0000000..3d6c731 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java @@ -0,0 +1,89 @@ +/* + * Sone - ImageLinkFilter.java - Copyright © 2011 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.template; + +import java.io.StringReader; +import java.io.StringWriter; +import java.util.Map; + +import net.pterodactylus.sone.data.Image; +import net.pterodactylus.util.number.Numbers; +import net.pterodactylus.util.template.Filter; +import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.template.TemplateContextFactory; +import net.pterodactylus.util.template.TemplateParser; + +/** + * Template filter that turns an {@link Image} into an HTML <img> tag, + * using some parameters to influence parameters of the image. + * + * @author David ‘Bombe’ Roden + */ +public class ImageLinkFilter implements Filter { + + /** The template to render for the <img%gt; tag. */ + private static final Template linkTemplate = TemplateParser.parse(new StringReader(" class=\"<%class|css>\"<%/if> src=\"<%src|html>\" alt=\"<%alt|html>\" title=\"<%title|html>\" width=\"<%width|html>\" height=\"<%height|html>\" />")); + + /** The template context factory. */ + private final TemplateContextFactory templateContextFactory; + + /** + * Creates a new image link filter. + * + * @param templateContextFactory + * The template context factory + */ + public ImageLinkFilter(TemplateContextFactory templateContextFactory) { + this.templateContextFactory = templateContextFactory; + } + + /** + * {@inheritDoc} + */ + @Override + public Object format(TemplateContext templateContext, Object data, Map parameters) { + Image image = (Image) data; + 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); + + TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext(); + linkTemplateContext.set("class", imageClass); + if (image.isInserted()) { + linkTemplateContext.set("src", "/" + image.getKey()); + } else { + linkTemplateContext.set("src", "getImage.html?image=" + image.getId()); + } + int imageWidth = image.getWidth(); + int imageHeight = image.getHeight(); + double scale = 1; + if ((imageWidth > maxWidth) || (imageHeight > maxHeight)) { + scale = Math.min(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight); + } + linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5)); + linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5)); + linkTemplateContext.set("alt", image.getDescription()); + linkTemplateContext.set("title", image.getTitle()); + + StringWriter stringWriter = new StringWriter(); + linkTemplate.render(linkTemplateContext, stringWriter); + return stringWriter.toString(); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index eedba4d..5a16f8c 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -50,6 +50,7 @@ import net.pterodactylus.sone.template.CollectionAccessor; import net.pterodactylus.sone.template.CssClassNameFilter; import net.pterodactylus.sone.template.GetPagePlugin; import net.pterodactylus.sone.template.IdentityAccessor; +import net.pterodactylus.sone.template.ImageLinkFilter; import net.pterodactylus.sone.template.JavascriptFilter; import net.pterodactylus.sone.template.NotificationManagerAccessor; import net.pterodactylus.sone.template.ParserFilter; @@ -208,8 +209,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.addPlugin("getpage", new GetPagePlugin()); - templateContextFactory.addPlugin("paginate", new PaginationPlugin()); + templateContextFactory.addFilter("image-link", new ImageLinkFilter(templateContextFactory)); templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER); templateContextFactory.addProvider(new ClassPathTemplateProvider()); templateContextFactory.addTemplateObject("formPassword", formPassword); -- 2.7.4