Skip to content

Commit

Permalink
allow specifying a charset
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDiamondPro committed Feb 1, 2024
1 parent c15eff3 commit 5773d35
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 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.0.0"
version = "1.0.1"

repositories {
mavenCentral()
Expand Down
54 changes: 42 additions & 12 deletions src/main/java/dev/dediamondpro/minemark/MineMarkCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -74,13 +74,14 @@ protected MineMarkCore(TextElementLoader<S, R> textElement, Map<List<String>, El
*
* @param markdown The markdown text to parse
* @param style The style passed to all elements
* @param charSet The charset to use when parsing the markdown
* @return The parsed markdown element
* @throws SAXException An exception during SAX parsing
* @throws IOException An IOException during parsing
*/
public MineMarkElement<S, R> parse(@NotNull S style, @NotNull String markdown) throws SAXException, IOException {
public MineMarkElement<S, R> parse(@NotNull S style, @NotNull String markdown, Charset charSet) throws SAXException, IOException {
Node document = markdownParser.parse(markdown);
return parseDocument(style, document);
return parseDocument(style, document, charSet);
}

/**
Expand All @@ -92,18 +93,47 @@ public MineMarkElement<S, R> parse(@NotNull S style, @NotNull String markdown) t
* @throws SAXException An exception during SAX parsing
* @throws IOException An IOException during parsing
*/
public MineMarkElement<S, R> parse(@NotNull S style, @NotNull Reader markdown) throws SAXException, IOException {
public MineMarkElement<S, R> parse(@NotNull S style, @NotNull String markdown) throws SAXException, IOException {
return parse(style, markdown, StandardCharsets.UTF_8);
}

/**
* Parse markdown to an element used to render it
*
* @param markdown The markdown text to parse
* @param style The style passed to all elements
* @param charSet The charset to use when parsing the markdown
* @return The parsed markdown element
* @throws SAXException An exception during SAX parsing
* @throws IOException An IOException during parsing
*/
public MineMarkElement<S, R> parse(@NotNull S style, @NotNull Reader markdown, Charset charSet) throws SAXException, IOException {
Node document = markdownParser.parseReader(markdown);
return parseDocument(style, document);
return parseDocument(style, document, charSet);
}

/**
* Parse markdown to an element used to render it
*
* @param markdown The markdown text to parse
* @param style The style passed to all elements
* @return The parsed markdown element
* @throws SAXException An exception during SAX parsing
* @throws IOException An IOException during parsing
*/
public MineMarkElement<S, R> parse(@NotNull S style, @NotNull Reader markdown) throws SAXException, IOException {
return parse(style, markdown, StandardCharsets.UTF_8);
}

private MineMarkElement<S, R> parseDocument(@NotNull S style, Node document) throws SAXException, IOException {
private MineMarkElement<S, R> parseDocument(@NotNull S style, Node document, Charset charSet) throws SAXException, IOException {
String html = "<minemark>\n" + htmlRenderer.render(document) + "</minemark>";
try {
// Acquire the lock to make sure this thread is the only one using the parser
parsingLock.lock();
// 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))) {
htmlParser.setStyle(style, new LayoutStyle(style));
xmlParser.parse(new InputSource(new ByteArrayInputStream(html.getBytes())));
InputSource source = new InputSource(stream);
source.setEncoding(charSet.name());
xmlParser.parse(source);
return htmlParser.getParsedResult();
} finally {
htmlParser.cleanUp();
Expand Down

0 comments on commit 5773d35

Please sign in to comment.