Skip to content

Commit

Permalink
#242 - Converted noExtensionUri feature to use 2 config options.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbullock committed Jun 14, 2016
1 parent a71fea0 commit 90b8375
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/main/java/org/jbake/app/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ public static interface Keys {
static final String ASSET_FOLDER = "asset.folder";

/**
* URI prefix for content that should be given extensionless output URI's
* Flag indicating if content matching prefix below should be given extension-less URI's
*/
static final String URI_NO_EXTENSION = "uri.noExtension";

/**
* URI prefix for content that should be given extension-less output URI's
*/
static final String URI_NO_EXTENSION_PREFIX = "uri.noExtension.prefix";

/**
* Flag indicating if hidden asset resources should be ignored
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/org/jbake/app/Crawler.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static interface Status {
static final String SHA1 = "sha1";
static final String ROOTPATH = "rootpath";
static final String ID = "id";
static final String NO_EXTENSION_URI = "noExtensionUri";

}
private static final Logger LOGGER = LoggerFactory.getLogger(Crawler.class);
Expand Down Expand Up @@ -134,12 +135,18 @@ private String buildHash(final File sourceFile) {

private String buildURI(final File sourceFile) {
String uri = FileUtil.asPath(sourceFile.getPath()).replace(FileUtil.asPath( contentPath), "");
String noExtensionUrlFolder = config.getString(Keys.URI_NO_EXTENSION);
if (!noExtensionUrlFolder.equals("false") && uri.startsWith(noExtensionUrlFolder)) {
uri = "/" + FilenameUtils.getPath(uri) + FilenameUtils.getBaseName(uri) + "/index" + config.getString(Keys.OUTPUT_EXTENSION);

boolean noExtensionUri = config.getBoolean(Keys.URI_NO_EXTENSION);
String noExtensionUriPrefix = config.getString(Keys.URI_NO_EXTENSION_PREFIX);
if (noExtensionUri && noExtensionUriPrefix != null && noExtensionUriPrefix.length() > 0) {
// convert URI from xxx.html to xxx/index.html
if (uri.startsWith(noExtensionUriPrefix)) {
uri = "/" + FilenameUtils.getPath(uri) + FilenameUtils.getBaseName(uri) + "/index" + config.getString(Keys.OUTPUT_EXTENSION);
}
} else {
uri = uri.substring(0, uri.lastIndexOf(".")) + config.getString(Keys.OUTPUT_EXTENSION);
}

// strip off leading / to enable generating non-root based sites
if (uri.startsWith("/")) {
uri = uri.substring(1, uri.length());
Expand Down Expand Up @@ -171,8 +178,8 @@ private void crawlSourceFile(final File sourceFile, final String sha1, final Str
}
}

if (!config.getString(Keys.URI_NO_EXTENSION).equals("false")) {
fileContents.put("noExtensionUri", uri.replace("/index.html", "/"));
if (config.getBoolean(Keys.URI_NO_EXTENSION)) {
fileContents.put(Attributes.NO_EXTENSION_URI, uri.replace("/index.html", "/"));
}

ODocument doc = new ODocument(documentType);
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,7 @@ markdown.maxParsingTimeInMillis=2000
db.store=memory
# database path
db.path=cache
# Set to a path (starting with a slash) for which to generate a folder and index.html
# enable extension-less URI option?
uri.noExtension=false
# Set to a prefix path (starting with a slash) for which to generate extension-less URI's (i.e. a folder with index.html in)
uri.noExtension.prefix=
3 changes: 2 additions & 1 deletion src/test/java/org/jbake/app/CrawlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public void crawl() throws ConfigurationException {
@Test
public void renderWithPrettyUrls() throws Exception {
Map<String, Object> testProperties = new HashMap<String, Object>();
testProperties.put(Keys.URI_NO_EXTENSION, "/blog");
testProperties.put(Keys.URI_NO_EXTENSION, true);
testProperties.put(Keys.URI_NO_EXTENSION_PREFIX, "/blog");

CompositeConfiguration config = new CompositeConfiguration();
config.addConfiguration(new MapConfiguration(testProperties));
Expand Down

0 comments on commit 90b8375

Please sign in to comment.