Skip to content

Commit

Permalink
Merged in #58
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbullock committed Dec 26, 2013
2 parents 5a9427b + 13176b6 commit 64d33b1
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/jbake/app/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public class Filter {
* @param posts The posts to filter
* @return Just the published posts
*/
public static List<Map<String, Object>> getPublishedPosts(List<Map<String, Object>> posts) {
List<Map<String, Object>> publishedPosts = new ArrayList<Map<String, Object>>();
for (Map<String, Object> post : posts) {
if (post.get("status") != null) {
if (((String)post.get("status")).equalsIgnoreCase("published")) {
publishedPosts.add(post);
public static List<Map<String, Object>> getPublishedContent(List<Map<String, Object>> contentList) {
List<Map<String, Object>> publishedContent = new ArrayList<Map<String, Object>>();
for (Map<String, Object> content : contentList) {
if (content.get("status") != null) {
if (((String)content.get("status")).equalsIgnoreCase("published")) {
publishedContent.add(content);
}
}
}

return publishedPosts;
return publishedContent;
}
}
10 changes: 8 additions & 2 deletions src/main/java/org/jbake/app/Oven.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ public void bake() throws Exception {
}
}

// only interested in published posts from here on
List<Map<String, Object>> publishedPosts = Filter.getPublishedPosts(posts);
// only interested in published content from here on
List<Map<String, Object>> publishedPosts = Filter.getPublishedContent(posts);
List<Map<String, Object>> publishedPages = Filter.getPublishedContent(pages);

// write index file
if (config.getBoolean("render.index")) {
Expand All @@ -153,6 +154,11 @@ public void bake() throws Exception {
renderer.renderFeed(publishedPosts, config.getString("feed.file"));
}

// write sitemap file
if (config.getBoolean("render.sitemap")) {
renderer.renderSitemap(publishedPages, publishedPosts, config.getString("sitemap.file"));
}

// write master archive file
if (config.getBoolean("render.archive")) {
renderer.renderArchive(publishedPosts, config.getString("archive.file"));
Expand Down
31 changes: 28 additions & 3 deletions src/main/java/org/jbake/app/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,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, List<Map<String, Object>> posts, String sitemapFile) {
File outputFile = new File(destination.getPath() + File.separator + sitemapFile);
System.out.print("Rendering sitemap [" + outputFile + "]... ");

Map<String, Object> model = new HashMap<String, Object>();
model.put("published_pages", pages);
model.put("published_posts", posts);

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 Expand Up @@ -209,7 +234,7 @@ public void renderTags(Map<String, List<Map<String, Object>>> tags, String tagPa
Map<String, Object> model = new HashMap<String, Object>();
model.put("tag", tag);
// TODO: sort posts here
List<Map<String, Object>> posts = Filter.getPublishedPosts(tags.get(tag));
List<Map<String, Object>> posts = Filter.getPublishedContent(tags.get(tag));
model.put("tag_posts", posts);

tag = tag.trim().replace(" ", "-");
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ template.feed.file=feed.ftl
template.archive.file=archive.ftl
# filename of tag template file
template.tag.file=tags.ftl
# filename of sitemap template file
template.sitemap.file=sitemap.ftl
# folder that contains all content files
content.folder=content
# folder that contains all asset files
Expand All @@ -33,6 +35,10 @@ feed.file=feed.xml
render.archive=true
# filename to use for archive file
archive.file=archive.html
# render sitemap.xml file?
render.sitemap=true
# filename to use for sitemap file
sitemap.file=sitemap.xml
# render tag files?
render.tags=true
# folder name to use for tag files
Expand All @@ -51,4 +57,4 @@ asciidoctor.attributes=source-highlighter=prettify
date.format=yyyy-MM-dd
# comma delimited default markdown extensions; for available extensions:
# http://www.decodified.com/pegdown/api/org/pegdown/Extensions.html
markdown.extensions=HARDWRAPS,AUTOLINKS,FENCED_CODE_BLOCKS,DEFINITIONS
markdown.extensions=HARDWRAPS,AUTOLINKS,FENCED_CODE_BLOCKS,DEFINITIONS
Binary file modified src/main/templates/base.zip
Binary file not shown.
35 changes: 34 additions & 1 deletion src/test/java/org/jbake/app/RendererTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package org.jbake.app;

import java.io.File;
import java.io.IOException;
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.configuration.CompositeConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -149,7 +159,30 @@ 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(Filter.getPublishedContent(crawler.getPages()), Filter.getPublishedContent(crawler.getPosts()), config.getString("sitemap.file"));
File outputFile = new File(destinationFolder, config.getString("sitemap.file"));
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[3].trim().contains("about.html"));
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
3 changes: 2 additions & 1 deletion src/test/resources/custom.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ archive.file=archive.html
render.tags=false
tag.path=tags
test.property=testing123
markdown.extensions=HARDWRAPS,AUTOLINKS,FENCED_CODE_BLOCKS,DEFINITIONS
markdown.extensions=HARDWRAPS,AUTOLINKS,FENCED_CODE_BLOCKS,DEFINITIONS
site.host=http://www.jbake.org
15 changes: 15 additions & 0 deletions src/test/resources/templates/sitemap.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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 published_pages as page>
<url>
<loc>${config.site_host}${page.uri}</loc>
<lastmod>${page.date?string("yyyy-MM-dd")}</lastmod>
</url>
</#list>
<#list published_posts as post>
<url>
<loc>${config.site_host}${post.uri}</loc>
<lastmod>${post.date?string("yyyy-MM-dd")}</lastmod>
</url>
</#list>
</urlset>

0 comments on commit 64d33b1

Please sign in to comment.