X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FImageLinkFilter.java;h=fc4b803ffcfb354be9ec1b839fbedd2fe3ac4ed2;hb=c6c4802b4e389b4457adf0827efafca1cd7d1e9c;hp=d7b5eeabb772c2f0728cb4cc73120a6364350fa4;hpb=b28eb5baea51a37d9765833e0184e2a10a86819b;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java
index d7b5eea..fc4b803 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 David Roden
+ * Sone - ImageLinkFilter.java - Copyright © 2011â2019 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,63 +17,71 @@
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;
+import net.pterodactylus.sone.core.Core;
import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.util.number.Numbers;
-import net.pterodactylus.util.object.Default;
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;
+
/**
* 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 templateContextFactory
- * The template context factory
- */
- public ImageLinkFilter(TemplateContextFactory templateContextFactory) {
- this.templateContextFactory = templateContextFactory;
+ public ImageLinkFilter(Core core) {
+ this.core = core;
+ templateContextFactory.addFilter("html", new HtmlFilter());
+ templateContextFactory.addFilter("css", new CssClassNameFilter());
}
/**
* {@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);
- String mode = String.valueOf(parameters.get("mode"));
- String title = parameters.get("title");
- if ((title != null) && title.startsWith("=")) {
- title = String.valueOf(templateContext.get(title.substring(1)));
+ public Object format(TemplateContext templateContext, Object data, Map parameters) {
+ Image image = null;
+ if (data instanceof String) {
+ image = core.getImage((String) data, false);
+ } else if (data instanceof Image) {
+ image = (Image) data;
+ }
+ if (image == null) {
+ return null;
}
+ 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());
}
@@ -93,12 +101,21 @@ public class ImageLinkFilter implements Filter {
linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
}
- linkTemplateContext.set("alt", Default.forNull(title, image.getDescription()));
- linkTemplateContext.set("title", Default.forNull(title, image.getTitle()));
+ linkTemplateContext.set("alt", Optional.fromNullable(title).or(image.getDescription()));
+ linkTemplateContext.set("title", Optional.fromNullable(title).or(image.getTitle()));
StringWriter stringWriter = new StringWriter();
linkTemplate.render(linkTemplateContext, stringWriter);
return stringWriter.toString();
}
+ private Function