From 0e47d66d68fd4146f5fe66196b9682664a97b1df Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Sun, 20 Oct 2024 11:06:20 +0200 Subject: [PATCH] Support inline examples (:content_model: simple) --- .../processors/ExampleNodeProcessor.java | 16 +++++- .../maven/site/parser/processors/Styles.java | 1 + .../processors/ExampleNodeProcessorTest.java | 56 ++++++++++++++++++- 3 files changed, 69 insertions(+), 4 deletions(-) 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 index 067f4081..d179112a 100644 --- 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 @@ -50,12 +50,22 @@ public void process(StructuralNode node) { final List 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_(); } } 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 index e0c2df5e..58f56678 100644 --- 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 @@ -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("; ")); } diff --git a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ExampleNodeProcessorTest.java b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ExampleNodeProcessorTest.java index 09c0a099..2e7abfb6 100644 --- a/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ExampleNodeProcessorTest.java +++ b/asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ExampleNodeProcessorTest.java @@ -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.*; @@ -20,7 +21,7 @@ class ExampleNodeProcessorTest { public static final String EXAMPLE_TITLE_OPENING = "
"; - public static final String EXAMPLE_CONTENT_OPENING = "
"; + public static final String EXAMPLE_CONTENT_OPENING = "
"; private Asciidoctor asciidoctor; private NodeProcessor nodeProcessor; @@ -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" + + "
")); + } + + @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
" + + EXAMPLE_CONTENT_OPENING + + "SomeText" + + "
")); + } + + @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, https://docs.asciidoctor.org/" + + "")); + } + } }