mirror of
https://github.com/HangarMC/Hangar.git
synced 2024-11-27 06:01:08 +08:00
feat: improve bbcode converter by moving headlines to front and handling wrapped bold tags (closes #1101)
This commit is contained in:
parent
f05b25e111
commit
b4e6d1dfc3
@ -2,6 +2,8 @@ package io.papermc.hangar.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class BBCodeConverter {
|
||||
@ -13,6 +15,11 @@ public class BBCodeConverter {
|
||||
private static final char TAG_SUFFIX = ']';
|
||||
private static final char ARG_PREFIX = '=';
|
||||
|
||||
// https://regex101.com/r/MWDUfk/1
|
||||
private static final Pattern headlinePattern = Pattern.compile("([^#]+)(#+ )(.+)");
|
||||
// https://regex101.com/r/s4Nxgc/1
|
||||
private static final Pattern boldNewlinePattern = Pattern.compile("(\\*\\*)(.*?)(\\n)?(.*?)(\\*\\*)");
|
||||
|
||||
private String currentTag;
|
||||
private String currentArg;
|
||||
private String currentContent;
|
||||
@ -202,7 +209,7 @@ public class BBCodeConverter {
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
return cleanup(s);
|
||||
}
|
||||
|
||||
private @Nullable String removeTrailingWhitespaces(String s) {
|
||||
@ -333,4 +340,34 @@ public class BBCodeConverter {
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
private static String cleanup( String input) {
|
||||
final String[] lines = input.split("\n");
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
final String line = lines[i];
|
||||
// move headlines to the start
|
||||
if (!line.startsWith("#") && line.contains("#")) {
|
||||
lines[i] = headlinePattern.matcher(line).replaceAll("$2$1$3");
|
||||
}
|
||||
}
|
||||
input = String.join("\n", lines);
|
||||
|
||||
// make bold wrap around newlines correctly
|
||||
input = boldNewlinePattern.matcher(input).replaceAll((matchResult) -> {
|
||||
// $1$2$1$3$5$4$5
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (StringUtils.isNotBlank(matchResult.group(2))) {
|
||||
sb.append(matchResult.group(1)).append(matchResult.group(2)).append(matchResult.group(1));
|
||||
}
|
||||
if (matchResult.group(3) != null) {
|
||||
sb.append(matchResult.group(3));
|
||||
}
|
||||
if (StringUtils.isNotBlank(matchResult.group(4))) {
|
||||
sb.append(matchResult.group(5)).append(matchResult.group(4)).append(matchResult.group(5));
|
||||
}
|
||||
return sb.toString();
|
||||
});
|
||||
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ class BBCodeConverterTest {
|
||||
@Test
|
||||
void testList() {
|
||||
final String result = this.converter.convertToMarkdown("[list]\n[*]Element 1\n[*]Element 2\n[/list]");
|
||||
Assertions.assertEquals("\n* Element 1\n* Element 2\n", result);
|
||||
Assertions.assertEquals("\n* Element 1\n* Element 2", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -174,4 +174,38 @@ class BBCodeConverterTest {
|
||||
final String result = this.converter.convertToMarkdown(input);
|
||||
Assertions.assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveHeadingToFront() {
|
||||
final String input = "[B][SIZE=5]Dum[/SIZE][/B]";
|
||||
final String expected = "### **Dum**";
|
||||
final String result = this.converter.convertToMarkdown(input);
|
||||
Assertions.assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWrapBoldAroundNewline() {
|
||||
final String input = "[B]Dum\nDum[/B]";
|
||||
final String expected = "**Dum**\n**Dum**";
|
||||
final String result = this.converter.convertToMarkdown(input);
|
||||
Assertions.assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBoldList() {
|
||||
final String input = """
|
||||
[LIST]
|
||||
[*][B]bold[/B] not
|
||||
[*][B]bold[/B] not
|
||||
[*][B]bold[/B] not
|
||||
[/LIST]
|
||||
""";
|
||||
final String expected = """
|
||||
|
||||
* **bold** not
|
||||
* **bold** not
|
||||
* **bold** not""";
|
||||
final String result = this.converter.convertToMarkdown(input);
|
||||
Assertions.assertEquals(expected, result);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
**
|
||||
This plugin lets you activate a maintenance mode on your BungeeCord as well as Spigot server, which will prevent players without a permission to join the server.**
|
||||
|
||||
**This plugin lets you activate a maintenance mode on your BungeeCord as well as Spigot server, which will prevent players without a permission to join the server.**
|
||||
**Whatever version you want to use, just place the plugin inside your plugin folder of your server running Spigot or BungeeCord!**
|
||||
|
||||
|
||||
@ -27,7 +27,7 @@ IMPORTANT: If using the plugin on a Spigot server, the plugin **[ProtocolLib](ht
|
||||
|
||||
<center></center>
|
||||
|
||||
<center>### Did you find a bug? Use the [**issue tracker**](https://github.com/kennytv/maintenance/issues)
|
||||
### <center>Did you find a bug? Use the [**issue tracker**](https://github.com/kennytv/maintenance/issues)
|
||||
Other questions or problems? Click the wonderful icon below ✨
|
||||
|
||||
[](https://discord.gg/vgcuzhq)
|
||||
|
Loading…
Reference in New Issue
Block a user