This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-doxia.git

commit ce6a669d6a75b0a59ae336eaa166be93950dc4a4
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Tue Nov 3 22:45:50 2026 +0000

    Put the snippet class on a nested code element
    
    highlight.js reads the language class only from the code element, so a
    class on the pre element went unnoticed. With a class, the macro now
    emits the same events as a Markdown fenced code block: verbatim without
    the source decoration, then an inline code event carrying the class.
---
 .../maven/doxia/macro/snippet/SnippetMacro.java    | 22 ++++----
 .../doxia/macro/snippet/SnippetMacroTest.java      | 60 +++++++++++++++++++---
 2 files changed, 66 insertions(+), 16 deletions(-)

diff --git 
a/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetMacro.java
 
b/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetMacro.java
index aa6e9c49..ff66a76c 100644
--- 
a/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetMacro.java
+++ 
b/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetMacro.java
@@ -143,15 +143,19 @@ public class SnippetMacro extends AbstractMacro {
             throw new MacroExecutionException("Error reading snippet", e);
         }
 
-        if (verbatim) {
-            SinkEventAttributeSet attributes = new SinkEventAttributeSet();
-            if (source) {
-                attributes.addAttributes(SinkEventAttributeSet.SOURCE);
-            }
-            if (cssClass != null) {
-                attributes.addAttribute(SinkEventAttributes.CLASS, cssClass);
-            }
-            sink.verbatim(attributes.getAttributeCount() > 0 ? attributes : 
null);
+        if (verbatim && cssClass != null) {
+            // the class goes on a nested code element, as for a Markdown 
fenced code block: highlight.js reads it
+            // only there, Prism there or on the pre element
+            sink.verbatim(null);
+            sink.inline(new SinkEventAttributeSet(
+                    SinkEventAttributes.CLASS, cssClass, 
SinkEventAttributes.SEMANTICS, "code"));
+
+            sink.text(snippet.toString());
+
+            sink.inline_();
+            sink.verbatim_();
+        } else if (verbatim) {
+            sink.verbatim(source ? SinkEventAttributeSet.SOURCE : null);
 
             sink.text(snippet.toString());
 
diff --git 
a/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java
 
b/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java
index 172c5efd..11d7663f 100644
--- 
a/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java
+++ 
b/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java
@@ -19,6 +19,7 @@
 package org.apache.maven.doxia.macro.snippet;
 
 import java.io.File;
+import java.io.StringWriter;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
@@ -26,15 +27,18 @@ import java.util.Map;
 import org.apache.maven.doxia.macro.MacroExecutionException;
 import org.apache.maven.doxia.macro.MacroRequest;
 import org.apache.maven.doxia.parser.Xhtml5BaseParser;
+import org.apache.maven.doxia.sink.Sink;
 import org.apache.maven.doxia.sink.SinkEventAttributes;
 import org.apache.maven.doxia.sink.impl.SinkEventElement;
 import org.apache.maven.doxia.sink.impl.SinkEventTestingSink;
+import org.apache.maven.doxia.sink.impl.Xhtml5BaseSink;
 import org.codehaus.plexus.testing.PlexusTest;
 import org.junit.jupiter.api.Test;
 
 import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
@@ -158,24 +162,66 @@ class SnippetMacroTest {
 
         SinkEventTestingSink sink = executeSnippetMacro(macroParameters);
 
-        SinkEventElement event = sink.getEventList().iterator().next();
-        SinkEventAttributes attributes = (SinkEventAttributes) 
event.getArgs()[0];
+        Iterator<SinkEventElement> it = sink.getEventList().iterator();
+        SinkEventElement verbatim = it.next();
+        assertEquals("verbatim", verbatim.getName());
+        assertNull(verbatim.getArgs()[0]);
+        SinkEventElement inline = it.next();
+        assertEquals("inline", inline.getName());
+        SinkEventAttributes attributes = (SinkEventAttributes) 
inline.getArgs()[0];
+        assertTrue(attributes.containsAttribute(SinkEventAttributes.SEMANTICS, 
"code"));
         assertTrue(attributes.containsAttribute(SinkEventAttributes.CLASS, 
"language-yaml"));
-        
assertTrue(attributes.containsAttribute(SinkEventAttributes.DECORATION, 
"source"));
+        assertEquals("text", it.next().getName());
+        assertEquals("inline_", it.next().getName());
+        assertEquals("verbatim_", it.next().getName());
+        assertFalse(it.hasNext());
+    }
+
+    @Test
+    void cssClassOnCodeElementInXhtml5() throws Exception {
+        Map<String, Object> macroParameters = new HashMap<>();
+        macroParameters.put("file", 
"src/test/resources/macro/snippet/testSnippet.txt");
+        macroParameters.put("id", "firstId");
+        macroParameters.put("class", "language-yaml");
+
+        StringWriter out = new StringWriter();
+        try (Xhtml5BaseSink sink = new Xhtml5BaseSink(out)) {
+            executeSnippetMacro(sink, macroParameters);
+        }
+
+        String html = out.toString();
+        assertTrue(html.startsWith("<pre><code class=\"language-yaml\">"), 
html);
+        assertTrue(html.endsWith("</code></pre>"), html);
+    }
+
+    @Test
+    void sourceWithoutCssClassInXhtml5() throws Exception {
+        Map<String, Object> macroParameters = new HashMap<>();
+        macroParameters.put("file", 
"src/test/resources/macro/snippet/testSnippet.txt");
+        macroParameters.put("id", "firstId");
+
+        StringWriter out = new StringWriter();
+        try (Xhtml5BaseSink sink = new Xhtml5BaseSink(out)) {
+            executeSnippetMacro(sink, macroParameters);
+        }
+
+        assertTrue(out.toString().startsWith("<pre><code>"), out.toString());
     }
 
     private SinkEventTestingSink executeSnippetMacro(Map<String, Object> 
macroParameters)
             throws MacroExecutionException {
+        SinkEventTestingSink sink = new SinkEventTestingSink();
+        executeSnippetMacro(sink, macroParameters);
+        return sink;
+    }
+
+    private void executeSnippetMacro(Sink sink, Map<String, Object> 
macroParameters) throws MacroExecutionException {
         File basedir = new File(getBasedir());
 
         Xhtml5BaseParser parser = new Xhtml5BaseParser();
 
-        SinkEventTestingSink sink = new SinkEventTestingSink();
-
         MacroRequest request = new MacroRequest(null, parser, macroParameters, 
basedir);
         SnippetMacro macro = new SnippetMacro();
         macro.execute(sink, request);
-
-        return sink;
     }
 }

Reply via email to