Skip to content

Commit

Permalink
#216 update shortcode syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Jun 24, 2024
1 parent 40aef6c commit 6329752
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ public class ConfigurationManagement implements Runnable {
final ScheduledExecutorService scheduler;
final EventBus eventBus;

private List<ConfigurationResource> watched_configurations = new ArrayList<>();
private final List<ConfigurationResource> watched_configurations = new ArrayList<>();

public void init() throws IOException {
public void reload () throws IOException {
watched_configurations.clear();
init_files();
}

private void init_files () throws IOException {
// init config files
addPathToWatch(db.getFileSystem().resolve("site.yaml"), SiteConfiguration.class);

Expand All @@ -83,6 +88,10 @@ public void init() throws IOException {
}
});
}
}

public void init() throws IOException {
init_files();

// setup scheduler
scheduler.scheduleWithFixedDelay(this, 1, 1, TimeUnit.MINUTES);
Expand All @@ -97,10 +106,14 @@ private void addPathToWatch(final Path configFile, final Class<? extends Config>
);
}

private List<ConfigurationResource> getConfigurations () {
return new ArrayList<>(watched_configurations);
}

@Override
public void run() {
log.trace("check for modified configurations {}", db.getFileSystem().resolve(".").toString());
watched_configurations.forEach(config -> {
getConfigurations().forEach(config -> {
try {
var tempMod = Files.getLastModifiedTime(config.configFile).toMillis();

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

import com.github.thmarx.cms.content.markdown.Block;
import com.github.thmarx.cms.content.markdown.BlockElementRule;
import com.github.thmarx.cms.content.markdown.InlineBlock;
import com.github.thmarx.cms.content.markdown.InlineElementRule;
import com.github.thmarx.cms.content.markdown.InlineRenderer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.github.thmarx.cms.content.shortcodes;

/*-
* #%L
* cms-content
* %%
* 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%
*/

import com.github.thmarx.cms.api.model.Parameter;
import java.util.*;
import java.util.function.Function;
import java.util.regex.*;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

public class ShortCodeParser {

private static final String SHORTCODE_REGEX = "\\[\\[(\\w+)([^\\]]*)\\]\\](.*?)\\[\\[\\/\\1\\]\\]|\\[\\[(\\w+)([^\\]]*)\\/\\]\\]";

public static List<Match> parseShortcodes(String text) {
List<Match> shortcodes = new ArrayList<>();
Pattern pattern = Pattern.compile(SHORTCODE_REGEX, Pattern.DOTALL);
Matcher matcher = pattern.matcher(text);

while (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() : "";

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

Pattern paramPattern = Pattern.compile("(\\w+)=(\"[^\"]*\"|'[^']*')");
Matcher paramMatcher = paramPattern.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);
}

shortcodes.add(match);
}

return shortcodes;
}

public static String replace (String content, Codes codes) {
String newContent = "";

int lastPosition = 0;
var matches = parseShortcodes(content);
for(var match : matches) {

newContent += content.substring(lastPosition, match.getStart());

newContent += codes.get(match.getName()).apply(match.getParameters());

lastPosition = match.getEnd();
}

if (content.length() > lastPosition) {
newContent += content.substring(lastPosition);
}

return newContent;
}

@RequiredArgsConstructor
@Getter
public static class Match {
private final String name;
private final int start;
private final int end;
private Parameter parameters = new Parameter();

@Setter
private String content;
}

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

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

public void add (final String codeName, Function<Parameter, String> function) {
codes.put(codeName, function);
}
public Function<Parameter, String> get (final String codeName) {
return codes.getOrDefault(codeName, (params) -> "");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,75 +45,14 @@ public class ShortCodes {

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 Codes codes;
private final ShortCodeParser.Codes codes;

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

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 += codes.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 Codes {
private Map<String, Function<Parameter, String>> codes = new HashMap<>();

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

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

0 comments on commit 6329752

Please sign in to comment.