Skip to content

Commit

Permalink
Added: tables, liusting, figure, partial example. Missing appendix
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
abelsromero committed Oct 19, 2024
1 parent 912de34 commit 24e2365
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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 <figure> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 <figure> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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("<div class=\"source\">");

final String title = node.getTitle();
if (StringUtils.isNotBlank(title)) {
contentBuilder.append("<div style=\"color: #7a2518; margin-bottom: .25em;\" >" + title + "</div>");
}

processTitle(node, contentBuilder);
contentBuilder.append("<pre class=\"")
.append(FLUIDO_SKIN_SOURCE_HIGHLIGHTER);
if (isLinenumsEnabled(node))
Expand All @@ -77,6 +73,13 @@ public void process(StructuralNode node) {
getSink().rawText(contentBuilder.toString());
}

private static void processTitle(StructuralNode node, StringBuilder contentBuilder) {
final String title = TitleExtractor.getText(node);
if (isNotBlank(title)) {
contentBuilder.append("<div style=\"" + Styles.CAPTION + "\" >" + title + "</div>");
}
}

private boolean isLinenumsEnabled(StructuralNode node) {
// linenums attribute can be set with empty string value
return LINENUMS_ATTRIBUTE.equals(node.getAttribute("linenums"))
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.asciidoctor.maven.site.parser.processors;

class Styles {

public static final String CAPTION = "color: #7a2518; margin-bottom: .25em;";
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void process(StructuralNode node) {
sink.table();
sink.tableRows(new int[]{JUSTIFY_LEFT}, false);
List<Row> header = tableNode.getHeader();
List<StructuralNode> blocks = node.getBlocks();
if (!header.isEmpty()) {
sink.tableRow();

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 24e2365

Please sign in to comment.