From 912de340a1a412ec6d38f07de1f8cc91cca3f1af Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Sat, 19 Oct 2024 12:17:52 +0200 Subject: [PATCH] Refactor of AST traversal method (#944) * Solve limitations in the current method used to traverse the AST. The new method does not look externally (outside of NodeProcessors) but instead injects NodeSinker into the NodeProcessors so each one decides if further traversal needs to happen. This has simplified the code notably, and removes the need for specific dependency injection. * Changes in how HTML content is generated: * Empty document no longer generated empty

* Empty literal no longer generated empty

  * Sections are now wrapped around 
elements for better organization. --- CHANGELOG.adoc | 3 + .../asciidoctor/maven/site/HeadParser.java | 2 + .../parser/AsciidoctorAstDoxiaParser.java | 22 +-- .../maven/site/parser/NodeSinker.java | 66 ++++++++ .../maven/site/parser/NodesSinker.java | 99 ------------ .../processors/AbstractSinkNodeProcessor.java | 19 ++- .../DescriptionListNodeProcessor.java | 21 +-- .../processors/DocumentNodeProcessor.java | 22 ++- .../parser/processors/ImageNodeProcessor.java | 8 +- .../processors/ListItemNodeProcessor.java | 30 +--- .../processors/ListingNodeProcessor.java | 20 +-- .../processors/LiteralNodeProcessor.java | 29 ++-- .../parser/processors/NoOpNodeProcessor.java | 43 +++++ .../processors/OrderedListNodeProcessor.java | 31 ++-- .../processors/ParagraphNodeProcessor.java | 17 +- .../processors/PreambleNodeProcessor.java | 9 +- .../processors/SectionNodeProcessor.java | 31 ++-- .../parser/processors/TableNodeProcessor.java | 12 +- .../UnorderedListNodeProcessor.java | 14 +- .../parser/AsciidoctorAstDoxiaParserTest.java | 16 +- .../maven/site/parser/NodeSinkerTest.java | 37 +++-- .../DescriptionListNodeProcessorTest.java | 27 +--- .../processors/DocumentNodeProcessorTest.java | 18 ++- .../processors/ImageNodeProcessorTest.java | 1 - .../processors/ListItemNodeProcessorTest.java | 35 ++--- .../processors/ListingNodeProcessorTest.java | 1 - .../processors/LiteralNodeProcessorTest.java | 13 +- .../OrderedListNodeProcessorTest.java | 8 - .../processors/PreambleNodeProcessorTest.java | 28 ++-- .../processors/SectionNodeProcessorTest.java | 148 ++++++++++++------ .../processors/TableNodeProcessorTest.java | 1 - .../UnorderedListNodeProcessorTest.java | 76 ++++----- .../test/TestNodeProcessorFactory.java | 7 +- ...parser-module-setup-and-configuration.adoc | 1 + 34 files changed, 485 insertions(+), 430 deletions(-) create mode 100644 asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/NodeSinker.java delete mode 100644 asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/NodesSinker.java create mode 100644 asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/NoOpNodeProcessor.java diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index e5d18bf6..28ea77b3 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -23,6 +23,9 @@ Improvements:: * Added support for AsciidoctorJ v3.0.0 (#651) * Add compatibility with maven-site-plugin v3.20.0 and Doxia v2.0.0 (#933) * Add support for code blocks titles in asciidoctor-parser-doxia-module (#935) + * Refactor AST traversal method in asciidoctor-parser-doxia-module (#944) + * Empty titles in document or empty literals no longer generate

or
 in asciidoctor-parser-doxia-module (#944)
+  * Sections are now wrapped in 
in asciidoctor-parser-doxia-module (#944) Build / Infrastructure:: diff --git a/asciidoctor-maven-commons/src/main/java/org/asciidoctor/maven/site/HeadParser.java b/asciidoctor-maven-commons/src/main/java/org/asciidoctor/maven/site/HeadParser.java index cdbce1da..3da3d828 100644 --- a/asciidoctor-maven-commons/src/main/java/org/asciidoctor/maven/site/HeadParser.java +++ b/asciidoctor-maven-commons/src/main/java/org/asciidoctor/maven/site/HeadParser.java @@ -9,6 +9,8 @@ * This allows Doxia to build: * - breadcrumbs * - HTML head's meta elements + * + * @since 3.0.0 */ public class HeadParser { diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/AsciidoctorAstDoxiaParser.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/AsciidoctorAstDoxiaParser.java index 23d077f4..9ab0424a 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/AsciidoctorAstDoxiaParser.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/AsciidoctorAstDoxiaParser.java @@ -5,6 +5,7 @@ import java.io.File; import java.io.IOException; import java.io.Reader; +import java.util.List; import java.util.logging.Logger; import org.apache.maven.doxia.parser.AbstractTextParser; @@ -28,6 +29,7 @@ import org.asciidoctor.maven.site.SiteConversionConfiguration; import org.asciidoctor.maven.site.SiteConversionConfigurationParser; import org.asciidoctor.maven.site.SiteLogHandlerDeserializer; +import org.asciidoctor.maven.site.parser.processors.DescriptionListNodeProcessor; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.xml.Xpp3Dom; @@ -82,7 +84,7 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept final Asciidoctor asciidoctor = Asciidoctor.Factory.create(); SiteConversionConfiguration conversionConfig = new SiteConversionConfigurationParser(project) - .processAsciiDocConfig(siteConfig, defaultOptions(siteDirectory), defaultAttributes()); + .processAsciiDocConfig(siteConfig, defaultOptions(siteDirectory), defaultAttributes()); for (String require : conversionConfig.getRequires()) { requireLibrary(asciidoctor, require); } @@ -98,7 +100,7 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept try { // process log messages according to mojo configuration new LogRecordsProcessors(logHandler, siteDirectory, errorMessage -> logger.error(errorMessage)) - .processLogRecords(memoryLogHandler); + .processLogRecords(memoryLogHandler); } catch (Exception exception) { throw new ParseException(exception.getMessage(), exception); @@ -110,10 +112,8 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept new HeadParser(sink) .parse(headerMetadata); - sink.body(); - final NodesSinker nodesSinker = new NodesSinker(sink); - nodesSinker.processNode(document); - sink.body_(); + new NodeSinker(sink) + .sink(document); } private MemoryLogHandler asciidoctorLoggingSetup(Asciidoctor asciidoctor, LogHandler logHandler, File siteDirectory) { @@ -148,15 +148,15 @@ protected File resolveSiteDirectory(MavenProject project, Xpp3Dom siteConfig) { protected OptionsBuilder defaultOptions(File siteDirectory) { return Options.builder() - .backend("xhtml") - .safe(SafeMode.UNSAFE) - .baseDir(new File(siteDirectory, ROLE_HINT)); + .backend("xhtml") + .safe(SafeMode.UNSAFE) + .baseDir(new File(siteDirectory, ROLE_HINT)); } protected AttributesBuilder defaultAttributes() { return Attributes.builder() - .attribute("idprefix", "@") - .attribute("showtitle", "@"); + .attribute("idprefix", "@") + .attribute("showtitle", "@"); } private void requireLibrary(Asciidoctor asciidoctor, String require) { diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/NodeSinker.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/NodeSinker.java new file mode 100644 index 00000000..604bfced --- /dev/null +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/NodeSinker.java @@ -0,0 +1,66 @@ +package org.asciidoctor.maven.site.parser; + +import java.util.Arrays; +import java.util.List; + +import org.apache.maven.doxia.sink.Sink; +import org.asciidoctor.ast.StructuralNode; +import org.asciidoctor.maven.site.parser.processors.DescriptionListNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.DocumentNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.ImageNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.ListItemNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.ListingNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.LiteralNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.NoOpNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.OrderedListNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.ParagraphNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.PreambleNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.SectionNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.TableNodeProcessor; +import org.asciidoctor.maven.site.parser.processors.UnorderedListNodeProcessor; + +/** + * Factory and repository for NodeProcessors. + * + * @author abelsromero + * @since 3.1.0 + */ +public class NodeSinker { + + private final List nodeProcessors; + + private final NodeProcessor noOpProcessor; + + public NodeSinker(Sink sink) { + nodeProcessors = Arrays.asList( + new DescriptionListNodeProcessor(sink, this), + new DocumentNodeProcessor(sink, this), + new ImageNodeProcessor(sink, this), + new ListItemNodeProcessor(sink, this), + new ListingNodeProcessor(sink, this), + new ListingNodeProcessor(sink, this), + new LiteralNodeProcessor(sink, this), + new OrderedListNodeProcessor(sink, this), + new ParagraphNodeProcessor(sink, this), + new PreambleNodeProcessor(sink, this), + new SectionNodeProcessor(sink, this), + new TableNodeProcessor(sink, this), + new UnorderedListNodeProcessor(sink, this) + ); + noOpProcessor = new NoOpNodeProcessor(sink, this); + } + + /** + * Returns first NodeProcessor that can treat the node. + **/ + private NodeProcessor get(StructuralNode node) { + return nodeProcessors.stream() + .filter(nodeProcessor -> nodeProcessor.applies(node)) + .findFirst() + .orElse(noOpProcessor); + } + + public void sink(StructuralNode node) { + get(node).process(node); + } +} diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/NodesSinker.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/NodesSinker.java deleted file mode 100644 index 85dab98a..00000000 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/NodesSinker.java +++ /dev/null @@ -1,99 +0,0 @@ -package org.asciidoctor.maven.site.parser; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; - -import org.apache.maven.doxia.sink.Sink; -import org.asciidoctor.ast.StructuralNode; -import org.asciidoctor.maven.site.parser.processors.DescriptionListNodeProcessor; -import org.asciidoctor.maven.site.parser.processors.DocumentNodeProcessor; -import org.asciidoctor.maven.site.parser.processors.ImageNodeProcessor; -import org.asciidoctor.maven.site.parser.processors.ListItemNodeProcessor; -import org.asciidoctor.maven.site.parser.processors.ListingNodeProcessor; -import org.asciidoctor.maven.site.parser.processors.LiteralNodeProcessor; -import org.asciidoctor.maven.site.parser.processors.OrderedListNodeProcessor; -import org.asciidoctor.maven.site.parser.processors.ParagraphNodeProcessor; -import org.asciidoctor.maven.site.parser.processors.PreambleNodeProcessor; -import org.asciidoctor.maven.site.parser.processors.SectionNodeProcessor; -import org.asciidoctor.maven.site.parser.processors.TableNodeProcessor; -import org.asciidoctor.maven.site.parser.processors.UnorderedListNodeProcessor; - -/** - * Document processor. - * Responsible for initializing the different Node Processors - * and traverse the AST. - * - * @author abelsromero - * @since 3.0.0 - */ -public class NodesSinker { - - private final List nodeProcessors; - - /** - * Constructor. - * - * @param sink Doxia {@link Sink} - */ - public NodesSinker(Sink sink) { - - UnorderedListNodeProcessor unorderedListNodeProcessor = new UnorderedListNodeProcessor(sink); - OrderedListNodeProcessor orderedListNodeProcessor = new OrderedListNodeProcessor(sink); - DescriptionListNodeProcessor descriptionListNodeProcessor = new DescriptionListNodeProcessor(sink); - - ListItemNodeProcessor listItemNodeProcessor = new ListItemNodeProcessor(sink); - listItemNodeProcessor.setNodeProcessors(Arrays.asList(unorderedListNodeProcessor, orderedListNodeProcessor, descriptionListNodeProcessor)); - unorderedListNodeProcessor.setItemNodeProcessor(listItemNodeProcessor); - orderedListNodeProcessor.setItemNodeProcessor(listItemNodeProcessor); - descriptionListNodeProcessor.setItemNodeProcessor(listItemNodeProcessor); - - nodeProcessors = Arrays.asList( - new DocumentNodeProcessor(sink), - new ImageNodeProcessor(sink), - new ListingNodeProcessor(sink), - new LiteralNodeProcessor(sink), - new ParagraphNodeProcessor(sink), - new PreambleNodeProcessor(sink), - new SectionNodeProcessor(sink), - new TableNodeProcessor(sink), - descriptionListNodeProcessor, - orderedListNodeProcessor, - unorderedListNodeProcessor - ); - } - - /** - * Processes an Asciidoctor AST node. - * - * @param node Asciidoctor {@link StructuralNode} - */ - public void processNode(StructuralNode node) { - processNode(node, 0); - } - - private void processNode(StructuralNode node, int depth) { - try { - // Only one matches in current NodeProcessors implementation - Optional nodeProcessor = nodeProcessors.stream() - .filter(np -> np.applies(node)) - .findFirst(); - if (nodeProcessor.isPresent()) { - NodeProcessor processor = nodeProcessor.get(); - processor.process(node); - if (!processor.isTerminal(node)) { - traverse(node, depth); - } - } else { - traverse(node, depth); - } - } catch (Exception e) { - throw new RuntimeException("Could not process node", e); - } - } - - private void traverse(StructuralNode node, int depth) { - node.getBlocks() - .forEach(b -> processNode(b, depth + 1)); - } -} diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/AbstractSinkNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/AbstractSinkNodeProcessor.java index b6a6ff81..dc088eb6 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/AbstractSinkNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/AbstractSinkNodeProcessor.java @@ -2,6 +2,8 @@ import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.ast.ContentNode; +import org.asciidoctor.ast.StructuralNode; +import org.asciidoctor.maven.site.parser.NodeSinker; /** * Recommended base case to build a {@link org.asciidoctor.maven.site.parser.NodeProcessor}. @@ -12,14 +14,17 @@ public class AbstractSinkNodeProcessor { private final Sink sink; + private final NodeSinker nodeSinker; /** * Constructor. * - * @param sink Doxia {@link Sink} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public AbstractSinkNodeProcessor(Sink sink) { + public AbstractSinkNodeProcessor(Sink sink, NodeSinker nodeSinker) { this.sink = sink; + this.nodeSinker = nodeSinker; } /** @@ -31,6 +36,16 @@ protected Sink getSink() { return sink; } + /** + * Delegates the processing of the new node to the appropriate processor. + * Similar to {@link org.asciidoctor.maven.site.parser.NodeProcessor#process(StructuralNode)} + * but this selects the processor from the ones available. + * + * @param node Node to process + */ + protected void sink(StructuralNode node) { + nodeSinker.sink(node); + } /** * Tests for the presence of an attribute in current and parent nodes. diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/DescriptionListNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/DescriptionListNodeProcessor.java index 417d543b..4c02f7d2 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/DescriptionListNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/DescriptionListNodeProcessor.java @@ -8,6 +8,7 @@ import org.asciidoctor.ast.ListItem; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; /** * Description list processor. @@ -17,24 +18,14 @@ */ public class DescriptionListNodeProcessor extends AbstractSinkNodeProcessor implements NodeProcessor { - private ListItemNodeProcessor itemNodeProcessor; - /** * Constructor. * - * @param sink Doxia {@link Sink} - */ - public DescriptionListNodeProcessor(Sink sink) { - super(sink); - } - - /** - * Inject a {@link ListItemNodeProcessor}. - * - * @param nodeProcessor {@link ListItemNodeProcessor} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public void setItemNodeProcessor(ListItemNodeProcessor nodeProcessor) { - this.itemNodeProcessor = nodeProcessor; + public DescriptionListNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } @Override @@ -67,7 +58,7 @@ public void process(StructuralNode node) { if (description.getBlocks().isEmpty()) { sink.rawText(description.getText()); } else { - itemNodeProcessor.process(description); + super.sink(description); } sink.definition_(); } diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/DocumentNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/DocumentNodeProcessor.java index 5e2b6d42..9437cb1c 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/DocumentNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/DocumentNodeProcessor.java @@ -3,6 +3,9 @@ import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; + +import static org.asciidoctor.maven.commons.StringUtils.isNotBlank; /** * Root document processor. @@ -17,8 +20,8 @@ public class DocumentNodeProcessor extends AbstractSinkNodeProcessor implements * * @param sink Doxia {@link Sink} */ - public DocumentNodeProcessor(Sink sink) { - super(sink); + public DocumentNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } @Override @@ -28,7 +31,18 @@ public boolean applies(StructuralNode node) { @Override public void process(StructuralNode node) { - // NOTE: H1 generation fixed in doxia 2.0.0-MX - getSink().rawText(String.format("

%s

", node.getTitle())); + final Sink sink = getSink(); + + sink.body(); + String title = node.getTitle(); + if (isNotBlank(title)) { + sink.sectionTitle1(); + sink.rawText(title); + sink.sectionTitle1_(); + } + node.getBlocks() + .forEach(this::sink); + + sink.body_(); } } diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ImageNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ImageNodeProcessor.java index 6e372328..47a48d9b 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ImageNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ImageNodeProcessor.java @@ -7,6 +7,7 @@ import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; import static org.asciidoctor.maven.commons.StringUtils.isBlank; @@ -21,10 +22,11 @@ public class ImageNodeProcessor extends AbstractSinkNodeProcessor implements Nod /** * Constructor. * - * @param sink Doxia {@link Sink} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public ImageNodeProcessor(Sink sink) { - super(sink); + public ImageNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } @Override diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ListItemNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ListItemNodeProcessor.java index a84586f4..2f923a90 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ListItemNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ListItemNodeProcessor.java @@ -1,11 +1,10 @@ package org.asciidoctor.maven.site.parser.processors; -import java.util.List; - import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.ast.ListItem; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; /** * List items processor, including numbered and unnumbered. @@ -15,25 +14,14 @@ */ public class ListItemNodeProcessor extends AbstractSinkNodeProcessor implements NodeProcessor { - private List nodeProcessors; - /** * Constructor. * - * @param sink Doxia {@link Sink} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public ListItemNodeProcessor(Sink sink) { - super(sink); - - } - - /** - * Inject a list of {@link NodeProcessor}. - * - * @param nodeProcessors {@link List} of {@link NodeProcessor} - */ - public void setNodeProcessors(List nodeProcessors) { - this.nodeProcessors = nodeProcessors; + public ListItemNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } @Override @@ -60,13 +48,7 @@ public void process(StructuralNode node) { final String text = item.getText(); sink.rawText(text == null ? "" : text); - for (StructuralNode subNode : node.getBlocks()) { - for (NodeProcessor np : nodeProcessors) { - if (np.applies(subNode)) { - np.process(subNode); - } - } - } + node.getBlocks().forEach(this::sink); switch (listType) { case ordered: diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ListingNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ListingNodeProcessor.java index 37e6ce07..e885f0b0 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ListingNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ListingNodeProcessor.java @@ -2,9 +2,9 @@ import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.ast.StructuralNode; -import org.asciidoctor.jruby.ast.impl.BlockImpl; import org.asciidoctor.maven.commons.StringUtils; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; import static org.asciidoctor.maven.commons.StringUtils.isNotBlank; @@ -21,16 +21,14 @@ public class ListingNodeProcessor extends AbstractSinkNodeProcessor implements N private static final String LINENUMS_ATTRIBUTE = "linenums"; private static final String LINENUMS_OPTION_ATTRIBUTE = LINENUMS_ATTRIBUTE + "-option"; - private final ListItemNodeProcessor itemNodeProcessor; - /** * Constructor. * - * @param sink Doxia {@link Sink} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public ListingNodeProcessor(Sink sink) { - super(sink); - this.itemNodeProcessor = new ListItemNodeProcessor(sink); + public ListingNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } @Override @@ -56,7 +54,7 @@ public void process(StructuralNode node) { } contentBuilder.append("
");
         }
 
-        contentBuilder.append(((BlockImpl) node).getSource());
+        // Use 'content' (not 'source') to apply substitutions of special characters
+        contentBuilder.append((String) node.getContent());
 
         if (isSourceBlock) {
             contentBuilder.append("");
         }
 
         contentBuilder.append("
"); + getSink().rawText(contentBuilder.toString()); } private boolean isLinenumsEnabled(StructuralNode node) { // linenums attribute can be set with empty string value return LINENUMS_ATTRIBUTE.equals(node.getAttribute("linenums")) - || node.getAttribute(LINENUMS_OPTION_ATTRIBUTE) != null; + || node.getAttribute(LINENUMS_OPTION_ATTRIBUTE) != null; } private boolean isSourceBlock(String language, String style) { diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/LiteralNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/LiteralNodeProcessor.java index e4183917..e12ac6f8 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/LiteralNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/LiteralNodeProcessor.java @@ -3,7 +3,9 @@ import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.jruby.ast.impl.BlockImpl; +import org.asciidoctor.maven.commons.StringUtils; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; /** * Literal (aka. monospace) text processor. @@ -13,16 +15,14 @@ */ public class LiteralNodeProcessor extends AbstractSinkNodeProcessor implements NodeProcessor { - private final ListItemNodeProcessor itemNodeProcessor; - /** * Constructor. * - * @param sink Doxia {@link Sink} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public LiteralNodeProcessor(Sink sink) { - super(sink); - this.itemNodeProcessor = new ListItemNodeProcessor(sink); + public LiteralNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } @Override @@ -32,12 +32,15 @@ public boolean applies(StructuralNode node) { @Override public void process(StructuralNode node) { - final StringBuilder contentBuilder = new StringBuilder(); - contentBuilder.append("
");
-
-        contentBuilder.append(((BlockImpl) node).getSource());
-
-        contentBuilder.append("
"); - getSink().rawText(contentBuilder.toString()); + final Sink sink = getSink(); + + String source = ((BlockImpl) node).getSource(); + if (StringUtils.isNotBlank(source)) { + sink.division(); + sink.rawText("
");
+            sink.rawText(source);
+            sink.rawText("
"); + sink.division_(); + } } } diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/NoOpNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/NoOpNodeProcessor.java new file mode 100644 index 00000000..8aa8fc8e --- /dev/null +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/NoOpNodeProcessor.java @@ -0,0 +1,43 @@ +package org.asciidoctor.maven.site.parser.processors; + +import java.util.List; + +import org.apache.maven.doxia.sink.Sink; +import org.asciidoctor.ast.StructuralNode; +import org.asciidoctor.maven.site.parser.AsciidoctorAstDoxiaParser; +import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; +import org.slf4j.LoggerFactory; + +/** + * Fallback NodeProcessor to collect nodes that have not dedicated processor. + */ +public class NoOpNodeProcessor extends AbstractSinkNodeProcessor implements NodeProcessor { + + private final org.slf4j.Logger logger = LoggerFactory.getLogger(AsciidoctorAstDoxiaParser.class); + + /** + * Constructor. + * + * @param sink Doxia {@link Sink} + * @param nodeSinker + */ + public NoOpNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); + } + + @Override + public boolean applies(StructuralNode node) { + return false; + } + + @Override + public void process(StructuralNode node) { + final List blocks = node.getBlocks(); + + logger.warn("Fallback behaviour for node: {}", node.getNodeName()); + if (!blocks.isEmpty()) { + blocks.forEach(this::sink); + } + } +} diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/OrderedListNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/OrderedListNodeProcessor.java index a3fadd66..79dc5928 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/OrderedListNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/OrderedListNodeProcessor.java @@ -5,6 +5,7 @@ import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; /** * Ordered list processor. @@ -14,24 +15,14 @@ */ public class OrderedListNodeProcessor extends AbstractSinkNodeProcessor implements NodeProcessor { - private ListItemNodeProcessor itemNodeProcessor; - /** * Constructor. * - * @param sink Doxia {@link Sink} - */ - public OrderedListNodeProcessor(Sink sink) { - super(sink); - } - - /** - * Inject a {@link ListItemNodeProcessor}. - * - * @param nodeProcessor {@link ListItemNodeProcessor} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public void setItemNodeProcessor(ListItemNodeProcessor nodeProcessor) { - this.itemNodeProcessor = nodeProcessor; + public OrderedListNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } @Override @@ -46,23 +37,19 @@ public boolean isTerminal(StructuralNode node) { @Override public void process(StructuralNode node) { - final List items = node.getBlocks(); + final List subNodes = node.getBlocks(); final Sink sink = getSink(); - /** + /* * doxia numberingStyle 0: 1. 2. * 1: a. b. * 2: A. B. * 3: i. ii. * >: 1. 2. */ - if (!items.isEmpty()) { + if (!subNodes.isEmpty()) { sink.numberedList(0); - for (StructuralNode item : items) { - if (itemNodeProcessor.applies(item)) { - itemNodeProcessor.process(item); - } - } + subNodes.forEach(this::sink); sink.numberedList_(); } } diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ParagraphNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ParagraphNodeProcessor.java index 160cd043..23970c16 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ParagraphNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ParagraphNodeProcessor.java @@ -3,6 +3,7 @@ import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; /** * Paragraph processor. @@ -15,10 +16,11 @@ public class ParagraphNodeProcessor extends AbstractSinkNodeProcessor implements /** * Constructor. * - * @param sink Doxia {@link Sink} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public ParagraphNodeProcessor(Sink sink) { - super(sink); + public ParagraphNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } @Override @@ -28,11 +30,10 @@ public boolean applies(StructuralNode node) { @Override public void process(StructuralNode node) { - getSink().paragraph(); + final Sink sink = getSink(); + sink.paragraph(); // content returns HTML processed including bold, italics, monospace, etc. attributes resolution - // TODO run convert() instead of getContent? - String content = (String) node.getContent(); - getSink().rawText(content); - getSink().paragraph_(); + sink.rawText((String) node.getContent()); + sink.paragraph_(); } } diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/PreambleNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/PreambleNodeProcessor.java index dab84c86..85e4894d 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/PreambleNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/PreambleNodeProcessor.java @@ -3,6 +3,7 @@ import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; /** * Document preamble processor. @@ -15,10 +16,11 @@ public class PreambleNodeProcessor extends AbstractSinkNodeProcessor implements /** * Constructor. * - * @param sink Doxia {@link Sink} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public PreambleNodeProcessor(Sink sink) { - super(sink); + public PreambleNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } @Override @@ -31,5 +33,6 @@ public boolean applies(StructuralNode node) { **/ @Override public void process(StructuralNode node) { + node.getBlocks().forEach(this::sink); } } diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/SectionNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/SectionNodeProcessor.java index e75d665d..a1d06208 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/SectionNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/SectionNodeProcessor.java @@ -3,8 +3,8 @@ import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.ast.Section; import org.asciidoctor.ast.StructuralNode; -import org.asciidoctor.jruby.ast.impl.SectionImpl; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,10 +22,11 @@ public class SectionNodeProcessor extends AbstractSinkNodeProcessor implements N /** * Constructor. * - * @param sink Doxia {@link Sink} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public SectionNodeProcessor(Sink sink) { - super(sink); + public SectionNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } @Override @@ -35,11 +36,13 @@ public boolean applies(StructuralNode node) { @Override public void process(StructuralNode node) { - sectionTitle(getSink(), node.getLevel(), node.getTitle(), (Section) node); - } + final Sink sink = getSink(); + + final String title = node.getTitle(); + final String formattedTitle = formatTitle(title, (Section) node); - private void sectionTitle(Sink sink, int level, String title, Section node) { - final String formattedTitle = formatTitle(title, node); + sink.division(); + int level = node.getLevel(); if (level == 0) { // Kept for completeness, real document title is treated in // DocumentNodeProcessor @@ -55,10 +58,13 @@ private void sectionTitle(Sink sink, int level, String title, Section node) { siteLevel = 5; } sink.sectionTitle(siteLevel, null); - anchor(sink, node); + anchor(sink, (Section) node); sink.text(formattedTitle); sink.sectionTitle_(siteLevel); } + + node.getBlocks().forEach(this::sink); + sink.division_(); } private void anchor(Sink sink, Section node) { @@ -67,18 +73,17 @@ private void anchor(Sink sink, Section node) { } private String formatTitle(String title, Section node) { - final Boolean numbered = node.isNumbered(); + final boolean numbered = node.isNumbered(); final Long sectnumlevels = getSectnumlevels(node); final int level = node.getLevel(); if (numbered && level <= sectnumlevels) { - String sectnum = ((SectionImpl) node).getString("sectnum"); - return String.format("%s %s", sectnum, title); + return String.format("%s %s", node.getSectnum(), title); } return title; } private Long getSectnumlevels(Section node) { - Object sectnumlevels = node.getDocument().getAttribute("sectnumlevels"); + final Object sectnumlevels = node.getDocument().getAttribute("sectnumlevels"); if (sectnumlevels != null) { // Injecting from Maven configuration diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/TableNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/TableNodeProcessor.java index 1b47b353..fdb4dce7 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/TableNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/TableNodeProcessor.java @@ -8,6 +8,7 @@ import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.jruby.ast.impl.TableImpl; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; import static org.apache.maven.doxia.sink.Sink.JUSTIFY_LEFT; import static org.asciidoctor.maven.commons.StringUtils.isBlank; @@ -24,10 +25,11 @@ public class TableNodeProcessor extends AbstractSinkNodeProcessor implements Nod /** * Constructor. * - * @param sink Doxia {@link Sink} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public TableNodeProcessor(Sink sink) { - super(sink); + public TableNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } @Override @@ -43,6 +45,7 @@ public void process(StructuralNode node) { sink.table(); sink.tableRows(new int[]{JUSTIFY_LEFT}, false); List header = tableNode.getHeader(); + List blocks = node.getBlocks(); if (!header.isEmpty()) { sink.tableRow(); @@ -79,7 +82,8 @@ private void processCaption(StructuralNode node, Sink sink) { final String title = node.getTitle(); if (isNotBlank(title)) { - node.getContentModel(); + // TODO why do we do this next line? + // node.getContentModel(); sink.tableCaption(); // It's safe: getCaption returns "" when '[caption=]' is set if (isBlank(node.getCaption())) diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/UnorderedListNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/UnorderedListNodeProcessor.java index ecf67fbf..111be081 100644 --- a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/UnorderedListNodeProcessor.java +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/UnorderedListNodeProcessor.java @@ -5,6 +5,7 @@ import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; /** * Unordered list processor. @@ -19,10 +20,11 @@ public class UnorderedListNodeProcessor extends AbstractSinkNodeProcessor implem /** * Constructor. * - * @param sink Doxia {@link Sink} + * @param sink Doxia {@link Sink} + * @param nodeSinker */ - public UnorderedListNodeProcessor(Sink sink) { - super(sink); + public UnorderedListNodeProcessor(Sink sink, NodeSinker nodeSinker) { + super(sink, nodeSinker); } /** @@ -51,11 +53,7 @@ public void process(StructuralNode node) { if (!items.isEmpty()) { sink.list(); - for (StructuralNode item : items) { - if (itemNodeProcessor.applies(item)) { - itemNodeProcessor.process(item); - } - } + node.getBlocks().forEach(this::sink); sink.list_(); } } diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/AsciidoctorAstDoxiaParserTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/AsciidoctorAstDoxiaParserTest.java index 2a836449..fc30ef6e 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/AsciidoctorAstDoxiaParserTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/AsciidoctorAstDoxiaParserTest.java @@ -1,5 +1,11 @@ package org.asciidoctor.maven.site.parser; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.StringReader; +import java.io.StringWriter; + import lombok.SneakyThrows; import org.apache.maven.doxia.parser.AbstractTextParser; import org.apache.maven.doxia.parser.ParseException; @@ -10,8 +16,6 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import java.io.*; - import static org.asciidoctor.maven.site.parser.AsciidoctorAstDoxiaParserTest.TestMocks.mockAsciidoctorDoxiaParser; import static org.asciidoctor.maven.site.parser.processors.test.ReflectionUtils.extractField; import static org.asciidoctor.maven.site.parser.processors.test.StringTestUtils.clean; @@ -45,17 +49,23 @@ void should_convert_html_without_any_configuration() throws FileNotFoundExceptio assertThat(result) .isEqualTo("

Document Title

Preamble paragraph.

" + + "
"+ "

Section A

" + "

Section A paragraph.

" + + "
"+ "

Section A Subsection

" + "

Section A 'subsection' paragraph.

" + + "
"+ + "
"+ + "
"+ "

Section B

" + "

Section B paragraph.

" + "
    " + "
  • Item 1
  • " + "
  • Item 2
  • " + "
  • Item 3
" + - "
require 'asciidoctor'
"); + "
require 'asciidoctor'
" + + "
"); } @Test diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/NodeSinkerTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/NodeSinkerTest.java index b616ff2a..fc04e6ab 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/NodeSinkerTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/NodeSinkerTest.java @@ -1,10 +1,10 @@ package org.asciidoctor.maven.site.parser; import java.io.StringWriter; -import java.util.Arrays; import java.util.List; import org.apache.maven.doxia.sink.Sink; +import org.asciidoctor.ast.Block; import org.asciidoctor.ast.Document; import org.asciidoctor.ast.ListItem; import org.asciidoctor.ast.StructuralNode; @@ -25,20 +25,19 @@ */ class NodeSinkerTest { - private NodesSinker nodesSinker; + private NodeSinker nodeSinker; private StringWriter sinkWriter; - @BeforeEach void setup() throws NoSuchFieldException, IllegalAccessException { Sink sink = createSink(); - nodesSinker = new NodesSinker(sink); + nodeSinker = new NodeSinker(sink); sinkWriter = extractField(sink, "writer"); } @Test void should_init() { - assertThat(nodesSinker).isNotNull(); + assertThat(nodeSinker).isNotNull(); } @@ -46,7 +45,7 @@ void should_init() { void should_not_fail_when_processing_invalid_node() { StructuralNode mockNode = mockNode("this-is-not-a-node"); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isEmpty(); } @@ -54,8 +53,9 @@ void should_not_fail_when_processing_invalid_node() { @Test void should_process_document_node() { StructuralNode mockNode = mockNode("document"); + Mockito.when(mockNode.getTitle()).thenReturn("Something"); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isNotBlank(); } @@ -64,7 +64,7 @@ void should_process_document_node() { void should_process_preamble_literal() { StructuralNode mockNode = mockNode("literal", BlockImpl.class); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isNotBlank(); } @@ -75,7 +75,7 @@ void should_process_preamble_node() { StructuralNode literalBlock = mockNode("literal", BlockImpl.class); Mockito.when(mockNode.getBlocks()).thenReturn(List.of(literalBlock)); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isNotBlank(); } @@ -85,7 +85,7 @@ void should_process_paragraph_node() { StructuralNode mockNode = mockNode("paragraph"); Mockito.when(mockNode.getContent()).thenReturn("something"); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isNotBlank(); } @@ -97,7 +97,7 @@ void should_process_section_node() { Mockito.when(mockDocument.getAttribute(Mockito.anyString())).thenReturn(null); Mockito.when(mockNode.getDocument()).thenReturn(mockDocument); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isNotBlank(); } @@ -106,7 +106,7 @@ void should_process_section_node() { void should_process_table_node() { StructuralNode mockNode = mockNode("table", TableImpl.class); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isNotBlank(); } @@ -115,7 +115,7 @@ void should_process_table_node() { void should_process_listing_node() { StructuralNode mockNode = mockNode("listing", BlockImpl.class); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isNotBlank(); } @@ -125,7 +125,7 @@ void should_process_image_node() { StructuralNode mockNode = mockNode("image", BlockImpl.class); Mockito.when(mockNode.getAttribute(Mockito.eq("target"))).thenReturn("image.png"); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isNotBlank(); } @@ -134,7 +134,7 @@ void should_process_image_node() { void should_process_literal_node() { StructuralNode mockNode = mockNode("literal", BlockImpl.class); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isNotBlank(); } @@ -146,7 +146,7 @@ void should_process_ulist_node() { Mockito.when(mockListItem.getMarker()).thenReturn("*"); Mockito.when(mockNode.getBlocks()).thenReturn(List.of(mockListItem)); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isNotBlank(); } @@ -158,7 +158,7 @@ void should_process_olist_node() { Mockito.when(mockListItem.getMarker()).thenReturn("."); Mockito.when(mockNode.getBlocks()).thenReturn(List.of(mockListItem)); - nodesSinker.processNode(mockNode); + nodeSinker.sink(mockNode); assertThat(sinkWriter.toString()).isNotBlank(); } @@ -172,6 +172,9 @@ private static StructuralNode mockNode(String nodeName) { private static T mockNode(String nodeName, Class clazz) { StructuralNode mockNode = Mockito.mock(clazz); Mockito.when(mockNode.getNodeName()).thenReturn(nodeName); + if (Block.class.isAssignableFrom(clazz)) { + Mockito.when(((Block) mockNode).getSource()).thenReturn("Something"); + } return (T) mockNode; } } diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/DescriptionListNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/DescriptionListNodeProcessorTest.java index 740364d3..b4854b61 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/DescriptionListNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/DescriptionListNodeProcessorTest.java @@ -1,7 +1,6 @@ package org.asciidoctor.maven.site.parser.processors; import java.io.StringWriter; -import java.util.Arrays; import java.util.Collections; import org.apache.maven.doxia.sink.Sink; @@ -10,18 +9,9 @@ import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.maven.site.parser.NodeProcessor; import org.asciidoctor.maven.site.parser.processors.test.NodeProcessorTest; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.asciidoctor.maven.site.parser.processors.test.Html.LIST_STYLE_TYPE_DECIMAL; -import static org.asciidoctor.maven.site.parser.processors.test.Html.dd; -import static org.asciidoctor.maven.site.parser.processors.test.Html.dt; -import static org.asciidoctor.maven.site.parser.processors.test.Html.italics; -import static org.asciidoctor.maven.site.parser.processors.test.Html.li; -import static org.asciidoctor.maven.site.parser.processors.test.Html.monospace; -import static org.asciidoctor.maven.site.parser.processors.test.Html.ol; -import static org.asciidoctor.maven.site.parser.processors.test.Html.strong; -import static org.asciidoctor.maven.site.parser.processors.test.Html.ul; +import static org.asciidoctor.maven.site.parser.processors.test.Html.*; import static org.asciidoctor.maven.site.parser.processors.test.StringTestUtils.clean; import static org.assertj.core.api.Assertions.assertThat; @@ -33,21 +23,6 @@ class DescriptionListNodeProcessorTest { private Sink sink; private StringWriter sinkWriter; - - @BeforeEach - void setup() { - ListItemNodeProcessor listItemNodeProcessor = new ListItemNodeProcessor(sink); - ((DescriptionListNodeProcessor) nodeProcessor).setItemNodeProcessor(listItemNodeProcessor); - - OrderedListNodeProcessor oListNodeProcessor = new OrderedListNodeProcessor(sink); - oListNodeProcessor.setItemNodeProcessor(listItemNodeProcessor); - - UnorderedListNodeProcessor uListNodeProcessor = new UnorderedListNodeProcessor(sink); - uListNodeProcessor.setItemNodeProcessor(listItemNodeProcessor); - - listItemNodeProcessor.setNodeProcessors(Arrays.asList(nodeProcessor, oListNodeProcessor, uListNodeProcessor)); - } - @Test void should_convert_simple_list() { String content = buildDocumentWithSimpleList(); diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/DocumentNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/DocumentNodeProcessorTest.java index 6d19d252..3fd02335 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/DocumentNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/DocumentNodeProcessorTest.java @@ -1,5 +1,7 @@ package org.asciidoctor.maven.site.parser.processors; +import java.io.StringWriter; + import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.ast.StructuralNode; @@ -7,8 +9,6 @@ import org.asciidoctor.maven.site.parser.processors.test.NodeProcessorTest; import org.junit.jupiter.api.Test; -import java.io.StringWriter; - import static org.assertj.core.api.Assertions.assertThat; @NodeProcessorTest(DocumentNodeProcessor.class) @@ -18,6 +18,16 @@ class DocumentNodeProcessorTest { private NodeProcessor nodeProcessor; private StringWriter sinkWriter; + @Test + void should_not_fail_if_document_is_empty() { + String content = ""; + + String html = process(content, 0); + + assertThat(html) + .isEmpty(); + } + @Test void should_convert_document_title() { String content = "= Document tile"; @@ -25,7 +35,7 @@ void should_convert_document_title() { String html = process(content, 0); assertThat(html) - .isEqualTo("

Document tile

"); + .isEqualTo("

Document tile

"); } @Test @@ -35,7 +45,7 @@ void should_convert_document_title_with_markup() { String html = process(content, 0); assertThat(html) - .isEqualTo("

Document tile

"); + .isEqualTo("

Document tile

"); } private String process(String content, int level) { diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ImageNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ImageNodeProcessorTest.java index 3c95024a..c46ed1bd 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ImageNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ImageNodeProcessorTest.java @@ -22,7 +22,6 @@ class ImageNodeProcessorTest { private NodeProcessor nodeProcessor; private StringWriter sinkWriter; - @Test void should_convert_document_with_image() { String content = documentWithImage(); diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ListItemNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ListItemNodeProcessorTest.java index bdd8e124..c8deb13e 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ListItemNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ListItemNodeProcessorTest.java @@ -1,5 +1,8 @@ package org.asciidoctor.maven.site.parser.processors; +import java.io.StringWriter; +import java.util.Collections; + import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.ast.StructuralNode; @@ -9,9 +12,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.io.StringWriter; -import java.util.Collections; - import static org.assertj.core.api.Assertions.assertThat; @NodeProcessorTest(ListItemNodeProcessor.class) @@ -21,56 +21,55 @@ class ListItemNodeProcessorTest { private NodeProcessor nodeProcessor; private StringWriter sinkWriter; - @ParameterizedTest @ValueSource(strings = {"*", "-"}) void should_convert_list_item(String marker) { String content = new DocumentBuilder() - .listItem(marker) - .toString(); + .listItem(marker) + .toString(); String html = process(content); assertThat(html) - .isEqualTo(htmlListItem()); + .isEqualTo(htmlListItem()); } @Test void should_convert_ordered_list_item() { String content = new DocumentBuilder() - .listItem(".") - .toString(); + .listItem(".") + .toString(); String html = process(content); assertThat(html) - .isEqualTo(htmlListItem()); + .isEqualTo(htmlListItem()); } @ParameterizedTest @ValueSource(strings = {"*", "-"}) void should_convert_ordered_list_item_with_formatting() { String content = new DocumentBuilder() - .formattedListItem("*") - .toString(); + .formattedListItem("*") + .toString(); String html = process(content); assertThat(html) - .isEqualTo(htmlListItemWithFormatting()); + .isEqualTo(htmlListItemWithFormatting()); } @ParameterizedTest @ValueSource(strings = {"*", "-"}) void should_convert_ordered_list_item_with_link(String marker) { String content = new DocumentBuilder() - .linkListItem(marker) - .toString(); + .linkListItem(marker) + .toString(); String html = process(content); assertThat(html) - .isEqualTo(htmlListItemWithLink()); + .isEqualTo(htmlListItemWithLink()); } private static String htmlListItem() { @@ -117,8 +116,8 @@ public String toString() { private String process(String content) { StructuralNode node = asciidoctor.load(content, Options.builder().build()) - .findBy(Collections.singletonMap("context", ":list_item")) - .get(0); + .findBy(Collections.singletonMap("context", ":list_item")) + .get(0); nodeProcessor.process(node); diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ListingNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ListingNodeProcessorTest.java index 97f49c40..b1d5aa17 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ListingNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ListingNodeProcessorTest.java @@ -21,7 +21,6 @@ class ListingNodeProcessorTest { private NodeProcessor nodeProcessor; private StringWriter sinkWriter; - @Test void should_convert_full_source_block() { String content = documentWithFullSourceBlock(); diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/LiteralNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/LiteralNodeProcessorTest.java index a80f0b11..231e1a5f 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/LiteralNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/LiteralNodeProcessorTest.java @@ -19,26 +19,25 @@ class LiteralNodeProcessorTest { private NodeProcessor nodeProcessor; private StringWriter sinkWriter; - @Test void should_convert_simple_literal() { String content = documentWithLiteralBlock(); - String html = process(content, 0); + String html = process(content); assertThat(html) - .isEqualTo("
This is a literal line.
"); + .isEqualTo("
This is a literal line.
"); } private String documentWithLiteralBlock() { return "= Document tile\n\n" - + "== Section\n\n This is a literal line.\n"; + + "== Section\n\n This is a literal line.\n"; } - private String process(String content, int level) { + private String process(String content) { StructuralNode node = asciidoctor.load(content, Options.builder().build()) - .findBy(Collections.singletonMap("context", ":literal")) - .get(0); + .findBy(Collections.singletonMap("context", ":literal")) + .get(0); nodeProcessor.process(node); diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/OrderedListNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/OrderedListNodeProcessorTest.java index c47f64ae..290fb677 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/OrderedListNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/OrderedListNodeProcessorTest.java @@ -24,14 +24,6 @@ class OrderedListNodeProcessorTest { private Sink sink; private StringWriter sinkWriter; - - @BeforeEach - void setup() { - ListItemNodeProcessor listItemNodeProcessor = new ListItemNodeProcessor(sink); - ((OrderedListNodeProcessor) nodeProcessor).setItemNodeProcessor(listItemNodeProcessor); - listItemNodeProcessor.setNodeProcessors(Arrays.asList(nodeProcessor)); - } - @Test void should_convert_simple_list() { String content = buildDocumentWithSimpleList(); diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/PreambleNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/PreambleNodeProcessorTest.java index 6900ae27..99712620 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/PreambleNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/PreambleNodeProcessorTest.java @@ -1,5 +1,8 @@ package org.asciidoctor.maven.site.parser.processors; +import java.io.StringWriter; +import java.util.Collections; + import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.ast.StructuralNode; @@ -7,15 +10,8 @@ import org.asciidoctor.maven.site.parser.processors.test.NodeProcessorTest; import org.junit.jupiter.api.Test; -import java.io.StringWriter; -import java.util.Collections; - import static org.assertj.core.api.Assertions.assertThat; -/** - * PreambleNodeProcessor does nothing, contents are blocks processed - * by their respective processors. - */ @NodeProcessorTest(PreambleNodeProcessor.class) class PreambleNodeProcessorTest { @@ -30,7 +26,9 @@ void should_convert_preamble() { String html = process(content); assertThat(html) - .isEqualTo(""); + .isEqualTo("

This is a preamble." + + System.lineSeparator() + + "With two lines.

"); } @Test @@ -40,23 +38,25 @@ void should_convert_preamble_with_markup() { String html = process(content); assertThat(html) - .isEqualTo(""); + .isEqualTo("

This is a simple preamble.

"); } private String documentWithPreamble() { - return documentWithPreamble("This is a preamble.\nWith two lines."); + return documentWithPreamble("This is a preamble." + + System.lineSeparator() + + "With two lines."); } private String documentWithPreamble(String text) { return "= Document tile\n\n" - + text + "\n\n" - + "== Section\n\nSection body\n\n"; + + text + "\n\n" + + "== Section\n\nSection body\n\n"; } private String process(String content) { StructuralNode node = asciidoctor.load(content, Options.builder().build()) - .findBy(Collections.singletonMap("context", ":preamble")) - .get(0); + .findBy(Collections.singletonMap("context", ":preamble")) + .get(0); nodeProcessor.process(node); diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/SectionNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/SectionNodeProcessorTest.java index cb3cc0f3..89afada7 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/SectionNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/SectionNodeProcessorTest.java @@ -1,5 +1,8 @@ package org.asciidoctor.maven.site.parser.processors; +import java.io.StringWriter; +import java.util.Collections; + import org.asciidoctor.Asciidoctor; import org.asciidoctor.Attributes; import org.asciidoctor.Options; @@ -8,9 +11,6 @@ import org.asciidoctor.maven.site.parser.processors.test.NodeProcessorTest; import org.junit.jupiter.api.Test; -import java.io.StringWriter; -import java.util.Collections; - import static org.assertj.core.api.Assertions.assertThat; @NodeProcessorTest(SectionNodeProcessor.class) @@ -20,7 +20,6 @@ class SectionNodeProcessorTest { private NodeProcessor nodeProcessor; private StringWriter sinkWriter; - @Test void should_convert_document_title() { String content = documentWithSections(); @@ -28,7 +27,7 @@ void should_convert_document_title() { String html = process(content, 0); assertThat(html) - .isEqualTo("

Document tile

"); + .isEqualTo("

Document tile

"); } @Test @@ -38,7 +37,21 @@ void should_convert_section_level_2() { String html = process(content, 1); assertThat(html) - .isEqualTo("

First section title

"); + .isEqualTo("
" + + "

First section title

" + + "

First section body

" + + "
" + + "

Second section title

" + + "

Second section body

" + + "
" + + "

Third section title

" + + "

Third section body

" + + "
" + + "
Fourth section title
" + + "

Fourth section body

" + + "
" + + "
Fifth section title
" + + "

Fifth section body

"); } @Test @@ -48,7 +61,18 @@ void should_convert_section_level_3() { String html = process(content, 2); assertThat(html) - .isEqualTo("

Second section title

"); + .isEqualTo("
" + + "

Second section title

" + + "

Second section body

" + + "
" + + "

Third section title

" + + "

Third section body

" + + "
" + + "
Fourth section title
" + + "

Fourth section body

" + + "
" + + "
Fifth section title
" + + "

Fifth section body

"); } @Test @@ -58,7 +82,15 @@ void should_convert_section_level_4() { String html = process(content, 3); assertThat(html) - .isEqualTo("

Third section title

"); + .isEqualTo("
" + + "

Third section title

" + + "

Third section body

" + + "
" + + "
Fourth section title
" + + "

Fourth section body

" + + "
" + + "
Fifth section title
" + + "

Fifth section body

"); } @Test @@ -68,7 +100,12 @@ void should_convert_section_level_5() { String html = process(content, 4); assertThat(html) - .isEqualTo("
Fourth section title
"); + .isEqualTo("
" + + "
Fourth section title
" + + "

Fourth section body

" + + "
" + + "
Fifth section title
" + + "

Fifth section body

"); } @Test @@ -78,59 +115,74 @@ void should_convert_section_level_6() { String html = process(content, 5); assertThat(html) - .isEqualTo("
Fifth section title
"); + .isEqualTo("
" + + "
Fifth section title
" + + "

Fifth section body

"); } @Test void should_convert_section_with_sectionNumbers() { Attributes attributes = Attributes.builder() - .sectionNumbers(true) - .build(); + .sectionNumbers(true) + .build(); String content = documentWithSections(); // With numbering assertThat(process(content, 1, attributes)) - .isEqualTo("

1. First section title

"); - assertThat(process(content, 2, attributes)) - .isEqualTo("

1.1. Second section title

"); - assertThat(process(content, 3, attributes)) - .isEqualTo("

1.1.1. Third section title

"); - - // Without numbering by default - assertThat(process(content, 4, attributes)) - .isEqualTo("
Fourth section title
"); - assertThat(process(content, 5, attributes)) - .isEqualTo("
Fifth section title
"); + .isEqualTo("
" + + "

1. First section title

" + + "

First section body

" + + "
" + + "

1.1. Second section title

" + + "

Second section body

" + + "
" + + "

1.1.1. Third section title

" + + "

Third section body

" + + "
" + + "
Fourth section title
" + + "

Fourth section body

" + + "
" + + "
Fifth section title
" + + "

Fifth section body

"); } @Test void should_convert_section_with_sectionNumbers_and_sectNumLevels() { Attributes attributes = Attributes.builder() - .sectionNumbers(true) - .sectNumLevels(5) - .build(); + .sectionNumbers(true) + .sectNumLevels(5) + .build(); String content = documentWithSections(); // With numbering assertThat(process(content, 1, attributes)) - .isEqualTo("

1. First section title

"); - assertThat(process(content, 2, attributes)) - .isEqualTo("

1.1. Second section title

"); - assertThat(process(content, 3, attributes)) - .isEqualTo("

1.1.1. Third section title

"); - assertThat(process(content, 4, attributes)) - .isEqualTo("
1.1.1.1. Fourth section title
"); - assertThat(process(content, 5, attributes)) - .isEqualTo("
1.1.1.1.1. Fifth section title
"); + .isEqualTo("
" + + "

1. First section title

" + + "

First section body

" + + "
" + + "

1.1. Second section title

" + + "

Second section body

" + + "
" + + "

1.1.1. Third section title

" + + "

Third section body

" + + "
" + + "
1.1.1.1. Fourth section title
" + + "

Fourth section body

" + + "
" + + "
1.1.1.1.1. Fifth section title
" + + "

Fifth section body

"); } private String documentWithSections() { return "= Document tile\n\n" - + "== First section title\n\nFirst section body\n\n" - + "=== Second section title\n\nSecond section body\n\n" - + "==== Third section title\n\nThird section body\n\n" - + "===== Fourth section title\n\nFourth section body\n\n" - + "====== Fifth section title\n\nFifth section body\n\n"; + + "== First section title\n\nFirst section body\n\n" + + "=== Second section title\n\nSecond section body\n\n" + + "==== Third section title\n\nThird section body\n\n" + + "===== Fourth section title\n\nFourth section body\n\n" + + "====== Fifth section title\n\nFifth section body\n\n" + + "== First section title\n\nFirst section body\n\n" + + "=== Second section title\n\nSecond section body\n\n" + ; } private String process(String content, int level) { @@ -139,15 +191,19 @@ private String process(String content, int level) { private String process(String content, int level, Attributes attributes) { StructuralNode node = asciidoctor.load(content, Options.builder().attributes(attributes).build()) - .findBy(Collections.singletonMap("context", ":section")) - .stream() - .filter(n -> n.getLevel() == level) - .findFirst() - .get(); + .findBy(Collections.singletonMap("context", ":section")) + .stream() + .filter(n -> n.getLevel() == level) + .findFirst() + .get(); reset(sinkWriter); nodeProcessor.process(node); - return sinkWriter.toString().trim(); + return removeLineBreaks(sinkWriter.toString().trim()); + } + + private static String removeLineBreaks(String html) { + return html.replaceAll("(\r)?\n", ""); } private void reset(StringWriter sinkWriter) { diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/TableNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/TableNodeProcessorTest.java index 5bea20d9..4e2de710 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/TableNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/TableNodeProcessorTest.java @@ -36,7 +36,6 @@ class TableNodeProcessorTest { private NodeProcessor nodeProcessor; private StringWriter sinkWriter; - @Test void should_convert_table_with_header() { String content = documentWithTable(true, noCaption, emptyList()); diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/UnorderedListNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/UnorderedListNodeProcessorTest.java index c026de56..26d790e3 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/UnorderedListNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/UnorderedListNodeProcessorTest.java @@ -1,18 +1,16 @@ package org.asciidoctor.maven.site.parser.processors; +import java.io.StringWriter; +import java.util.Collections; + import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.maven.site.parser.NodeProcessor; import org.asciidoctor.maven.site.parser.processors.test.NodeProcessorTest; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.StringWriter; -import java.util.Arrays; -import java.util.Collections; - import static org.asciidoctor.maven.site.parser.processors.test.StringTestUtils.clean; import static org.assertj.core.api.Assertions.assertThat; @@ -24,14 +22,6 @@ class UnorderedListNodeProcessorTest { private Sink sink; private StringWriter sinkWriter; - - @BeforeEach - void setup() { - ListItemNodeProcessor listItemNodeProcessor = new ListItemNodeProcessor(sink); - ((UnorderedListNodeProcessor) nodeProcessor).setItemNodeProcessor(listItemNodeProcessor); - listItemNodeProcessor.setNodeProcessors(Arrays.asList(nodeProcessor)); - } - @Test void should_convert_simple_list() { String content = buildDocumentWithSimpleList(); @@ -39,10 +29,10 @@ void should_convert_simple_list() { String html = process(content); assertThat(html) - .isEqualTo("
    " + - "
  • unordered item 1
  • " + - "
  • unordered item 2
  • " + - "
"); + .isEqualTo("
    " + + "
  • unordered item 1
  • " + + "
  • unordered item 2
  • " + + "
"); } @Test @@ -52,44 +42,44 @@ void should_convert_nested_list() { String html = process(content); assertThat(html) - .isEqualTo("
    " + - "
  • unordered item 1" + - "
      " + - "
    • unordered item 1 1
    • " + - "
    • unordered item 1 2
    • " + - "
    " + - "
  • " + - "
  • unordered item 2" + - "
      " + - "
    • unordered item 2 1" + - "
        " + - "
      • unordered item 2 1 1
    " + - "
  • " + - "
"); + .isEqualTo("
    " + + "
  • unordered item 1" + + "
      " + + "
    • unordered item 1 1
    • " + + "
    • unordered item 1 2
    • " + + "
    " + + "
  • " + + "
  • unordered item 2" + + "
      " + + "
    • unordered item 2 1" + + "
        " + + "
      • unordered item 2 1 1
    " + + "
  • " + + "
"); } private static String buildDocumentWithSimpleList() { return "= Document tile\n\n" - + "== Section\n\n" - + "* unordered item 1\n" - + "* unordered item 2\n"; + + "== Section\n\n" + + "* unordered item 1\n" + + "* unordered item 2\n"; } private static String buildDocumentWithNestedLists() { return "= Document tile\n\n" - + "== Section\n\n" - + "* unordered item 1\n" - + "** unordered item 1 1\n" - + "** unordered item 1 2\n" - + "* unordered item 2\n" - + "** unordered item 2 1\n" - + "*** unordered item 2 1 1\n"; + + "== Section\n\n" + + "* unordered item 1\n" + + "** unordered item 1 1\n" + + "** unordered item 1 2\n" + + "* unordered item 2\n" + + "** unordered item 2 1\n" + + "*** unordered item 2 1 1\n"; } private String process(String content) { StructuralNode node = asciidoctor.load(content, Options.builder().build()) - .findBy(Collections.singletonMap("context", ":ulist")) - .get(0); + .findBy(Collections.singletonMap("context", ":ulist")) + .get(0); nodeProcessor.process(node); diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/test/TestNodeProcessorFactory.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/test/TestNodeProcessorFactory.java index d96e4445..3ae38be5 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/test/TestNodeProcessorFactory.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/test/TestNodeProcessorFactory.java @@ -8,6 +8,7 @@ import org.apache.maven.doxia.siterenderer.DocumentRenderingContext; import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink; import org.asciidoctor.maven.site.parser.NodeProcessor; +import org.asciidoctor.maven.site.parser.NodeSinker; import org.mockito.Mockito; public class TestNodeProcessorFactory { @@ -15,8 +16,10 @@ public class TestNodeProcessorFactory { @SneakyThrows public static Pair create(Class clazz) { final Sink siteRendererSink = createSink(); - Constructor constructor = clazz.getConstructor(Sink.class); - return Pair.of(constructor.newInstance(siteRendererSink), siteRendererSink); + final NodeSinker nodeSinker = new NodeSinker(siteRendererSink); + + Constructor constructor = clazz.getConstructor(Sink.class, NodeSinker.class); + return Pair.of(constructor.newInstance(siteRendererSink, nodeSinker), siteRendererSink); } public static Sink createSink() { diff --git a/docs/modules/site-integration/pages/parser-module-setup-and-configuration.adoc b/docs/modules/site-integration/pages/parser-module-setup-and-configuration.adoc index 031c7b78..91c05ed1 100644 --- a/docs/modules/site-integration/pages/parser-module-setup-and-configuration.adoc +++ b/docs/modules/site-integration/pages/parser-module-setup-and-configuration.adoc @@ -168,6 +168,7 @@ We are aware this is not ideal and are tracking any development on the Maven sid This module is still under development, here is a summary of supported features: * Document Title, present in page +* Document preamble * Section titles from 1 to level 5 ** Support for `sectnums` and `sectnumlevels`