Skip to content

Commit

Permalink
Force white background in parser-doxia-module tables (#974)
Browse files Browse the repository at this point in the history
* This prevents other colors from leaking into white rows.
For example when the table is in an example block.
* Refactor some test
* Correct use of swing constants by String for styles

Fixes #948
  • Loading branch information
abelsromero authored Nov 2, 2024
1 parent f9c8e67 commit f18c942
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 75 deletions.
6 changes: 6 additions & 0 deletions asciidoctor-maven-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-core</artifactId>
<version>2.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.asciidoctor.maven.site.parser.NodeProcessor;
import org.asciidoctor.maven.site.parser.NodeSinker;

import static javax.swing.text.html.HTML.Attribute.STYLE;
import static org.apache.maven.doxia.sink.SinkEventAttributes.STYLE;
import static org.asciidoctor.maven.commons.StringUtils.isNotBlank;

/**
Expand Down Expand Up @@ -50,20 +50,20 @@ public void process(StructuralNode node) {

final List<StructuralNode> blocks = node.getBlocks();
if (!blocks.isEmpty()) {
divWrap(sink, node, () -> blocks.forEach(this::sink));
divWrap(sink, () -> blocks.forEach(this::sink));
} else {
// For :content_model: simple (inline)
// https://docs.asciidoctor.org/asciidoc/latest/blocks/example-blocks/#example-style-syntax
final String content = (String) node.getContent();
if (isNotBlank(content)) {
divWrap(sink, node, () -> sink.rawText(content));
divWrap(sink, () -> sink.rawText(content));
}
}

sink.division_();
}

void divWrap(Sink sink, StructuralNode node, Runnable consumer) {
void divWrap(Sink sink, Runnable consumer) {
sink.division(SinkAttributes.of(STYLE, Styles.EXAMPLE));
consumer.run();
sink.division_();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
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 javax.swing.text.html.HTML.Attribute.STYLE;
import static org.apache.maven.doxia.sink.SinkEventAttributes.ALT;
import static org.apache.maven.doxia.sink.SinkEventAttributes.STYLE;
import static org.asciidoctor.maven.commons.StringUtils.isBlank;
import static org.asciidoctor.maven.commons.StringUtils.isNotBlank;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
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) {
static SinkEventAttributes of(String name, String value) {
final var attributes = new SinkEventAttributeSet();
attributes.addAttribute(name, value);
return attributes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ class Styles {
"margin-bottom: 1.25em",
"padding: 1.25em"
).collect(Collectors.joining("; "));

public static final String TABLE = "background: #FFFFFF";

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.asciidoctor.maven.site.parser.NodeProcessor;
import org.asciidoctor.maven.site.parser.NodeSinker;

import static javax.swing.text.html.HTML.Attribute.STYLE;
import static org.apache.maven.doxia.sink.Sink.JUSTIFY_LEFT;
import static org.apache.maven.doxia.sink.SinkEventAttributes.STYLE;
import static org.asciidoctor.maven.commons.StringUtils.isNotBlank;

/**
Expand Down Expand Up @@ -42,7 +42,7 @@ public void process(StructuralNode node) {
final TableImpl tableNode = (TableImpl) node;

final Sink sink = getSink();
sink.table();
sink.table(SinkAttributes.of(STYLE, Styles.TABLE));
sink.tableRows(new int[]{JUSTIFY_LEFT}, false);
final List<Row> header = tableNode.getHeader();
final List<Row> rows = tableNode.getBody();
Expand Down Expand Up @@ -92,7 +92,6 @@ private void processCaption(StructuralNode node, Sink sink) {
final String title = TitleCaptionExtractor.getText(node);
if (isNotBlank(title)) {
// Contrary to other cases where we use <div>, we use <caption>: same as Fluido and Asciidoctor
// TODO Have a proper CSS stylesheet injected
sink.tableCaption(SinkAttributes.of(STYLE, Styles.CAPTION + "; text-align: left"));
// getCaption returns
// - "" when '[caption=]'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.StringWriter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.asciidoctor.Asciidoctor;
Expand All @@ -20,9 +21,6 @@
@NodeProcessorTest(ExampleNodeProcessor.class)
class ExampleNodeProcessorTest {

public static final String EXAMPLE_TITLE_OPENING = "<div style=\"color: #7a2518; margin-bottom: .25em\">";
public static final String EXAMPLE_CONTENT_OPENING = "<div style=\"background: #fffef7; border-color: #e0e0dc; border: 1px solid #e6e6e6; box-shadow: 0 1px 4px #e0e0dc; margin-bottom: 1.25em; padding: 1.25em\">";

private Asciidoctor asciidoctor;
private NodeProcessor nodeProcessor;
private StringWriter sinkWriter;
Expand All @@ -45,9 +43,7 @@ void should_convert_minimal_example() {

assertThat(html)
.isEqualTo(div(
EXAMPLE_CONTENT_OPENING +
p("SomeText") +
"</div>"));
exampleContentDiv(p("SomeText"))));
}

@Test
Expand All @@ -57,11 +53,7 @@ void should_convert_example_with_title() {
String html = process(content);

assertThat(html)
.isEqualTo(div(
EXAMPLE_TITLE_OPENING + "Example 1. The title</div>" +
EXAMPLE_CONTENT_OPENING +
p("SomeText") +
"</div>"));
.isEqualTo(div(exampleTitleDiv("Example 1. The title") + exampleContentDiv(p("SomeText"))));
}

@Test
Expand All @@ -71,11 +63,7 @@ void should_convert_example_with_title_without_caption() {
String html = process(content);

assertThat(html)
.isEqualTo(div(
EXAMPLE_TITLE_OPENING + "The title</div>" +
EXAMPLE_CONTENT_OPENING +
p("SomeText") +
"</div>"));
.isEqualTo(div(exampleTitleDiv("The title") + exampleContentDiv(p("SomeText"))));
}

@Test
Expand All @@ -87,9 +75,9 @@ void should_convert_with_nested_link() {

assertThat(html)
.isEqualTo(div(
EXAMPLE_CONTENT_OPENING +
p("<a href=\"https://docs.asciidoctor.org/\" class=\"bare\">https://docs.asciidoctor.org/</a>") +
"</div>"));
exampleContentDiv(
p("<a href=\"https://docs.asciidoctor.org/\" class=\"bare\">https://docs.asciidoctor.org/</a>")
)));
}

@Test
Expand All @@ -105,12 +93,12 @@ void should_convert_with_nested_table() {

assertThat(html)
.isEqualTo(div(
EXAMPLE_CONTENT_OPENING +
"<table class=\"bodyTable\">" +
"<tr class=\"a\"><td style=\"text-align: left;\">Header 1</td><td>Header 2</td></tr>" +
"<tr class=\"b\"><td style=\"text-align: left;\">Column 1, row 1</td><td>Column 2, row 1</td></tr>" +
"</table>" +
"</div>"));
exampleContentDiv(
"<table class=\"bodyTable\" style=\"background: #FFFFFF\">" +
tr("a", td("Header 1", Map.of("style", "text-align: left;")) + td("Header 2")) +
tr("b", td("Column 1, row 1", Map.of("style", "text-align: left;")) + td("Column 2, row 1")) +
"</table>"
)));
}

@Test
Expand All @@ -125,12 +113,10 @@ void should_convert_with_nested_list() {

assertThat(html)
.isEqualTo(div(
EXAMPLE_CONTENT_OPENING +
ul(
li("Un" + ul(li("Dos"))) +
li("Tres" + ul(li("Quatre")))
) +
"</div>"));
exampleContentDiv(ul(
li("Un" + ul(li("Dos"))),
li("Tres" + ul(li("Quatre")))
))));
}

@Test
Expand All @@ -146,13 +132,13 @@ void should_convert_with_multiple_nested_elements() {

assertThat(html)
.isEqualTo(div(
EXAMPLE_CONTENT_OPENING +
exampleContentDiv(
ul(
li("Un" + ul(li("Dos"))) +
li("Tres" + ul(li("Quatre")))
li("Un" + ul(li("Dos"))),
li("Tres" + ul(li("Quatre")))
) +
p("<a href=\"https://docs.asciidoctor.org/\" class=\"bare\">https://docs.asciidoctor.org/</a>") +
"</div>"));
p("<a href=\"https://docs.asciidoctor.org/\" class=\"bare\">https://docs.asciidoctor.org/</a>")
)));
}

private String documentWithExample(String text, String title, List<String> attributes) {
Expand Down Expand Up @@ -187,10 +173,7 @@ void should_convert_minimal_example() {

// Content is directly embedded instead of delegated to paragraph processor
assertThat(html)
.isEqualTo(div(
EXAMPLE_CONTENT_OPENING +
"SomeText" +
"</div>"));
.isEqualTo(div(exampleContentDiv("SomeText")));
}

@Test
Expand All @@ -203,11 +186,7 @@ void should_convert_minimal_example_with_title() {
String html = process(content);

assertThat(html)
.isEqualTo(div(
EXAMPLE_TITLE_OPENING + "Example 1. Optional title</div>" +
EXAMPLE_CONTENT_OPENING +
"SomeText" +
"</div>"));
.isEqualTo(div(exampleTitleDiv("Example 1. Optional title") + exampleContentDiv("SomeText")));
}

@Test
Expand All @@ -220,10 +199,15 @@ void should_convert_minimal_example_with_link() {
String html = process(content);

assertThat(html)
.isEqualTo(div(
EXAMPLE_CONTENT_OPENING +
"SomeText, <a href=\"https://docs.asciidoctor.org/\" class=\"bare\">https://docs.asciidoctor.org/</a>" +
"</div>"));
.isEqualTo(div(exampleContentDiv("SomeText, <a href=\"https://docs.asciidoctor.org/\" class=\"bare\">https://docs.asciidoctor.org/</a>")));
}
}

private static String exampleTitleDiv(String text) {
return div("color: #7a2518; margin-bottom: .25em", text);
}

private static String exampleContentDiv(String text) {
return div("background: #fffef7; border-color: #e0e0dc; border: 1px solid #e6e6e6; box-shadow: 0 1px 4px #e0e0dc; margin-bottom: 1.25em; padding: 1.25em", text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ void should_convert_table_with_header() {

// Header for now is just first row with class=a
assertThat(html)
.isEqualTo("<table class=\"bodyTable\">" +
.isEqualTo("<table class=\"bodyTable\" style=\"background: #FFFFFF\">" +
"<tr class=\"a\">" +
"<th>Name</th>" +
"<th>Language</th></tr>" +
"<tr class=\"b\">" +
"<td style=\"text-align: left;\">JRuby</td>" +
"<td>Java</td></tr>" +
td("JRuby", Map.of("style", "text-align: left;")) +
td("Java") + "</tr>" +
"<tr class=\"a\">" +
"<td style=\"text-align: left;\">Rubinius</td>" +
"<td>Ruby</td></tr></table>");
Expand All @@ -83,7 +83,7 @@ void should_convert_table_without_header() {
String html = process(content);

assertThat(html)
.isEqualTo(removeLineBreaks("<table class=\"bodyTable\">" +
.isEqualTo(removeLineBreaks("<table class=\"bodyTable\" style=\"background: #FFFFFF\">" +
"<tr class=\"a\">" +
"<td style=\"text-align: left;\">JRuby</td>" +
"<td>Java</td></tr>" +
Expand All @@ -109,7 +109,8 @@ void should_convert_table_with_caption_and_title() {
String html = process(content);

assertThat(html)
.isEqualTo("<table class=\"bodyTable\"><caption style=\"" + CAPTION_STYLE + "\">Table 1. Table caption&#8230;&#8203;or title</caption>" +
.isEqualTo("<table class=\"bodyTable\" style=\"background: #FFFFFF\">" +
"<caption style=\"" + CAPTION_STYLE + "\">Table 1. Table caption&#8230;&#8203;or title</caption>" +
"<tr class=\"a\">" +
"<td style=\"text-align: left;\">JRuby</td>" +
"<td>Java</td></tr>" +
Expand Down Expand Up @@ -151,7 +152,7 @@ void formatted_text() {
String html = process(content);

assertThat(html)
.isEqualTo("<table class=\"bodyTable\">" +
.isEqualTo("<table class=\"bodyTable\" style=\"background: #FFFFFF\">" +
tr("a", td("JRuby", textAlignLeft()) + td("Java")) +
tr("b", td("Rubinius", textAlignLeft()) + td("Ruby")) +
tr("a",
Expand All @@ -168,7 +169,7 @@ void links() {
String html = process(content);

assertThat(html)
.isEqualTo("<table class=\"bodyTable\">" +
.isEqualTo("<table class=\"bodyTable\" style=\"background: #FFFFFF\">" +
tr("a", td("JRuby", textAlignLeft()) + td("Java")) +
tr("b", td("Rubinius", textAlignLeft()) + td("Ruby")) +
tr("a",
Expand All @@ -185,7 +186,7 @@ void inline_images() {
String html = process(content);

assertThat(html)
.isEqualTo("<table class=\"bodyTable\">" +
.isEqualTo("<table class=\"bodyTable\" style=\"background: #FFFFFF\">" +
tr("a", td("JRuby", textAlignLeft()) + td("Java")) +
tr("b", td("Rubinius", textAlignLeft()) + td("Ruby")) +
tr("a",
Expand All @@ -200,12 +201,12 @@ private Map<String, String> textAlignLeft() {
}

private static String expectedNoLabelBeginning() {
return "<table class=\"bodyTable\">" +
return "<table class=\"bodyTable\" style=\"background: #FFFFFF\">" +
"<caption style=\"" + CAPTION_STYLE + "\">Table caption&#8230;&#8203;or title</caption>";
}

private static String expectedTableWithoutLabel() {
return "<table class=\"bodyTable\">" +
return "<table class=\"bodyTable\" style=\"background: #FFFFFF\">" +
"<caption style=\"" + CAPTION_STYLE + "\">Table caption&#8230;&#8203;or title</caption>" +
"<tr class=\"a\">" +
"<td style=\"text-align: left;\">JRuby</td>" +
Expand All @@ -216,7 +217,7 @@ private static String expectedTableWithoutLabel() {
}

private static String expectedTableWithoutCaption() {
return "<table class=\"bodyTable\">" +
return "<table class=\"bodyTable\" style=\"background: #FFFFFF\">" +
"<tr class=\"a\">" +
"<td style=\"text-align: left;\">JRuby</td>" +
"<td>Java</td></tr>" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ public static String monospace(String text) {
return htmlElement("code", text);
}

public static String div(String text) {
return htmlElement("div", text);
public static String div(String... elements) {
return htmlElement("div", String.join("", elements));
}

public static String div(String style, String text) {
return htmlElement("div", text, Map.of(STYLE, style));
}

public static String ul(String... elements) {
Expand Down

0 comments on commit f18c942

Please sign in to comment.