Skip to content

Commit

Permalink
markdown adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Aug 5, 2024
1 parent cbacbb1 commit 08a98d9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 35 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def currentArchitecture = DefaultNativePlatform.getCurrentArchitecture()

dependencies {
implementation 'org.lz4:lz4-java:1.8.0'

implementation 'com.vladsch.flexmark:flexmark-all:0.64.8'
implementation 'org.jgrapht:jgrapht-core:1.5.2'
implementation 'org.jgrapht:jgrapht-ext:1.5.2'
implementation 'com.amazonaws:aws-java-sdk-bom:1.12.649'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ public CityBranch(CityNode.CachedCity cached) {
});
resortList(rssSorted);
}
for (Building building : rssSorted) {
System.out.println("rmv:|| " + building.name() + " " + rssBuildings.get(building) + " profit");
}

List<Building> allBuildings = new ArrayList<>(rssSorted);
for (Building building : Buildings.values()) {
Expand All @@ -104,9 +101,6 @@ public CityBranch(CityNode.CachedCity cached) {
if (building instanceof ServiceBuilding) allBuildings.add(building);
}
this.buildings = allBuildings.toArray(new Building[0]);
for (Building building : buildings) {
System.out.println("rmv:|| All " + building.name());
}
}

private static void resortList(List<Building> rssSorted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.util.*;
import java.util.concurrent.CompletableFuture;

import static link.locutus.discord.util.MarkupUtil.formatDiscordMarkdown;
import static link.locutus.discord.util.MarkupUtil.markdownToHTML;

public abstract class AMessageBuilder implements IMessageBuilder {
public final StringBuilder content = new StringBuilder();
public final Map<String, String> buttons = new LinkedHashMap<>();
Expand Down Expand Up @@ -194,9 +197,9 @@ public void appendJson(JsonObject json) {
for (var buttonJson : json.getAsJsonArray("buttons")) {
JsonObject buttonObj = buttonJson.getAsJsonObject();
if (buttonObj.has("cmd")) {
this.buttons.put(buttonObj.get("cmd").getAsString(), buttonObj.get("label").getAsString());
commandButton(buttonObj.get("cmd").getAsString(), buttonObj.get("label").getAsString());
} else if (buttonObj.has("href")) {
this.links.put(buttonObj.get("href").getAsString(), buttonObj.get("label").getAsString());
linkButton(buttonObj.get("href").getAsString(), buttonObj.get("label").getAsString());
}
}
}
Expand Down Expand Up @@ -251,14 +254,14 @@ private Guild getGuildOrNull() {

public String toSimpleHtml(boolean includeFiles, boolean includeButtons) {
StringBuilder html = new StringBuilder();
html.append("<p>").append(MarkupUtil.formatDiscordMarkdown(content.toString(), getGuildOrNull())).append("</p>");
html.append("<p>").append(markdownToHTML(formatDiscordMarkdown(content.toString(), getGuildOrNull()))).append("</p>");
for (MessageEmbed embed : embeds) {
String title = embed.getTitle();
String description = MarkupUtil.formatDiscordMarkdown(embed.getDescription(), getGuildOrNull());
String description = markdownToHTML(formatDiscordMarkdown(embed.getDescription(), getGuildOrNull()));
String footerText = null;
MessageEmbed.Footer footer = embed.getFooter();
if (footer != null) {
footerText = MarkupUtil.formatDiscordMarkdown(footer.getText(), getGuildOrNull());
footerText = markdownToHTML(formatDiscordMarkdown(footer.getText(), getGuildOrNull()));
}
List<MessageEmbed.Field> fields = embed.getFields();
StringBuilder embedHtml = new StringBuilder();
Expand All @@ -278,6 +281,7 @@ public String toSimpleHtml(boolean includeFiles, boolean includeButtons) {
embedHtml.append("<sub>").append(footerText).append("</sub>");
}
embedHtml.append("</div>");
html.append(embedHtml);
}


Expand All @@ -292,7 +296,7 @@ public String toSimpleHtml(boolean includeFiles, boolean includeButtons) {
String name = entry.getKey();
// use <pre>
html.append("<h3>").append(name).append("</h3>");
html.append("<pre>").append(new String(entry.getValue())).append("</pre>\n");
html.append("<pre>").append(new String(entry.getValue())).append("</pre><br />");
}
}
if (includeButtons) {
Expand Down Expand Up @@ -325,13 +329,13 @@ public String toSimpleHtml(boolean includeFiles, boolean includeButtons) {
public IMessageBuilder writeTo(IMessageBuilder output) {
if (!content.isEmpty()) output.append(content.toString());
for (MessageEmbed embed : embeds) {
embed(embed);
output.embed(embed);
}
for (Map.Entry<String, String> entry : buttons.entrySet()) {
output.commandButton(entry.getValue(), entry.getKey());
output.commandButton(entry.getKey(), entry.getValue());
}
for (Map.Entry<String, String> entry : links.entrySet()) {
output.linkButton(entry.getValue(), entry.getKey());
output.linkButton(entry.getKey(), entry.getValue());
}
for (Map.Entry<String, byte[]> entry : images.entrySet()) {
output.image(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1834,11 +1834,10 @@ public String mailCommandOutput(NationPlaceholders placeholders, ValueStore stor
if (sheet == null) {
sheet = SpreadSheet.create(db, SheetKey.MAIL_RESPONSES_SHEET);
}
if (body != null) {
GPTUtil.checkThrowModeration(body + "\n" + command);
} else {
GPTUtil.checkThrowModeration(command);
}
String checkModMsg = command;
if (subject != null) checkModMsg += subject;
if (body != null) checkModMsg += body;
GPTUtil.checkThrowModeration(checkModMsg);

List<String> header = new ArrayList<>(Arrays.asList(
"nation",
Expand Down Expand Up @@ -1893,7 +1892,7 @@ public String mailCommandOutput(NationPlaceholders placeholders, ValueStore stor

if (respType == CommandResult.SUCCESS) {
if (!bodyFormat.isEmpty()) {
bodyFormat = MarkupUtil.markdownToHTML(bodyFormat) + "<br>";
bodyFormat = bodyFormat + "\n";
}

header.set(0, MarkupUtil.sheetUrl(nation.getNation(), nation.getUrl()));
Expand Down Expand Up @@ -2069,6 +2068,7 @@ public String mailSheet(@Me GuildDB db, @Me JSONObject command, @Me IMessageIO i
msg.send();
result.add("\n- **dm**: Sent dm");
} catch (Throwable e) {
e.printStackTrace();
result.add("\n- **dm**: Failed to send dm (`" + e.getMessage() + "`)");
}
}
Expand Down
69 changes: 56 additions & 13 deletions src/main/java/link/locutus/discord/util/MarkupUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package link.locutus.discord.util;

import com.overzealous.remark.Remark;
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataSet;
import link.locutus.discord.Locutus;
import link.locutus.discord.commands.external.guild.KeyStore;
import link.locutus.discord.commands.manager.v2.command.CommandCallable;
Expand All @@ -26,11 +31,8 @@
import org.primeframework.transformer.service.BBCodeToHTMLTransformer;
import org.primeframework.transformer.service.Transformer;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -44,6 +46,25 @@ public static String markdownUrl(String name, String url) {
private static final Pattern MENTIONED_ROLE = Pattern.compile("<@&([0-9]{11,21})>");
private static final Pattern MENTIONED_CHANNEL = Pattern.compile("<#([0-9]{11,21})>");
private static final Pattern MENTIONED_USER = Pattern.compile("<@!?([0-9]{11,21})>");
private static final Pattern MENTIONED_TIMESTAMP = Pattern.compile("<t:([0-9]+)(?::[a-zA-Z])?>");

public static String formatQuotedTimestamps(String input) {
Matcher m = MENTIONED_TIMESTAMP.matcher(input);
StringBuffer sb = new StringBuffer();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss 'UTC'");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String url = "https://www.epochconverter.com/countdown?q="; // Plus the time in either millis or seconds

while (m.find()) {
long timestamp = Long.parseLong(m.group(1)) * 1000; // Convert seconds to milliseconds
Date date = new Date(timestamp);
String formattedDate = dateFormat.format(date);
String urlDate = "<a href=\"" + url + timestamp + "\">" + formattedDate + "</a>";
m.appendReplacement(sb, formattedDate);
}
m.appendTail(sb);
return sb.toString();
}

public static String formatQuotedCommands(String input) {
Matcher m = QUOTED_COMMAND.matcher(input);
Expand Down Expand Up @@ -139,6 +160,7 @@ public static String formatDiscordMarkdown(String input, Guild guild) {
input = formatQuotedCommands(input);
input = formatMentionedCommands(input);
input = formatMentionedUsers(input);
input = formatQuotedTimestamps(input);
if (guild != null) {
input = formatMentionedRoles(input, guild);
input = formatMentionedChannels(input, guild);
Expand Down Expand Up @@ -299,15 +321,36 @@ public static String transformURLIntoMarkup(String text){
return sb.toString();
}

private static final MutableDataSet MARKDOWN_TO_HTML = new MutableDataSet();
private static final Parser MARKDOWN_PARSER;
private static final HtmlRenderer MARKDOWN_RENDERER;
static {
MARKDOWN_TO_HTML.set(Parser.EXTENSIONS, Arrays.asList(StrikethroughExtension.create()));
MARKDOWN_PARSER = Parser.builder(MARKDOWN_TO_HTML).build();
MARKDOWN_RENDERER = HtmlRenderer.builder(MARKDOWN_TO_HTML).build();
}

public static String markdownToHTML(String source) {
source = source.replace("_", "\u200B\t").replace(" * ", "\u200B\r");
source = source.replaceAll("```", "`");
TextProcessor processor = BBProcessorFactory.getInstance()
.createFromResource(ConfigurationFactory.MARKDOWN_CONFIGURATION_FILE);
source = processor.process(source);
source = source.replace("\n", "<br>").replace("\u200B\r", " * ").replace("\u200B\t", "_");
source = transformURLIntoLinks(source);
return source;
source = source.replaceAll("``` ", "```\n");
Node document = MARKDOWN_PARSER.parse(source);
String html = MARKDOWN_RENDERER.render(document).trim();
if (html.startsWith("<p>") && html.endsWith("</p>")) {
int lastIndex = html.lastIndexOf("<p>");
if (lastIndex == 0) {
html = html.substring(3, html.length() - 4);
}
html = transformURLIntoLinks(html);
}
return html.replaceAll("\n", "");
//
// source = source.replace("_", "\u200B\t").replace(" * ", "\u200B\r");
// source = source.replaceAll("```", "`");
// TextProcessor processor = BBProcessorFactory.getInstance()
// .createFromResource(ConfigurationFactory.MARKDOWN_CONFIGURATION_FILE);
// source = processor.process(source);
// source = source.replace("\n", "<br>").replace("\u200B\r", " * ").replace("\u200B\t", "_");
// source = transformURLIntoLinks(source);
// return source;
}

public static String htmlToBBCode(String source) {
Expand Down

0 comments on commit 08a98d9

Please sign in to comment.