Skip to content

Commit

Permalink
Permalink Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
manikmagar committed Jun 26, 2016
2 parents 7aad1ad + 6e4a165 commit 84cf4ec
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/main/java/org/jbake/app/Crawler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Set;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import com.orientechnologies.orient.core.record.impl.ODocument;
Expand Down Expand Up @@ -226,7 +228,7 @@ private String buildPermalink(Map<String, Object> fileContents){
if(config.containsKey(Attributes.PERMALINK +"."+ fileContents.get(Attributes.TYPE))){
permalinkPattern = config.getString(Attributes.PERMALINK +"."+ fileContents.get(Attributes.TYPE));
}
if (Objects.nonNull(permalinkPattern) && !permalinkPattern.trim().isEmpty()) {
if (permalinkPattern != null && !permalinkPattern.trim().isEmpty()) {

String pattern = permalinkPattern;
if(pattern.startsWith(":")) pattern = separator+pattern;
Expand Down Expand Up @@ -257,7 +259,7 @@ private String buildPermalink(Map<String, Object> fileContents){
}
} else if (Arrays.asList("YEAR","MONTH","DAY").contains(part.toUpperCase())) {
Date publishedDate = (Date) fileContents.get("date");
if(Objects.nonNull(publishedDate)){
if(publishedDate != null){
String dateValue = null;
if(part.equalsIgnoreCase("YEAR")){
dateValue = DateFormatUtils.format(publishedDate, "yyyy");
Expand All @@ -273,7 +275,7 @@ private String buildPermalink(Map<String, Object> fileContents){
}
}

permalink = String.join(separator, pLink);
permalink = StringUtils.join(pLink, separator);
permalink = sanitize(permalink).concat(separator);
String uri = permalink;
boolean noExtensionUri = config.getBoolean(Keys.URI_NO_EXTENSION);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jbake/parser/MarkupEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public Map<String, Object> parse(Configuration config, File file, String content
// then read engine specific headers
processHeader(context);

if (content.get(Crawler.Attributes.DATE) == null) {
content.put(Crawler.Attributes.DATE, new Date(file.lastModified()));
}

if (config.getString(Keys.DEFAULT_STATUS) != null) {
// default status has been set
if (content.get(Crawler.Attributes.STATUS) == null) {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/jbake/template/ThymeleafTemplateEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,21 @@ public class ThymeleafTemplateEngine extends AbstractTemplateEngine {
private TemplateEngine templateEngine;
private FileTemplateResolver templateResolver;

private String templateMode;

public ThymeleafTemplateEngine(final CompositeConfiguration config, final ContentStore db, final File destination, final File templatesPath) {
super(config, db, destination, templatesPath);
initializeTemplateEngine();
}

private void initializeTemplateEngine() {
private void initializeTemplateEngine(String mode) {
if (mode.equals(templateMode)) {
return;
}
templateMode = mode;
templateResolver = new FileTemplateResolver();
templateResolver.setPrefix(templatesPath.getAbsolutePath() + File.separatorChar);
templateResolver.setCharacterEncoding(config.getString(Keys.TEMPLATE_ENCODING));
templateResolver.setTemplateMode(mode);
templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
try {
Expand All @@ -66,7 +72,6 @@ public void renderDocument(final Map<String, Object> model, final String templat
Context context = new Context(locale, wrap(model));
lock.lock();
try {
initializeTemplateEngine();
@SuppressWarnings("unchecked")
Map<String, Object> config = (Map<String, Object>) model.get("config");
@SuppressWarnings("unchecked")
Expand All @@ -79,7 +84,7 @@ public void renderDocument(final Map<String, Object> model, final String templat
mode = configMode;
}
}
templateResolver.setTemplateMode(mode);
initializeTemplateEngine(mode);
templateEngine.process(templateName, context, writer);
} finally {
lock.unlock();
Expand Down
22 changes: 16 additions & 6 deletions src/test/java/org/jbake/app/CrawlerTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.jbake.app;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
Expand All @@ -10,25 +8,29 @@
import java.util.List;
import java.util.Map;

import com.orientechnologies.orient.core.record.impl.ODocument;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;

import org.jbake.app.ConfigUtil.Keys;

import org.apache.commons.configuration.MapConfiguration;
import org.apache.commons.io.FilenameUtils;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.jbake.app.ConfigUtil.Keys;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.orientechnologies.orient.core.record.impl.ODocument;
import static org.assertj.core.api.Assertions.*;

public class CrawlerTest {
private CompositeConfiguration config;
private ContentStore db;
private File sourceFolder;

@Before
public void setup() throws Exception, IOException, URISyntaxException {
URL sourceUrl = this.getClass().getResource("/");
Expand All @@ -37,7 +39,7 @@ public void setup() throws Exception, IOException, URISyntaxException {
if (!sourceFolder.exists()) {
throw new Exception("Cannot find sample data structure!");
}

config = ConfigUtil.load(new File(this.getClass().getResource("/").getFile()));
Assert.assertEquals(".html", config.getString(Keys.OUTPUT_EXTENSION));
db = DBUtil.createDataStore("memory", "documents"+System.currentTimeMillis());
Expand Down Expand Up @@ -66,6 +68,14 @@ public void crawl() throws ConfigurationException {
.containsValue("../../");
}

List<ODocument> draftPosts = db.getAllContent("post");
DocumentList draftList = DocumentList.wrap(draftPosts.iterator());
for (Map<String,Object> content : list) {
if (content.get(Crawler.Attributes.TITLE).equals("Draft Post")) {
assertThat(content).containsKey(Crawler.Attributes.DATE);
}
}

// covers bug #213
List<ODocument> publishedPostsByTag = db.getPublishedPostsByTag("blog");
Assert.assertEquals(2, publishedPostsByTag.size());
Expand Down
1 change: 0 additions & 1 deletion src/test/resources/content/blog/2016/draft-post.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
title=Draft Post
date=2016-06-22
type=post
tags=blog
status=draft
Expand Down

0 comments on commit 84cf4ec

Please sign in to comment.