--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ImageLinkFilter implements Filter {
+
+ /** The template to render for the <img%gt; tag. */
+ private static final Template linkTemplate = TemplateParser.parse(new StringReader("<img<%ifnull !class> 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<String, String> 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();
+ }
+
+}
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;
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);