From 24e23656172eab83274f975d490273829e943165 Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Sun, 13 Oct 2024 22:44:02 +0200 Subject: [PATCH] Added: tables, liusting, figure, partial example. Missing appendix # Conflicts: # asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/NodesSinker.java # asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ListingNodeProcessor.java # asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/TableNodeProcessor.java --- .../maven/site/parser/NodesSinker.java | 0 .../processors/ExampleNodeProcessor.java | 59 +++++++++++++++++++ .../parser/processors/ImageNodeProcessor.java | 22 +++++-- .../processors/ListingNodeProcessor.java | 19 +++--- .../parser/processors/SinkAttributes.java | 16 +++++ .../maven/site/parser/processors/Styles.java | 6 ++ .../parser/processors/TableNodeProcessor.java | 12 ++-- .../parser/processors/TitleExtractor.java | 17 ++++++ 8 files changed, 133 insertions(+), 18 deletions(-) create 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/ExampleNodeProcessor.java create mode 100644 asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/SinkAttributes.java create mode 100644 asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/Styles.java create mode 100644 asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/TitleExtractor.java 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 new file mode 100644 index 00000000..e69de29b diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ExampleNodeProcessor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ExampleNodeProcessor.java new file mode 100644 index 00000000..5ab95fb5 --- /dev/null +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ExampleNodeProcessor.java @@ -0,0 +1,59 @@ +package org.asciidoctor.maven.site.parser.processors; + +import org.apache.maven.doxia.sink.Sink; +import org.asciidoctor.ast.StructuralNode; +import org.asciidoctor.maven.site.parser.NodeProcessor; + +import javax.swing.text.html.HTML.Attribute; +import java.nio.file.FileSystems; + +import static javax.swing.text.html.HTML.Attribute.ALT; +import static org.asciidoctor.maven.commons.StringUtils.isBlank; +import static org.asciidoctor.maven.commons.StringUtils.isNotBlank; + +/** + * Inline images are processed as paragraphs. + * + * @author abelsromero + * @since 3.0.0 + */ +public class ExampleNodeProcessor extends AbstractSinkNodeProcessor implements NodeProcessor { + + /** + * Constructor. + * + * @param sink Doxia {@link Sink} + */ + public ExampleNodeProcessor(Sink sink) { + super(sink); + } + + @Override + public boolean applies(StructuralNode node) { + return "example".equals(node.getNodeName()); + } + + @Override + public void process(StructuralNode node) { + // Add caption as a div (same as Asciidoctor): + // - For consistency + // - Using `figureCaption` requires wrapping the image in
which adds indentation + final Sink sink = getSink(); +// sink.division(); + final String title = TitleExtractor.getText(node); + if (isNotBlank(title)) { + sink.division(SinkAttributes.of(Attribute.STYLE, Styles.CAPTION)); + sink.text(title); + sink.division_(); + } +// sink.division_(); + } + + private String formatPath(String imagesdir, String target) { + if (imagesdir.endsWith("/") || imagesdir.endsWith("\\")) { + return imagesdir + target; + } else { + return imagesdir + FileSystems.getDefault().getSeparator() + target; + } + } +} 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 47a48d9b..910d1aec 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 @@ -4,12 +4,13 @@ import java.nio.file.FileSystems; import org.apache.maven.doxia.sink.Sink; -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 javax.swing.text.html.HTML.Attribute.ALT; import static org.asciidoctor.maven.commons.StringUtils.isBlank; +import static org.asciidoctor.maven.commons.StringUtils.isNotBlank; /** * Inline images are processed as paragraphs. @@ -40,12 +41,21 @@ public void process(StructuralNode node) { final String alt = (String) node.getAttribute("alt"); final String imagesdir = (String) node.getAttribute("imagesdir"); - String imagePath = isBlank(imagesdir) ? target : formatPath(imagesdir, target); - final SinkEventAttributeSet attributes = new SinkEventAttributeSet(); - if (!isBlank(alt)) - attributes.addAttribute(Attribute.ALT, alt); + final String imagePath = isBlank(imagesdir) ? target : formatPath(imagesdir, target); - getSink().figureGraphics(imagePath, attributes); + // Add caption as a div (same as Asciidoctor): + // - For consistency + // - Using `figureCaption` requires wrapping the image in
which adds indentation + final Sink sink = getSink(); + sink.division(); + sink.figureGraphics(imagePath, !isBlank(alt) ? SinkAttributes.of(ALT, alt) : null); + final String title = TitleExtractor.getText(node); + if (isNotBlank(title)) { + sink.division(SinkAttributes.of(Attribute.STYLE, Styles.CAPTION)); + sink.text(title); + sink.division_(); + } + sink.division_(); } private String formatPath(String imagesdir, String target) { 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 e885f0b0..4e8b04e2 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 @@ -3,6 +3,7 @@ import org.apache.maven.doxia.sink.Sink; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.maven.commons.StringUtils; +import org.asciidoctor.jruby.ast.impl.BlockImpl; import org.asciidoctor.maven.site.parser.NodeProcessor; import org.asciidoctor.maven.site.parser.NodeSinker; @@ -39,20 +40,15 @@ public boolean applies(StructuralNode node) { @Override public void process(StructuralNode node) { final StringBuilder contentBuilder = new StringBuilder(); - String language = (String) node.getAttribute("language"); - String style = node.getStyle(); + final String language = (String) node.getAttribute("language"); + final String style = node.getStyle(); boolean isSourceBlock = isSourceBlock(language, style); if (isSourceBlock) { // source class triggers prettify auto-detection contentBuilder.append("
"); - - final String title = node.getTitle(); - if (StringUtils.isNotBlank(title)) { - contentBuilder.append("
" + title + "
"); - } - + processTitle(node, contentBuilder); contentBuilder.append("
" + title + "
"); + } + } + private boolean isLinenumsEnabled(StructuralNode node) { // linenums attribute can be set with empty string value return LINENUMS_ATTRIBUTE.equals(node.getAttribute("linenums")) diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/SinkAttributes.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/SinkAttributes.java new file mode 100644 index 00000000..6636e741 --- /dev/null +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/SinkAttributes.java @@ -0,0 +1,16 @@ +package org.asciidoctor.maven.site.parser.processors; + +import javax.swing.text.html.HTML.Attribute; + +import org.apache.maven.doxia.sink.SinkEventAttributes; +import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet; + +class SinkAttributes { + + static SinkEventAttributes of(Attribute name, String value) { + final var attributes = new SinkEventAttributeSet(); + attributes.addAttribute(name, value); + return attributes; + } + +} diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/Styles.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/Styles.java new file mode 100644 index 00000000..31e24b9b --- /dev/null +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/Styles.java @@ -0,0 +1,6 @@ +package org.asciidoctor.maven.site.parser.processors; + +class Styles { + + public static final String CAPTION = "color: #7a2518; margin-bottom: .25em;"; +} 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 fdb4dce7..100c3536 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 @@ -45,7 +45,6 @@ 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(); @@ -78,14 +77,19 @@ public void process(StructuralNode node) { private void processCaption(StructuralNode node, Sink sink) { // 'null' when not set or '[caption=]' final String tableCaption = (String) node.getAttribute("table-caption"); + final String caption = node.getCaption(); // disable single caption + // if "[caption=]" -> remove caption + // disable too, when ":table-caption!:" + final String title = node.getTitle(); if (isNotBlank(title)) { - // TODO why do we do this next line? - // node.getContentModel(); sink.tableCaption(); - // It's safe: getCaption returns "" when '[caption=]' is set + sink.figureCaption(); + // getCaption returns + // - "" when '[caption=]' + // - null when ':table-caption!: if (isBlank(node.getCaption())) sink.text(node.getTitle()); else diff --git a/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/TitleExtractor.java b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/TitleExtractor.java new file mode 100644 index 00000000..95bc7350 --- /dev/null +++ b/asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/TitleExtractor.java @@ -0,0 +1,17 @@ +package org.asciidoctor.maven.site.parser.processors; + +import org.asciidoctor.ast.StructuralNode; + +import static org.asciidoctor.maven.commons.StringUtils.isBlank; + +class TitleExtractor { + + static String getText(StructuralNode node) { + // Caption is returned when a title is set in: + // - Listings + // - Image blocks + final String caption = node.getCaption(); + return isBlank(caption) ? node.getTitle() : caption + node.getTitle(); + } + +}