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

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-doxia.git


The following commit(s) were added to refs/heads/master by this push:
     new 33fea800 [DOXIA-758] Consider emitComments flag in MarkdownSink (#246)
33fea800 is described below

commit 33fea8000b4367d0f9951605aaf0a7afa0ae1bf1
Author: Konrad Windszus <k...@apache.org>
AuthorDate: Sat Nov 9 10:22:06 2024 +0100

    [DOXIA-758] Consider emitComments flag in MarkdownSink (#246)
---
 .../maven/doxia/parser/AbstractParserTest.java     | 15 +++++++++-
 .../doxia/module/markdown/MarkdownParser.java      |  1 +
 .../doxia/module/markdown/MarkdownParserTest.java  | 33 ++++++++++++++++++++++
 .../src/test/resources/comments.md                 |  5 ++++
 4 files changed, 53 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..8b9f2466 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,38 @@ 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);
+        assertComment(it, "comment at beginning");
+        assertSinkStartsWith(it, "paragraph", "text", "paragraph_");
+        assertComment(it, "comment in the middle");
+        assertSinkStartsWith(it, "paragraph", "text", "paragraph_");
+        assertComment(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 assertComment(ListIterator<SinkEventElement> it, 
String comment) {
+        assertSinkEquals(it.next(), "comment", comment);
+        // every comment ends with a line break in the emitted html which 
leads to an additional text event containing a
+        // line break in the event list
+        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

Reply via email to