Skip to content

Commit

Permalink
[wildfly-extras#1] Support more formatting options for the generated …
Browse files Browse the repository at this point in the history
…asciidoc.
  • Loading branch information
jamezp committed Aug 25, 2020
1 parent e61a94b commit f5cc8fe
Show file tree
Hide file tree
Showing 13 changed files with 672 additions and 9 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2016-2018 Red Hat, Inc. and/or its affiliates
Copyright 2016-2020 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -31,6 +31,7 @@

<packaging>maven-plugin</packaging>
<properties>
<version.org.jsoup.jsoup>1.13.1</version.org.jsoup.jsoup>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down Expand Up @@ -64,5 +65,11 @@
<artifactId>maven-plugin-annotations</artifactId>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${version.org.jsoup.jsoup}</version>
</dependency>
</dependencies>
</project>
19 changes: 11 additions & 8 deletions src/main/java/org/wildfly/tool/plugin/PluginAdocGenerator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 Red Hat, Inc. and/or its affiliates
* Copyright 2016-2020 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
Expand All @@ -33,6 +34,7 @@
import org.apache.maven.tools.plugin.generator.GeneratorUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.wildfly.tool.plugin.formatter.Formatters;

/**
*
Expand Down Expand Up @@ -434,8 +436,8 @@ private void writeParameterList(MojoDescriptor mojoDescriptor, String title, Lis
}
}

// description
w.print("|");
// description, start with "a|" to allow for asciidoc formatting within the cell.
w.print(" a|");
String description;
if (StringUtils.isNotEmpty(parameter.getDeprecated())) {
description = format("pluginasciidoc.mojodescriptor.parameter.deprecated",
Expand All @@ -445,7 +447,11 @@ private void writeParameterList(MojoDescriptor mojoDescriptor, String title, Lis
} else {
description = getString("pluginasciidoc.nodescription");
}
w.println(description + " +");
w.println(description);
w.println();
// Print a new line instead of a "+" as the next line should not be associated as a continuation of this
// line.
///w.println();

if (StringUtils.isNotEmpty(parameter.getDefaultValue())) {
w.print(format("pluginasciidoc.mojodescriptor.parameter.defaultValue",
Expand All @@ -472,10 +478,7 @@ private void writeParameterList(MojoDescriptor mojoDescriptor, String title, Lis

private String makeHtmlValid(String description) {
String result = GeneratorUtils.makeHtmlValid(description);
result = result.replaceAll("<code>", "`");
result = result.replaceAll("</code>", "`");
result = result.replaceAll("<br/>", " +");
result = result.replaceAll("<br />", " +");
result = Formatters.format(result);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2016-2020 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.tool.plugin.formatter;

import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
abstract class AbstractNodeVisitor implements AppendableNodeVisitor {
private final StringBuilder builder;

AbstractNodeVisitor(final StringBuilder builder) {
this.builder = builder;
}

@Override
public AbstractNodeVisitor append(final CharSequence csq) {
builder.append(csq);
return this;
}

@Override
public AbstractNodeVisitor append(final CharSequence csq, final int start, final int end) {
builder.append(csq, start, end);
return this;
}

@Override
public AbstractNodeVisitor append(final char c) {
builder.append(c);
return this;
}

@Override
public AppendableNodeVisitor appendNewLine() {
builder.append(System.lineSeparator());
return this;
}

@Override
public String toString() {
return builder.toString();
}

/**
* Gets the possible text for a node. If the node is not a {@link TextNode} and empty string is returned.
*
* @param node the node to get the text from
*
* @return the text for the node or an empty string
*/
String getText(final Node node) {
if (node instanceof TextNode) {
final TextNode textNode = (TextNode) node;
return textNode.getWholeText();
}
return "";
}

/**
* Gets the possible text for the first child node. If the child node is not a {@link TextNode} and empty string is
* returned.
*
* @param node the node to get the first child from
*
* @return the text for the first child node or an empty string
*/
String getChildText(final Node node) {
if (node.childNodeSize() > 0) {
return getText(node.childNode(0));
}
return "";
}

/**
* Checks the node name against the names and returns {@code true} if one of the names matches the nodes name.
*
* @param node the node to check
* @param names the names that may match
*
* @return {@code true} if the node name matches one of the names, otherwise {@code false}
*/
boolean nodeNameMatches(final Node node, final String... names) {
final String nodeName = node.nodeName();
for (String name : names) {
if (name.equals(nodeName)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2016-2020 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.tool.plugin.formatter;

import org.jsoup.nodes.Node;
import org.jsoup.select.NodeVisitor;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
interface AppendableNodeVisitor extends Appendable, NodeVisitor {


@Override
AppendableNodeVisitor append(CharSequence csq);

@Override
AppendableNodeVisitor append(CharSequence csq, int start, int end);

@Override
AppendableNodeVisitor append(char c);

/**
* Appends the platforms {@linkplain System#lineSeparator() line separator}.
*
* @return this appender
*/
AppendableNodeVisitor appendNewLine();

/**
* Indicates whether or not this visitor can process the node.
*
* @param node the node to check
*
* @return {@code true} if this visitor can process the node, otherwise {@code null}
*/
boolean canProcess(Node node);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2016-2020 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.tool.plugin.formatter;

import org.jsoup.nodes.Node;
import org.jsoup.select.NodeVisitor;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
class BoldNodeVisitor extends AbstractNodeVisitor implements NodeVisitor {

BoldNodeVisitor(final StringBuilder builder) {
super(builder);
}

@Override
public void head(final Node node, final int depth) {
append('*');
}

@Override
public void tail(final Node node, final int depth) {
append("* ");
}

@Override
public boolean canProcess(final Node node) {
return nodeNameMatches(node, "b", "strong");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2016-2020 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.tool.plugin.formatter;

import org.jsoup.nodes.Node;
import org.jsoup.select.NodeVisitor;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
class BreakNodeVisitor extends AbstractNodeVisitor implements NodeVisitor {

BreakNodeVisitor(final StringBuilder builder) {
super(builder);
}

@Override
public void head(final Node node, final int depth) {
append(" +");
}

@Override
public void tail(final Node node, final int depth) {
appendNewLine();
}

@Override
public boolean canProcess(final Node node) {
return nodeNameMatches(node, "br");
}
}
Loading

0 comments on commit f5cc8fe

Please sign in to comment.