Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOXIA-758] Consider emitComments flag in MarkdownSink #246

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
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;

Expand Down Expand Up @@ -270,7 +272,8 @@ public static void assertSinkAttributesEqual(
* @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();
Expand Down Expand Up @@ -302,6 +305,16 @@ public static void assertSinkEquals(Iterator<SinkEventElement> it, String... nam
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public void parse(Reader source, Sink sink, String reference) throws ParseExcept
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -834,6 +835,38 @@ public void testNonConsecutiveHeadingSections() throws ParseException, IOExcepti
"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");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!--comment at beginning-->
Test
<!--comment in the middle-->
Test
<!--comment at end-->