From 6ff0b2a03429c522057f3b9d44fedfd44f52ec80 Mon Sep 17 00:00:00 2001 From: Ilja Date: Tue, 26 Nov 2013 14:38:38 +0200 Subject: [PATCH] Added functionality render sitemap.xml Signed-off-by: Ilja --- pom.xml | 6 ++++ src/main/java/org/jbake/app/Oven.java | 7 ++++ src/main/java/org/jbake/app/Renderer.java | 31 ++++++++++++++++-- src/main/resources/default.properties | 6 +++- src/main/templates/base.zip | Bin 116794 -> 117318 bytes src/test/java/org/jbake/app/RendererTest.java | 27 ++++++++++++++- src/test/resources/custom.properties | 1 + src/test/resources/templates/sitemap.ftl | 9 +++++ 8 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/templates/sitemap.ftl diff --git a/pom.xml b/pom.xml index e2a6b59bb..feba4683b 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,7 @@ yyyy-MM-dd HH:mm 0.1.4 + 3.2.1 2.4 1.9 2.0.23 @@ -184,6 +185,11 @@ + + commons-collections + commons-collections + ${commons.collections.version} + commons-io commons-io diff --git a/src/main/java/org/jbake/app/Oven.java b/src/main/java/org/jbake/app/Oven.java index 14d032570..eb1a038ed 100644 --- a/src/main/java/org/jbake/app/Oven.java +++ b/src/main/java/org/jbake/app/Oven.java @@ -9,6 +9,8 @@ import java.util.List; import java.util.Map; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.ListUtils; import org.apache.commons.configuration.CompositeConfiguration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; @@ -159,6 +161,11 @@ public void bake() throws Exception { renderer.renderFeed(publishedPosts, config.getString("feed.file")); } + // write sitemap file + if (config.getBoolean("render.sitemap")) { + renderer.renderSitemap(ListUtils.union(pages, publishedPosts)); + } + // write master archive file if (config.getBoolean("render.archive")) { renderer.renderArchive(publishedPosts, config.getString("archive.file")); diff --git a/src/main/java/org/jbake/app/Renderer.java b/src/main/java/org/jbake/app/Renderer.java index 416ed4f08..a51b93df1 100644 --- a/src/main/java/org/jbake/app/Renderer.java +++ b/src/main/java/org/jbake/app/Renderer.java @@ -35,6 +35,8 @@ public class Renderer { private CompositeConfiguration config; private List> posts; private List> pages; + + public static final String SITEMAP_XML_FILE_NAME = "sitemap.xml"; /** * Creates a new instance of Renderer with supplied references to folders. @@ -178,8 +180,33 @@ public void renderFeed(List> posts, String feedFile) { System.out.println("failed!"); } } - - /** + + /** + * Render an XML sitemap file using the supplied content. + * + * @param pages The combined list of pages and posts to render + * + * @see About Sitemaps + * @see Sitemap protocol + */ + public void renderSitemap(List> pages) { + + final File outputFile = new File(destination.getPath() + File.separator + SITEMAP_XML_FILE_NAME); + System.out.print("Rendering sitemap [" + outputFile + "]... "); + + Map model = new HashMap(); + model.put("pages", pages); + + try { + render(model, config.getString("template.sitemap.file"), outputFile); + System.out.println("done!"); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("failed!"); + } + } + + /** * Render an archive file using the supplied content. * * @param posts The content to render diff --git a/src/main/resources/default.properties b/src/main/resources/default.properties index b15fa378b..956c626c3 100644 --- a/src/main/resources/default.properties +++ b/src/main/resources/default.properties @@ -43,4 +43,8 @@ server.port=8820 # default template file base.template=base.zip # default asciidoctor options -asciidoctor.options=source-highlighter=prettify \ No newline at end of file +asciidoctor.options=source-highlighter=prettify +# render sitemap.xml file? +render.sitemap=true +# filename of sitemap template file +template.sitemap.file=sitemap.ftl \ No newline at end of file diff --git a/src/main/templates/base.zip b/src/main/templates/base.zip index 754ace88cd3fce3258633b2d1bba93dca5eb385e..97474b851a1038e9707aa1c0ec109d85f29f90e9 100644 GIT binary patch delta 559 zcmdlrf&JJN_J%Et^LE!WvxqP-FmN!O%dB#qyLZ)`Bt`~?4kiW$Q3e@?lGNOSoWzpU zV*TPwAUm-@FRdgeG=!6Zc}mliAP_FC;AUWCdBM!U04AnRI_TeQAad;d>-fe}!>R>$ zOU`*V9PM8I*2MJQG(M{gCf%3Xhc>;b-!8K4MdAC9S?_<>*RFprsCRqQhq@hX3sYK_ zr!?8>2~Xvk>H6dIQhNd_9{T3=F<|K?VT; D8s_!N delta 48 zcmX>$g?-lq_J%Et^L9^HILyd0-R}e=+xE!4jCDLBtZWQGpbUf^JPZtTb~6Hb0GhfD A9RL6T diff --git a/src/test/java/org/jbake/app/RendererTest.java b/src/test/java/org/jbake/app/RendererTest.java index bf0d7a3d6..36fc44bfe 100644 --- a/src/test/java/org/jbake/app/RendererTest.java +++ b/src/test/java/org/jbake/app/RendererTest.java @@ -5,11 +5,14 @@ import java.io.PrintWriter; import java.net.URISyntaxException; import java.net.URL; +import java.util.List; import java.util.Map; import java.util.Scanner; +import org.apache.commons.collections.ListUtils; import org.apache.commons.configuration.CompositeConfiguration; import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.commons.io.FileUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -146,7 +149,29 @@ public void renderFeed() throws Exception { Assert.assertTrue(foundFirstTitle); Assert.assertTrue(foundSecondTitle); } - + + @Test + public void renderSitemaps() throws Exception { + Crawler crawler = new Crawler(sourceFolder, config); + crawler.crawl(new File(sourceFolder.getPath()+File.separator+"content")); + Renderer renderer = new Renderer(sourceFolder, destinationFolder, templateFolder, config, crawler.getPosts(), crawler.getPages()); + renderer.renderSitemap(ListUtils.union(crawler.getPages(), crawler.getPosts())); + File outputFile = new File(destinationFolder, "sitemap.xml"); + Assert.assertTrue(outputFile.exists()); + + final String[] lines = FileUtils.readLines(outputFile).toArray(new String[0]); + + Assert.assertTrue(lines[0].trim().equals("")); + Assert.assertTrue(lines[1].trim().startsWith("")); + Assert.assertTrue(lines[3].trim().startsWith("")); + Assert.assertTrue(lines[4].trim().startsWith("")); + + Assert.assertTrue(lines[lines.length - 2].trim().equals("")); + Assert.assertTrue(lines[lines.length - 1].trim().equals("")); + + } + @Test public void renderArchive() throws Exception { Crawler crawler = new Crawler(sourceFolder, config); diff --git a/src/test/resources/custom.properties b/src/test/resources/custom.properties index 48ba7525a..17555231d 100644 --- a/src/test/resources/custom.properties +++ b/src/test/resources/custom.properties @@ -10,3 +10,4 @@ archive.file=archive.html render.tags=false tag.path=tags test.property=testing123 +site.host=http://www.jbake.org diff --git a/src/test/resources/templates/sitemap.ftl b/src/test/resources/templates/sitemap.ftl new file mode 100644 index 000000000..3c73ff1ca --- /dev/null +++ b/src/test/resources/templates/sitemap.ftl @@ -0,0 +1,9 @@ + + +<#list pages as page> + + ${config.site_host}${page.uri} + ${page.date?string("yyyy-MM-dd")} + + + \ No newline at end of file