Skip to content

Commit 34beef4

Browse files
committed
Fix some whitespaces not being stripped properly
1 parent b5327e1 commit 34beef4

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ plugins {
2121
}
2222

2323
group = "dev.dediamondpro"
24-
version = "1.2.1+localtest1"
24+
version = "1.2.2+localtest"
2525

2626
repositories {
2727
mavenCentral()

minecraft/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Mod Configuration
22
mod_name=MineMark
33
mod_id=minemark
4-
mod_version=1.2.1
4+
mod_version=1.2.2
55
mod_description=Markdown rendering library
66
mod_license=LGPL

src/main/java/dev/dediamondpro/minemark/MineMarkCore.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import dev.dediamondpro.minemark.elements.creators.TextElementCreator;
2323
import dev.dediamondpro.minemark.elements.formatting.FormattingElement;
2424
import dev.dediamondpro.minemark.style.Style;
25+
import dev.dediamondpro.minemark.utils.PrefixedReader;
2526
import org.commonmark.Extension;
2627
import org.commonmark.node.Node;
2728
import org.commonmark.parser.Parser;
@@ -37,6 +38,7 @@
3738
import java.nio.charset.StandardCharsets;
3839
import java.util.List;
3940
import java.util.concurrent.locks.ReentrantLock;
41+
import java.util.regex.Pattern;
4042

4143
/**
4244
* Class responsible for integrating parsing, layout and rendering
@@ -45,6 +47,7 @@
4547
* @param <R> The class passed to the rendering implementation at render time
4648
*/
4749
public class MineMarkCore<S extends Style, R> {
50+
private static final Pattern ACTIVATION_PATTERN = Pattern.compile("<minemark-activator>.*?</minemark-activator>", Pattern.DOTALL);
4851
private final Parser markdownParser;
4952
private final HtmlRenderer htmlRenderer;
5053
private final MineMarkHtmlParser<S, R> htmlParser;
@@ -81,6 +84,9 @@ protected MineMarkCore(TextElementCreator<S, R> textElement, List<ElementCreator
8184
* @throws IOException An IOException during parsing
8285
*/
8386
public MineMarkElement<S, R> parse(@NotNull S style, @NotNull String markdown, @NotNull Charset charSet) throws SAXException, IOException {
87+
// Trick the markdown renderer to activate early,
88+
// this makes it so some problematic whitespaces are handled for us
89+
markdown = "<minemark-activator>\n\n**MineMark-activation**\n\n</minemark-activator>" + markdown;
8490
Node document = markdownParser.parse(markdown);
8591
return parseDocument(style, document, charSet);
8692
}
@@ -109,6 +115,9 @@ public MineMarkElement<S, R> parse(@NotNull S style, @NotNull String markdown) t
109115
* @throws IOException An IOException during parsing
110116
*/
111117
public MineMarkElement<S, R> parse(@NotNull S style, @NotNull Reader markdown, @NotNull Charset charSet) throws SAXException, IOException {
118+
// Trick the markdown renderer to activate early,
119+
// this makes it so some problematic whitespaces are handled for us
120+
markdown = new PrefixedReader("<minemark-activator>\n\n**MineMark-activation**\n\n</minemark-activator>", markdown);
112121
Node document = markdownParser.parseReader(markdown);
113122
return parseDocument(style, document, charSet);
114123
}
@@ -127,7 +136,12 @@ public MineMarkElement<S, R> parse(@NotNull S style, @NotNull Reader markdown) t
127136
}
128137

129138
private MineMarkElement<S, R> parseDocument(@NotNull S style, Node document, @NotNull Charset charSet) throws SAXException, IOException {
130-
String html = "<minemark>\n" + htmlRenderer.render(document) + "</minemark>";
139+
// Render the document to HTML
140+
String html = htmlRenderer.render(document);
141+
// Remove the markdown activation part
142+
html = ACTIVATION_PATTERN.matcher(html).replaceFirst("");
143+
// Prepare the HTML for parsing
144+
html = "<minemark>\n" + html + "</minemark>";
131145
// Acquire the lock to make sure this thread is the only one using the parser
132146
parsingLock.lock();
133147
try (InputStream stream = new ByteArrayInputStream(html.getBytes(charSet))) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* This file is part of MineMark
3+
* Copyright (C) 2024 DeDiamondPro
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License Version 3 as published by the Free Software Foundation.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package dev.dediamondpro.minemark.utils;
19+
20+
import org.jetbrains.annotations.NotNull;
21+
22+
import java.io.IOException;
23+
import java.io.Reader;
24+
import java.io.StringReader;
25+
26+
27+
/**
28+
* Utility class to add a prefix to a reader, used by MineMarkCore to trick the Markdown parser to activate early
29+
*/
30+
public class PrefixedReader extends Reader {
31+
private final StringReader prefixReader;
32+
private final Reader mainReader;
33+
private boolean prefixDone;
34+
35+
public PrefixedReader(String prefix, Reader mainReader) {
36+
this.prefixReader = new StringReader(prefix);
37+
this.mainReader = mainReader;
38+
this.prefixDone = false;
39+
}
40+
41+
@Override
42+
public int read(char @NotNull [] cbuf, int off, int len) throws IOException {
43+
// Read from the prefixReader first
44+
if (!prefixDone) {
45+
int numRead = prefixReader.read(cbuf, off, len);
46+
if (numRead == -1) {
47+
prefixDone = true; // Prefix is done, switch to mainReader
48+
} else {
49+
return numRead;
50+
}
51+
}
52+
53+
// Now read from the mainReader
54+
return mainReader.read(cbuf, off, len);
55+
}
56+
57+
@Override
58+
public void close() throws IOException {
59+
prefixReader.close();
60+
mainReader.close();
61+
}
62+
}

0 commit comments

Comments
 (0)