From 94b5e7b4c85a4bde4848bf3d85c1adc7cd73acc6 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 9 Feb 2020 20:02:20 +0100 Subject: Lafargue: Implement basic viewer. Change-Id: If24f58aa069a14139454708d02ac40109c2181ef --- .../eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java | 1 + .../benki/bookmarks/MarkdownConverter.java | 46 ------------- .../mulkcms2/benki/lazychat/LazychatMessage.java | 7 ++ .../mulkcms2/benki/lazychat/LazychatResource.java | 78 ++++++++++++++++++++++ .../common/markdown/MarkdownConverter.java | 46 +++++++++++++ 5 files changed, 132 insertions(+), 46 deletions(-) delete mode 100644 src/main/java/eu/mulk/mulkcms2/benki/bookmarks/MarkdownConverter.java create mode 100644 src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatResource.java create mode 100644 src/main/java/eu/mulk/mulkcms2/common/markdown/MarkdownConverter.java (limited to 'src/main/java/eu') diff --git a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java index 6bbd3a6..503f551 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/Bookmark.java @@ -10,6 +10,7 @@ import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.Table; import javax.persistence.Transient; +import eu.mulk.mulkcms2.common.markdown.MarkdownConverter; @Entity @Table(name = "bookmarks", schema = "benki") diff --git a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/MarkdownConverter.java b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/MarkdownConverter.java deleted file mode 100644 index 1fae62c..0000000 --- a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/MarkdownConverter.java +++ /dev/null @@ -1,46 +0,0 @@ -package eu.mulk.mulkcms2.benki.bookmarks; - -import com.vladsch.flexmark.ext.abbreviation.AbbreviationExtension; -import com.vladsch.flexmark.ext.autolink.AutolinkExtension; -import com.vladsch.flexmark.ext.definition.DefinitionExtension; -import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension; -import com.vladsch.flexmark.ext.tables.TablesExtension; -import com.vladsch.flexmark.ext.typographic.TypographicExtension; -import com.vladsch.flexmark.html.HtmlRenderer; -import com.vladsch.flexmark.parser.Parser; -import com.vladsch.flexmark.util.data.MutableDataSet; -import java.util.Arrays; -import javax.enterprise.context.ApplicationScoped; - -@ApplicationScoped -public class MarkdownConverter { - - private final Parser parser; - private final HtmlRenderer renderer; - - MarkdownConverter() { - var options = new MutableDataSet(); - options.set( - Parser.EXTENSIONS, - Arrays.asList( - TablesExtension.create(), - StrikethroughExtension.create(), - AbbreviationExtension.create(), - AutolinkExtension.create(), - DefinitionExtension.create(), - TypographicExtension.create())); - options.set(Parser.FENCED_CODE_BLOCK_PARSER, true); - options.set(HtmlRenderer.SOFT_BREAK, "\n"); - options.set(HtmlRenderer.HARD_BREAK, "
\n"); - options.set(TypographicExtension.ENABLE_SMARTS, true); - options.set(TypographicExtension.ENABLE_QUOTES, true); - - this.parser = Parser.builder(options).build(); - this.renderer = HtmlRenderer.builder(options).build(); - } - - public String htmlify(String markdown) { - var parsedDocument = parser.parse(markdown); - return renderer.render(parsedDocument); - } -} diff --git a/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessage.java b/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessage.java index de1e0cc..0afc294 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessage.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatMessage.java @@ -1,5 +1,6 @@ package eu.mulk.mulkcms2.benki.lazychat; +import eu.mulk.mulkcms2.common.markdown.MarkdownConverter; import eu.mulk.mulkcms2.benki.generic.Post; import java.util.Collection; import javax.persistence.Column; @@ -7,6 +8,7 @@ import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.OneToMany; import javax.persistence.Table; +import javax.persistence.Transient; @Entity @Table(name = "lazychat_messages", schema = "benki") @@ -20,4 +22,9 @@ public class LazychatMessage extends Post { @OneToMany(mappedBy = "referrer", fetch = FetchType.LAZY) public Collection references; + + @Transient + public String getContentHtml() { + return new MarkdownConverter().htmlify(content); + } } diff --git a/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatResource.java b/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatResource.java new file mode 100644 index 0000000..8be3d4a --- /dev/null +++ b/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatResource.java @@ -0,0 +1,78 @@ +package eu.mulk.mulkcms2.benki.lazychat; + +import static javax.ws.rs.core.MediaType.TEXT_HTML; + +import eu.mulk.mulkcms2.benki.accesscontrol.Role; +import eu.mulk.mulkcms2.benki.bookmarks.Bookmark; +import eu.mulk.mulkcms2.benki.users.User; +import io.quarkus.panache.common.Sort; +import io.quarkus.qute.Template; +import io.quarkus.qute.TemplateExtension; +import io.quarkus.qute.TemplateInstance; +import io.quarkus.qute.api.ResourcePath; +import io.quarkus.security.identity.SecurityIdentity; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.time.temporal.TemporalAccessor; +import java.util.List; +import javax.inject.Inject; +import javax.json.spi.JsonProvider; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import org.jboss.logging.Logger; + +@Path("/lazychat") +public class LazychatResource { + + private static Logger log = Logger.getLogger(LazychatResource.class); + + private static DateTimeFormatter htmlDateFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; + + private static DateTimeFormatter humanDateFormatter = + DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT); + + private static JsonProvider jsonProvider = JsonProvider.provider(); + + @ResourcePath("benki/lazychat/lazychatList.html") + @Inject + Template lazychatList; + + @Inject SecurityIdentity identity; + + @GET + @Produces(TEXT_HTML) + public TemplateInstance getPage() { + List lazychatMessages; + if (identity.isAnonymous()) { + Role world = Role.find("from Role r join r.tags tag where tag = 'world'").singleResult(); + lazychatMessages = + Bookmark.find( + "select bm from LazychatMessage lm join lm.targets target left join fetch lm.owner where target = ?1", + Sort.by("date").descending(), + world) + .list(); + } else { + var userName = identity.getPrincipal().getName(); + User user = + User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult(); + lazychatMessages = + Bookmark.find( + "select lm from BenkiUser u inner join u.visibleLazychatMessages lm left join fetch lm.owner where u.id = ?1", + Sort.by("date").descending(), + user.id) + .list(); + } + return lazychatList.data("lazychatMessages", lazychatMessages); + } + + @TemplateExtension + static String humanDateTime(TemporalAccessor x) { + return humanDateFormatter.format(x); + } + + @TemplateExtension + static String htmlDateTime(TemporalAccessor x) { + return htmlDateFormatter.format(x); + } +} diff --git a/src/main/java/eu/mulk/mulkcms2/common/markdown/MarkdownConverter.java b/src/main/java/eu/mulk/mulkcms2/common/markdown/MarkdownConverter.java new file mode 100644 index 0000000..68f7a18 --- /dev/null +++ b/src/main/java/eu/mulk/mulkcms2/common/markdown/MarkdownConverter.java @@ -0,0 +1,46 @@ +package eu.mulk.mulkcms2.common.markdown; + +import com.vladsch.flexmark.ext.abbreviation.AbbreviationExtension; +import com.vladsch.flexmark.ext.autolink.AutolinkExtension; +import com.vladsch.flexmark.ext.definition.DefinitionExtension; +import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension; +import com.vladsch.flexmark.ext.tables.TablesExtension; +import com.vladsch.flexmark.ext.typographic.TypographicExtension; +import com.vladsch.flexmark.html.HtmlRenderer; +import com.vladsch.flexmark.parser.Parser; +import com.vladsch.flexmark.util.data.MutableDataSet; +import java.util.Arrays; +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class MarkdownConverter { + + private final Parser parser; + private final HtmlRenderer renderer; + + public MarkdownConverter() { + var options = new MutableDataSet(); + options.set( + Parser.EXTENSIONS, + Arrays.asList( + TablesExtension.create(), + StrikethroughExtension.create(), + AbbreviationExtension.create(), + AutolinkExtension.create(), + DefinitionExtension.create(), + TypographicExtension.create())); + options.set(Parser.FENCED_CODE_BLOCK_PARSER, true); + options.set(HtmlRenderer.SOFT_BREAK, "\n"); + options.set(HtmlRenderer.HARD_BREAK, "
\n"); + options.set(TypographicExtension.ENABLE_SMARTS, true); + options.set(TypographicExtension.ENABLE_QUOTES, true); + + this.parser = Parser.builder(options).build(); + this.renderer = HtmlRenderer.builder(options).build(); + } + + public String htmlify(String markdown) { + var parsedDocument = parser.parse(markdown); + return renderer.render(parsedDocument); + } +} -- cgit v1.2.3