Skip to content

Commit

Permalink
#134 rename content tags to shortcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Dec 26, 2023
1 parent 4c1daec commit 752f914
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 53 deletions.
2 changes: 1 addition & 1 deletion cms-server/hosts/demo/content/forms/error.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Extending
title: Error sending form
template: error.html
menu:
visible: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
title: Content-Tags
title: Shortcodes
template: start.html
menu:
position: 91
---

## Tags
## Shortcodes

[[hello name='Thorsten'/]]

Or call a tag provided by the default theme
Or call a shortcode provided by the default theme

[[theme_name /]]
4 changes: 2 additions & 2 deletions cms-server/hosts/demo/extensions/test.extension.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UTF_8 } from 'system/charsets.mjs';
import { $http } from 'system/http.mjs';
import { $template } from 'system/template.mjs';
import { $tags } from 'system/tags.mjs';
import { $shortcodes } from 'system/shortcodes.mjs';
import { getLogger } from 'system/logging.mjs';

const logger = getLogger("extensions");
Expand Down Expand Up @@ -31,7 +31,7 @@ $template.registerTemplateFunction(
(name) => "Hello " + name + "!"
)

$tags.addTag(
$shortcodes.register(
"hello",
(params) => `Hello ${params.get("name")}, I'm a TAG!`
)
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public String render(final Path contentFile, final RequestContext context, final
var content = contentParser.parse(contentFile);

var markdownContent = content.content();
markdownContent = context.get(RenderContext.class).contentTags().replace(markdownContent);
markdownContent = context.get(RenderContext.class).shortCodes().replace(markdownContent);

return render(contentFile, context, sections, content.meta(), markdownContent, (model) -> {
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
*/
@Slf4j
@RequiredArgsConstructor
public class ContentTags {
public class ShortCodes {

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

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

private final Tags tags;
private final Codes codes;

public ContentTags (Map<String, Function<Parameter, String>> tags) {
this.tags = new Tags();
this.tags.addAll(tags);
public ShortCodes (Map<String, Function<Parameter, String>> codes) {
this.codes = new Codes();
this.codes.addAll(codes);
}

public String replace (final String content) {
Expand All @@ -70,7 +70,7 @@ private String _replace (final String content, final Pattern pattern) {
if (matcher.namedGroups().containsKey("content")) {
params.put("content", matcher.group("content"));
}
newContent += tags.get(tagName).apply(params);
newContent += codes.get(tagName).apply(params);

lastPosition = matcher.end();
}
Expand Down Expand Up @@ -101,18 +101,18 @@ private Parameter parseParameters(final String paramString) {
return params;
}

public static class Tags {
private Map<String, Function<Parameter, String>> tags = new HashMap<>();
public static class Codes {
private Map<String, Function<Parameter, String>> codes = new HashMap<>();

public void addAll(Map<String, Function<Parameter, String>> tags) {
this.tags.putAll(tags);
public void addAll(Map<String, Function<Parameter, String>> codes) {
this.codes.putAll(codes);
}

public void add (final String tagName, Function<Parameter, String> function) {
tags.put(tagName, function);
public void add (final String codeName, Function<Parameter, String> function) {
codes.put(codeName, function);
}
public Function<Parameter, String> get (final String tagName) {
return tags.getOrDefault(tagName, (params) -> "");
public Function<Parameter, String> get (final String codeName) {
return codes.getOrDefault(codeName, (params) -> "");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* #L%
*/

import com.github.thmarx.cms.content.ContentTags;
import com.github.thmarx.cms.content.ShortCodes;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import com.github.thmarx.cms.api.feature.Feature;
import com.github.thmarx.cms.api.theme.Theme;
Expand All @@ -33,7 +33,7 @@
* @author t.marx
*/
@Slf4j
public record RenderContext(MarkdownRenderer markdownRenderer, ContentTags contentTags, Theme theme)
public record RenderContext(MarkdownRenderer markdownRenderer, ShortCodes shortCodes, Theme theme)
implements AutoCloseable, Feature {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import com.github.thmarx.cms.api.request.features.SitePropertiesFeature;
import com.github.thmarx.cms.api.request.features.ThemeFeature;
import com.github.thmarx.cms.api.theme.Theme;
import com.github.thmarx.cms.content.ContentTags;
import com.github.thmarx.cms.content.ShortCodes;
import com.github.thmarx.cms.extensions.ExtensionManager;
import com.github.thmarx.cms.api.utils.HTTPUtil;
import com.google.inject.Injector;
Expand Down Expand Up @@ -79,7 +79,7 @@ public RequestContext create(

RenderContext renderContext = new RenderContext(
markdownRenderer.get(),
new ContentTags(requestExtensions.getTags()),
new ShortCodes(requestExtensions.getShortCodes()),
requestTheme);

var context = new RequestContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import com.github.thmarx.cms.api.extensions.http.ExtensionHttpHandler;
import com.github.thmarx.cms.api.feature.Feature;
import com.github.thmarx.cms.content.ContentTags;
import com.github.thmarx.cms.content.ShortCodes;
import com.github.thmarx.cms.extensions.HttpHandlerExtension;
import com.github.thmarx.cms.extensions.TemplateFunctionExtension;
import com.github.thmarx.cms.extensions.TemplateSupplierExtension;
Expand Down Expand Up @@ -53,7 +53,7 @@ public class RequestExtensions implements AutoCloseable, Feature {
@Getter
private final List<TemplateFunctionExtension> registerTemplateFunctions = new ArrayList<>();
@Getter
private final Map<String, Function<ContentTags.Parameter, String>> tags = new HashMap<>();
private final Map<String, Function<ShortCodes.Parameter, String>> shortCodes = new HashMap<>();

@Getter
private final Context context;
Expand All @@ -75,8 +75,8 @@ public void registerTemplateFunction(final String path, final Function<?, ?> fun
registerTemplateFunctions.add(new TemplateFunctionExtension(path, function));
}

public void addTag(final String tag, final Function<ContentTags.Parameter, String> function) {
tags.put(tag, function);
public void registerShortCode(final String shortCode, final Function<ShortCodes.Parameter, String> function) {
shortCodes.put(shortCode, function);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const $shortcodes = {
register : (name, fun) => {
extensions.registerShortCode(name, fun)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.github.thmarx.cms.api.request.features.InjectorFeature;
import com.github.thmarx.cms.api.request.features.RequestFeature;
import com.github.thmarx.cms.api.request.features.SiteMediaServiceFeature;
import com.github.thmarx.cms.content.ContentTags;
import com.github.thmarx.cms.content.ShortCodes;
import com.github.thmarx.cms.media.FileMediaService;
import com.github.thmarx.cms.request.RenderContext;
import com.github.thmarx.cms.request.RequestExtensions;
Expand Down Expand Up @@ -57,7 +57,7 @@ public static RequestContext requestContext(String uri) {
RequestContext context = new RequestContext();
context.add(RequestFeature.class, new RequestFeature(uri, Map.of()));
context.add(RequestExtensions.class, new RequestExtensions(null, null));
context.add(RenderContext.class, new RenderContext(markdownRenderer, new ContentTags(Map.of()), DefaultTheme.EMPTY));
context.add(RenderContext.class, new RenderContext(markdownRenderer, new ShortCodes(Map.of()), DefaultTheme.EMPTY));

context.add(SiteMediaServiceFeature.class, new SiteMediaServiceFeature(new FileMediaService(null)));
context.add(InjectorFeature.class, new InjectorFeature(Mockito.mock(Injector.class)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
*
* @author t.marx
*/
public class ContentTagsTest {
public class ShortCodesTest {

static ContentTags contentTags;
static ShortCodes shortCodes;

@BeforeAll
public static void init () {
ContentTags.Tags tags = new ContentTags.Tags();
ShortCodes.Codes tags = new ShortCodes.Codes();
tags.add(
"youtube",
(params) -> "<video src='%s'></video>".formatted(params.getOrDefault("id", "")));
Expand All @@ -54,22 +54,22 @@ public static void init () {
params -> "<mark class='%s'>%s</mark>".formatted(params.get("class"), params.get("content"))
);

contentTags = new ContentTags(tags);
shortCodes = new ShortCodes(tags);
}


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

result = contentTags.replace("[[youtube/]]");
result = shortCodes.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 = shortCodes.replace("before [[youtube /]] after");
Assertions.assertThat(result).isEqualTo("before <video src=''></video> after");
}

Expand All @@ -84,7 +84,7 @@ void complexTest () {
some text after
""";

var result = contentTags.replace(content);
var result = shortCodes.replace(content);

var expected = """
some text before
Expand All @@ -99,32 +99,32 @@ void complexTest () {

@Test
void unknown_tag () {
var result = contentTags.replace("before [[vimeo id='TEST' /]] after");
var result = shortCodes.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 = shortCodes.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' /]]");
result = shortCodes.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' /]]");
result = shortCodes.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]]");
var result = shortCodes.replace("[[mark]]Important[[/mark]]");

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

@Test
void test_long_with_params () {
var result = contentTags.replace("[[mark2 class='test-class']]Important[[/mark2]]");
var result = shortCodes.replace("[[mark2 class='test-class']]Important[[/mark2]]");

Assertions.assertThat(result).isEqualTo("<mark class='test-class'>Important</mark>");
}
Expand All @@ -140,7 +140,7 @@ void long_complex () {
some text after
""";

var result = contentTags.replace(content);
var result = shortCodes.replace(content);

var expected = """
some text before
Expand Down
4 changes: 2 additions & 2 deletions cms-server/themes/default/extensions/theme.extension.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $tags } from 'system/tags.mjs';
import { $shortcodes } from 'system/shortcodes.mjs';

$tags.addTag(
$shortcodes.register(
"theme_name",
(params) => `Hello, I'm your default theme.`
)

0 comments on commit 752f914

Please sign in to comment.