Skip to content

Commit

Permalink
issue #235 - added support of declaring confluence macro inside html …
Browse files Browse the repository at this point in the history
…comment. Removed confluence macro detection skipping the escaping of markdown text
  • Loading branch information
bsorrentino committed Dec 11, 2020
1 parent 9ad6560 commit 957a048
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 49 deletions.
49 changes: 33 additions & 16 deletions core/src/main/java/org/bsc/markdown/MarkdownVisitorHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bsc.markdown;

import lombok.NonNull;
import org.bsc.confluence.FileExtension;
import org.bsc.confluence.model.Site;

Expand Down Expand Up @@ -121,22 +122,38 @@ public static String processLinkUrl( String url, MarkdownParserContext parseCont

}

public enum SkipEscapeMarkdownText {
// public enum SkipEscapeMarkdownText {
//
// TOC( "^\\{[Tt][Oo][Cc](([:]\\w+=\\w+)([|].+)*)?\\}$" ),
// CHILDREN( "^\\{[Cc]hildren(([:]\\w+=\\w+)([|].+)*)?\\}$" )
// ;
//
// private final Pattern patternToSkip;
//
// public boolean matches( String text ) {
// return this.patternToSkip.matcher(text).matches();
// }
//
// SkipEscapeMarkdownText( String patternToSkip ) {
// this.patternToSkip = Pattern.compile(patternToSkip);
// }
//
// }

TOC( "^\\{[Tt][Oo][Cc](([:]\\w+=\\w+)([|].+)*)?\\}$" ),
CHILDREN( "^\\{[Cc]hildren(([:]\\w+=\\w+)([|].+)*)?\\}$" )
;

private final Pattern patternToSkip;

public boolean matches( String text ) {
return this.patternToSkip.matcher(text).matches();
}

SkipEscapeMarkdownText( String patternToSkip ) {
this.patternToSkip = Pattern.compile(patternToSkip);
}
/**
*
*/
private static Pattern isConfluenceMacroPattern = Pattern.compile( "^[\\s]*\\{([\\w-]+)(([:][\\w-]+(=(.+))?)([|].+)*)?\\}[\\s]*$" );

/**
*
* @param text
* @return
*/
public static boolean isConfluenceMacro( String text ) {
// GUARD
if( text == null || text.isEmpty() ) return false;
return isConfluenceMacroPattern.matcher(text).matches();
}

/**
Expand All @@ -148,8 +165,8 @@ public static String escapeMarkdownText( String text ) {
// GUARD
if( text == null || text.isEmpty() ) return text;

if( SkipEscapeMarkdownText.TOC.matches( text ) ) return text;
if( SkipEscapeMarkdownText.CHILDREN.matches( text ) ) return text;
// if( SkipEscapeMarkdownText.TOC.matches( text ) ) return text;
// if( SkipEscapeMarkdownText.CHILDREN.matches( text ) ) return text;

final BiFunction<String,String,String> replaceAll = (pattern, value ) -> {
final Matcher m = Pattern.compile(pattern).matcher(value);
Expand Down
51 changes: 51 additions & 0 deletions core/src/test/kotlin/org/bsc/markdown/MacroParsingTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.bsc.markdown

import org.bsc.markdown.MarkdownVisitorHelper.isConfluenceMacro
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertTrue
import java.util.regex.Pattern

class MacroParsingTest {

@Test
fun parse() {

val regexp = Pattern.compile( "^[\\s]*\\{([\\w-]+)(([:][\\w-]+(=(.+))?)([|].+)*)?\\}[\\s]*$" )

val macros = arrayOf(
" {toc} ",
"{toc:style=disc|indent=20px}",
"{toc:type=flat|separator=pipe|maxLevel=3}",
"{toc:outline=true|indent=0px|minLevel=2}",
"{children}",
"{children:all=true}",
"{children:depth=x}",
"{children:depth=x|style=h3}",
"{children:excerpt=true}",
"{children:page=Another Page}",
" {children:page=/}",
"{children:page=SPACEKEY:} ",
"{children:page=SPACEKEY:Page+Title}",
" {children:first=x}",
"{children:sort=<mode>|reverse=false}",
"{blog-posts:max=5}",
"{blog-posts:max=5|content=excerpts}",
"{blog-posts:max=5|content=titles}",
"{blog-posts:time=7d|spaces=@all}",
"{blog-posts:max=15|time=14d|content=excerpts}",
"{blog-posts:labels=confluence,atlassian}",
"{blog-posts:labels=+atlassian,+confluence,+content}",
"{navmap:mylabel}",
"{navmap:mylabel|wrapAfter=3|cellWidth=110|cellHeight=20|theme=mytheme}",
"{toc-zone:separator=brackets|location=top} ",
"{toc-zone}",
)

for( literal in macros ) {
println( "evaluate macro $literal")
assertTrue( isConfluenceMacro(literal) )
}

}

}
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<kotlin.version>1.3.72</kotlin.version>
<kotlin.version>1.4.21</kotlin.version>
</properties>

<repositories>
Expand Down Expand Up @@ -216,7 +216,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bsc.markdown.commonmark;

import lombok.NonNull;
import org.bsc.markdown.MarkdownParserContext;
import org.bsc.markdown.MarkdownVisitorHelper;
import org.bsc.markdown.commonmark.extension.NoticeBlock;
Expand All @@ -14,17 +15,21 @@
import java.util.List;
import java.util.Optional;
import java.util.Stack;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.String.format;
import static java.util.Optional.ofNullable;
import static org.bsc.markdown.MarkdownVisitorHelper.processImageUrl;
import static org.bsc.markdown.MarkdownVisitorHelper.processLinkUrl;

import static org.bsc.markdown.MarkdownVisitorHelper.*;

/**
* CommonmarkConfluenceWikiVisitor
*
* class that implements the Commonmark Visitor for translating to Confluence Wiki
*
*/
public class CommonmarkConfluenceWikiVisitor /*extends AbstractVisitor*/ implements Visitor {

public static class Parser {
Expand Down Expand Up @@ -78,6 +83,27 @@ public static String escapeMarkdownText( Node node, String text ) {
return MarkdownVisitorHelper.escapeMarkdownText(text);
}

private final static Pattern isHTMLCommentPattern = Pattern.compile( "^<!--(?:[\\s]*)(.+)-->$", Pattern.DOTALL );

/**
*
* @param text
* @return
*/
public static Matcher parseHTMLComment( @NonNull String text ) {
return isHTMLCommentPattern.matcher(text);
}

/**
* Visit document node
*
* @param node
*/
@Override
public void visit(Document node) {
processChildren(node).process(false);
}

/**
* A sequence of non-blank lines that cannot be interpreted as other kinds of blocks forms a paragraph.
* The contents of the paragraph are the result of parsing the paragraph’s raw content as inlines.
Expand All @@ -88,9 +114,8 @@ public static String escapeMarkdownText( Node node, String text ) {
@Override
public void visit(Paragraph node) {

final ChildrenProcessor p =
processChildren(node)
;
final ChildrenProcessor p = processChildren(node);

if( isParentRoot(node) ) {
p.post("\n");
}
Expand Down Expand Up @@ -127,7 +152,6 @@ public void visit(SoftLineBreak node) {

@Override
public void visit(Text node) {

final String literal = escapeMarkdownText( node, node.getLiteral() );
processChildren(node)
.pre( literal )
Expand All @@ -136,11 +160,9 @@ public void visit(Text node) {

@Override
public void visit(Heading node) {

processChildren(node)
.pre( format( "h%s. ", node.getLevel()) )
.process();

}

@Override
Expand Down Expand Up @@ -190,11 +212,6 @@ public void visit(Code node) {
.process(false);
}

@Override
public void visit(Document node) {
processChildren(node).process(false);
}

@Override
public void visit(FencedCodeBlock node) {
final Function<String,String> info = (v) -> (v==null || v.length()==0 ) ? "" : ":"+v ;
Expand All @@ -211,7 +228,6 @@ public void visit(Emphasis node) {

}


@Override
public void visit(StrongEmphasis node) {
processChildren(node).pre("*").post("*").process(false);;
Expand Down Expand Up @@ -291,7 +307,23 @@ public void visit( TableCell node ) {

@Override
public void visit(HtmlBlock node) {
processChildren(node).pre("{html}\n%s\n", node.getLiteral()).post("{html}").process();
final String literal = node.getLiteral();

final Matcher m = parseHTMLComment(literal);
if( m.matches() && isConfluenceMacro( m.group(1) ) ) {
processChildren(node)
.post(m.group(1)).process();
}
else {
processChildren(node)
.pre("{html}\n%s\n", literal)
.post("{html}").process();
}
}

@Override
public void visit(HtmlInline node) {
processChildren(node).pre("<<HTMI>>").post("<</HTMI>>").process();
}

//@Custom
Expand Down Expand Up @@ -355,12 +387,6 @@ else if( node instanceof NoticeBlock) {
processChildren(node).pre("<<CSTB type=\"%s\">>", node.getClass().getSimpleName()).post("<</CSTB>>").process();
}


@Override
public void visit(HtmlInline node) {
processChildren(node).pre("<<HTMI>>").post("<</HTMI>>").process();
}

@Override
public void visit(IndentedCodeBlock node) {
processChildren(node).pre("<<ICB>>").post("<</ICB>>").process();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.bsc.makdown.commonmark

import org.bsc.confluence.model.Site
import org.bsc.markdown.commonmark.CommonmarkConfluenceWikiVisitor.parseHTMLComment
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import java.nio.file.Paths
import java.util.regex.Pattern

class Issue235Test {

var site = Site().apply {
basedir = Paths.get(System.getProperty("user.dir"))
}

@Test
fun parseHtmlBlock() {

val singleLineMatcher = parseHTMLComment("<!-- {xxxxxxx:yyyyyy} -->")

assertTrue( singleLineMatcher.matches() )
assertEquals( 1, singleLineMatcher.groupCount() )
assertEquals( "{xxxxxxx:yyyyyy}", singleLineMatcher.group(1).trimEnd() )

val multiLineMatcher = parseHTMLComment("""<!--
{xxxxxxx:yyyyyy}
-->
""".trimIndent())

assertTrue( multiLineMatcher.matches() )
assertEquals( 1, multiLineMatcher.groupCount() )
assertEquals( "{xxxxxxx:yyyyyy}", multiLineMatcher.group(1).trimEnd() )

}

@Test
fun parseMacros() {
val content = parseResource( this.javaClass, "issue235", this.site )

assertEquals("""
{html}
<!--
Hello world!
-->
{html}
{macro}
{macro:scope}
{macros:scope|property=value}
{macros:scope|property1=value1, property2=value2}
""".trimIndent(), content )
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ class MacroTest {
fun `parse macros`() = Assertions.assertEquals(
"""
h1. This is table of content
{toc}
{toc:minLevel=2}
{toc}
{toc:minLevel=2}
{toc:type=flat|separator=pipe:minLevel=2}
""".trimIndent(),
parse() )
Expand Down
11 changes: 11 additions & 0 deletions processor-commonmark/src/test/resources/issue235.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!--
Hello world!
-->

<!-- {macro} -->

<!-- {macro:scope} -->

<!-- {macros:scope|property=value} -->

<!-- {macros:scope|property1=value1, property2=value2} -->
8 changes: 4 additions & 4 deletions processor-commonmark/src/test/resources/macro.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This is table of content

{toc}

{toc:minLevel=2}
<!-- {toc} -->

{toc:type=flat|separator=pipe:minLevel=2}
<!-- {toc:minLevel=2} -->

<!-- {toc:type=flat|separator=pipe:minLevel=2} -->

0 comments on commit 957a048

Please sign in to comment.