Skip to content

Commit

Permalink
Fix some whitespaces not being stripped properly
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDiamondPro committed Jul 21, 2024
1 parent b5327e1 commit 34beef4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ plugins {
}

group = "dev.dediamondpro"
version = "1.2.1+localtest1"
version = "1.2.2+localtest"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion minecraft/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mod Configuration
mod_name=MineMark
mod_id=minemark
mod_version=1.2.1
mod_version=1.2.2
mod_description=Markdown rendering library
mod_license=LGPL
16 changes: 15 additions & 1 deletion src/main/java/dev/dediamondpro/minemark/MineMarkCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import dev.dediamondpro.minemark.elements.creators.TextElementCreator;
import dev.dediamondpro.minemark.elements.formatting.FormattingElement;
import dev.dediamondpro.minemark.style.Style;
import dev.dediamondpro.minemark.utils.PrefixedReader;
import org.commonmark.Extension;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
Expand All @@ -37,6 +38,7 @@
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;

/**
* Class responsible for integrating parsing, layout and rendering
Expand All @@ -45,6 +47,7 @@
* @param <R> The class passed to the rendering implementation at render time
*/
public class MineMarkCore<S extends Style, R> {
private static final Pattern ACTIVATION_PATTERN = Pattern.compile("<minemark-activator>.*?</minemark-activator>", Pattern.DOTALL);
private final Parser markdownParser;
private final HtmlRenderer htmlRenderer;
private final MineMarkHtmlParser<S, R> htmlParser;
Expand Down Expand Up @@ -81,6 +84,9 @@ protected MineMarkCore(TextElementCreator<S, R> textElement, List<ElementCreator
* @throws IOException An IOException during parsing
*/
public MineMarkElement<S, R> parse(@NotNull S style, @NotNull String markdown, @NotNull Charset charSet) throws SAXException, IOException {
// Trick the markdown renderer to activate early,
// this makes it so some problematic whitespaces are handled for us
markdown = "<minemark-activator>\n\n**MineMark-activation**\n\n</minemark-activator>" + markdown;
Node document = markdownParser.parse(markdown);
return parseDocument(style, document, charSet);
}
Expand Down Expand Up @@ -109,6 +115,9 @@ public MineMarkElement<S, R> parse(@NotNull S style, @NotNull String markdown) t
* @throws IOException An IOException during parsing
*/
public MineMarkElement<S, R> parse(@NotNull S style, @NotNull Reader markdown, @NotNull Charset charSet) throws SAXException, IOException {
// Trick the markdown renderer to activate early,
// this makes it so some problematic whitespaces are handled for us
markdown = new PrefixedReader("<minemark-activator>\n\n**MineMark-activation**\n\n</minemark-activator>", markdown);
Node document = markdownParser.parseReader(markdown);
return parseDocument(style, document, charSet);
}
Expand All @@ -127,7 +136,12 @@ public MineMarkElement<S, R> parse(@NotNull S style, @NotNull Reader markdown) t
}

private MineMarkElement<S, R> parseDocument(@NotNull S style, Node document, @NotNull Charset charSet) throws SAXException, IOException {
String html = "<minemark>\n" + htmlRenderer.render(document) + "</minemark>";
// Render the document to HTML
String html = htmlRenderer.render(document);
// Remove the markdown activation part
html = ACTIVATION_PATTERN.matcher(html).replaceFirst("");
// Prepare the HTML for parsing
html = "<minemark>\n" + html + "</minemark>";
// Acquire the lock to make sure this thread is the only one using the parser
parsingLock.lock();
try (InputStream stream = new ByteArrayInputStream(html.getBytes(charSet))) {
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/dev/dediamondpro/minemark/utils/PrefixedReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* This file is part of MineMark
* Copyright (C) 2024 DeDiamondPro
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License Version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.dediamondpro.minemark.utils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;


/**
* Utility class to add a prefix to a reader, used by MineMarkCore to trick the Markdown parser to activate early
*/
public class PrefixedReader extends Reader {
private final StringReader prefixReader;
private final Reader mainReader;
private boolean prefixDone;

public PrefixedReader(String prefix, Reader mainReader) {
this.prefixReader = new StringReader(prefix);
this.mainReader = mainReader;
this.prefixDone = false;
}

@Override
public int read(char @NotNull [] cbuf, int off, int len) throws IOException {
// Read from the prefixReader first
if (!prefixDone) {
int numRead = prefixReader.read(cbuf, off, len);
if (numRead == -1) {
prefixDone = true; // Prefix is done, switch to mainReader
} else {
return numRead;
}
}

// Now read from the mainReader
return mainReader.read(cbuf, off, len);
}

@Override
public void close() throws IOException {
prefixReader.close();
mainReader.close();
}
}

0 comments on commit 34beef4

Please sign in to comment.