kwin commented on code in PR #619:
URL: 
https://github.com/apache/maven-doxia-sitetools/pull/619#discussion_r2837652934


##########
doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/sink/SiteRendererSink.java:
##########
@@ -130,6 +142,122 @@ public void date_() {
         resetTextBuffer();
     }
 
+    @Override
+    public void verbatim(SinkEventAttributes attributes) {
+        if (mermaidConfig != null && 
normalizeClassAttributesForMermaid(attributes)) {
+            containsMermaidDiagram = true;
+            // remove the decoration code for Mermaid diagrams (otherwise 
Skins may add line numbers to the code
+            // element, which breaks Mermaid rendering)
+            SinkEventAttributes filteredAttributes = (SinkEventAttributes)
+                    SinkUtils.filterAttributes(attributes, new String[] 
{SinkEventAttributes.DECORATION});
+            super.verbatim(filteredAttributes);
+        } else {
+            // write subsequent verbatim content to a buffer, to be able to 
detect Mermaid diagrams in it and remove
+            // code element if needed
+            verbatimBuffer = new StringBuilder();
+            super.verbatim(attributes);
+        }
+    }
+
+    @Override
+    public void verbatim_() {
+        flushVerbatimBuffer(false);
+        super.verbatim_();
+    }
+
+    @Override
+    public void inline(SinkEventAttributes attributes) {
+        if (attributes.containsAttributes(SinkEventAttributeSet.Semantics.CODE)
+                && mermaidConfig != null
+                && normalizeClassAttributesForMermaid(attributes)) {
+            containsMermaidDiagram = true;
+            // writes to buffer
+            super.inline(attributes);
+            // remove code element from inline stack to prevent closing it
+            inlineStack.pop();
+            insideMermaidCodeElement = true;
+            // remove the code element from the verbatim buffer, to prevent 
Skins from adding line numbers to it, which
+            // breaks Mermaid rendering
+            flushVerbatimBuffer(true);
+        } else {
+            flushVerbatimBuffer(false);
+            super.inline(attributes);
+        }
+    }
+
+    @Override
+    public void inline_() {
+        if (insideMermaidCodeElement) {
+            // this is the end of the code tag (which has been removed), so no 
need to close it
+            insideMermaidCodeElement = false;
+        } else {
+            super.inline_();
+        }
+    }
+
+    private void flushVerbatimBuffer(boolean stripCodeElement) {
+        if (verbatimBuffer != null) {
+            String buffer = verbatimBuffer.toString();
+            if (stripCodeElement) {
+                // remove code element and instead add attributes to the 
parent element <pre> to prevent the skin from
+                // adding code highlighting/line numbers, which breaks Mermaid 
rendering
+                buffer = buffer.replaceFirst("<pre><code([^>]*)>", "<pre$1>");
+            }
+            verbatimBuffer = null;
+            write(buffer);
+        }
+    }
+
+    /**
+     * Normalize class attributes for Mermaid diagrams, to allow using either 
"mermaid" or "language-mermaid" as class.
+     *
+     * @param attributes the attributes to check and normalize.
+     * @return {@code true} if the attributes indicate a Mermaid diagram, 
{@code false} otherwise.
+     */
+    boolean normalizeClassAttributesForMermaid(SinkEventAttributes attributes) 
{
+        String lang = attributes != null ? (String) 
attributes.getAttribute(SinkEventAttributes.CLASS) : null;
+        if ("language-mermaid"
+                .equals(lang)) { // "language-" prefix is used by some 
markdown parsers, e.g. flexmark-java
+            attributes.addAttribute(SinkEventAttributes.CLASS, "mermaid");
+            return true;
+        } else if ("mermaid".equals(lang)) {
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Include the Mermaid rendering script (either internal or external) and 
call it on diagrams afterwards.
+     * @see <a 
href="https://mermaid.ai/open-source/intro/getting-started.html#_4-calling-the-mermaid-javascript-api";>Calling
 the Mermaid JavaScript API</a>
+     */
+    private void writeMermaidScript() {
+        if (mermaidConfig.getExternalJs() != null) {
+            write(mermaidConfig.getExternalJs().asScriptTag());
+        } else {
+            write("\n<script src=\"");
+            write(docRenderingContext.getRelativePath());
+
+            if (mermaidConfig.isUseTiny()) {
+                // use integrated tiny version of mermaid, which is smaller 
and faster to load, but has some limitations
+                // (e.g. no sequence diagrams)
+                write("/js/mermaid-" + DefaultSiteRenderer.MERMAID_VERSION + 
".tiny.min.js");
+            } else {
+                // use integrated full version of mermaid, which is larger and 
slower to load, but has all features
+                write("/js/mermaid-" + DefaultSiteRenderer.MERMAID_VERSION + 
".min.js");
+            }
+            write("\"></script>\n");

Review Comment:
   `write(...)` always normalizes EOLs



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to