You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2026/09/19 21:40:04 UTC
(maven-plugin-tools) branch maven-plugin-tools-3.x updated: Fix javadoc rendering of code blocks, nested lists and sibling links
This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch maven-plugin-tools-3.x
in repository https://gitbox.apache.org/repos/asf/maven-plugin-tools.git
The following commit(s) were added to refs/heads/maven-plugin-tools-3.x by this push:
new a3caed38 Fix javadoc rendering of code blocks, nested lists and sibling links
a3caed38 is described below
commit a3caed38bf9ea60b70d42d4fcc8809dd45db33ef
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Sat Sep 19 12:47:53 2026 +0200
Fix javadoc rendering of code blocks, nested lists and sibling links
- {@code} and other inline tags end at the balanced closing brace, so a
${property} inside the argument no longer truncates it (MPLUGIN-513)
- the help text keeps <pre> blocks verbatim instead of collapsing their
whitespace (MPLUGIN-535)
- nested list items are indented in the help text (MPLUGIN-505)
- links to this plugin's own goal pages are no longer stripped from
the report because the page does not exist yet (MPLUGIN-518)
---
.../maven/plugin/plugin/report/GoalRenderer.java | 24 ++++++-
.../plugin/plugin/report/GoalRendererTest.java | 27 +++++++
.../JavadocInlineTagsToXhtmlConverter.java | 84 +++++++++++++++-------
.../JavadocInlineTagsToXhtmlConverterTest.java | 18 +++++
.../plugin/generator/HtmlToPlainTextConverter.java | 64 +++++++++++++++--
.../generator/HtmlToPlainTextConverterTest.java | 20 ++++++
6 files changed, 206 insertions(+), 31 deletions(-)
diff --git a/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/GoalRenderer.java b/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/GoalRenderer.java
index 20d2ea45..b105f143 100644
--- a/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/GoalRenderer.java
+++ b/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/GoalRenderer.java
@@ -484,7 +484,9 @@ public class GoalRenderer extends AbstractPluginReportRenderer {
URI link;
try {
link = new URI(matcher.group(1));
- if (!link.isAbsolute() && !JavadocLinkGenerator.isLinkValid(link, reportOutputDirectory.toPath())) {
+ if (!link.isAbsolute()
+ && !isLinkToThisReport(link)
+ && !JavadocLinkGenerator.isLinkValid(link, reportOutputDirectory.toPath())) {
matcher.appendReplacement(sanitizedXhtmlText, matcher.group(2));
log.debug(String.format("Removed invalid link %s in %s", link, context));
} else {
@@ -500,6 +502,26 @@ public class GoalRenderer extends AbstractPluginReportRenderer {
return sanitizedXhtmlText.toString();
}
+ /**
+ * Pages written by this report may not exist yet when a goal page referring to them is rendered, so they cannot be
+ * validated against the output directory like javadoc links.
+ */
+ private boolean isLinkToThisReport(URI link) {
+ String path = link.getPath();
+ if (path == null || path.isEmpty() || path.contains("/")) {
+ return false;
+ }
+ if (path.equals("plugin-info.html")) {
+ return true;
+ }
+ if (descriptor == null || descriptor.getPluginDescriptor() == null) {
+ return false;
+ }
+ return descriptor.getPluginDescriptor().getMojos().stream()
+ .map(mojo -> mojo.getGoal() + "-mojo.html")
+ .anyMatch(path::equals);
+ }
+
/** Convenience method.
*
* @param key not null
diff --git a/maven-plugin-report-plugin/src/test/java/org/apache/maven/plugin/plugin/report/GoalRendererTest.java b/maven-plugin-report-plugin/src/test/java/org/apache/maven/plugin/plugin/report/GoalRendererTest.java
index 12ad1911..78dde825 100644
--- a/maven-plugin-report-plugin/src/test/java/org/apache/maven/plugin/plugin/report/GoalRendererTest.java
+++ b/maven-plugin-report-plugin/src/test/java/org/apache/maven/plugin/plugin/report/GoalRendererTest.java
@@ -21,6 +21,8 @@ package org.apache.maven.plugin.plugin.report;
import java.io.File;
import java.util.Locale;
+import org.apache.maven.plugin.descriptor.MojoDescriptor;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.junit.jupiter.api.Test;
@@ -37,6 +39,31 @@ class GoalRendererTest {
assertEquals("List<...>", GoalRenderer.getShortType("java.util.List<java.util.List<java.lang.String>>"));
}
+ @Test
+ void linksToSiblingGoalPagesAreKept() throws Exception {
+ // MPLUGIN-518: the goal pages of this plugin are written by this report and may not exist yet
+ File baseDir = new File(this.getClass().getResource("").getFile());
+ PluginDescriptor pluginDescriptor = new PluginDescriptor();
+ MojoDescriptor test = new MojoDescriptor();
+ test.setGoal("test");
+ pluginDescriptor.addMojo(test);
+ MojoDescriptor other = new MojoDescriptor();
+ other.setGoal("other");
+ pluginDescriptor.addMojo(other);
+ // set by PluginDescriptorBuilder when the descriptor is read from plugin.xml
+ other.setPluginDescriptor(pluginDescriptor);
+ GoalRenderer renderer =
+ new GoalRenderer(null, null, Locale.ROOT, null, other, baseDir, false, new SystemStreamLog());
+
+ String siblingLinks =
+ "see <a href=\"test-mojo.html\">test</a>, <a href=\"test-mojo.html#param\">a parameter</a>"
+ + " and <a href=\"plugin-info.html\">the overview</a>";
+ assertEquals(siblingLinks, renderer.getXhtmlWithValidatedLinks(siblingLinks, "test"));
+
+ String unknownPage = "see <a href=\"unknown-mojo.html\">unknown</a>";
+ assertEquals("see unknown", renderer.getXhtmlWithValidatedLinks(unknownPage, "test"));
+ }
+
@Test
void getXhtmlWithValidatedLinks() {
File baseDir = new File(this.getClass().getResource("").getFile());
diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/converter/JavadocInlineTagsToXhtmlConverter.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/converter/JavadocInlineTagsToXhtmlConverter.java
index 10553b4a..b8272e4c 100644
--- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/converter/JavadocInlineTagsToXhtmlConverter.java
+++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/converter/JavadocInlineTagsToXhtmlConverter.java
@@ -23,8 +23,6 @@ import javax.inject.Named;
import javax.inject.Singleton;
import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.JavadocTagToHtmlConverter;
import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.inline.JavadocInlineTagToHtmlConverter;
@@ -43,9 +41,7 @@ public class JavadocInlineTagsToXhtmlConverter {
private final Map<String, JavadocInlineTagToHtmlConverter> converters;
- private static final Pattern INLINE_TAG_PATTERN = Pattern.compile("\\{@([^\\s]*)(?:\\s([^\\}]*))?\\}");
- private static final int GROUP_TAG_NAME = 1;
- private static final int GROUP_REFERENCE = 2;
+ private static final String INLINE_TAG_START = "{@";
@Inject
public JavadocInlineTagsToXhtmlConverter(Map<String, JavadocInlineTagToHtmlConverter> converters) {
@@ -59,30 +55,70 @@ public class JavadocInlineTagsToXhtmlConverter {
* @return
*/
public String convert(String text, ConverterContext context) {
- Matcher matcher = INLINE_TAG_PATTERN.matcher(text);
- StringBuffer sb = new StringBuffer();
- while (matcher.find()) {
- String tagName = matcher.group(GROUP_TAG_NAME);
- JavadocTagToHtmlConverter converter = converters.get(tagName);
- String patternReplacement;
- if (converter == null) {
- patternReplacement = matcher.group(0) + "<!-- unsupported tag '" + tagName + "' -->";
- LOG.warn("Found unsupported javadoc inline tag '{}' in {}", tagName, context.getLocation());
- } else {
- try {
- patternReplacement = converter.convert(matcher.group(GROUP_REFERENCE), context);
- } catch (Throwable t) {
- patternReplacement = matcher.group(0) + "<!-- error processing javadoc tag '" + tagName + "': "
- + t.getMessage() + " -->"; // leave original javadoc in place
- LOG.warn("Error converting javadoc inline tag '{}' in {}", tagName, context.getLocation(), t);
- }
+ StringBuilder sb = new StringBuilder();
+ int pos = 0;
+ int start;
+ while ((start = text.indexOf(INLINE_TAG_START, pos)) >= 0) {
+ int nameEnd = start + INLINE_TAG_START.length();
+ while (nameEnd < text.length()
+ && !Character.isWhitespace(text.charAt(nameEnd))
+ && text.charAt(nameEnd) != '}') {
+ nameEnd++;
+ }
+ int end = findClosingBrace(text, nameEnd);
+ if (end < 0) {
+ // unbalanced braces: leave the rest of the text untouched
+ break;
}
- matcher.appendReplacement(sb, Matcher.quoteReplacement(patternReplacement));
+ String tagName = text.substring(start + INLINE_TAG_START.length(), nameEnd);
+ // the single whitespace character separating the tag name from its argument is not part of the argument
+ String reference = nameEnd < end ? text.substring(nameEnd + 1, end) : null;
+ String original = text.substring(start, end + 1);
+ sb.append(text, pos, start);
+ sb.append(convertTag(tagName, reference, original, context));
+ pos = end + 1;
}
- matcher.appendTail(sb);
+ sb.append(text, pos, text.length());
return toXHTML(sb.toString());
}
+ /**
+ * Finds the brace closing the inline tag whose argument starts at {@code from}. Braces inside the argument are
+ * allowed as long as they are balanced, as with {@code {@code ${project.basedir}}} in javadoc itself.
+ *
+ * @return the index of the closing brace, or {@code -1} if the braces are not balanced
+ */
+ private static int findClosingBrace(String text, int from) {
+ int depth = 1;
+ for (int i = from; i < text.length(); i++) {
+ char c = text.charAt(i);
+ if (c == '{') {
+ depth++;
+ } else if (c == '}') {
+ depth--;
+ if (depth == 0) {
+ return i;
+ }
+ }
+ }
+ return -1;
+ }
+
+ private String convertTag(String tagName, String reference, String original, ConverterContext context) {
+ JavadocTagToHtmlConverter converter = converters.get(tagName);
+ if (converter == null) {
+ LOG.warn("Found unsupported javadoc inline tag '{}' in {}", tagName, context.getLocation());
+ return original + "<!-- unsupported tag '" + tagName + "' -->";
+ }
+ try {
+ return converter.convert(reference, context);
+ } catch (Throwable t) {
+ LOG.warn("Error converting javadoc inline tag '{}' in {}", tagName, context.getLocation(), t);
+ // leave original javadoc in place
+ return original + "<!-- error processing javadoc tag '" + tagName + "': " + t.getMessage() + " -->";
+ }
+ }
+
static String toXHTML(String bodySnippet) {
String html = "<html><head></head><body>" + bodySnippet + "</body>"; // make it a valid HTML document
final Document document = Jsoup.parse(html);
diff --git a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/converter/JavadocInlineTagsToXhtmlConverterTest.java b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/converter/JavadocInlineTagsToXhtmlConverterTest.java
index 07f055df..24699853 100644
--- a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/converter/JavadocInlineTagsToXhtmlConverterTest.java
+++ b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/converter/JavadocInlineTagsToXhtmlConverterTest.java
@@ -78,6 +78,24 @@ class JavadocInlineTagsToXhtmlConverterTest {
assertEquals("<code>test</code>", converter.convert(test, context));
}
+ @Test
+ void codeWithNestedBraces() {
+ // MPLUGIN-513: braces inside the argument are balanced, so the tag ends at the matching brace
+ String test = "<pre>{@code <agentPath>${project.basedir}/lib/agent.jar</agentPath>}</pre> tail";
+ assertEquals(
+ "<pre><code><agentPath>${project.basedir}/lib/agent.jar</agentPath></code></pre>\n tail",
+ converter.convert(test, context));
+
+ test = "{@code a {b {c}} d} and {@code e}";
+ assertEquals("<code>a {b {c}} d</code> and <code>e</code>", converter.convert(test, context));
+ }
+
+ @Test
+ void unbalancedBracesAreLeftUntouched() {
+ String test = "text {@code ${unterminated";
+ assertEquals("text {@code ${unterminated", converter.convert(test, context));
+ }
+
@Test
void literal() {
String test = "{@literal text}";
diff --git a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/HtmlToPlainTextConverter.java b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/HtmlToPlainTextConverter.java
index 6906c75c..d4438bf3 100644
--- a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/HtmlToPlainTextConverter.java
+++ b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/HtmlToPlainTextConverter.java
@@ -59,17 +59,41 @@ public class HtmlToPlainTextConverter implements Converter {
// the formatting rules, implemented in a breadth-first DOM traverse
private static class FormattingVisitor implements NodeVisitor {
- private StringBuilder accum = new StringBuilder(); // holds the accumulated text
+ private final StringBuilder accum = new StringBuilder(); // holds the accumulated text
+
+ /** Text outside {@code <pre>} with whitespace collapsed, plus the preformatted blocks kept as they are. */
+ private final StringBuilder result = new StringBuilder();
+
+ /** Stands in for the indentation of nested list items until the whitespace has been collapsed. */
+ private static final char INDENT = '\u0001';
+
+ private int preformattedDepth;
// hit when the node is first seen
public void head(Node node, int depth) {
String name = node.nodeName();
if (node instanceof TextNode) {
- accum.append(((TextNode) node).text()); // TextNodes carry all user-readable text in the DOM.
+ TextNode textNode = (TextNode) node;
+ // TextNodes carry all user-readable text in the DOM.
+ accum.append(preformattedDepth > 0 ? textNode.getWholeText() : textNode.text());
} else if (name.equals("li")) {
- accum.append("\n * ");
+ int listDepth = listDepth(node);
+ accum.append("\n");
+ if (listDepth <= 1) {
+ accum.append(" * ");
+ } else {
+ // two spaces per nesting level; the space before the bullet is collapsed away on the top level
+ for (int i = 1; i < listDepth; i++) {
+ accum.append(INDENT).append(INDENT);
+ }
+ accum.append("* ");
+ }
} else if (name.equals("dt")) {
accum.append(" ");
+ } else if (name.equals("pre")) {
+ flush();
+ preformattedDepth++;
+ accum.append("\n");
} else if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5", "tr")) {
accum.append("\n");
}
@@ -78,7 +102,11 @@ public class HtmlToPlainTextConverter implements Converter {
// hit when all of the node's children (if any) have been visited
public void tail(Node node, int depth) {
String name = node.nodeName();
- if (StringUtil.in(name, "br", "dd", "dt", "p", "h1", "h2", "h3", "h4", "h5")) {
+ if (name.equals("pre")) {
+ accum.append("\n");
+ flush();
+ preformattedDepth--;
+ } else if (StringUtil.in(name, "br", "dd", "dt", "p", "h1", "h2", "h3", "h4", "h5")) {
accum.append("\n");
} else if (name.equals("a")) {
// link is empty if it cannot be made absolute
@@ -89,10 +117,34 @@ public class HtmlToPlainTextConverter implements Converter {
}
}
+ /** Number of enclosing lists of a list item: 1 for a top-level item. */
+ private static int listDepth(Node listItem) {
+ int result = 0;
+ for (Node parent = listItem.parent(); parent != null; parent = parent.parent()) {
+ if (StringUtil.in(parent.nodeName(), "ul", "ol")) {
+ result++;
+ }
+ }
+ return result;
+ }
+
+ private void flush() {
+ if (preformattedDepth > 0) {
+ result.append(accum);
+ } else {
+ // collate multiple consecutive spaces
+ result.append(accum.toString()
+ .replaceAll(" +", " ")
+ .replace("\n ", "\n")
+ .replace(INDENT, ' '));
+ }
+ accum.setLength(0);
+ }
+
@Override
public String toString() {
- // collate multiple consecutive spaces
- return accum.toString().replaceAll(" +", " ").replace("\n ", "\n");
+ flush();
+ return result.toString();
}
}
}
diff --git a/maven-plugin-tools-generators/src/test/java/org/apache/maven/tools/plugin/generator/HtmlToPlainTextConverterTest.java b/maven-plugin-tools-generators/src/test/java/org/apache/maven/tools/plugin/generator/HtmlToPlainTextConverterTest.java
index 9152b4f0..9837f579 100644
--- a/maven-plugin-tools-generators/src/test/java/org/apache/maven/tools/plugin/generator/HtmlToPlainTextConverterTest.java
+++ b/maven-plugin-tools-generators/src/test/java/org/apache/maven/tools/plugin/generator/HtmlToPlainTextConverterTest.java
@@ -58,6 +58,26 @@ class HtmlToPlainTextConverterTest {
assertEquals("field", converter.convert(test));
}
+ @Test
+ void preformattedBlockKeepsLineBreaksAndIndentation() {
+ // MPLUGIN-535
+ String test = "For example:<pre>\n<binaryUrlPlugins>\n <binaryUrlPlugin>\n"
+ + " <url>https://example.org/plugin.exe</url>\n </binaryUrlPlugin>\n"
+ + "</binaryUrlPlugins>\n</pre>and after.";
+ assertEquals(
+ "For example:\n<binaryUrlPlugins>\n <binaryUrlPlugin>\n"
+ + " <url>https://example.org/plugin.exe</url>\n </binaryUrlPlugin>\n"
+ + "</binaryUrlPlugins>\n\nand after.",
+ converter.convert(test));
+ }
+
+ @Test
+ void nestedLists() {
+ // MPLUGIN-505
+ String test = "<ul><li>one<ul><li>one.a</li><li>one.b</li></ul></li><li>two</li></ul>";
+ assertEquals("\n* one\n * one.a\n * one.b\n* two", converter.convert(test));
+ }
+
@Test
void nullValue() {
assertNull(converter.convert(null));