Skip to content

Commit 784b5b7

Browse files
authored
Add support for code blocks titles in asciidoctor-parser-doxia-module (#936)
Adding non Doxia/fluido supported feature. For now we embed some styles. Fixes #935
1 parent 9a528ac commit 784b5b7

File tree

6 files changed

+66
-10
lines changed

6 files changed

+66
-10
lines changed

CHANGELOG.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Improvements::
2222

2323
* Added support for AsciidoctorJ v3.0.0 (#651)
2424
* Add compatibility with maven-site-plugin v3.20.0 and Doxia v2.0.0 (#933)
25+
* Add support for code blocks titles in asciidoctor-parser-doxia-module (#935)
2526

2627
Build / Infrastructure::
2728

asciidoctor-parser-doxia-module/src/it/maven-site-plugin/src/site/asciidoc/sample.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ image::images/asciidoctor-logo.png[Asciidoctor is awesome]
3939
=== Code blocks
4040

4141
[source,ruby]
42+
.Ruby example
4243
----
4344
puts "Hello, World!"
4445
----

asciidoctor-parser-doxia-module/src/it/maven-site-plugin/validate.groovy

+14
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ new HtmlAsserter(htmlContent).with { asserter ->
3535
asserter.containsLiteral("This is a literal.")
3636

3737
asserter.containsSectionTitle("Code blocks", 3)
38+
asserter.containsCodeBlock("Ruby example")
39+
asserter.containsCodeBlock(null) // Java example without title
3840

3941
asserter.containsSectionTitle("Lists", 3)
4042

@@ -210,6 +212,18 @@ class HtmlAsserter {
210212
lastAssertionCursor = end
211213
}
212214

215+
void containsCodeBlock(String title) {
216+
def blockKey = "<div class=\"source\">"
217+
def found = find(blockKey)
218+
assertFound("Code blockKey", blockKey, found)
219+
220+
if (title != null) {
221+
def titleKey = "<div style=\"color:"
222+
def foundTitle = find(titleKey)
223+
assertFound("Code blockKey title", title, foundTitle)
224+
}
225+
}
226+
213227
void assertTableCaption(String htmlBlock, String caption) {
214228
def start = htmlBlock.indexOf("<caption>") + "<caption>".length()
215229
def end = htmlBlock.indexOf("</caption>")

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.maven.doxia.sink.Sink;
44
import org.asciidoctor.ast.StructuralNode;
55
import org.asciidoctor.jruby.ast.impl.BlockImpl;
6+
import org.asciidoctor.maven.commons.StringUtils;
67
import org.asciidoctor.maven.site.parser.NodeProcessor;
78

89
import static org.asciidoctor.maven.commons.StringUtils.isNotBlank;
@@ -41,14 +42,19 @@ public boolean applies(StructuralNode node) {
4142
public void process(StructuralNode node) {
4243
final StringBuilder contentBuilder = new StringBuilder();
4344
String language = (String) node.getAttribute("language");
44-
String style = (String) node.getAttribute("style");
45+
String style = node.getStyle();
4546

4647
boolean isSourceBlock = isSourceBlock(language, style);
4748

4849
if (isSourceBlock) {
4950
// source class triggers prettify auto-detection
5051
contentBuilder.append("<div class=\"source\">");
5152

53+
final String title = node.getTitle();
54+
if (StringUtils.isNotBlank(title)) {
55+
contentBuilder.append("<div style=\"color: #7a2518; margin-bottom: .25em;\" >" + title + "</div>");
56+
}
57+
5258
contentBuilder.append("<pre class=\"")
5359
.append(FLUIDO_SKIN_SOURCE_HIGHLIGHTER);
5460
if (isLinenumsEnabled(node))

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

+42-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.asciidoctor.maven.site.parser.processors.test.NodeProcessorTest;
1111
import org.junit.jupiter.api.Test;
1212

13+
import static org.asciidoctor.maven.commons.StringUtils.isNotBlank;
1314
import static org.asciidoctor.maven.site.parser.processors.test.StringTestUtils.clean;
1415
import static org.assertj.core.api.Assertions.assertThat;
1516

@@ -31,6 +32,17 @@ void should_convert_full_source_block() {
3132
.isEqualTo(expectedHtmlCodeBlock());
3233
}
3334

35+
@Test
36+
void should_convert_full_source_block_with_caption() {
37+
final String title = "Java example";
38+
String content = buildDocument(title, "[source,java]");
39+
40+
String html = process(content);
41+
42+
assertThat(html)
43+
.isEqualTo(expectedHtmlCodeBlock(title));
44+
}
45+
3446
@Test
3547
void should_convert_shorthand_source_block() {
3648
String content = documentWithShorthandSourceBlock();
@@ -41,15 +53,6 @@ void should_convert_shorthand_source_block() {
4153
.isEqualTo(expectedHtmlCodeBlock());
4254
}
4355

44-
private static String expectedHtmlCodeBlock() {
45-
// Actual styling is added in JS by prettify
46-
return "<div class=\"source\"><pre class=\"prettyprint\"><code>class HelloWorldLanguage {" +
47-
" public static void main(String[] args) {" +
48-
" System.out.println(\"Hello, World!\");" +
49-
" }" +
50-
"}</code></pre></div>";
51-
}
52-
5356
@Test
5457
void should_convert_full_source_block_with_line_numbers_attribute() {
5558
String content = buildDocument("[source,java,linenums]");
@@ -97,8 +100,13 @@ private String documentWithListingStyleSourceBlock() {
97100
}
98101

99102
private static String buildDocument(String blockDefinition) {
103+
return buildDocument(null, blockDefinition);
104+
}
105+
106+
private static String buildDocument(String title, String blockDefinition) {
100107
return "= Document tile\n\n"
101108
+ "== Section\n\n"
109+
+ buildTitle(title)
102110
+ blockDefinition + "\n" +
103111
"----\n" +
104112
"class HelloWorldLanguage {\n" +
@@ -109,6 +117,31 @@ private static String buildDocument(String blockDefinition) {
109117
"----\n";
110118
}
111119

120+
private static String buildTitle(String title) {
121+
if (isNotBlank(title))
122+
return "." + title + "\n";
123+
return "";
124+
}
125+
126+
private static String expectedHtmlCodeBlock() {
127+
return expectedHtmlCodeBlock(null);
128+
}
129+
130+
private static String expectedHtmlCodeBlock(String title) {
131+
// Actual styling is added in JS by prettify
132+
return "<div class=\"source\">" +
133+
(isNotBlank(title) ? expectedTitle(title) : "") +
134+
"<pre class=\"prettyprint\"><code>class HelloWorldLanguage {" +
135+
" public static void main(String[] args) {" +
136+
" System.out.println(\"Hello, World!\");" +
137+
" }" +
138+
"}</code></pre></div>";
139+
}
140+
141+
private static String expectedTitle(String title) {
142+
return "<div style=\"color: #7a2518; margin-bottom: .25em;\" >" + title + "</div>";
143+
}
144+
112145
private String process(String content) {
113146
StructuralNode node = asciidoctor.load(content, Options.builder().build())
114147
.findBy(Collections.singletonMap("context", ":listing"))

docs/modules/site-integration/pages/parser-module-setup-and-configuration.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ NOTE: Unlike in Asciidoctor lists, descriptions are not surrounded by `<p>` and
186186

187187
* Code blocks with source-highlighting using https://maven.apache.org/skins/maven-fluido-skin/#source-code-line-numbers[Fluido Skin Pretiffy].
188188
** Support for numbered lines with `linenums`
189+
** Support for AsciiDoc titles
189190

190191
* Literal blocks
191192
* Quotes

0 commit comments

Comments
 (0)