Skip to content

Commit

Permalink
#65 extend content tags for open and close tags
Browse files Browse the repository at this point in the history
[[tag]]Hello people.[[/tag]]
short version is also possible
[[tag /]]
  • Loading branch information
Thorsten Marx committed Nov 9, 2023
1 parent c17e157 commit 84f0e44
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
@RequiredArgsConstructor
public class ContentTags {

public static final Pattern TAG_PARAMS_PATTERN = Pattern.compile("\\[{2}(?<tag>.*?)( (?<params>.*?))?\\]{2}");
public static final Pattern TAG_PARAMS_PATTERN_SHORT = Pattern.compile("\\[{2}(?<tag>[a-z_A-Z]+)( (?<params>.*?))?\\p{Blank}*/\\]{2}");

public static final Pattern TAG_PARAMS_PATTERN_LONG = Pattern.compile("\\[{2}(?<tag>[a-z_A-Z]+)( (?<params>.*?))?\\]{2}(?<content>.*)\\[{2}/\\k<tag>\\]{2}");

private final Tags tags;

Expand All @@ -49,15 +51,23 @@ public ContentTags (Map<String, Function<Parameter, String>> tags) {

public String replace (final String content) {

var matcher = TAG_PARAMS_PATTERN.matcher(content);
var newContent = _replace(content, TAG_PARAMS_PATTERN_SHORT);
return _replace(newContent, TAG_PARAMS_PATTERN_LONG);
}

private String _replace (final String content, final Pattern pattern) {
var matcher = pattern.matcher(content);

String newContent = "";
int lastPosition = 0;
while (matcher.find()) {
while (matcher.find(lastPosition)) {
var tagName = matcher.group("tag");

newContent += content.substring(lastPosition, matcher.start());
Parameter params = parseParameters(matcher.group("params"));
if (matcher.namedGroups().containsKey("content")) {
params.put("content", matcher.group("content"));
}
newContent += tags.get(tagName).apply(params);

lastPosition = matcher.end();
Expand All @@ -67,7 +77,6 @@ public String replace (final String content) {
}

return newContent;

}

private Parameter parseParameters(final String paramString) {
Expand Down
20 changes: 20 additions & 0 deletions cms-server/src/main/java/com/github/thmarx/cms/utils/HTTPUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package com.github.thmarx.cms.utils;

/*-
* #%L
* cms-server
* %%
* Copyright (C) 2023 Marx-Software
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import com.google.common.base.Strings;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* #L%
*/

import java.util.regex.Matcher;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -42,19 +43,24 @@ public static void init () {
"hello_from",
(params) -> "<p><h3>%s</h3><small>from %s</small></p>".formatted(params.getOrDefault("name", ""), params.getOrDefault("from", "")));

tags.add(
"mark",
params -> "<mark>%s</mark>".formatted(params.get("content"))
);

contentTags = new ContentTags(tags);
}


@Test
void simpleTest () {
var result = contentTags.replace("[[youtube]]");
var result = contentTags.replace("[[youtube /]]");
Assertions.assertThat(result).isEqualTo("<video src=''></video>");
}

@Test
void simple_with_text_before_and_After () {
var result = contentTags.replace("before [[youtube]] after");
var result = contentTags.replace("before [[youtube /]] after");
Assertions.assertThat(result).isEqualTo("before <video src=''></video> after");
}

Expand All @@ -63,9 +69,9 @@ void complexTest () {

var content = """
some text before
[[youtube id='id1']]
[[youtube id='id1' /]]
some text between
[[youtube id='id2']]
[[youtube id='id2' /]]
some text after
""";

Expand All @@ -84,14 +90,50 @@ void complexTest () {

@Test
void unknown_tag () {
var result = contentTags.replace("before [[vimeo id='TEST']] after");
var result = contentTags.replace("before [[vimeo id='TEST' /]] after");
Assertions.assertThat(result).isEqualToIgnoringWhitespace("before after");
}

@Test
void hello_from () {
var result = contentTags.replace("[[hello_from name='Thorsten',from='Bochum']]");
var result = contentTags.replace("[[hello_from name='Thorsten',from='Bochum' /]]");
Assertions.assertThat(result).isEqualTo("<p><h3>Thorsten</h3><small>from Bochum</small></p>");

result = contentTags.replace("[[hello_from name='Thorsten',from='Bochum' /]]");
Assertions.assertThat(result).isEqualTo("<p><h3>Thorsten</h3><small>from Bochum</small></p>");

result = contentTags.replace("[[hello_from name='Thorsten', from='Bochum' /]]");
Assertions.assertThat(result).isEqualTo("<p><h3>Thorsten</h3><small>from Bochum</small></p>");
}

@Test
void test_long () {
var result = contentTags.replace("[[mark]]Important[[/mark]]");

Assertions.assertThat(result).isEqualTo("<mark>Important</mark>");
}

@Test
void long_complex () {

var content = """
some text before
[[mark]]Hello world![[/mark]]
some text between
[[mark]]Hello people![[/mark]]
some text after
""";

var result = contentTags.replace(content);

var expected = """
some text before
<mark>Hello world!</mark>
some text between
<mark>Hello people!</mark>
some text after
""";

Assertions.assertThat(result).isEqualToIgnoringWhitespace(expected);
}
}

0 comments on commit 84f0e44

Please sign in to comment.