From a0b5a0c84691daa9d3959d741a099ec28e9a0c66 Mon Sep 17 00:00:00 2001 From: dkovacevic Date: Wed, 26 Jun 2019 16:30:39 +0200 Subject: [PATCH] Using commonmark --- pom.xml | 10 +++++ .../com/wire/bots/recording/Collector.java | 45 ++++++++++--------- .../resources/templates/conversation.html | 2 +- .../bots/recording/MessageTemplateTest.java | 19 +++++--- 4 files changed, 47 insertions(+), 29 deletions(-) diff --git a/pom.xml b/pom.xml index 5e1768e..ba7ab83 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,16 @@ compiler 0.9.5 + + com.atlassian.commonmark + commonmark + 0.12.1 + + + com.atlassian.commonmark + commonmark-ext-autolink + 0.12.1 + org.jsoup jsoup diff --git a/src/main/java/com/wire/bots/recording/Collector.java b/src/main/java/com/wire/bots/recording/Collector.java index 072edef..f64bf4a 100644 --- a/src/main/java/com/wire/bots/recording/Collector.java +++ b/src/main/java/com/wire/bots/recording/Collector.java @@ -6,14 +6,17 @@ import com.wire.bots.recording.model.*; import com.wire.bots.sdk.WireClient; import com.wire.bots.sdk.tools.Logger; +import org.commonmark.Extension; +import org.commonmark.ext.autolink.AutolinkExtension; +import org.commonmark.node.Node; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.HtmlRenderer; import javax.annotation.Nullable; import java.io.*; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.LinkedList; -import java.util.UUID; +import java.util.*; class Collector { private static MustacheFactory mf = new DefaultMustacheFactory(); @@ -50,7 +53,7 @@ void add(DBRecord record) { private Message newMessage(DBRecord record) { Message message = new Message(); if (record.mimeType.equalsIgnoreCase("txt")) { - message.text = record.text; + message.text = render(record.text); } message.time = toTime(record.timestamp); @@ -63,23 +66,6 @@ private Message newMessage(DBRecord record) { return message; } - private String replaceUrls(String text) { - String ret = text; - int index = text.indexOf("http"); - while (index != -1) { - int end = text.indexOf(" ", index); - if (end == -1) - end = text.length(); - - String url = text.substring(index, end); - // String href = String.format("%s", url, url); - String href = String.format("[%s](%s)", url, url); - ret = ret.replace(url, href); - index = text.indexOf("http", index + 1); - } - return ret; - } - private Day newDay(DBRecord record, Sender sender) { Day day = new Day(); day.date = toDate(record.timestamp); @@ -195,4 +181,21 @@ private String execute(Object model) throws IOException { return sw.toString(); } } + + private String render(String text) { + List extensions = Collections.singletonList(AutolinkExtension.create()); + + Parser parser = Parser + .builder() + .extensions(extensions) + .build(); + + Node document = parser.parse(text); + HtmlRenderer renderer = HtmlRenderer + .builder() + .escapeHtml(true) + .extensions(extensions) + .build(); + return renderer.render(document); + } } diff --git a/src/main/resources/templates/conversation.html b/src/main/resources/templates/conversation.html index 7747f9a..eb7072c 100644 --- a/src/main/resources/templates/conversation.html +++ b/src/main/resources/templates/conversation.html @@ -102,7 +102,7 @@

{{ title }}

{{#text}} -

{{ text }}

+ {{{ text }}} {{/text}} {{#image}} diff --git a/src/test/java/com/wire/bots/recording/MessageTemplateTest.java b/src/test/java/com/wire/bots/recording/MessageTemplateTest.java index 8b501c8..8bc9eb8 100644 --- a/src/test/java/com/wire/bots/recording/MessageTemplateTest.java +++ b/src/test/java/com/wire/bots/recording/MessageTemplateTest.java @@ -73,13 +73,13 @@ public void templateTest() throws Exception { Collector collector = new Collector(); collector.add(newTxtRecord(dejan, DEJAN, thursday, "1")); - collector.add(newTxtRecord(lipis, LIPIS, thursday, "2")); + collector.add(newTxtRecord(lipis, LIPIS, thursday, "")); collector.add(newTxtRecord(dejan, DEJAN, thursday, "3")); collector.add(newTxtRecord(dejan, DEJAN, thursday, "4")); collector.add(newTxtRecord(lipis, LIPIS, thursday, "5 👍")); - collector.add(newTxtRecord(lipis, LIPIS, thursday, "😃Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed " + + collector.add(newTxtRecord(lipis, LIPIS, thursday, "😃Lorem ipsum **dolor** sit amet, consectetur adipiscing elit, sed " + "do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam," + - " quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " + + " quis nostrud exercitation ullamco _laboris_ nisi ut aliquip ex ea commodo consequat. " + "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum")); collector.add(newImageRecord(dejan, DEJAN, friday, "SP", "image/jpeg")); collector.add(newTxtRecord(dejan, DEJAN, friday, "7")); @@ -88,17 +88,22 @@ public void templateTest() throws Exception { collector.add(newImageRecord(lipis, LIPIS, saturday, "small", "image/png")); collector.add(newTxtRecord(dejan, DEJAN, saturday, "9")); collector.add(newTxtRecord(dejan, DEJAN, saturday, "10")); - collector.add(newTxtRecord(lipis, LIPIS, saturday, "11")); + collector.add(newTxtRecord(lipis, LIPIS, saturday, "```collector.add(newImageRecord(dejan, DEJAN, friday," + + " \"SP\", \"image/jpeg\"));\n" + + " collector.add(newTxtRecord(dejan, DEJAN, friday, \"7\"));\n" + + " collector.add(newTxtRecord(lipis, LIPIS, saturday, \"8\"));\n" + + " collector.add(newImageRecord(lipis, LIPIS, saturday, \"ognjiste2\", \"image/png\"));\n" + + " collector.add(newImageRecord(lipis, LIPIS, saturday, \"small\", \"image/png\")); ```")); collector.add(newTxtRecord(dejan, DEJAN, saturday, "12")); collector.add(newTxtRecord(lipis, LIPIS, saturday, "13")); collector.add(newImageRecord(dejan, DEJAN, saturday, "ognjiste", "image/png")); collector.add(newTxtRecord(lipis, LIPIS, saturday, "14")); collector.add(newTxtRecord(lipis, LIPIS, saturday, "15")); - collector.add(newTxtRecord(dejan, DEJAN, saturday, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed " + + collector.add(newTxtRecord(dejan, DEJAN, saturday, "Lorem ipsum **dolor** sit amet, consectetur adipiscing elit, sed " + "do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam," + " quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " + "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " + - "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est" + + "Excepteur sint occaecat cupidatat non _proident_, sunt in culpa qui officia deserunt mollit anim id est" + " laborum.")); collector.add(newTxtRecord(lipis, LIPIS, saturday, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed " + "do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam," + @@ -107,7 +112,7 @@ public void templateTest() throws Exception { "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est" + " laborum.")); collector.add(newImageRecord(dejan, DEJAN, saturday, "Praha", "image/jpeg")); - collector.add(newTxtRecord(dejan, DEJAN, saturday, "This is some url https://google.com")); + collector.add(newTxtRecord(dejan, DEJAN, saturday, "This is some url [google](https://google.com)")); collector.add(newTxtRecord(dejan, DEJAN, saturday, "https://google.com")); collector.add(newTxtRecord(dejan, DEJAN, saturday, "This is some url https://google.com and some text")); collector.add(newTxtRecord(dejan, DEJAN, saturday, "These two urls https://google.com https://wire.com"));