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

add interlinks plugin for markdown #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/main/java/org/jbake/app/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.io.IOUtils;
import org.jbake.parser.Engines;
import org.jbake.parser.MarkdownEngine;
import org.jbake.parser.MarkupEngine;
import org.jbake.parser.ParserContext;
import org.jbake.plugins.ContentPlugin;
import org.jbake.plugins.InterlinksContentPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -101,6 +104,14 @@ public Map<String, Object> processFile(File file) {

// generate default body
processBody(fileContents, content);

{
ContentPlugin interlinks = new InterlinksContentPlugin();

if (engine instanceof MarkdownEngine) {
interlinks.parseMarkdown(content, config);
}
}

// eventually process body using specific engine
if (engine.validate(context)) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/jbake/plugins/ContentPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jbake.plugins;

import java.util.Map;

import org.apache.commons.configuration.CompositeConfiguration;

public interface ContentPlugin {

public void parseMarkdown(Map<String, Object> content, CompositeConfiguration config) throws ContentPluginException;

}
11 changes: 11 additions & 0 deletions src/main/java/org/jbake/plugins/ContentPluginException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jbake.plugins;

public class ContentPluginException extends RuntimeException {

public ContentPluginException(String string) {
super(string);
}

private static final long serialVersionUID = 1L;

}
49 changes: 49 additions & 0 deletions src/main/java/org/jbake/plugins/InterlinksContentPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.jbake.plugins;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.configuration.CompositeConfiguration;

public class InterlinksContentPlugin implements ContentPlugin {

private Pattern pattern;

public static final String PLUGIN_INTERLINKS_PREFIX = "interlinks.";

public InterlinksContentPlugin() {

pattern = Pattern.compile("\\((.+?)>\\)");
}

@Override
public void parseMarkdown(Map<String, Object> content, CompositeConfiguration config)
throws ContentPluginException {

String body = (String) content.get("body");

Matcher matcher = pattern.matcher(body);

StringBuffer sb = new StringBuffer();

while (matcher.find()) {

String interlinkName = matcher.group(1);

String link = config.getString(PLUGIN_INTERLINKS_PREFIX
+ interlinkName);

if (link == null) {
throw new ContentPluginException(
"Could not find interlinks link '" + interlinkName
+ "' in config file ");
}
matcher.appendReplacement(sb, "(" + link + ")");
}

matcher.appendTail(sb);

content.put("body", sb.toString());
}
}
32 changes: 32 additions & 0 deletions src/test/java/org/jbake/plugins/InterlinksContentPluginTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jbake.plugins;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.configuration.CompositeConfiguration;
import org.junit.Test;

public class InterlinksContentPluginTest {

@Test
public void test() throws ContentPluginException {

CompositeConfiguration config = new CompositeConfiguration();
config.addProperty("interlinks.coollink", "http://www.cool-link.org");

String mdContent = "This is a [cool link](coollink>) that links to this site.";
Map<String, Object> map = new HashMap<String, Object>();
map.put("body", mdContent);

String expected = "This is a [cool link](http://www.cool-link.org) that links to this site.";

InterlinksContentPlugin plugin = new InterlinksContentPlugin();

plugin.parseMarkdown(map, config);

assertEquals(expected, map.get("body"));
}

}