Skip to content

Commit

Permalink
add support for more formatting tags, add lock to parsing to prevent …
Browse files Browse the repository at this point in the history
…issues and small cleanup
  • Loading branch information
DeDiamondPro committed Jan 3, 2024
1 parent e1dedc9 commit 86d9e84
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "dev.dediamondpro"
version = "1.0-SNAPSHOT61"
version = "1.0-SNAPSHOT62"

repositories {
mavenCentral()
Expand Down
36 changes: 27 additions & 9 deletions src/main/java/dev/dediamondpro/minemark/MineMarkCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +27,7 @@ public class MineMarkCore<L extends LayoutConfig, R> {
private final HtmlRenderer htmlRenderer;
private final MineMarkHtmlParser<L, R> 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
Expand All @@ -50,24 +52,40 @@ protected MineMarkCore(Map<List<String>, ElementLoader<L, R>> 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<L, R> parse(@NotNull L layoutConfig, @NotNull String markdown) {
long start1 = System.currentTimeMillis();
public MineMarkElement<L, R> 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<L, R> parse(@NotNull L layoutConfig, @NotNull Reader markdown) throws SAXException, IOException {
Node document = markdownParser.parseReader(markdown);
return parseDocument(layoutConfig, document);
}

private MineMarkElement<L, R> parseDocument(@NotNull L layoutConfig, Node document) throws SAXException, IOException {
String html = "<minemark>\n" + htmlRenderer.render(document) + "</minemark>";
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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Float> callback : layoutCallbacks) {
callback.accept(height);
}
System.out.println("Finished generating layout, took " + (System.currentTimeMillis() - start) + "ms");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ public FormattingElement(@NotNull L layoutConfig, @Nullable Element<L, R> 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":
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/BasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<LayoutConfig, Object> element = core.parse(config, "```\ntest\n```\nHello<br>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<LayoutConfig, Object> element = core.parse(config, readFromInputStream(getClass().getResourceAsStream("test.md")));
System.out.println(element.getTree());
}
Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ that's an issue?

- Hi

</div>
</div>

<s>taco</s>

0 comments on commit 86d9e84

Please sign in to comment.