Skip to content

Commit

Permalink
Add support for code blocks titles in asciidoctor-parser-doxia-module (
Browse files Browse the repository at this point in the history
…#936)

Adding non Doxia/fluido supported feature.
For now we embed some styles.

Fixes #935
  • Loading branch information
abelsromero authored Oct 13, 2024
1 parent 9a528ac commit 784b5b7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Improvements::

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

Build / Infrastructure::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ image::images/asciidoctor-logo.png[Asciidoctor is awesome]
=== Code blocks

[source,ruby]
.Ruby example
----
puts "Hello, World!"
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ new HtmlAsserter(htmlContent).with { asserter ->
asserter.containsLiteral("This is a literal.")

asserter.containsSectionTitle("Code blocks", 3)
asserter.containsCodeBlock("Ruby example")
asserter.containsCodeBlock(null) // Java example without title

asserter.containsSectionTitle("Lists", 3)

Expand Down Expand Up @@ -210,6 +212,18 @@ class HtmlAsserter {
lastAssertionCursor = end
}

void containsCodeBlock(String title) {
def blockKey = "<div class=\"source\">"
def found = find(blockKey)
assertFound("Code blockKey", blockKey, found)

if (title != null) {
def titleKey = "<div style=\"color:"
def foundTitle = find(titleKey)
assertFound("Code blockKey title", title, foundTitle)
}
}

void assertTableCaption(String htmlBlock, String caption) {
def start = htmlBlock.indexOf("<caption>") + "<caption>".length()
def end = htmlBlock.indexOf("</caption>")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.maven.doxia.sink.Sink;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.jruby.ast.impl.BlockImpl;
import org.asciidoctor.maven.commons.StringUtils;
import org.asciidoctor.maven.site.parser.NodeProcessor;

import static org.asciidoctor.maven.commons.StringUtils.isNotBlank;
Expand Down Expand Up @@ -41,14 +42,19 @@ public boolean applies(StructuralNode node) {
public void process(StructuralNode node) {
final StringBuilder contentBuilder = new StringBuilder();
String language = (String) node.getAttribute("language");
String style = (String) node.getAttribute("style");
String style = node.getStyle();

boolean isSourceBlock = isSourceBlock(language, style);

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

final String title = node.getTitle();
if (StringUtils.isNotBlank(title)) {
contentBuilder.append("<div style=\"color: #7a2518; margin-bottom: .25em;\" >" + title + "</div>");
}

contentBuilder.append("<pre class=\"")
.append(FLUIDO_SKIN_SOURCE_HIGHLIGHTER);
if (isLinenumsEnabled(node))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.asciidoctor.maven.site.parser.processors.test.NodeProcessorTest;
import org.junit.jupiter.api.Test;

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

Expand All @@ -31,6 +32,17 @@ void should_convert_full_source_block() {
.isEqualTo(expectedHtmlCodeBlock());
}

@Test
void should_convert_full_source_block_with_caption() {
final String title = "Java example";
String content = buildDocument(title, "[source,java]");

String html = process(content);

assertThat(html)
.isEqualTo(expectedHtmlCodeBlock(title));
}

@Test
void should_convert_shorthand_source_block() {
String content = documentWithShorthandSourceBlock();
Expand All @@ -41,15 +53,6 @@ void should_convert_shorthand_source_block() {
.isEqualTo(expectedHtmlCodeBlock());
}

private static String expectedHtmlCodeBlock() {
// Actual styling is added in JS by prettify
return "<div class=\"source\"><pre class=\"prettyprint\"><code>class HelloWorldLanguage {" +
" public static void main(String[] args) {" +
" System.out.println(\"Hello, World!\");" +
" }" +
"}</code></pre></div>";
}

@Test
void should_convert_full_source_block_with_line_numbers_attribute() {
String content = buildDocument("[source,java,linenums]");
Expand Down Expand Up @@ -97,8 +100,13 @@ private String documentWithListingStyleSourceBlock() {
}

private static String buildDocument(String blockDefinition) {
return buildDocument(null, blockDefinition);
}

private static String buildDocument(String title, String blockDefinition) {
return "= Document tile\n\n"
+ "== Section\n\n"
+ buildTitle(title)
+ blockDefinition + "\n" +
"----\n" +
"class HelloWorldLanguage {\n" +
Expand All @@ -109,6 +117,31 @@ private static String buildDocument(String blockDefinition) {
"----\n";
}

private static String buildTitle(String title) {
if (isNotBlank(title))
return "." + title + "\n";
return "";
}

private static String expectedHtmlCodeBlock() {
return expectedHtmlCodeBlock(null);
}

private static String expectedHtmlCodeBlock(String title) {
// Actual styling is added in JS by prettify
return "<div class=\"source\">" +
(isNotBlank(title) ? expectedTitle(title) : "") +
"<pre class=\"prettyprint\"><code>class HelloWorldLanguage {" +
" public static void main(String[] args) {" +
" System.out.println(\"Hello, World!\");" +
" }" +
"}</code></pre></div>";
}

private static String expectedTitle(String title) {
return "<div style=\"color: #7a2518; margin-bottom: .25em;\" >" + title + "</div>";
}

private String process(String content) {
StructuralNode node = asciidoctor.load(content, Options.builder().build())
.findBy(Collections.singletonMap("context", ":listing"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ NOTE: Unlike in Asciidoctor lists, descriptions are not surrounded by `<p>` and

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

* Literal blocks
* Quotes
Expand Down

0 comments on commit 784b5b7

Please sign in to comment.