Skip to content

Commit

Permalink
Support inline examples (:content_model: simple)
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Oct 20, 2024
1 parent ce28096 commit 0e47d66
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,22 @@ public void process(StructuralNode node) {

final List<StructuralNode> blocks = node.getBlocks();
if (!blocks.isEmpty()) {
sink.division(SinkAttributes.of(STYLE, Styles.EXAMPLE));
blocks.forEach(this::sink);
sink.division_();
divWrap(sink, node, () -> 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));
}
}

sink.division_();
}

void divWrap(Sink sink, StructuralNode node, Runnable consumer) {
sink.division(SinkAttributes.of(STYLE, Styles.EXAMPLE));
consumer.run();
sink.division_();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Styles {
"border-color: #e0e0dc",
"border: 1px solid #e6e6e6",
"box-shadow: 0 1px 4px #e0e0dc",
"margin-bottom: 1.25em",
"padding: 1.25em"
).collect(Collectors.joining("; "));
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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.Nested;
import org.junit.jupiter.api.Test;

import static org.asciidoctor.maven.site.parser.processors.test.Html.*;
Expand All @@ -20,7 +21,7 @@
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; padding: 1.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;
Expand Down Expand Up @@ -172,4 +173,57 @@ private String process(String content) {

return removeLineBreaks(sinkWriter.toString());
}

@Nested
class WithSimpleContentModel {

@Test
void should_convert_minimal_example() {
String content = "= Tile\n\n" + "== Section\n\n" +
"[example]\n" +
"SomeText";

String html = process(content);

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

@Test
void should_convert_minimal_example_with_title() {
String content = "= Tile\n\n" + "== Section\n\n" +
".Optional title\n" +
"[example]\n" +
"SomeText";

String html = process(content);

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

@Test
void should_convert_minimal_example_with_link() {
final String link = "https://docs.asciidoctor.org/";
String content = "= Tile\n\n" + "== Section\n\n" +
"[example]\n" +
"SomeText, " + 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>"));
}
}
}

0 comments on commit 0e47d66

Please sign in to comment.