Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to render sitemap.xml #58

Merged
merged 1 commit into from
Dec 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>

<asciidoctor.java.version>0.1.4</asciidoctor.java.version>
<commons.collections.version>3.2.1</commons.collections.version>
<commons.io.version>2.4</commons.io.version>
<commons.configuration.version>1.9</commons.configuration.version>
<args4j.version>2.0.23</args4j.version>
Expand Down Expand Up @@ -184,6 +185,11 @@
</resources>
</build>
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons.collections.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/jbake/app/Oven.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
Expand Down
31 changes: 29 additions & 2 deletions src/main/java/org/jbake/app/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class Renderer {
private CompositeConfiguration config;
private List<Map<String, Object>> posts;
private List<Map<String, Object>> pages;

public static final String SITEMAP_XML_FILE_NAME = "sitemap.xml";

/**
* Creates a new instance of Renderer with supplied references to folders.
Expand Down Expand Up @@ -178,8 +180,33 @@ public void renderFeed(List<Map<String, Object>> 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 <a href="https://support.google.com/webmasters/answer/156184?hl=en&ref_topic=8476">About Sitemaps</a>
* @see <a href="http://www.sitemaps.org/">Sitemap protocol</a>
*/
public void renderSitemap(List<Map<String, Object>> pages) {

final File outputFile = new File(destination.getPath() + File.separator + SITEMAP_XML_FILE_NAME);
System.out.print("Rendering sitemap [" + outputFile + "]... ");

Map<String, Object> model = new HashMap<String, Object>();
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
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ server.port=8820
# default template file
base.template=base.zip
# default asciidoctor options
asciidoctor.options=source-highlighter=prettify
asciidoctor.options=source-highlighter=prettify
# render sitemap.xml file?
render.sitemap=true
# filename of sitemap template file
template.sitemap.file=sitemap.ftl
Binary file modified src/main/templates/base.zip
Binary file not shown.
27 changes: 26 additions & 1 deletion src/test/java/org/jbake/app/RendererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
Assert.assertTrue(lines[1].trim().startsWith("<urlset"));
Assert.assertTrue(lines[2].trim().equals("<url>"));
Assert.assertTrue(lines[3].trim().startsWith("<loc>"));
Assert.assertTrue(lines[4].trim().startsWith("<lastmod>"));

Assert.assertTrue(lines[lines.length - 2].trim().equals("</url>"));
Assert.assertTrue(lines[lines.length - 1].trim().equals("</urlset>"));

}

@Test
public void renderArchive() throws Exception {
Crawler crawler = new Crawler(sourceFolder, config);
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/custom.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ archive.file=archive.html
render.tags=false
tag.path=tags
test.property=testing123
site.host=http://www.jbake.org
9 changes: 9 additions & 0 deletions src/test/resources/templates/sitemap.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<#list pages as page>
<url>
<loc>${config.site_host}${page.uri}</loc>
<lastmod>${page.date?string("yyyy-MM-dd")}</lastmod>
</url>
</#list>
</urlset>