You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kw...@apache.org on 2024/11/04 19:34:49 UTC

(maven-doxia) 01/01: [DOXIA-758] Consider emitComments flag in MarkdownSink

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch bugfix/support-stripping-comments-in-md
in repository https://gitbox.apache.org/repos/asf/maven-doxia.git

commit c6fc06e44993750c4b168e7a541cc93cbf577d13
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Mon Nov 4 20:34:38 2024 +0100

    [DOXIA-758] Consider emitComments flag in MarkdownSink
---
 .../maven/doxia/parser/AbstractParserTest.java     | 15 ++++++++++-
 .../doxia/module/markdown/MarkdownParser.java      |  1 +
 .../doxia/module/markdown/MarkdownParserTest.java  | 31 ++++++++++++++++++++++
 .../src/test/resources/comments.md                 |  5 ++++
 4 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/doxia-core/src/test/java/org/apache/maven/doxia/parser/AbstractParserTest.java b/doxia-core/src/test/java/org/apache/maven/doxia/parser/AbstractParserTest.java
index 2fb62d72..6533aa2b 100644
--- a/doxia-core/src/test/java/org/apache/maven/doxia/parser/AbstractParserTest.java
+++ b/doxia-core/src/test/java/org/apache/maven/doxia/parser/AbstractParserTest.java
@@ -22,6 +22,8 @@ import java.io.IOException;
 import java.io.Reader;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.ListIterator;
 
@@ -270,7 +272,8 @@ public abstract class AbstractParserTest extends AbstractModuleTest {
      * @param expectedText the expected text which is compared with the concatenated text of all text events
      * @param trimText {@code true} to trim the actual text before comparing with the expected one, otherwise compare without trimming
      */
-    void assertConcatenatedTextEquals(ListIterator<SinkEventElement> it, String expectedText, boolean trimText) {
+    protected static void assertConcatenatedTextEquals(
+            ListIterator<SinkEventElement> it, String expectedText, boolean trimText) {
         StringBuilder builder = new StringBuilder();
         while (it.hasNext()) {
             SinkEventElement currentEvent = it.next();
@@ -302,6 +305,16 @@ public abstract class AbstractParserTest extends AbstractModuleTest {
         Assertions.assertEquals(expected.toString(), actual.toString());
     }
 
+    public static void assertSinkDoesNotContain(Iterator<SinkEventElement> it, String... names) {
+        Collection<String> forbiddenNames = Arrays.asList(names);
+        while (it.hasNext()) {
+            String name = it.next().getName();
+            if (forbiddenNames.contains(name)) {
+                Assertions.fail("Found unexpected event: " + name);
+            }
+        }
+    }
+
     public static void assertSinkStartsWith(Iterator<SinkEventElement> it, String... names) {
         StringBuilder expected = new StringBuilder();
         StringBuilder actual = new StringBuilder();
diff --git a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownParser.java b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownParser.java
index 17093b0a..175ee615 100644
--- a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownParser.java
+++ b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownParser.java
@@ -178,6 +178,7 @@ public class MarkdownParser extends AbstractTextParser implements TextMarkup {
             // this requires writing a custom renderer not leveraging the XHTML parser
 
             // then HTML to Sink API
+            parser.setEmitComments(isEmitComments());
             parser.parse(html, getWrappedSink(sink), "Intermediate HTML from " + reference);
         } catch (IOException e) {
             throw new ParseException("Failed reading Markdown source document", e);
diff --git a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownParserTest.java b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownParserTest.java
index 3bffb066..273062c4 100644
--- a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownParserTest.java
+++ b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownParserTest.java
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.Reader;
 import java.util.Iterator;
 import java.util.List;
+import java.util.ListIterator;
 
 import org.apache.maven.doxia.parser.AbstractParser;
 import org.apache.maven.doxia.parser.AbstractParserTest;
@@ -834,6 +835,36 @@ public class MarkdownParserTest extends AbstractParserTest {
                 "body_");
     }
 
+    @Test
+    public void testCommentsNotRemovedWithEmitCommentsTrue() throws ParseException, IOException {
+        parser.setEmitComments(true);
+        List<SinkEventElement> eventList =
+                parseFileToEventTestingSink("comments").getEventList();
+
+        ListIterator<SinkEventElement> it = eventList.listIterator();
+        assertEventPrefix(it);
+        assertSinkStartsWithComment(it, "comment at beginning");
+        assertSinkStartsWith(it, "paragraph", "text", "paragraph_");
+        assertSinkStartsWithComment(it, "comment in the middle");
+        assertSinkStartsWith(it, "paragraph", "text", "paragraph_");
+        assertSinkStartsWithComment(it, "comment at end");
+        assertEventSuffix(it);
+    }
+
+    @Test
+    public void testCommentsRemovedWithEmitCommentsFalse() throws ParseException, IOException {
+        parser.setEmitComments(false);
+        List<SinkEventElement> eventList =
+                parseFileToEventTestingSink("comments").getEventList();
+
+        assertSinkDoesNotContain(eventList.iterator(), "comment", "comment_");
+    }
+
+    protected static void assertSinkStartsWithComment(ListIterator<SinkEventElement> it, String comment) {
+        assertSinkEquals(it.next(), "comment", comment);
+        assertConcatenatedTextEquals(it, "", true);
+    }
+
     @Override
     protected void assertEventPrefix(Iterator<SinkEventElement> eventIterator) {
         assertSinkStartsWith(eventIterator, "head", "head_", "body");
diff --git a/doxia-modules/doxia-module-markdown/src/test/resources/comments.md b/doxia-modules/doxia-module-markdown/src/test/resources/comments.md
new file mode 100644
index 00000000..eefedbf1
--- /dev/null
+++ b/doxia-modules/doxia-module-markdown/src/test/resources/comments.md
@@ -0,0 +1,5 @@
+<!--comment at beginning-->
+Test
+<!--comment in the middle-->
+Test
+<!--comment at end-->
\ No newline at end of file