This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit c52d4a3eac10108fee4b0be05a5122bc91a72b95 Author: Juan Pablo Santos RodrÃguez <[email protected]> AuthorDate: Thu Jan 13 11:28:25 2022 +0100 JSPWIKI-802: enable definition and tables extensions, and provided tests for markup allowed by newly introduced extensions --- .../wiki/parser/markdown/MarkdownDocument.java | 58 ++++++++++++---------- .../wiki/render/markdown/MarkdownRendererTest.java | 40 +++++++++++++++ 2 files changed, 71 insertions(+), 27 deletions(-) diff --git a/jspwiki-markdown/src/main/java/org/apache/wiki/parser/markdown/MarkdownDocument.java b/jspwiki-markdown/src/main/java/org/apache/wiki/parser/markdown/MarkdownDocument.java index bae8130..c12936b 100644 --- a/jspwiki-markdown/src/main/java/org/apache/wiki/parser/markdown/MarkdownDocument.java +++ b/jspwiki-markdown/src/main/java/org/apache/wiki/parser/markdown/MarkdownDocument.java @@ -19,7 +19,9 @@ package org.apache.wiki.parser.markdown; import com.vladsch.flexmark.ext.attributes.AttributesExtension; +import com.vladsch.flexmark.ext.definition.DefinitionExtension; import com.vladsch.flexmark.ext.footnotes.FootnoteExtension; +import com.vladsch.flexmark.ext.tables.TablesExtension; import com.vladsch.flexmark.ext.toc.TocExtension; import com.vladsch.flexmark.parser.Parser; import com.vladsch.flexmark.parser.ParserEmulationProfile; @@ -42,36 +44,38 @@ import java.util.List; */ public class MarkdownDocument extends WikiDocument { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - private final Node md; + private final Node md; - public MarkdownDocument( final Page page, final Node md ) { - super( page ); - this.md = md; - } + public MarkdownDocument( final Page page, final Node md ) { + super( page ); + this.md = md; + } - public Node getMarkdownNode() { - return md; - } + public Node getMarkdownNode() { + return md; + } - /** - * configuration options for MarkdownRenderers. - * - * @param context current wikicontext - * @return configuration options for MarkdownRenderers. - */ - public static MutableDataSet options( final Context context, final boolean isImageInlining, final List< Pattern > inlineImagePatterns ) { - final MutableDataSet options = new MutableDataSet(); - options.setFrom( ParserEmulationProfile.COMMONMARK ); - options.set( AttributesExtension.ASSIGN_TEXT_ATTRIBUTES, true ); - // align style of Markdown's footnotes extension with jspwiki footnotes refs - options.set( FootnoteExtension.FOOTNOTE_LINK_REF_CLASS, JSPWikiMarkupParser.CLASS_FOOTNOTE_REF ); - options.set( Parser.EXTENSIONS, Arrays.asList( new Extension[] { new MarkdownForJSPWikiExtension( context, isImageInlining, inlineImagePatterns ), - AttributesExtension.create(), - FootnoteExtension.create(), - TocExtension.create() } ) ); - return options; - } + /** + * Configuration options for MarkdownRenderers. + * + * @param context current wiki context + * @return configuration options for MarkdownRenderers. + */ + public static MutableDataSet options( final Context context, final boolean isImageInlining, final List< Pattern > inlineImagePatterns ) { + final MutableDataSet options = new MutableDataSet(); + options.setFrom( ParserEmulationProfile.COMMONMARK ); + options.set( AttributesExtension.ASSIGN_TEXT_ATTRIBUTES, true ); + // align style of Markdown's footnotes extension with jspwiki footnotes refs + options.set( FootnoteExtension.FOOTNOTE_LINK_REF_CLASS, JSPWikiMarkupParser.CLASS_FOOTNOTE_REF ); + options.set( Parser.EXTENSIONS, Arrays.asList( new Extension[] { new MarkdownForJSPWikiExtension( context, isImageInlining, inlineImagePatterns ), + AttributesExtension.create(), + DefinitionExtension.create(), + FootnoteExtension.create(), + TablesExtension.create(), + TocExtension.create() } ) ); + return options; + } } diff --git a/jspwiki-markdown/src/test/java/org/apache/wiki/render/markdown/MarkdownRendererTest.java b/jspwiki-markdown/src/test/java/org/apache/wiki/render/markdown/MarkdownRendererTest.java index 7fb1061..76070ab 100644 --- a/jspwiki-markdown/src/test/java/org/apache/wiki/render/markdown/MarkdownRendererTest.java +++ b/jspwiki-markdown/src/test/java/org/apache/wiki/render/markdown/MarkdownRendererTest.java @@ -100,6 +100,46 @@ public class MarkdownRendererTest { } @Test + public void testMarkupLinkWithCustomAttributes() throws Exception { + final String src = "This should be a [link with custom attributes](http://google.com){target=blank}"; + + Assertions.assertEquals( "<p>This should be a <a href=\"http://google.com\" class=\"external\" target=\"blank\">link with custom attributes</a></p>\n", translate( src ) ); + } + + @Test + public void testMarkupPWithCustomAttributes() throws Exception { + // {..} are separated from the link, so they apply to the nearest p or span containing them + final String src0 = "This should be a [link](http://google.com) {style='background-color:#ddd'}"; + Assertions.assertEquals( "<p style=\"background-color:#ddd\">This should be a <a href=\"http://google.com\" class=\"external\">link</a></p>\n", translate( src0 ) ); + + final String src1 = "This should be a [link](http://google.com) {#a1}"; + Assertions.assertEquals( "<p id=\"a1\">This should be a <a href=\"http://google.com\" class=\"external\">link</a></p>\n", translate( src1 ) ); + + final String src2 = "This should be a [link](http://google.com) {.warning}"; + Assertions.assertEquals( "<p class=\"warning\">This should be a <a href=\"http://google.com\" class=\"external\">link</a></p>\n", translate( src2 ) ); + } + + @Test + public void testMarkupDefinitionList() throws Exception { + final String src = "Definition Term\n" + + ": definition description"; + + Assertions.assertEquals( "<dl>\n<dt>Definition Term</dt>\n<dd>definition description</dd>\n</dl>\n", translate( src ) ); + } + + @Test + public void testMarkupTable() throws Exception { + final String src = "| a | b | c \n" + + "|:--- |:---:|--- \n" + + "| d | e | f \n" + + "||| g, h and f "; + + Assertions.assertEquals( "<table>\n<thead>\n<tr><th align=\"left\">a</th><th align=\"center\">b</th><th>c</th></tr>\n</thead>\n" + + "<tbody>\n<tr><td align=\"left\">d</td><td align=\"center\">e</td><td>f</td></tr>\n" + + "<tr><td align=\"left\" colspan=\"2\"></td><td>g, h and f</td></tr>\n</tbody>\n</table>\n", translate( src ) ); + } + + @Test public void testMarkupExtensionACL() throws Exception { final String src = "[{ALLOW view PerryMason}]() This should be visible if the ACL allows you to see it"; // text is seen because although ACL is added to the page, it is not applied while parsing / rendering
