Skip to content

Commit

Permalink
fix some shortcode issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Sep 23, 2024
1 parent e596a75 commit 480aee0
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.condation.cms.content.markdown.Block;
import com.condation.cms.content.markdown.BlockElementRule;
import com.condation.cms.content.markdown.InlineRenderer;
import com.google.common.html.HtmlEscapers;
import com.condation.cms.content.shortcodes.ShortCodeParser;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -41,8 +41,12 @@ public class ShortCodeBlockRule implements BlockElementRule {
public static final Pattern TAG_PARAMS_PATTERN_LONG = Pattern.compile("^(\\[{2})(?<tag>[a-z_A-Z0-9]+)( (?<params>.*?))?\\]{2}(?<content>.*)\\[{2}/\\k<tag>\\]{2}",
Pattern.MULTILINE | Pattern.DOTALL | Pattern.UNIX_LINES);

public static final Pattern SHORTCODE_PATTERN = Pattern.compile("^" + ShortCodeParser.SHORTCODE_REGEX,
Pattern.DOTALL | Pattern.MULTILINE);

@Override
public Block next(final String md) {
/*
Matcher matcher = TAG_PARAMS_PATTERN_SHORT.matcher(md);
if (matcher.find()) {
return new ShortCodeBlock(matcher.start(), matcher.end(),
Expand All @@ -55,6 +59,20 @@ public Block next(final String md) {
matcher.group("tag"), matcher.group("params"), matcher.group("content")
);
}
*/
Matcher matcher = SHORTCODE_PATTERN.matcher(md);
if (matcher.matches()) {
String name = matcher.group(1) != null ? matcher.group(1) : matcher.group(4);
String params = matcher.group(2) != null ? matcher.group(2).trim() : matcher.group(5).trim();
String content = matcher.group(3) != null ? matcher.group(3).trim() : "";

ShortCodeParser.Match match = new ShortCodeParser.Match(name, matcher.start(), matcher.end());
match.setContent(content);
match.getParameters().put("content", content);

return new ShortCodeBlock(matcher.start(), matcher.end(), name, params, content);
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.InlineElementRule;
import com.condation.cms.content.shortcodes.ShortCodeParser;
import static com.condation.cms.content.shortcodes.ShortCodeParser.PARAM_PATTERN;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -41,6 +43,7 @@ public class ShortCodeInlineBlockRule implements InlineElementRule {

@Override
public InlineBlock next(final String md) {
/*
Matcher matcher = TAG_PARAMS_PATTERN_SHORT.matcher(md);
if (matcher.find()) {
return new ShortCodeInlineBlock(matcher.start(), matcher.end(),
Expand All @@ -53,6 +56,32 @@ public InlineBlock next(final String md) {
matcher.group("tag"), matcher.group("params"), matcher.group("content")
);
}
*/

Matcher matcher = ShortCodeParser.SHORTCODE_PATTERN.matcher(md);
if (matcher.find()) {
String name = matcher.group(1) != null ? matcher.group(1) : matcher.group(4);
String params = matcher.group(2) != null ? matcher.group(2).trim() : matcher.group(5).trim();
String content = matcher.group(3) != null ? matcher.group(3).trim() : "";

ShortCodeParser.Match match = new ShortCodeParser.Match(name, matcher.start(), matcher.end());
match.setContent(content);
match.getParameters().put("content", content);

/*
Matcher paramMatcher = PARAM_PATTERN.matcher(params);
while (paramMatcher.find()) {
String key = paramMatcher.group(1);
String value = paramMatcher.group(2);
// Remove the surrounding quotes
value = value.substring(1, value.length() - 1);
match.getParameters().put(key, value);
}
*/
return new ShortCodeInlineBlock(matcher.start(), matcher.end(), name, params, content);
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
@Slf4j
public class ShortCodeParser {

private static final String SHORTCODE_REGEX = "\\[\\[(\\w+)([^\\]]*)\\]\\](.*?)\\[\\[\\/\\1\\]\\]|\\[\\[(\\w+)([^\\]]*)\\s*\\/\\]\\]";
private static final Pattern SHORTCODE_PATTERN = Pattern.compile(SHORTCODE_REGEX, Pattern.DOTALL);
private static final Pattern PARAM_PATTERN = Pattern.compile("(\\w+)=(\"[^\"]*\"|'[^']*')");
public static final String SHORTCODE_REGEX = "\\[\\[(\\w+)([^\\]]*)\\]\\](.*?)\\[\\[\\/\\1\\]\\]|\\[\\[(\\w+)([^\\]]*)\\s*\\/\\]\\]";
public static final Pattern SHORTCODE_PATTERN = Pattern.compile(SHORTCODE_REGEX, Pattern.DOTALL);
public static final Pattern PARAM_PATTERN = Pattern.compile("(\\w+)=(\"[^\"]*\"|'[^']*')");

public static List<Match> parseShortcodes(String text) {
List<Match> shortcodes = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
* #L%
*/

import com.condation.cms.content.markdown.Options;
import com.condation.cms.content.markdown.CMSMarkdown;
import java.io.IOException;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.condation.cms.content.markdown;

/*-
* #%L
* cms-content
* %%
* Copyright (C) 2023 - 2024 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import java.io.IOException;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.RepeatedTest;

/**
*
* @author t.marx
*/
public class IssuesTest extends MarkdownTest {

static CMSMarkdown SUT;

@BeforeAll
public static void setup() {
SUT = new CMSMarkdown(Options.all());
}

@RepeatedTest(1)
public void issue_with_multiple_code_blocks() throws IOException {

var md = load("issues/issue.code-with-shortcode.md").trim();
var expected = load("issues/issue.code-with-shortcode.html");
expected = removeComments(expected);

var result = SUT.render(md);
result = "<div>" + result + "</div>";
Assertions.assertThat(result).isEqualToIgnoringWhitespace(expected);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,10 @@
* #L%
*/

import com.condation.cms.content.markdown.Options;
import com.condation.cms.content.markdown.CMSMarkdown;
import com.condation.cms.content.markdown.rules.block.ListBlockRule;
import java.io.IOException;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/


import com.condation.cms.content.markdown.rules.block.CodeBlockRule;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
*/


import com.condation.cms.content.markdown.rules.block.ShortCodeBlockRule;
import com.condation.cms.content.markdown.Block;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -74,4 +72,23 @@ void short_form() {

Assertions.assertThat(next.render((content) -> content)).isEqualTo("[[link url=\"https://google.de/\"]][[/link]]");
}

@Test
void test_issue () {
String md = "[[video type=\"youtube\" id=\"y0sF5xhGreA\" title=\"Everybody loves little cats\" /]]";

Block next = sut.next(md);

Assertions.assertThat(next)
.isNotNull()
.isInstanceOf(ShortCodeBlockRule.ShortCodeBlock.class)
.asInstanceOf(InstanceOfAssertFactories.type(ShortCodeBlockRule.ShortCodeBlock.class))
.hasFieldOrPropertyWithValue("tag", "video")
.hasFieldOrPropertyWithValue("params", "type=\"youtube\" id=\"y0sF5xhGreA\" title=\"Everybody loves little cats\"")
;

Assertions.assertThat(next.render((content) -> content))
.isEqualTo("[[video type=\"youtube\" id=\"y0sF5xhGreA\" title=\"Everybody loves little cats\"]][[/video]]");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@
*/


import com.condation.cms.content.markdown.rules.inline.ShortCodeInlineBlockRule;
import com.condation.cms.content.markdown.Block;
import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.rules.block.ShortCodeBlockRule;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
-->
<div>
<h1>task list test</h1>
<p>a paragraph with [[link id="google",title="Google"]][[/link]]</p>
<p>a paragraph with [[link id="google" title="Google"]][[/link]]</p>

<h1>next</h1>
<p></p>
[[link id="apache",title="Apache"]][[/link]]
<p>[[link id="apache" title="Apache"]][[/link]]</p>
</div>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# task list test

a paragraph with [[link id="google",title="Google" /]]
a paragraph with [[link id="google" title="Google" /]]

# next

[[link id="apache",title="Apache" /]]
[[link id="apache" title="Apache" /]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--
#%L
cms-markdown
%%
Copyright (C) 2023 - 2024 Marx-Software
%%
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/gpl-3.0.html>.
#L%
-->
<div><pre><code class='lang-html'>\&#91;\&#91;video type=&quot;youtube&quot; id=&quot;y0sF5xhGreA&quot; title=&quot;Everybody loves little cats&quot; /\&#93;\&#93;</code></pre></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
```html
\\[\\[video type="youtube" id="y0sF5xhGreA" title="Everybody loves little cats" /\\]\\]
```


0 comments on commit 480aee0

Please sign in to comment.