diff --git a/build.gradle.kts b/build.gradle.kts index c1efeab..a33eb91 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "dev.dediamondpro" -version = "1.0-SNAPSHOT61" +version = "1.0-SNAPSHOT62" repositories { mavenCentral() diff --git a/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java b/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java index 67ce396..360f134 100644 --- a/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java +++ b/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java @@ -14,9 +14,10 @@ import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.Reader; import java.util.List; import java.util.Map; -import java.util.regex.Pattern; +import java.util.concurrent.locks.ReentrantLock; /** * Class responsible for integrating parsing, layout and rendering @@ -26,6 +27,7 @@ public class MineMarkCore { private final HtmlRenderer htmlRenderer; private final MineMarkHtmlParser htmlParser; private final org.ccil.cowan.tagsoup.Parser xmlParser; + private final ReentrantLock parsingLock = new ReentrantLock(); /** * @param elements Elements supported to create a layout and render @@ -50,24 +52,40 @@ protected MineMarkCore(Map, ElementLoader> elements, Iterable * @param markdown The markdown text to parse * @param layoutConfig The config used at parsing time to create the layout * @return The parsed markdown element - * @throws RuntimeException When parsing fails or an error occurs + * @throws SAXException An exception during SAX parsing + * @throws IOException An IOException during parsing */ - public MineMarkElement parse(@NotNull L layoutConfig, @NotNull String markdown) { - long start1 = System.currentTimeMillis(); + public MineMarkElement parse(@NotNull L layoutConfig, @NotNull String markdown) throws SAXException, IOException { Node document = markdownParser.parse(markdown); + return parseDocument(layoutConfig, document); + } + + /** + * Parse markdown to an element used to render it + * + * @param markdown The markdown text to parse + * @param layoutConfig The config used at parsing time to create the layout + * @return The parsed markdown element + * @throws SAXException An exception during SAX parsing + * @throws IOException An IOException during parsing + */ + public MineMarkElement parse(@NotNull L layoutConfig, @NotNull Reader markdown) throws SAXException, IOException { + Node document = markdownParser.parseReader(markdown); + return parseDocument(layoutConfig, document); + } + + private MineMarkElement parseDocument(@NotNull L layoutConfig, Node document) throws SAXException, IOException { String html = "\n" + htmlRenderer.render(document) + ""; System.out.println(html); - long start2 = System.currentTimeMillis(); - System.out.println("Finished generating html, took " + (start2 - start1) + "ms"); try { + // Acquire the lock to make sure this thread is the only one using the parser + parsingLock.lock(); htmlParser.setLayoutConfig(layoutConfig); xmlParser.parse(new InputSource(new ByteArrayInputStream(html.getBytes()))); return htmlParser.getParsedResult(); - } catch (SAXException | IOException e) { - throw new RuntimeException(e); } finally { htmlParser.cleanUp(); - System.out.println("Finished parsing html, took " + (System.currentTimeMillis() - start2) + "ms"); + parsingLock.unlock(); } } diff --git a/src/main/java/dev/dediamondpro/minemark/elements/Elements.java b/src/main/java/dev/dediamondpro/minemark/elements/Elements.java index 8597ea3..e4276af 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/Elements.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/Elements.java @@ -6,7 +6,7 @@ public enum Elements { PARAGRAPH(listOf("p")), TEXT(listOf("text")), - FORMATTING(listOf("strong", "em", "del", "pre")), + FORMATTING(listOf("strong", "b", "em", "i", "ins", "u", "del", "s", "pre")), HEADING(listOf("h1", "h2", "h3", "h4", "h5", "h6")), ALIGNMENT(listOf("div", "center")), LINK(listOf("a")), diff --git a/src/main/java/dev/dediamondpro/minemark/elements/MineMarkElement.java b/src/main/java/dev/dediamondpro/minemark/elements/MineMarkElement.java index 2e7c1f5..06f1c13 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/MineMarkElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/MineMarkElement.java @@ -19,13 +19,11 @@ public MineMarkElement(L layoutConfig, Attributes attributes) { @Override public void generateLayout(LayoutData layoutData) { - long start = System.currentTimeMillis(); super.generateLayout(layoutData); height = layoutData.getY() + layoutData.getLineHeight(); for (Consumer callback : layoutCallbacks) { callback.accept(height); } - System.out.println("Finished generating layout, took " + (System.currentTimeMillis() - start) + "ms"); } @Override diff --git a/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/FormattingElement.java b/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/FormattingElement.java index 6a5eeb6..2e32990 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/FormattingElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/FormattingElement.java @@ -14,15 +14,19 @@ public FormattingElement(@NotNull L layoutConfig, @Nullable Element parent this.layoutConfig = cloneLayoutConfig(layoutConfig); switch (qName) { case "strong": + case "b": this.layoutConfig.setBold(true); break; case "em": + case "i": this.layoutConfig.setItalic(true); break; case "ins": + case "u": this.layoutConfig.setUnderlined(true); break; case "del": + case "s": this.layoutConfig.setStrikethrough(true); break; case "pre": diff --git a/src/test/java/BasicTest.java b/src/test/java/BasicTest.java index f03068a..e829c2d 100644 --- a/src/test/java/BasicTest.java +++ b/src/test/java/BasicTest.java @@ -4,6 +4,7 @@ import dev.dediamondpro.minemark.elements.MineMarkElement; import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension; import org.junit.jupiter.api.Test; +import org.xml.sax.SAXException; import java.io.BufferedReader; import java.io.IOException; @@ -20,14 +21,14 @@ public class BasicTest { private final LayoutConfig config = new LayoutConfig(5, new LayoutConfig.SpacingConfig(1f, 2f, 2f, 5f), new LayoutConfig.HeadingConfig(6, 5, 4, 3, 2, 1)); @Test - public void test() { + public void test() throws IOException, SAXException { MineMarkElement element = core.parse(config, "```\ntest\n```\nHello
World"); //element.beforeDraw(0f, 0f, 25f, 0f, 0f, new Object()); System.out.println(element.getTree()); } @Test - public void testFile() throws IOException { + public void testFile() throws IOException, SAXException { MineMarkElement element = core.parse(config, readFromInputStream(getClass().getResourceAsStream("test.md"))); System.out.println(element.getTree()); } diff --git a/src/test/resources/test.md b/src/test/resources/test.md index f08e706..85e38f0 100644 --- a/src/test/resources/test.md +++ b/src/test/resources/test.md @@ -13,4 +13,6 @@ that's an issue? - Hi - \ No newline at end of file + + +taco \ No newline at end of file