import java.util.Map;
import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.AlbumPart;
import net.pterodactylus.sone.text.FreenetLinkPart;
import net.pterodactylus.sone.text.LinkPart;
import net.pterodactylus.sone.text.Part;
render(writer, (SonePart) part);
} else if (part instanceof PostPart) {
render(writer, (PostPart) part);
+ } else if (part instanceof AlbumPart) {
+ render(writer, (AlbumPart) part);
} else if (part instanceof Iterable<?>) {
render(writer, (Iterable<Part>) part);
}
* The part to render
*/
private void render(Writer writer, PostPart postPart) {
- SoneTextParser parser = new SoneTextParser(core, core);
+ SoneTextParser parser = new SoneTextParser(core, core, core);
SoneTextParserContext parserContext = new SoneTextParserContext(null, postPart.getPost().getSone());
try {
Iterable<Part> parts = parser.parse(parserContext, new StringReader(postPart.getPost().getText()));
}
}
+ private void render(Writer writer, AlbumPart albumPart) {
+ Album album = albumPart.getAlbum();
+ renderLink(writer, String.format("imageBrowser.html?album=%s", album.getId()), album.getTitle(), album.getDescription(), "in-sone");
+ }
+
/**
* Renders the given link.
*
--- /dev/null
+package net.pterodactylus.sone.text;
+
+import net.pterodactylus.sone.data.Album;
+
+/**
+ * {@link Part} implementation that contains information about the linked {@link Album}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class AlbumPart implements Part {
+
+ private final Album album;
+
+ public AlbumPart(Album album) {
+ this.album = album;
+ }
+
+ public Album getAlbum() {
+ return album;
+ }
+
+ @Override
+ public String getText() {
+ return album.getTitle();
+ }
+
+}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.impl.IdOnlySone;
+import net.pterodactylus.sone.database.AlbumProvider;
import net.pterodactylus.sone.database.PostProvider;
import net.pterodactylus.sone.database.SoneProvider;
import net.pterodactylus.util.io.Closer;
HTTP("http://", false),
HTTPS("https://", false),
SONE("sone://", false),
- POST("post://", false);
+ POST("post://", false),
+ ALBUM("album://", false);
private final String scheme;
private final boolean freenetLink;
}
- /** The Sone provider. */
private final SoneProvider soneProvider;
-
- /** The post provider. */
private final PostProvider postProvider;
+ private final AlbumProvider albumProvider;
- /**
- * Creates a new freenet link parser.
- *
- * @param soneProvider
- * The Sone provider
- * @param postProvider
- * The post provider
- */
- public SoneTextParser(SoneProvider soneProvider, PostProvider postProvider) {
+ public SoneTextParser(SoneProvider soneProvider, PostProvider postProvider, AlbumProvider albumProvider) {
this.soneProvider = soneProvider;
this.postProvider = postProvider;
+ this.albumProvider = albumProvider;
}
//
line = line.substring(link.length());
continue;
}
+ if (linkType == LinkType.ALBUM) {
+ Optional<Album> album = albumProvider.getAlbum(link.substring(linkType.getScheme().length()));
+ if (album.isPresent()) {
+ parts.add(new AlbumPart(album.get()));
+ } else {
+ parts.add(new PlainTextPart(link));
+ }
+ line = line.substring(link.length());
+ continue;
+ }
if (linkType.isFreenetLink()) {
FreenetURI uri;
public WebInterface(SonePlugin sonePlugin) {
this.sonePlugin = sonePlugin;
formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
- soneTextParser = new SoneTextParser(getCore(), getCore());
+ soneTextParser = new SoneTextParser(getCore(), getCore(), getCore());
templateContextFactory = new TemplateContextFactory();
templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
import java.util.HashMap;
import java.util.Map;
+import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.impl.IdOnlySone;
+import net.pterodactylus.sone.database.AlbumProvider;
import net.pterodactylus.sone.database.PostProvider;
import net.pterodactylus.sone.database.SoneProvider;
private final SoneProvider soneProvider = new TestSoneProvider();
private final TestPostProvider postProvider = new TestPostProvider();
- private final SoneTextParser soneTextParser = new SoneTextParser(soneProvider, postProvider);
+ private final TestAlbumProvider albumProvider = new TestAlbumProvider();
+ private final SoneTextParser soneTextParser = new SoneTextParser(soneProvider, postProvider, albumProvider);
/**
* Tests basic plain-text operation of the parser.
assertThat(convertText(parts, PlainTextPart.class, PostPart.class), is("This [post|old|foo|Post about foo...] is awesome."));
}
+ @Test
+ public void linksToAlbumIsParsedCorrectly() throws IOException {
+ albumProvider.addAlbumTitle("album-id", "Super Album");
+ Iterable<Part> parts = soneTextParser.parse(null, new StringReader("This album://album-id rocks!"));
+ assertThat(convertText(parts, PlainTextPart.class, AlbumPart.class), is("This [album|album-id|Super Album] rocks!"));
+ }
+
//
// PRIVATE METHODS
//
.append('|')
.append(postPart.getPost().getText())
.append(']');
+ } else if (part instanceof AlbumPart) {
+ Album album = ((AlbumPart) part).getAlbum();
+ text.append(String.format("[album|%s|%s]", album.getId(), album.getTitle()));
}
}
return text.toString();
}
+ private static class TestAlbumProvider implements AlbumProvider {
+
+ private final Map<String, String> albumTitles = new HashMap<String, String>();
+
+ public void addAlbumTitle(String albumId, String albumTitle) {
+ albumTitles.put(albumId, albumTitle);
+ }
+
+ @Override
+ public Optional<Album> getAlbum(String albumId) {
+ if (albumTitles.containsKey(albumId)) {
+ Album album = mock(Album.class);
+ when(album.getId()).thenReturn(albumId);
+ when(album.getTitle()).thenReturn(albumTitles.get(albumId));
+ return Optional.of(album);
+ }
+ return Optional.absent();
+ }
+
+ }
+
}