Skip to content

Commit

Permalink
[DOXIA-764] Trim left the text inside <source>
Browse files Browse the repository at this point in the history
It is block element in XDoc but potentially an inline element in the
Sink (XHTML emits <pre><code>)
  • Loading branch information
kwin committed Dec 12, 2024
1 parent ac99291 commit b5e7d5b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.doxia.macro.MacroExecutionException;
import org.apache.maven.doxia.macro.MacroRequest;
import org.apache.maven.doxia.macro.manager.MacroNotFoundException;
Expand Down Expand Up @@ -80,6 +81,8 @@ public class XdocParser extends Xhtml1BaseParser implements XdocMarkup {
*/
private boolean inHead;

private boolean inSource;

/**
* Indicates that &lt;title&gt; was called from &lt;properties&gt; or &lt;head&gt;.
*/
Expand Down Expand Up @@ -159,6 +162,7 @@ protected void handleStartTag(XmlPullParser parser, Sink sink)
attribs.addAttributes(SinkEventAttributeSet.SOURCE);

sink.verbatim(attribs);
inSource = true;
} else if (parser.getName().equals(PROPERTIES_TAG.toString())) {
if (!inHead) // we might be in head from a <head> already
{
Expand Down Expand Up @@ -216,6 +220,7 @@ protected void handleEndTag(XmlPullParser parser, Sink sink)
verbatim_();

sink.verbatim_();
inSource = false;
} else if (parser.getName().equals(PROPERTIES_TAG.toString())) {
// Do nothing, head is closed with BODY start.
} else if (parser.getName().equals(MACRO_TAG.toString())) {
Expand All @@ -241,6 +246,23 @@ protected void handleEndTag(XmlPullParser parser, Sink sink)
isEmptyElement = false;
}

/** {@inheritDoc} */
protected void handleText(XmlPullParser parser, Sink sink) throws XmlPullParserException {
// almost a copy of super.handleText(parser, sink), but with the following changes:
// trim left the first text inside source tag because it is emitted inside an inline tag
String text = parser.getText();
if (inSource) {
text = parser.getText();
if (text != null && text.length() > 0) {
text = StringUtils.stripStart(text, null);
}
inSource = false;
}
if ((text != null && !text.isEmpty()) && !isScriptBlock()) {
sink.text(text);
}
}

protected void consecutiveSections(int newLevel, Sink sink) {
closeOpenSections(newLevel, sink);
openMissingSections(newLevel, sink);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ public void testSourceEventsList() throws Exception {
it = parseText(text);

assertSinkEquals(it, "verbatim", "text", "verbatim_");

// the source should be trimmed left (DOXIA-764) because it is becoming the Sink.verbatim() method (which is
// inline)
text = "<source>\n" + "some.code() </source>";
it = parseText(text);

assertSinkEquals(it.next(), "verbatim", SinkEventAttributeSet.SOURCE);
assertSinkEquals(it.next(), "text", "some.code() ", null);
assertSinkEquals(it, "verbatim_");
}

@Test
Expand Down

0 comments on commit b5e7d5b

Please sign in to comment.