Skip to content

Commit

Permalink
over
Browse files Browse the repository at this point in the history
  • Loading branch information
mcchampions committed Sep 17, 2023
1 parent ff2aee9 commit 9ad6fec
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -1,54 +1,151 @@
package io.github.minecraftchampions.dodoopenjava.message;

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 java.util.AbstractMap.SimpleEntry;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;

/**
* 消息组件
*/
public class MessageComponent {
private String text;
private final String text;

private MessageComponent(String text) {
this.text = text;
}

public static MessageComponent parse(String text) {
return new MessageComponent(text);
/**
* 将Markdown转换为MessageComponent
*
* @param text Markdown
* @return MessageComponent
*/
public static MessageComponent parseMarkdown(String text) {
MutableDataSet options = new MutableDataSet();
Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder()
.build();
Node document = parser.parse(text);
String html = renderer.render(document);
html = html.substring(0,html.length()-1);
html = html.replaceAll("\\n<([^>]*)>\\n","\n<$1>");
Matcher matcher = MessageUtil.pattern.matcher(html);
while (matcher.find()) {
String tempToken = matcher.group("token");
int start = matcher.start("token");
int end = matcher.end("token");
Matcher m = MessageUtil.tokenPattern.matcher(tempToken);
if (m.find()) {
String token = m.group("token");
String attrKey = m.group("attrkey");
boolean isEnd = false;
if (token.indexOf("/") == 0) {
token = token.split("/")[1];
isEnd = true;
}
Map<String, TextComponent> map = TextComponentGroup.getHtmlReplaceMap();
if (map.containsKey(token)) {
TextComponent component = map.get(token);
String replacement = component.getKey();
token = token.replaceFirst(token, replacement);
String str = token;
if (!isEnd && attrKey != null) {
attrKey = component.getAttribute();
str = str + " " + attrKey + "=\"" + m.group("attrvalue") + "\"";
}
if (isEnd) {
str = "/" + str;
}
String tempStr = html.substring(start);
tempStr = tempStr.replaceFirst(tempToken, str);
html = html.substring(0, start) + tempStr;
}
}
}
html = html.replaceAll("</?content>", "").replaceAll("([^|])\\|([^|])", "$1&#124$2").replaceAll("\\|\\|([^|]*)\\|\\|", "<antispoiler>$1</antispoiler>").replaceAll("([^_])_([^_])", "$1&#95$2").replaceAll("__([^_]*)__", "<underline>$1</underline>").replaceAll("([^~])~([^~])", "$1&#126$2").replaceAll("~~([^~]*)~~", "<strikethrough>$1</strikethrough>");
return new MessageComponent(html);
}

public String toString() {
return text;
}

static MessageComponent parse(String miniMessage) {
return new MessageComponent(miniMessage);
}

/**
* 获取构造器
*
* @return 构造器
*/
public static MessageComponentBuilder builder() {
return new MessageComponentBuilder();
}

private static class MessageComponentBuilder {
MessageComponentBuilder() {}
private LinkedHashMap<TextComponent, Entry<String, String>> parts = new LinkedHashMap<>();
/**
* 构造器
*/
public static class MessageComponentBuilder {
MessageComponentBuilder() {
}

private final LinkedHashMap<TextComponent, Entry<String, String>> parts = new LinkedHashMap<>();

/**
* 增加组件
*
* @param component 指定组件类型
* @param str 字符
* @return 构造器
*/
public MessageComponentBuilder append(TextComponent component, String str) {
this.parts.put(component, new SimpleEntry<>(str,null));
this.parts.put(component, new SimpleEntry<>(str, null));
return this;
}

/**
* 增加文字组件
*
* @param str 字符
* @return 构造器
*/
public MessageComponentBuilder append(String str) {
this.parts.put(TextComponentGroup.contentComponent, new SimpleEntry<>(str,null));
this.parts.put(TextComponentGroup.contentComponent, new SimpleEntry<>(str, null));
return this;
}

/**
* 增加组件
*
* @param component 指定组件类型
* @param str 字符
* @param attr 属性值,目前为链接
* @return 构造器
*/
public MessageComponentBuilder append(TextComponent component, String str, String attr) {
this.parts.put(component, new SimpleEntry<>(str,attr));
this.parts.put(component, new SimpleEntry<>(str, attr));
return this;
}

/**
* 构造
*
* @return messageComponent
*/
public MessageComponent build() {
StringBuilder sb = new StringBuilder();
for (Entry<TextComponent,Entry<String,String>> entry : parts.entrySet()) {
sb.append(MessageUtil.toXml(entry));
for (Entry<TextComponent, Entry<String, String>> entry : parts.entrySet()) {
sb.append(MessageUtil.toMiniMessage(entry));
}
return MiniMessageParser.parse(sb.toString());
}
}

public static void main(String... args) {
;
}
}
Original file line number Diff line number Diff line change
@@ -1,83 +1,34 @@
package io.github.minecraftchampions.dodoopenjava.message;

import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 工具类
*/
public class MessageUtil {
private static final String START = "start";
private static final String TOKEN = "token";
private static final String INNER = "inner";
private static final String END = "end";
private static final Pattern pattern = Pattern.compile("((?<start><)(?<token>[^<>]+(:(?<inner>['\"]?([^'\"](\\\\['\"])?)+['\"]?))*)(?<end>>))+?");
public static final Pattern tokenPattern = Pattern.compile("(?<token>\\S+)(\\s(?<attr>(?<attrkey>[^=]+)=\"(?<attrvalue>[^>]*)\"))?");
public static final Pattern pattern = Pattern.compile("((?<start><)(?<token>[^<>]+(:(?<inner>['\"]?([^'\"](\\\\['\"])?)+['\"]?))*)(?<end>>))+?");

public static String mdToMiniMessage(String input) {
for (TextComponent textComponent : TextComponentGroup.getTextComponentList()) {
}
return null;
}

public static String escapeTokens(String str) {
StringBuilder sb = new StringBuilder();
Matcher matcher = pattern.matcher(str);
int lastEnd = 0;
while (matcher.find()) {
int startIndex = matcher.start();
int endIndex = matcher.end();

if (startIndex > lastEnd) {
sb.append(str, lastEnd, startIndex);
}
lastEnd = endIndex;

String start = matcher.group(START);
String token = matcher.group(TOKEN);
String inner = matcher.group(INNER);
String end = matcher.group(END);

// also escape inner
if (inner != null) {
token = token.replace(inner, escapeTokens(inner));
}

sb.append("\\").append(start).append(token).append("\\").append(end);
}

if (str.length() > lastEnd) {
sb.append(str.substring(lastEnd));
}

return sb.toString();
}

public static String stripTokens(String richMessage) {
StringBuilder sb = new StringBuilder();
Matcher matcher = pattern.matcher(richMessage);
int lastEnd = 0;
while (matcher.find()) {
int startIndex = matcher.start();
int endIndex = matcher.end();

if (startIndex > lastEnd) {
sb.append(richMessage, lastEnd, startIndex);
}
lastEnd = endIndex;
}

if (richMessage.length() > lastEnd) {
sb.append(richMessage.substring(lastEnd));
}

return sb.toString();
}

/**
* 替换特殊字符
* @param text 要替换的字符
* @return str
*/
public static String replaceXmlSpecialCharacters(String text) {
return text.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;").replaceAll("'","&apos;").replaceAll("\"","&quot;");
return text.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("'", "&apos;").replaceAll("\"", "&quot;");
}

public static String toXml(Entry<TextComponent,Entry<String,String>> entry) {
/**
* 替换为MiniMessage
*
* @param entry 第一个参数为TextComponent即对应的组件,第二个参数为Entry,里面的第一个参数为内容,第二个参数为属性(如link组件的url)
* @return Str
*/
public static String toMiniMessage(Entry<TextComponent, Entry<String, String>> entry) {
TextComponent textComponent = entry.getKey();
Entry<String,String> strE = entry.getValue();
Entry<String, String> strE = entry.getValue();
String str = strE.getKey();
String attr = strE.getValue();
if (textComponent == TextComponentGroup.contentComponent) {
Expand Down
Loading

0 comments on commit 9ad6fec

Please sign in to comment.