Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Using commonmark #9

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
<artifactId>compiler</artifactId>
<version>0.9.5</version>
</dependency>
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.12.1</version>
</dependency>
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark-ext-autolink</artifactId>
<version>0.12.1</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
Expand Down
45 changes: 24 additions & 21 deletions src/main/java/com/wire/bots/recording/Collector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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("<a href=\"%s\">%s</a>", 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);
Expand Down Expand Up @@ -195,4 +181,21 @@ private String execute(Object model) throws IOException {
return sw.toString();
}
}

private String render(String text) {
List<Extension> 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);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/conversation.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ <h1>{{ title }}</h1>
<tr>
<td class="message">
{{#text}}
<p>{{ text }}</p>
{{{ text }}}
{{/text}}
{{#image}}
<img class="image" src="{{ image }}" />
Expand Down
19 changes: 12 additions & 7 deletions src/test/java/com/wire/bots/recording/MessageTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<head>"));
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"));
Expand All @@ -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," +
Expand All @@ -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"));
Expand Down