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

Switch to Pegdown Markdown processor from MarkdownJ #57

Closed
wants to merge 11 commits into from
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<args4j.version>2.0.23</args4j.version>
<freemarker.version>2.3.19</freemarker.version>
<junit.version>4.8.1</junit.version>
<markdownj.version>0.3.0-1.0.2b4</markdownj.version>
<pegdown.version>1.4.1</pegdown.version>
<jetty.version>8.1.12.v20130726</jetty.version>
</properties>

Expand Down Expand Up @@ -216,9 +216,9 @@
<version>5.1.21</version>
</dependency> -->
<dependency>
<groupId>org.markdownj</groupId>
<artifactId>markdownj</artifactId>
<version>${markdownj.version}</version>
<groupId>org.pegdown</groupId>
<artifactId>pegdown</artifactId>
<version>${pegdown.version}</version>
</dependency>
<dependency>
<groupId>org.asciidoctor</groupId>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jbake/app/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ConfigUtil {
public static CompositeConfiguration load(File source) throws ConfigurationException {
if (config == null) {
config = new CompositeConfiguration();
config.setListDelimiter(',');
File customConfigFile = new File(source, "custom.properties");
if (customConfigFile.exists()) {
config.addConfiguration(new PropertiesConfiguration(customConfigFile));
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/jbake/app/Crawler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jbake.app;

import static java.io.File.separator;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -30,6 +32,7 @@ public class Crawler {
private List<Map<String, Object>> posts = new ArrayList<Map<String, Object>>();
private Map<String, List<Map<String, Object>>> postsByTags = new HashMap<String, List<Map<String, Object>>>();
// private Map<String, List<Map<String, Object>>> postsByarchive = new HashMap<String, List<Map<String, Object>>>();
private String contentPath;

/**
* Creates new instance of Crawler.
Expand All @@ -38,7 +41,8 @@ public class Crawler {
public Crawler(File source, CompositeConfiguration config) {
this.source = source;
this.config = config;
this.parser = new Parser(config);
this.contentPath = source.getPath() + separator + config.getString("content.folder");
this.parser = new Parser(config,contentPath);
}

/**
Expand All @@ -56,7 +60,7 @@ public void crawl(File path) {
Map<String, Object> fileContents = parser.processFile(contents[i]);
if (fileContents != null) {
fileContents.put("file", contents[i].getPath());
String uri = contents[i].getPath().replace(source.getPath() + File.separator + config.getString("content.folder"), "");
String uri = contents[i].getPath().replace(contentPath, "");
uri = uri.substring(0, uri.lastIndexOf("."));
fileContents.put("uri", uri+config.getString("output.extension"));

Expand All @@ -65,7 +69,6 @@ public void crawl(File path) {
} else {
// everything else is considered a post
posts.add(fileContents);

if (fileContents.get("tags") != null) {
String[] tags = (String[]) fileContents.get("tags");
for (String tag : tags) {
Expand All @@ -78,7 +81,7 @@ public void crawl(File path) {
}
}
}
if (fileContents.get("status").equals("published-date")) {
if (fileContents.get("date") != null && (fileContents.get("date") instanceof Date)) {
if (new Date().after((Date)fileContents.get("date"))) {
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/org/jbake/app/Oven.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package org.jbake.app;

import static org.jbake.app.SortUtil.REVERSE;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.FileUtils;

import static org.jbake.app.SortUtil.REVERSE;

/**
* All the baking happens in the Oven!
Expand Down
130 changes: 111 additions & 19 deletions src/main/java/org/jbake/app/Parser.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package org.jbake.app;

import com.petebevin.markdown.MarkdownProcessor;
import static org.apache.commons.lang.BooleanUtils.toBooleanObject;
import static org.apache.commons.lang.math.NumberUtils.*;
import static org.asciidoctor.AttributesBuilder.attributes;
import static org.asciidoctor.OptionsBuilder.options;
import static org.asciidoctor.SafeMode.UNSAFE;

import org.pegdown.Extensions;

import org.pegdown.PegDownProcessor;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.io.IOUtils;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Asciidoctor.Factory;
import org.asciidoctor.Attributes;
import org.asciidoctor.AttributesBuilder;
import org.asciidoctor.DocumentHeader;
import org.asciidoctor.Options;
import org.asciidoctor.OptionsBuilder;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -22,8 +29,12 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.configuration.Configuration;

/**
* Parses a File for content.
Expand All @@ -36,11 +47,69 @@ public class Parser {
private CompositeConfiguration config;
private Map<String, Object> content = new HashMap<String, Object>();
private Asciidoctor asciidoctor;

private String contentPath;
private PegDownProcessor pegdownProcessor;

public Parser(CompositeConfiguration config) {
/**
* Creates a new instance of Parser.
*/
public Parser(CompositeConfiguration config, String contentPath) {
this.config = config;
this.contentPath = contentPath;
asciidoctor = Factory.create();

String[] mdExts = config.getStringArray("markdown.extensions");

if (mdExts.length > 0) {
int extensions = Extensions.NONE;

for (int index = 0; index < mdExts.length; index++) {
if (mdExts[index].equals("HARDWRAPS")) {
extensions |= Extensions.HARDWRAPS;
}
else if (mdExts[index].equals("AUTOLINKS")) {
extensions |= Extensions.AUTOLINKS;
}
else if (mdExts[index].equals("FENCED_CODE_BLOCKS")) {
extensions |= Extensions.FENCED_CODE_BLOCKS;
}
else if (mdExts[index].equals("DEFINITIONS")) {
extensions |= Extensions.DEFINITIONS;
}
else if (mdExts[index].equals("ABBREVIATIONS")) {
extensions |= Extensions.ABBREVIATIONS;
}
else if (mdExts[index].equals("QUOTES")) {
extensions |= Extensions.QUOTES;
}
else if (mdExts[index].equals("SMARTS")) {
extensions |= Extensions.SMARTS;
}
else if (mdExts[index].equals("SMARTYPANTS")) {
extensions |= Extensions.SMARTYPANTS;
}
else if (mdExts[index].equals("SUPPRESS_ALL_HTML")) {
extensions |= Extensions.SUPPRESS_ALL_HTML;
}
else if (mdExts[index].equals("SUPPRESS_HTML_BLOCKS")) {
extensions |= Extensions.SUPPRESS_HTML_BLOCKS;
}
else if (mdExts[index].equals("SUPPRESS_INLINE_HTML")) {
extensions |= Extensions.SUPPRESS_INLINE_HTML;
}
else if (mdExts[index].equals("TABLES")) {
extensions |= Extensions.TABLES;
}
else if (mdExts[index].equals("WIKILINKS")) {
extensions |= Extensions.WIKILINKS;
}
else if (mdExts[index].equals("ALL")) {
extensions = Extensions.ALL;
}
}

pegdownProcessor = new PegDownProcessor(extensions);
}
}

/**
Expand Down Expand Up @@ -81,8 +150,7 @@ public Map<String, Object> processFile(File file) {
// process jbake header
processHeader(fileContents);
processBody(fileContents, file);
} else {
// try extracting meta data out of asciidoc header instead
} else {// try extracting meta data out of asciidoc header instead
if (validateAsciiDoc(file)) {
processAsciiDocHeader(file);
processAsciiDoc(fileContents);
Expand Down Expand Up @@ -186,14 +254,9 @@ private boolean validateAsciiDoc(File file) {
boolean statusFound = false;
boolean typeFound = false;

for (String key : header.getAttributes().keySet()) {
if (key.equals("jbake-status")) {
statusFound = true;
}
if (key.equals("jbake-type")) {
typeFound = true;
}
}
Set<String> headerAttKeys = header.getAttributes().keySet();
statusFound = headerAttKeys.contains("jbake-status");
typeFound = headerAttKeys.contains("jbake-type");

if (!statusFound || !typeFound) {
return false;
Expand Down Expand Up @@ -268,8 +331,12 @@ private void processBody(List<String> contents, File file) {
}

if (file.getPath().endsWith(".md")) {
MarkdownProcessor markdown = new MarkdownProcessor();
content.put("body", markdown.markdown(body.toString()));
if (pegdownProcessor == null) {
pegdownProcessor = new PegDownProcessor();
}

String markdown = pegdownProcessor.markdownToHtml(body.toString());
content.put("body", markdown);
} else if (file.getPath().endsWith(".ad") || file.getPath().endsWith(".asciidoc") || file.getPath().endsWith(".adoc")) {
processAsciiDoc(body);
} else {
Expand All @@ -293,8 +360,33 @@ private void processAsciiDoc(List<String> contents) {
}

private void processAsciiDoc(StringBuffer contents) {
Attributes attributes = AttributesBuilder.attributes(config.getString("asciidoctor.options")).get();
Options options = OptionsBuilder.options().attributes(attributes).get();
Options options = getAsciiDocOptionsAndAttributes();
content.put("body", asciidoctor.render(contents.toString(), options));
}

private Options getAsciiDocOptionsAndAttributes() {
Attributes attributes = attributes(config.getStringArray("asciidoctor.attributes")).get();
Configuration optionsSubset = config.subset("asciidoctor.option");
Options options = options().attributes(attributes).get();
for (Iterator<String> iterator = optionsSubset.getKeys(); iterator.hasNext();) {
String name = iterator.next();
options.setOption(name, guessTypeByContent(optionsSubset.getString(name)));
}
options.setBaseDir(contentPath);
options.setSafe(UNSAFE);
return options;
}

/**
* Guess the type by content it has.
* @param value
* @return boolean,integer of string as fallback
*/
private Object guessTypeByContent(String value){
if (toBooleanObject(value)!=null)
return toBooleanObject(value);
if(isNumber(value))
return toInt(value);
return value;
}
}
5 changes: 4 additions & 1 deletion src/main/resources/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ server.port=8820
# default template file
base.template=base.zip
# default asciidoctor options
asciidoctor.options=source-highlighter=prettify
asciidoctor.attributes=source-highlighter=prettify
# comma delimited default markdown extensions; for available extensions:
# http://www.decodified.com/pegdown/api/org/pegdown/Extensions.html
markdown.extensions=HARDWRAPS,AUTOLINKS,FENCED_CODE_BLOCKS,DEFINITIONS
Loading