diff --git a/src/main/java/org/jbake/app/Crawler.java b/src/main/java/org/jbake/app/Crawler.java index d3c2e0704..a1c1c826a 100644 --- a/src/main/java/org/jbake/app/Crawler.java +++ b/src/main/java/org/jbake/app/Crawler.java @@ -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; @@ -226,7 +228,7 @@ private String buildPermalink(Map 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; @@ -257,7 +259,7 @@ private String buildPermalink(Map 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"); @@ -273,7 +275,7 @@ private String buildPermalink(Map 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); diff --git a/src/main/java/org/jbake/parser/MarkupEngine.java b/src/main/java/org/jbake/parser/MarkupEngine.java index 3666be69d..916fe9320 100644 --- a/src/main/java/org/jbake/parser/MarkupEngine.java +++ b/src/main/java/org/jbake/parser/MarkupEngine.java @@ -91,6 +91,10 @@ public Map 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) { diff --git a/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java b/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java index 4a9888d18..8d501d2b7 100644 --- a/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java +++ b/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java @@ -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 { @@ -66,7 +72,6 @@ public void renderDocument(final Map model, final String templat Context context = new Context(locale, wrap(model)); lock.lock(); try { - initializeTemplateEngine(); @SuppressWarnings("unchecked") Map config = (Map) model.get("config"); @SuppressWarnings("unchecked") @@ -79,7 +84,7 @@ public void renderDocument(final Map model, final String templat mode = configMode; } } - templateResolver.setTemplateMode(mode); + initializeTemplateEngine(mode); templateEngine.process(templateName, context, writer); } finally { lock.unlock(); diff --git a/src/test/java/org/jbake/app/CrawlerTest.java b/src/test/java/org/jbake/app/CrawlerTest.java index 387d170d2..2cabf0477 100644 --- a/src/test/java/org/jbake/app/CrawlerTest.java +++ b/src/test/java/org/jbake/app/CrawlerTest.java @@ -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; @@ -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("/"); @@ -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()); @@ -66,6 +68,14 @@ public void crawl() throws ConfigurationException { .containsValue("../../"); } + List draftPosts = db.getAllContent("post"); + DocumentList draftList = DocumentList.wrap(draftPosts.iterator()); + for (Map content : list) { + if (content.get(Crawler.Attributes.TITLE).equals("Draft Post")) { + assertThat(content).containsKey(Crawler.Attributes.DATE); + } + } + // covers bug #213 List publishedPostsByTag = db.getPublishedPostsByTag("blog"); Assert.assertEquals(2, publishedPostsByTag.size()); diff --git a/src/test/resources/content/blog/2016/draft-post.html b/src/test/resources/content/blog/2016/draft-post.html index df33942e4..4c0efbef7 100644 --- a/src/test/resources/content/blog/2016/draft-post.html +++ b/src/test/resources/content/blog/2016/draft-post.html @@ -1,5 +1,4 @@ title=Draft Post -date=2016-06-22 type=post tags=blog status=draft