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;
/** The template to render for the <img> tag. */
private static final Template linkTemplate = TemplateParser.parse(new StringReader("<img<%ifnull !class> 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;
}
*/
@Override
public Object format(TemplateContext templateContext, Object data, Map<String, String> 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);
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());