This is an automated email from the ASF dual-hosted git repository. kwin pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-doxia.git
The following commit(s) were added to refs/heads/master by this push: new 0f4908c2 [DOXIA-750] Properly apply inlines inside HTML blocks 0f4908c2 is described below commit 0f4908c27fa898228e2c74a6c218d3175d217bb4 Author: Konrad Windszus <k...@apache.org> AuthorDate: Tue Oct 15 19:56:52 2024 +0200 [DOXIA-750] Properly apply inlines inside HTML blocks --- .../maven/doxia/module/markdown/MarkdownSink.java | 46 ++++++++++++++++------ .../doxia/module/markdown/MarkdownSinkTest.java | 19 +++++++++ 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java index 91dceae9..51c7bf1f 100644 --- a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java +++ b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java @@ -109,7 +109,8 @@ public class MarkdownSink extends AbstractTextSink implements MarkdownMarkup { true), // special type, as allows containing inlines, but not starting on a separate line // same parameters as BODY but paragraphs inside list items are handled differently LIST_ITEM("list item", Type.CONTAINER_BLOCK, MarkdownSink::escapeMarkdown, false, INDENT), - BLOCKQUOTE("blockquote", Type.CONTAINER_BLOCK, MarkdownSink::escapeMarkdown, false, BLOCKQUOTE_START_MARKUP); + BLOCKQUOTE("blockquote", Type.CONTAINER_BLOCK, MarkdownSink::escapeMarkdown, false, BLOCKQUOTE_START_MARKUP), + HTML_BLOCK("html block", Type.LEAF_BLOCK, MarkdownSink::escapeHtml, false, "", true); final String name; @@ -470,13 +471,14 @@ public class MarkdownSink extends AbstractTextSink implements MarkdownMarkup { public void definitionList(SinkEventAttributes attributes) { LOGGER.warn( "{}Definition list not natively supported in Markdown, rendering HTML instead", getLocationLogPrefix()); - ensureBlankLine(); + startContext(ElementContext.HTML_BLOCK); writeUnescaped("<dl>" + EOL); } @Override public void definitionList_() { - writeUnescaped("</dl>" + BLANK_LINE); + writeUnescaped("</dl>"); + endContext(ElementContext.HTML_BLOCK); } @Override @@ -772,6 +774,7 @@ public class MarkdownSink extends AbstractTextSink implements MarkdownMarkup { public void inline(SinkEventAttributes attributes) { Queue<String> endMarkups = Collections.asLifoQueue(new LinkedList<>()); + boolean requiresHtml = elementContextStack.element() == ElementContext.HTML_BLOCK; if (attributes != null && elementContextStack.element() != ElementContext.CODE_BLOCK && elementContextStack.element() != ElementContext.CODE_SPAN) { @@ -779,23 +782,38 @@ public class MarkdownSink extends AbstractTextSink implements MarkdownMarkup { if (attributes.containsAttribute(SinkEventAttributes.SEMANTICS, "code") || attributes.containsAttribute(SinkEventAttributes.SEMANTICS, "monospaced") || attributes.containsAttribute(SinkEventAttributes.STYLE, "monospaced")) { - writeUnescaped(MONOSPACED_START_MARKUP); - endMarkups.add(MONOSPACED_END_MARKUP); - elementContextStack.add(ElementContext.CODE_SPAN); + if (requiresHtml) { + writeUnescaped("<code>"); + endMarkups.add("</code>"); + } else { + writeUnescaped(MONOSPACED_START_MARKUP); + endMarkups.add(MONOSPACED_END_MARKUP); + elementContextStack.add(ElementContext.CODE_SPAN); + } } else { // in XHTML "<em>" is used, but some tests still rely on the outdated "<italic>" - if (attributes.containsAttribute(SinkEventAttributes.SEMANTICS, "em") + if (attributes.containsAttribute(SinkEventAttributes.SEMANTICS, "emphasis") || attributes.containsAttribute(SinkEventAttributes.SEMANTICS, "italic") || attributes.containsAttribute(SinkEventAttributes.STYLE, "italic")) { - writeUnescaped(ITALIC_START_MARKUP); - endMarkups.add(ITALIC_END_MARKUP); + if (requiresHtml) { + writeUnescaped("<em>"); + endMarkups.add("</em>"); + } else { + writeUnescaped(ITALIC_START_MARKUP); + endMarkups.add(ITALIC_END_MARKUP); + } } // in XHTML "<strong>" is used, but some tests still rely on the outdated "<bold>" if (attributes.containsAttribute(SinkEventAttributes.SEMANTICS, "strong") || attributes.containsAttribute(SinkEventAttributes.SEMANTICS, "bold") || attributes.containsAttribute(SinkEventAttributes.STYLE, "bold")) { - writeUnescaped(BOLD_START_MARKUP); - endMarkups.add(BOLD_END_MARKUP); + if (requiresHtml) { + writeUnescaped("<strong>"); + endMarkups.add("</strong>"); + } else { + writeUnescaped(BOLD_START_MARKUP); + endMarkups.add(BOLD_END_MARKUP); + } } } } @@ -944,7 +962,7 @@ public class MarkdownSink extends AbstractTextSink implements MarkdownMarkup { if (text == null) { return ""; } - text = HtmlTools.escapeHTML(text, true); // assume UTF-8 output, i.e. only use the mandatory XML entities + text = escapeHtml(text); // assume UTF-8 output, i.e. only use the mandatory XML entities int length = text.length(); StringBuilder buffer = new StringBuilder(length); @@ -977,6 +995,10 @@ public class MarkdownSink extends AbstractTextSink implements MarkdownMarkup { return buffer.toString(); } + private static String escapeHtml(String text) { + return HtmlTools.escapeHTML(text, true); + } + /** * Escapes the pipe character according to <a href="https://github.github.com/gfm/#tables-extension-">GFM Table Extension</a> in addition * to the regular markdown escaping. diff --git a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java index 44d1162e..99c81f1a 100644 --- a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java +++ b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java @@ -513,4 +513,23 @@ public class MarkdownSinkTest extends AbstractSinkTest { String expected = "Text" + EOL + "# Section1" + EOL + EOL; assertEquals(expected, getSinkContent(), "Wrong heading after inline element!"); } + + @Test + public void testDefinitionListWithInlineStyles() { + try (final Sink sink = getSink()) { + sink.definitionList(); + sink.definedTerm(); + sink.text("term"); + sink.definedTerm_(); + sink.definition(); + sink.text("prefix "); + sink.text("code", SinkEventAttributeSet.Semantics.CODE); + sink.text("suffix<a>test</a>", SinkEventAttributeSet.Semantics.EMPHASIS); + sink.definition_(); + sink.definitionList_(); + } + String expected = "<dl>" + EOL + "<dt>term</dt>" + EOL + + "<dd>prefix <code>code</code><em>suffix<a>test</a></em></dd>" + EOL + "</dl>" + EOL + EOL; + assertEquals(expected, getSinkContent(), "Wrong heading after inline element!"); + } }