This is an automated email from the ASF dual-hosted git repository. kwin pushed a commit to branch bugfix/trim-source-in-xdoc in repository https://gitbox.apache.org/repos/asf/maven-doxia.git
commit b5e7d5be0c6359a59cf962f1015f2c9dbc404eba Author: Konrad Windszus <k...@apache.org> AuthorDate: Thu Dec 12 13:44:10 2024 +0100 [DOXIA-764] Trim left the text inside <source> It is block element in XDoc but potentially an inline element in the Sink (XHTML emits <pre><code>) --- .../apache/maven/doxia/module/xdoc/XdocParser.java | 22 ++++++++++++++++++++++ .../maven/doxia/module/xdoc/XdocParserTest.java | 9 +++++++++ 2 files changed, 31 insertions(+) diff --git a/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XdocParser.java b/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XdocParser.java index 7ca832d2..e6f006aa 100644 --- a/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XdocParser.java +++ b/doxia-modules/doxia-module-xdoc/src/main/java/org/apache/maven/doxia/module/xdoc/XdocParser.java @@ -31,6 +31,7 @@ import java.util.LinkedHashMap; 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; @@ -80,6 +81,8 @@ public class XdocParser extends Xhtml1BaseParser implements XdocMarkup { */ private boolean inHead; + private boolean inSource; + /** * Indicates that <title> was called from <properties> or <head>. */ @@ -159,6 +162,7 @@ public class XdocParser extends Xhtml1BaseParser implements XdocMarkup { 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 { @@ -216,6 +220,7 @@ public class XdocParser extends Xhtml1BaseParser implements XdocMarkup { 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())) { @@ -241,6 +246,23 @@ public class XdocParser extends Xhtml1BaseParser implements XdocMarkup { 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); diff --git a/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocParserTest.java b/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocParserTest.java index f8bff32a..e605d6e8 100644 --- a/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocParserTest.java +++ b/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocParserTest.java @@ -291,6 +291,15 @@ public class XdocParserTest extends AbstractParserTest { 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