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
Adding non Doxia/fluido supported feature.
For now we embed some styles.

Fixes #935
  • Loading branch information
abelsromero committed Oct 13, 2024
1 parent 9a528ac commit b973c1b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
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 All @@ -225,11 +239,11 @@ class HtmlAsserter {

void assertTableHeaders(String htmlBlock, List<String> headers) {
def actualHeaders = Arrays.stream(htmlBlock.split("<"))
.filter(line -> line.startsWith("th>"))
.map(line -> {
return line.substring("th>".length())
})
.collect(Collectors.toList())
.filter(line -> line.startsWith("th>"))
.map(line -> {
return line.substring("th>".length())
})
.collect(Collectors.toList())

if (actualHeaders != headers)
fail("Table headers not valid. Found: $actualHeaders, expected: $headers")
Expand All @@ -252,8 +266,8 @@ class HtmlAsserter {
// Removes linebreaks to validate to avoid OS dependant issues.
private String clean(String value) {
return value.replaceAll("\r\n", "")
.replaceAll("\n", "")
.trim();
.replaceAll("\n", "")
.trim();
}
}

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 @@ -7,6 +7,7 @@
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

Expand All @@ -22,6 +23,7 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@Disabled
class AsciidoctorAstDoxiaParserTest {

private static final String TEST_DOCS_PATH = "src/test/resources/";
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

0 comments on commit b973c1b

Please sign in to comment.