X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FImageLinkFilter.java;h=85a7562bfb2b70df67e24a60eafd1ade4d0eb6b6;hp=eea912142f6dfcdd5e069669ddf9cef36efc84e8;hb=faf66247a34f64946990a985d2ea3003465969cb;hpb=d5efb086bee8f103cbe90c7a953ffbb7ff27b689 diff --git a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java index eea9121..85a7562 100644 --- a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java @@ -1,5 +1,5 @@ /* - * Sone - ImageLinkFilter.java - Copyright © 2011–2013 David Roden + * Sone - ImageLinkFilter.java - Copyright © 2011–2020 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 @@ -17,6 +17,10 @@ package net.pterodactylus.sone.template; +import static java.lang.Integer.MAX_VALUE; +import static java.lang.String.valueOf; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; + import java.io.StringReader; import java.io.StringWriter; import java.util.Map; @@ -24,42 +28,34 @@ import java.util.Map; import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Image; import net.pterodactylus.util.template.Filter; +import net.pterodactylus.util.template.HtmlFilter; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.template.TemplateContextFactory; import net.pterodactylus.util.template.TemplateParser; +import com.google.common.base.Function; import com.google.common.base.Optional; -import com.google.common.primitives.Ints; /** * 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> 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>\"/>")); + 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>\" 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; + private final TemplateContextFactory templateContextFactory = new TemplateContextFactory(); - /** - * Creates a new image link filter. - * - * @param core - * The core - * @param templateContextFactory - * The template context factory - */ - public ImageLinkFilter(Core core, TemplateContextFactory templateContextFactory) { + public ImageLinkFilter(Core core) { this.core = core; - this.templateContextFactory = templateContextFactory; + templateContextFactory.addFilter("html", new HtmlFilter()); + templateContextFactory.addFilter("css", new CssClassNameFilter()); } /** @@ -76,17 +72,16 @@ public class ImageLinkFilter implements Filter { if (image == null) { return null; } - String imageClass = String.valueOf(parameters.get("class")); - int maxWidth = Optional.fromNullable(Ints.tryParse(String.valueOf(parameters.get("max-width")))).or(Integer.MAX_VALUE); - int maxHeight = Optional.fromNullable(Ints.tryParse(String.valueOf(parameters.get("max-height")))).or(Integer.MAX_VALUE); - String mode = String.valueOf(parameters.get("mode")); - String title = String.valueOf(parameters.get("title")); + String imageClass = Optional.fromNullable(parameters.get("class")).transform(getStringValue()).orNull(); + int maxWidth = parseInt(valueOf(parameters.get("max-width")), MAX_VALUE); + int maxHeight = parseInt(valueOf(parameters.get("max-height")), MAX_VALUE); + String mode = valueOf(parameters.get("mode")); + String title = Optional.fromNullable(parameters.get("title")).transform(getStringValue()).orNull(); TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext(); linkTemplateContext.set("class", imageClass); if (image.isInserted()) { linkTemplateContext.set("src", "/" + image.getKey()); - linkTemplateContext.set("forceDownload", true); } else { linkTemplateContext.set("src", "getImage.html?image=" + image.getId()); } @@ -114,4 +109,13 @@ public class ImageLinkFilter implements Filter { return stringWriter.toString(); } + private Function getStringValue() { + return new Function() { + @Override + public String apply(Object input) { + return (input != null) ? input.toString() : null; + } + }; + } + }