Skip to content

Commit

Permalink
fixes #132 by moving all config keys into a constant interface.
Browse files Browse the repository at this point in the history
It's not yet optimal (as there is not yet any separation between the various rendering engines), but at least it's a good start, no ?
  • Loading branch information
Riduidel committed Sep 1, 2014
1 parent 7aaaaaa commit 19b119b
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 52 deletions.
108 changes: 107 additions & 1 deletion src/main/java/org/jbake/app/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,114 @@
* @author Jonathan Bullock <[email protected]>
*/
public class ConfigUtil {
/**
* Set of config keys used by JBake
* @author ndx
*
*/
public static interface Keys {

public final static String DATE_FORMAT = "date.format";
/**
* Path of archived file. Should only be used when {@link #RENDER_ARCHIVE} is true.
*/
static final String ARCHIVE_FILE = "archive.file";
static final String ASCIIDOCTOR_ATTRIBUTES = "asciidoctor.attributes";
static final String ASCIIDOCTOR_ATTRIBUTES_EXPORT = "asciidoctor.attributes.export";
static final String ASCIIDOCTOR_ATTRIBUTES_EXPORT_PREFIX = "asciidoctor.attributes.export.prefix";
static final String ASCIIDOCTOR_OPTION = "asciidoctor.option";
/**
* Folder where assets are stored. Assets are copied directly in output folder and not processed by JBake.
*/
static final String ASSET_FOLDER = "asset.folder";
/**
* Timestamp that will be output when generation start.
*/
static final String BUILD_TIMESTAMP = "build.timestamp";
/**
* Folder where content (that's to say files to be transformed) resides in
*/
static final String CONTENT_FOLDER = "content.folder";
/**
* How date is formated
*/
final static String DATE_FORMAT = "date.format";
static final String DB_PATH = "db.path";
static final String DB_STORE = "db.store";
/**
* Default status to use (in roder to avoid putting it in all files
*/
static final String DEFAULT_STATUS = "default.status";
/**
* Path where html files are generated in.
*/
static final String DESTINATION_FOLDER = "destination.folder";
/**
* Suffix used to identify draft files
*/
static final String DRAFT_SUFFIX = "draft.suffix";
/**
* Feed template file. IS required only when {@link #RENDER_FEED} is set to true
*/
static final String FEED_FILE = "feed.file";
/**
* Index template file. Required only when {@link #RENDER_INDEX} is set to true
*/
static final String INDEX_FILE = "index.file";
/**
* Common extension for all output files
*/
static final String OUTPUT_EXTENSION = "output.extension";
/**
* Flag indicating if archive file should be generated.
*/
static final String RENDER_ARCHIVE = "render.archive";
/**
* Encoding used to render files.
*/
static final String RENDER_ENCODING = "render.encoding";
/**
* Flag indicating if feed file should be generated.
*/
static final String RENDER_FEED = "render.feed";
/**
* Flag indicating if index file should be generated.
*/
static final String RENDER_INDEX = "render.index";
/**
* Flag indicating if sitemap file should be generated.
*/
static final String RENDER_SITEMAP = "render.sitemap";
/**
* Flag indicating if tags file should be generated.
*/
static final String RENDER_TAGS = "render.tags";
/**
* Port used when running Jetty server
*/
static final String SERVER_PORT = "server.port";
/**
* Sitemap template file name. Used only when {@link #RENDER_SITEMAP} is set to true
*/
static final String SITEMAP_FILE = "sitemap.file";
/**
* tags template file name. Used only when {@link #RENDER_TAGS} is set to true
*/
static final String TAG_PATH = "tag.path";
/**
* Ecoding for template files
*/
static final String TEMPLATE_ENCODING = "template.encoding";
/**
* Folder where template files are looked for
*/
static final String TEMPLATE_FOLDER = "template.folder";
/**
* Locale used for thymeleaf template rendering
*/
public static final String THYMELEAF_LOCALE = "thymeleaf.locale";
static final String VERSION = "version";

}

public static CompositeConfiguration load(File source) throws ConfigurationException {
CompositeConfiguration config = new CompositeConfiguration();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/jbake/app/Crawler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.model.DocumentStatus;
import org.jbake.model.DocumentTypes;
import org.slf4j.Logger;
Expand Down Expand Up @@ -39,7 +41,7 @@ public class Crawler {
public Crawler(ODatabaseDocumentTx db, File source, CompositeConfiguration config) {
this.db = db;
this.config = config;
this.contentPath = source.getPath() + separator + config.getString("content.folder");
this.contentPath = source.getPath() + separator + config.getString(ConfigUtil.Keys.CONTENT_FOLDER);
this.parser = new Parser(config, contentPath);
}

Expand Down Expand Up @@ -108,7 +110,7 @@ private String buildURI(final File sourceFile) {
if (uri.startsWith("/")) {
uri = uri.substring(1, uri.length());
}
uri = uri.substring(0, uri.lastIndexOf(".")) + config.getString("output.extension");
uri = uri.substring(0, uri.lastIndexOf(".")) + config.getString(Keys.OUTPUT_EXTENSION);
return uri;
}

Expand Down
32 changes: 17 additions & 15 deletions src/main/java/org/jbake/app/Oven.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.impl.ODocument;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.model.DocumentTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -74,7 +76,7 @@ private void ensureSource() throws Exception {

private void ensureDestination() throws Exception {
if (null == destination) {
destination = new File(config.getString("destination.folder"));
destination = new File(config.getString(Keys.DESTINATION_FOLDER));
}
if (!destination.exists()) {
destination.mkdirs();
Expand All @@ -91,9 +93,9 @@ private void ensureDestination() throws Exception {
*/
public void setupPaths() throws Exception {
ensureSource();
templatesPath = setupRequiredFolderFromConfig("template.folder");
contentsPath = setupRequiredFolderFromConfig("content.folder");
assetsPath = setupPathFromConfig("asset.folder");
templatesPath = setupRequiredFolderFromConfig(Keys.TEMPLATE_FOLDER);
contentsPath = setupRequiredFolderFromConfig(Keys.CONTENT_FOLDER);
assetsPath = setupPathFromConfig(Keys.ASSET_FOLDER);
if (!assetsPath.exists()) {
LOGGER.warn("No asset folder was found!");
}
Expand All @@ -118,7 +120,7 @@ private File setupRequiredFolderFromConfig(String key) throws Exception {
* @throws Exception
*/
public void bake() throws Exception {
ODatabaseDocumentTx db = DBUtil.createDB(config.getString("db.store"), config.getString("db.path"));
ODatabaseDocumentTx db = DBUtil.createDB(config.getString(Keys.DB_STORE), config.getString(Keys.DB_PATH));
updateDocTypesFromConfiguration();
DBUtil.updateSchema(db);
try {
Expand Down Expand Up @@ -148,45 +150,45 @@ public void bake() throws Exception {
}

// write index file
if (config.getBoolean("render.index")) {
if (config.getBoolean(Keys.RENDER_INDEX)) {
try {
renderer.renderIndex(config.getString("index.file"));
renderer.renderIndex(config.getString(Keys.INDEX_FILE));
} catch (Exception e) {
errors.add(e.getMessage());
}
}

// write feed file
if (config.getBoolean("render.feed")) {
if (config.getBoolean(Keys.RENDER_FEED)) {
try {
renderer.renderFeed(config.getString("feed.file"));
renderer.renderFeed(config.getString(Keys.FEED_FILE));
} catch (Exception e) {
errors.add(e.getMessage());
}
}

// write sitemap file
if (config.getBoolean("render.sitemap")) {
if (config.getBoolean(Keys.RENDER_SITEMAP)) {
try {
renderer.renderSitemap(config.getString("sitemap.file"));
renderer.renderSitemap(config.getString(Keys.SITEMAP_FILE));
} catch (Exception e) {
errors.add(e.getMessage());
}
}

// write master archive file
if (config.getBoolean("render.archive")) {
if (config.getBoolean(Keys.RENDER_ARCHIVE)) {
try {
renderer.renderArchive(config.getString("archive.file"));
renderer.renderArchive(config.getString(Keys.ARCHIVE_FILE));
} catch (Exception e) {
errors.add(e.getMessage());
}
}

// write tag files
if (config.getBoolean("render.tags")) {
if (config.getBoolean(Keys.RENDER_TAGS)) {
try {
renderer.renderTags(crawler.getTags(), config.getString("tag.path"));
renderer.renderTags(crawler.getTags(), config.getString(Keys.TAG_PATH));
} catch (Exception e) {
errors.add(e.getMessage());
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/jbake/app/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.io.IOUtils;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.parser.Engines;
import org.jbake.parser.MarkupEngine;
import org.jbake.parser.ParserContext;
Expand Down Expand Up @@ -52,7 +53,7 @@ public Map<String, Object> processFile(File file) {
List<String> fileContents = null;
try {
is = new FileInputStream(file);
fileContents = IOUtils.readLines(is, config.getString("render.encoding"));
fileContents = IOUtils.readLines(is, config.getString(Keys.RENDER_ENCODING));
} catch (IOException e) {
LOGGER.error("Error while opening file {}: {}", file, e);

Expand Down Expand Up @@ -84,11 +85,11 @@ public Map<String, Object> processFile(File file) {
// then read engine specific headers
engine.processHeader(context);

if (config.getString("default.status") != null) {
if (config.getString(Keys.DEFAULT_STATUS) != null) {
// default status has been set
if (content.get("status") == null) {
// file hasn't got status so use default
content.put("status", config.getString("default.status"));
content.put("status", config.getString(Keys.DEFAULT_STATUS));
}
}

Expand Down Expand Up @@ -173,7 +174,7 @@ private void processHeader(List<String> contents, final Map<String, Object> cont
String[] parts = line.split("=");
if (parts.length == 2) {
if (parts[0].equalsIgnoreCase("date")) {
DateFormat df = new SimpleDateFormat(config.getString(ConfigUtil.DATE_FORMAT));
DateFormat df = new SimpleDateFormat(config.getString(Keys.DATE_FORMAT));
Date date = null;
try {
date = df.parse(parts[1]);
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/jbake/app/Renderer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jbake.app;

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.template.DelegatingTemplateEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -61,19 +63,19 @@ public void render(Map<String, Object> content) throws Exception {
outputFilename = outputFilename.substring(0, outputFilename.lastIndexOf("."));

// delete existing versions if they exist in case status has changed either way
File draftFile = new File(outputFilename + config.getString("draft.suffix") + config.getString("output.extension"));
File draftFile = new File(outputFilename + config.getString(Keys.DRAFT_SUFFIX) + config.getString(Keys.OUTPUT_EXTENSION));
if (draftFile.exists()) {
draftFile.delete();
}
File publishedFile = new File(outputFilename + config.getString("output.extension"));
File publishedFile = new File(outputFilename + config.getString(Keys.OUTPUT_EXTENSION));
if (publishedFile.exists()) {
publishedFile.delete();
}

if (content.get("status").equals("draft")) {
outputFilename = outputFilename + config.getString("draft.suffix");
outputFilename = outputFilename + config.getString(Keys.DRAFT_SUFFIX);
}
File outputFile = new File(outputFilename + config.getString("output.extension"));
File outputFile = new File(outputFilename + config.getString(Keys.OUTPUT_EXTENSION));

StringBuilder sb = new StringBuilder();
sb.append("Rendering [").append(outputFile).append("]... ");
Expand Down Expand Up @@ -101,7 +103,7 @@ private Writer createWriter(File file) throws IOException {
file.createNewFile();
}

return new OutputStreamWriter(new FileOutputStream(file), config.getString("render.encoding"));
return new OutputStreamWriter(new FileOutputStream(file), config.getString(ConfigUtil.Keys.RENDER_ENCODING));
}

/**
Expand Down Expand Up @@ -230,7 +232,7 @@ public void renderTags(Set<String> tags, String tagPath) throws Exception {
model.put("content", Collections.singletonMap("type","tag"));

tag = tag.trim().replace(" ", "-");
File outputFile = new File(destination.getPath() + File.separator + tagPath + File.separator + tag + config.getString("output.extension"));
File outputFile = new File(destination.getPath() + File.separator + tagPath + File.separator + tag + config.getString(Keys.OUTPUT_EXTENSION));
StringBuilder sb = new StringBuilder();
sb.append("Rendering tags [").append(outputFile).append("]... ");

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/jbake/launcher/Init.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.FileInputStream;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.app.ZipUtil;

/**
Expand Down Expand Up @@ -37,13 +38,13 @@ public void run(File outputFolder, File templateLocationFolder, String templateT
if (contents != null) {
for (File content : contents) {
if (content.isDirectory()) {
if (content.getName().equalsIgnoreCase(config.getString("template.folder"))) {
if (content.getName().equalsIgnoreCase(config.getString(Keys.TEMPLATE_FOLDER))) {
safe = false;
}
if (content.getName().equalsIgnoreCase(config.getString("content.folder"))) {
if (content.getName().equalsIgnoreCase(config.getString(Keys.CONTENT_FOLDER))) {
safe = false;
}
if (content.getName().equalsIgnoreCase(config.getString("asset.folder"))) {
if (content.getName().equalsIgnoreCase(config.getString(Keys.ASSET_FOLDER))) {
safe = false;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/jbake/launcher/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.jbake.app.ConfigUtil;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.app.FileUtil;
import org.jbake.app.Oven;
import org.kohsuke.args4j.CmdLineException;
Expand Down Expand Up @@ -66,7 +67,7 @@ private void run(String[] args) {
System.exit(1);
}

System.out.println("JBake " + config.getString("version") + " (" + config.getString("build.timestamp") + ") [http://jbake.org]");
System.out.println("JBake " + config.getString(Keys.VERSION) + " (" + config.getString(Keys.BUILD_TIMESTAMP) + ") [http://jbake.org]");
System.out.println();

if (res.isHelpNeeded()) {
Expand All @@ -90,9 +91,9 @@ private void run(String[] args) {
if (res.isRunServer()) {
if (res.getSource().getPath().equals(".")) {
// use the default destination folder
runServer(config.getString("destination.folder"), config.getString("server.port"));
runServer(config.getString(Keys.DESTINATION_FOLDER), config.getString(Keys.SERVER_PORT));
} else {
runServer(res.getSource().getPath(), config.getString("server.port"));
runServer(res.getSource().getPath(), config.getString(Keys.SERVER_PORT));
}
}

Expand Down
Loading

0 comments on commit 19b119b

Please sign in to comment.