Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
*/
public interface InlineElementRule {

InlineBlock next (final String md);
InlineBlock next (final InlineElementTokenizer tokenizer, final String md);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* #L%
*/

import com.condation.cms.content.markdown.rules.inline.TextInlineRule;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -36,7 +37,11 @@ public class InlineElementTokenizer {

private final Options options;

protected List<InlineBlock> tokenize(final String original_md) throws IOException {
public List<InlineBlock> tokenize(final String original_md) throws IOException {
return doTokenize(this, original_md);
}

protected List<InlineBlock> doTokenize(final InlineElementTokenizer tokenizer, final String original_md) throws IOException {

var md = original_md.replaceAll("\r\n", "\n");
StringBuilder mdBuilder = new StringBuilder(md);
Expand All @@ -45,18 +50,22 @@ protected List<InlineBlock> tokenize(final String original_md) throws IOExceptio

for (var blockRule : options.inlineElementRules) {
InlineBlock block = null;
while ((block = blockRule.next(mdBuilder.toString())) != null) {
while ((block = blockRule.next(tokenizer, mdBuilder.toString())) != null) {

if (block.start() != 0) {
var before = mdBuilder.substring(0, block.start());
blocks.addAll(tokenize(before));
blocks.addAll(doTokenize(tokenizer, before));
}

blocks.add(block);
mdBuilder.delete(0, block.end());
}
}

if (mdBuilder.length() > 0) {
blocks.add(new TextInlineRule.TextBlock(0, mdBuilder.length(), mdBuilder.toString()));
}

return blocks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.InlineElementRule;
import com.condation.cms.content.markdown.InlineElementTokenizer;
import java.util.regex.Pattern;

/**
Expand All @@ -35,7 +36,7 @@ public class HighlightInlineRule implements InlineElementRule {
private static final Pattern PATTERN = Pattern.compile("(={2})(?<content>.*?)(={2})");

@Override
public InlineBlock next(String md) {
public InlineBlock next(InlineElementTokenizer tokenizer, String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new HighlightInlineBlock(matcher.start(), matcher.end(), matcher.group("content"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.condation.cms.api.utils.ImageUtil;
import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.InlineElementRule;
import com.condation.cms.content.markdown.InlineElementTokenizer;
import com.google.common.base.Strings;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -40,7 +41,7 @@ public class ImageInlineRule implements InlineElementRule {
public static final Pattern PATTERN = Pattern.compile("!\\[(?<alt>[^\\]]*)\\]\\((?<url>[^\\s)]+)(?: \"(?<title>[^\"]*)\")?\\)");

@Override
public InlineBlock next(String md) {
public InlineBlock next(InlineElementTokenizer tokenizer, String md) {
Matcher matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new ImageInlineRule.ImageInlineBlock(matcher.start(), matcher.end(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.condation.cms.api.utils.HTTPUtil;
import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.InlineElementRule;
import com.condation.cms.content.markdown.InlineElementTokenizer;
import java.util.regex.Pattern;

/**
Expand All @@ -42,7 +43,7 @@ public class ImageLinkInlineRule implements InlineElementRule {
static final Pattern PATTERN = Pattern.compile("\\[(" + IMAGE_PATTERN + ")\\]\\((?<url>.*?)\\)");

@Override
public InlineBlock next(String md) {
public InlineBlock next(InlineElementTokenizer tokenizer, String md) {
var matcher = PATTERN.matcher(md);

if (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,39 @@

import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.InlineElementRule;
import com.condation.cms.content.markdown.InlineElementTokenizer;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
*
* @author t.marx
*/
public class ItalicInlineRule implements InlineElementRule {

private static final Pattern PATTERN = Pattern.compile("(?<selector>_{1}|\\*{1})(?<content>.*?)(\\k<selector>)");
private static final Pattern PATTERN = Pattern.compile("(?<selector>_{1}|\\*{1})(?<content>.*?)(?<!\\\\)(\\k<selector>)");

@Override
public InlineBlock next(String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new ItalicInlineBlock(matcher.start(), matcher.end(), matcher.group("content"));
}
return null;
}

public static record ItalicInlineBlock(int start, int end, String content) implements InlineBlock {
@Override
public String render() {
return "<em>%s</em>".formatted(content);
}
}
@Override
public InlineBlock next(InlineElementTokenizer tokenizer, String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new ItalicInlineBlock(tokenizer, matcher.start(), matcher.end(), matcher.group("content"));
}
return null;
}

public static record ItalicInlineBlock(InlineElementTokenizer tokenizer, int start, int end, String content) implements InlineBlock {

@Override
public String render() {
try {
var renderedContent = tokenizer.tokenize(content).stream().map(b -> b.render()).collect(Collectors.joining());
return "<em>%s</em>".formatted(renderedContent);
} catch (IOException ex) {
return "<em>%s</em>".formatted(content);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.condation.cms.api.utils.HTTPUtil;
import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.InlineElementRule;
import com.condation.cms.content.markdown.InlineElementTokenizer;
import java.util.regex.Pattern;

/**
Expand All @@ -42,7 +43,7 @@ public class LinkInlineRule implements InlineElementRule {
static final Pattern PATTERN = Pattern.compile("\\[(?<text>[^\\]]*)\\]\\((?<url>[^\\s)]+)(?: \"(?<title>[^\"]*)\")?\\)");

@Override
public InlineBlock next(String md) {
public InlineBlock next(InlineElementTokenizer tokenizer, String md) {
var matcher = PATTERN.matcher(md);

if (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.InlineElementRule;
import com.condation.cms.content.markdown.InlineElementTokenizer;
import java.util.regex.Pattern;

/**
Expand All @@ -36,7 +37,7 @@ public class NewlineInlineRule implements InlineElementRule {
private static final Pattern PATTERN = Pattern.compile(" {2,}+$", Pattern.MULTILINE);

@Override
public InlineBlock next(String md) {
public InlineBlock next(InlineElementTokenizer tokenizer, String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new NewlineInlineBlock(matcher.start(), matcher.end());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,39 @@

import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.InlineElementRule;
import com.condation.cms.content.markdown.InlineElementTokenizer;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
*
* @author t.marx
*/
public class StrikethroughInlineRule implements InlineElementRule {

private static final Pattern PATTERN = Pattern.compile("(~{2})(?<content>.*?)(~{2})");
@Override
public InlineBlock next(String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new StrikethroughInlineBlock(matcher.start(), matcher.end(), matcher.group("content"));
}
return null;
}
public static record StrikethroughInlineBlock(int start, int end, String content) implements InlineBlock {
private static final Pattern PATTERN = Pattern.compile("(?<selector>~{2})(?<content>.*?)(?<!\\\\)(\\k<selector>)");

@Override
public InlineBlock next(InlineElementTokenizer tokenizer, String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new StrikethroughInlineBlock(tokenizer, matcher.start(), matcher.end(), matcher.group("content"));
}
return null;
}

public static record StrikethroughInlineBlock(InlineElementTokenizer tokenizer, int start, int end, String content) implements InlineBlock {

@Override
public String render() {
return "<del>%s</del>".formatted(content);
}
}
@Override
public String render() {
try {
var renderedContent = tokenizer.tokenize(content).stream().map(b -> b.render()).collect(Collectors.joining());
return "<del>%s</del>".formatted(renderedContent);
} catch (IOException ex) {
return "<del>%s</del>".formatted(content);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,39 @@

import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.InlineElementRule;
import com.condation.cms.content.markdown.InlineElementTokenizer;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
*
* @author t.marx
*/
public class StrongInlineRule implements InlineElementRule {

private static final Pattern PATTERN = Pattern.compile("(?<selector>_{2}|\\*{2})(?<content>.*?)(\\k<selector>)");

@Override
public InlineBlock next(String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new StrongInlineBlock(matcher.start(), matcher.end(), matcher.group("content"));
}
return null;
}

public static record StrongInlineBlock(int start, int end, String content) implements InlineBlock {

@Override
public String render() {
return "<strong>%s</strong>".formatted(content);
}
}


private static final Pattern PATTERN = Pattern.compile("(?<selector>_{2}|\\*{2})(?<content>.*?)(?<!\\\\)(\\k<selector>)");

@Override
public InlineBlock next(final InlineElementTokenizer tokenizer, final String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new StrongInlineBlock(tokenizer, matcher.start(), matcher.end(), matcher.group("content"));
}
return null;
}

public static record StrongInlineBlock(InlineElementTokenizer tokenizer, int start, int end, String content) implements InlineBlock {

@Override
public String render() {
try {
var renderedContent = tokenizer.tokenize(content).stream().map(b -> b.render()).collect(Collectors.joining());
return "<strong>%s</strong>".formatted(renderedContent);
} catch (IOException ex) {
return "<strong>%s</strong>".formatted(content);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.InlineElementRule;
import com.condation.cms.content.markdown.InlineElementTokenizer;
import java.util.regex.Pattern;

/**
Expand All @@ -33,13 +34,12 @@
*/
public class SubscriptInlineRule implements InlineElementRule {

private static final Pattern PATTERN = Pattern.compile("(={1})(?<content>.*?)(={1})");
private static final Pattern PATTERN = Pattern.compile("(?<selector>=)(?<content>.*?)(?<!\\\\)(\\k<selector>)");


@Override
public InlineBlock next(String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
@Override
public InlineBlock next(InlineElementTokenizer tokenizer, String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new SubscriptInlineBlock(matcher.start(), matcher.end(), matcher.group("content"));
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.condation.cms.content.markdown.InlineBlock;
import com.condation.cms.content.markdown.InlineElementRule;
import com.condation.cms.content.markdown.InlineElementTokenizer;
import java.util.regex.Pattern;

/**
Expand All @@ -33,22 +34,22 @@
*/
public class SuperscriptInlineRule implements InlineElementRule {

private static final Pattern PATTERN = Pattern.compile("(\\^{1})(?<content>.*?)(\\^{1})");
private static final Pattern PATTERN = Pattern.compile("(?<selector>\\^)(?<content>.*?)(?<!\\\\)(\\k<selector>)");

@Override
public InlineBlock next(String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new SuperscriptBlock(matcher.start(), matcher.end(), matcher.group("content"));
}
return null;
}
public static record SuperscriptBlock(int start, int end, String content) implements InlineBlock {
@Override
public String render() {
return "<sup>%s</sup>".formatted(content);
}
}
@Override
public InlineBlock next(InlineElementTokenizer tokenizer, String md) {
var matcher = PATTERN.matcher(md);
if (matcher.find()) {
return new SuperscriptBlock(matcher.start(), matcher.end(), matcher.group("content"));
}
return null;
}

public static record SuperscriptBlock(int start, int end, String content) implements InlineBlock {

@Override
public String render() {
return "<sup>%s</sup>".formatted(content);
}
}
}
Loading