Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

65 cross markdown engine working tags #67

Merged
merged 5 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cms-server/hosts/demo/content/tags/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Tags
template: start.html
menu:
position: 91
---

## Tags

[[hello name='Thorsten']]
6 changes: 6 additions & 0 deletions cms-server/hosts/demo/extensions/test.extension.js
Original file line number Diff line number Diff line change
@@ -1,6 +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 { getLogger } from 'system/logging.mjs';

const logger = getLogger("extensions");
Expand Down Expand Up @@ -28,4 +29,9 @@ $template.registerTemplateSupplier(
$template.registerTemplateFunction(
"getHello",
(name) => "Hello " + name + "!"
)

$tags.addTag(
"hello",
(params) => `Hello ${params.get("name")}, I'm a TAG!`
)
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"modules":{"flexmark-module":{"active":false,"id":"flexmark-module","moduleDir":"flexmark-module"},"thymeleaf-module":{"active":true,"id":"thymeleaf-module","moduleDir":"thymeleaf-module"},"freemarker-module":{"active":false,"id":"freemarker-module","moduleDir":"freemarker-module"},"search-module":{"active":true,"id":"search-module","moduleDir":"search-module"},"pebble-module":{"active":false,"id":"pebble-module","moduleDir":"pebble-module"},"example-module":{"active":true,"id":"example-module","moduleDir":"example-module"},"markedjs-module":{"active":true,"id":"markedjs-module","moduleDir":"markedjs-module"}}}
{"modules":{"flexmark-module":{"active":false,"id":"flexmark-module","moduleDir":"flexmark-module"},"thymeleaf-module":{"active":true,"id":"thymeleaf-module","moduleDir":"thymeleaf-module"},"freemarker-module":{"active":false,"id":"freemarker-module","moduleDir":"freemarker-module"},"search-module":{"active":true,"id":"search-module","moduleDir":"search-module"},"pug-module":{"active":false,"id":"pug-module","moduleDir":"pug-module"},"pebble-module":{"active":false,"id":"pebble-module","moduleDir":"pebble-module"},"example-module":{"active":true,"id":"example-module","moduleDir":"example-module"},"markedjs-module":{"active":true,"id":"markedjs-module","moduleDir":"markedjs-module"}}}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

import com.github.thmarx.cms.extensions.ExtensionHolder;
import com.github.thmarx.cms.content.ContentTags;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -29,7 +30,7 @@
* @author t.marx
*/
@Slf4j
public record RenderContext(ExtensionHolder extensionHolder, MarkdownRenderer markdownRenderer) {
public record RenderContext(ExtensionHolder extensionHolder, MarkdownRenderer markdownRenderer, ContentTags contentTags) {


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.thmarx.cms;
package com.github.thmarx.cms.content;

/*-
* #%L
Expand All @@ -23,6 +23,7 @@
import com.github.thmarx.cms.filesystem.FileSystem;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.thmarx.cms.Startup;
import com.google.common.base.Strings;
import java.io.IOException;
import java.nio.file.Path;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.thmarx.cms;
package com.github.thmarx.cms.content;

/*-
* #%L
Expand All @@ -20,6 +20,7 @@
* #L%
*/

import com.github.thmarx.cms.RequestContext;
import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.api.SiteProperties;
import com.github.thmarx.cms.api.extensions.TemplateModelExtendingExtentionPoint;
Expand Down Expand Up @@ -62,9 +63,12 @@ public String render (final Path contentFile, final RequestContext context) thro
public String render (final Path contentFile, final RequestContext context, final Map<String, List<ContentRenderer.Section>> sections) throws IOException {
var content = contentParser.parse(contentFile);

var markdownContent = content.content();
markdownContent = context.renderContext().contentTags().replace(markdownContent);

TemplateEngine.Model model = new TemplateEngine.Model(contentFile);
model.values.put("meta", content.meta());
model.values.put("content", context.renderContext().markdownRenderer().render(content.content()));
model.values.put("content", context.renderContext().markdownRenderer().render(markdownContent));
model.values.put("sections", sections);

model.values.put("navigation", new NavigationFunction(this.fileSystem, contentFile, contentParser, context.renderContext().markdownRenderer()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.thmarx.cms;
package com.github.thmarx.cms.content;

/*-
* #%L
Expand All @@ -20,6 +20,7 @@
* #L%
*/

import com.github.thmarx.cms.RequestContext;
import com.github.thmarx.cms.filesystem.FileSystem;
import com.github.thmarx.cms.filesystem.MetaData;
import com.github.thmarx.cms.api.utils.PathUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.github.thmarx.cms.content;

/*-
* #%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.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Pattern;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
*
* @author t.marx
*/
@Slf4j
@RequiredArgsConstructor
public class ContentTags {

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;

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

public String replace (final String 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(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();
}
if (content.length() > lastPosition) {
newContent += content.substring(lastPosition);
}

return newContent;
}

private Parameter parseParameters(final String paramString) {
Parameter params = new Parameter();

if (Strings.isNullOrEmpty(paramString)) {
return params;
}

Map<String, String> result = Splitter.on(',')
.trimResults()
.withKeyValueSeparator(
Splitter.on('=')
.limit(2)
.trimResults(CharMatcher.anyOf("'\" ")))
.split(paramString);

params.putAll(result);

return params;
}

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

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

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

public static class Parameter extends HashMap<String, Object> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
*/

import com.github.thmarx.cms.api.extensions.http.ExtensionHttpHandler;
import com.github.thmarx.cms.content.ContentTags;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
Expand All @@ -43,6 +46,8 @@ public class ExtensionHolder implements AutoCloseable {
private final List<TemplateSupplierExtension> registerTemplateSupplier = new ArrayList<>();
@Getter
private final List<TemplateFunctionExtension> registerTemplateFunctions = new ArrayList<>();
@Getter
private final Map<String, Function<ContentTags.Parameter, String>> tags = new HashMap<>();

@Getter
private final Context context;
Expand All @@ -62,6 +67,10 @@ public void registerTemplateSupplier(final String path, final Supplier<?> suppli
public void registerTemplateFunction(final String path, final Function<?, ?> function) {
registerTemplateFunctions.add(new TemplateFunctionExtension(path, function));
}

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

@Override
public void close() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
import com.github.thmarx.cms.api.ModuleFileSystem;
import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.ContentParser;
import com.github.thmarx.cms.content.ContentParser;
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.api.eventbus.events.ContentChangedEvent;
import com.github.thmarx.cms.api.eventbus.events.TemplateChangedEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
* #L%
*/

import com.github.thmarx.cms.ContentResolver;
import com.github.thmarx.cms.content.ContentResolver;
import com.github.thmarx.cms.RenderContext;
import com.github.thmarx.cms.RequestContext;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import com.github.thmarx.cms.content.ContentTags;
import com.github.thmarx.cms.extensions.ExtensionManager;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -55,7 +56,7 @@ public Optional<String> apply(String uri, Map<String, List<String>> parameters)
final MarkdownRenderer markdownRenderer = markdownRendererProvider.apply(contextHolder.getContext());) {

RequestContext context = new RequestContext(uri, parameters,
new RenderContext(contextHolder, markdownRenderer));
new RenderContext(contextHolder, markdownRenderer, new ContentTags(contextHolder.getTags())));
return contentResolver.get().getContent(context);
} catch (Exception e) {
log.error("", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
* limitations under the License.
* #L%
*/
import com.github.thmarx.cms.ContentParser;
import com.github.thmarx.cms.ContentRenderer;
import com.github.thmarx.cms.ContentResolver;
import com.github.thmarx.cms.content.ContentParser;
import com.github.thmarx.cms.content.ContentRenderer;
import com.github.thmarx.cms.content.ContentResolver;
import com.github.thmarx.cms.api.SiteProperties;
import com.github.thmarx.cms.PropertiesLoader;
import com.github.thmarx.cms.api.CMSModuleContext;
Expand All @@ -47,7 +47,6 @@
import java.util.Optional;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.graalvm.polyglot.Context;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
import com.github.thmarx.cms.api.extensions.http.Request;
import com.github.thmarx.cms.server.jetty.handler.JettyDefaultHandler;
import com.github.thmarx.cms.utils.HTTPUtil;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -63,7 +64,7 @@ public String getBody(final Charset charset) {

@Override
public List<String> getQueryParamter(final String name) {
var queryParameters = JettyDefaultHandler.queryParameters(original.getHttpURI().getQuery());
var queryParameters = HTTPUtil.queryParameters(original.getHttpURI().getQuery());
if (queryParameters.containsKey(name)) {
return queryParameters.get(name);
}
Expand All @@ -72,7 +73,7 @@ public List<String> getQueryParamter(final String name) {

@Override
public List<String> getQueryParamters() {
var queryParameters = JettyDefaultHandler.queryParameters(original.getHttpURI().getQuery());
var queryParameters = HTTPUtil.queryParameters(original.getHttpURI().getQuery());
return new ArrayList<>(queryParameters.keySet());
}
}
Loading