Skip to content

Commit 48ec5d4

Browse files
committed
Support inline examples (:content_model: simple)
1 parent ce28096 commit 48ec5d4

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/ExampleNodeProcessor.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,22 @@ public void process(StructuralNode node) {
5050

5151
final List<StructuralNode> blocks = node.getBlocks();
5252
if (!blocks.isEmpty()) {
53-
sink.division(SinkAttributes.of(STYLE, Styles.EXAMPLE));
54-
blocks.forEach(this::sink);
55-
sink.division_();
53+
divWrap(sink, node, () -> blocks.forEach(this::sink));
54+
} else {
55+
// For :content_model: simple (inline)
56+
// https://docs.asciidoctor.org/asciidoc/latest/blocks/example-blocks/#example-style-syntax
57+
final String content = (String) node.getContent();
58+
if (isNotBlank(content)) {
59+
divWrap(sink, node, () -> sink.text(content));
60+
}
5661
}
5762

5863
sink.division_();
64+
}
5965

66+
void divWrap(Sink sink, StructuralNode node, Runnable consumer) {
67+
sink.division(SinkAttributes.of(STYLE, Styles.EXAMPLE));
68+
consumer.run();
69+
sink.division_();
6070
}
6171
}

asciidoctor-parser-doxia-module/src/main/java/org/asciidoctor/maven/site/parser/processors/Styles.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Styles {
1616
"border-color: #e0e0dc",
1717
"border: 1px solid #e6e6e6",
1818
"box-shadow: 0 1px 4px #e0e0dc",
19+
"margin-bottom: 1.25em",
1920
"padding: 1.25em"
2021
).collect(Collectors.joining("; "));
2122
}

asciidoctor-parser-doxia-module/src/test/java/org/asciidoctor/maven/site/parser/processors/ExampleNodeProcessorTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.asciidoctor.ast.StructuralNode;
1111
import org.asciidoctor.maven.site.parser.NodeProcessor;
1212
import org.asciidoctor.maven.site.parser.processors.test.NodeProcessorTest;
13+
import org.junit.jupiter.api.Nested;
1314
import org.junit.jupiter.api.Test;
1415

1516
import static org.asciidoctor.maven.site.parser.processors.test.Html.*;
@@ -20,7 +21,7 @@
2021
class ExampleNodeProcessorTest {
2122

2223
public static final String EXAMPLE_TITLE_OPENING = "<div style=\"color: #7a2518; margin-bottom: .25em\">";
23-
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\">";
24+
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\">";
2425

2526
private Asciidoctor asciidoctor;
2627
private NodeProcessor nodeProcessor;
@@ -172,4 +173,41 @@ private String process(String content) {
172173

173174
return removeLineBreaks(sinkWriter.toString());
174175
}
176+
177+
@Nested
178+
class WithSimpleContentModel {
179+
180+
@Test
181+
void should_convert_minimal_example() {
182+
String content = "= Tile\n\n" + "== Section\n\n" +
183+
"[example]\n" +
184+
"SomeText";
185+
186+
String html = process(content);
187+
188+
// Content is directly embedded instead of delegated to paragraph processor
189+
assertThat(html)
190+
.isEqualTo(div(
191+
EXAMPLE_CONTENT_OPENING +
192+
"SomeText" +
193+
"</div>"));
194+
}
195+
196+
@Test
197+
void should_convert_minimal_example_with_title() {
198+
String content = "= Tile\n\n" + "== Section\n\n" +
199+
".Optional title\n" +
200+
"[example]\n" +
201+
"SomeText";
202+
203+
String html = process(content);
204+
205+
assertThat(html)
206+
.isEqualTo(div(
207+
EXAMPLE_TITLE_OPENING + "Example 1. Optional title</div>" +
208+
EXAMPLE_CONTENT_OPENING +
209+
"SomeText" +
210+
"</div>"));
211+
}
212+
}
175213
}

0 commit comments

Comments
 (0)