From f5838ab6036181d864be96a180e00b7295d5f497 Mon Sep 17 00:00:00 2001 From: Bernhard Haumacher Date: Thu, 25 Jun 2015 17:36:41 +0200 Subject: [PATCH 1/3] Prevent multiple initializations of the Thymeleaf template engine. Initializing the template engine is a costly operation, because this drops the resolved templates. When initializing the template engine for each page, all used templates must be resolved for each rendering operation. This greatly reduces rendering performance. Instead, only re-initialize the template engine, if the template mode changes. --- .../org/jbake/template/ThymeleafTemplateEngine.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java b/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java index cbcaa73a3..935e67dc7 100644 --- a/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java +++ b/src/main/java/org/jbake/template/ThymeleafTemplateEngine.java @@ -49,15 +49,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 { @@ -75,7 +81,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") @@ -88,7 +93,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(); From a173a33ad632564a69f15cef6db04af64fe7a94c Mon Sep 17 00:00:00 2001 From: Jonathan Bullock Date: Fri, 24 Jun 2016 23:08:22 +0100 Subject: [PATCH 2/3] Merged #217 manually due to change in project structure. --- src/main/java/org/jbake/parser/MarkupEngine.java | 4 ++++ 1 file changed, 4 insertions(+) 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) { From 809c197be5a21d6729c8e96ceba32a93fb4def3d Mon Sep 17 00:00:00 2001 From: Jonathan Bullock Date: Fri, 24 Jun 2016 23:20:07 +0100 Subject: [PATCH 3/3] Added test to cover #217. --- src/test/java/org/jbake/app/CrawlerTest.java | 10 ++++++++++ src/test/resources/content/blog/2016/draft-post.html | 1 - 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/jbake/app/CrawlerTest.java b/src/test/java/org/jbake/app/CrawlerTest.java index c5d46f409..7f9c209e5 100644 --- a/src/test/java/org/jbake/app/CrawlerTest.java +++ b/src/test/java/org/jbake/app/CrawlerTest.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; +import java.util.Date; import java.util.List; import java.util.Map; @@ -13,6 +14,7 @@ import org.apache.commons.configuration.CompositeConfiguration; import org.apache.commons.configuration.ConfigurationException; import org.jbake.app.ConfigUtil.Keys; + import java.util.HashMap; import java.util.Map; @@ -71,6 +73,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