Skip to content

Commit

Permalink
First pass at standardising the data model available to all templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbullock committed Apr 21, 2013
1 parent f626025 commit 3c4ba20
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ROADMAP.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ v2.1
- Create unit tests using JUnit/TestNG *
- Add post baking stats (how many posts/pages baked) *
- Make source folder arg optional, default to current directory
- Make more data available to the templates via the data model object (such as config options, all posts & pages)
- Make more data available to the templates via the data model object (such as config options, all posts & pages) *

v2.2
- Add logging support (Log4j or SLF4J), add logging options to configuration
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jbake/app/Oven.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void bake() throws Exception {
// sort posts
Collections.sort(posts, SortUtil.getComparator(REVERSE));

Renderer renderer = new Renderer(source, destination, templatesPath, config);
Renderer renderer = new Renderer(source, destination, templatesPath, config, posts, pages);

int renderedCount = 0;
int errorCount = 0;
Expand Down
39 changes: 33 additions & 6 deletions src/main/java/org/jbake/app/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand All @@ -32,6 +33,8 @@ public class Renderer {
private File destination;
private Configuration templateCfg;
private CompositeConfiguration config;
private List<Map<String, Object>> posts;
private List<Map<String, Object>> pages;

/**
* Creates a new instance of Renderer with supplied references to folders.
Expand All @@ -50,12 +53,36 @@ public Renderer(File source, File destination, File templatesPath, CompositeConf
} catch (IOException e) {
e.printStackTrace();
}
templateCfg.setObjectWrapper(new DefaultObjectWrapper());

templateCfg.setObjectWrapper(new DefaultObjectWrapper());
}

/**
* Creates a new instance of Renderer with supplied references to folders.
*
* @param source The source folder
* @param destination The destination folder
* @param templatesPath The templates folder
* @param posts The list of posts
* @param pages The list of pages
*/
public Renderer(File source, File destination, File templatesPath, CompositeConfiguration config, List<Map<String, Object>> posts, List<Map<String, Object>> pages) {
this(source, destination, templatesPath, config);
this.posts = posts;
this.pages = pages;
}

private void render(Map<String, Object> model, String templateFilename, File outputFile) throws Exception {
model.put("version", Main.VERSION);
model.put("posts", posts);
model.put("pages", pages);
Map<String, Object> configModel = new HashMap<String, Object>();
Iterator<String> configKeys = config.getKeys();
while (configKeys.hasNext()) {
String key = configKeys.next();
//replace "." in key so you can use dot notation in templates
configModel.put(key.replace(".", "_"), config.getProperty(key));
}
model.put("config", configModel);
Template template = null;
template = templateCfg.getTemplate(templateFilename);

Expand Down Expand Up @@ -119,7 +146,7 @@ public void renderIndex(List<Map<String, Object>> posts, String indexFile) {
File outputFile = new File(destination.getPath() + File.separator + indexFile);
System.out.print("Rendering index [" + outputFile + "]... ");
Map<String, Object> model = new HashMap<String, Object>();
model.put("posts", posts);
// model.put("posts", posts);

try {
render(model, config.getString("template.index.file"), outputFile);
Expand All @@ -140,7 +167,7 @@ public void renderFeed(List<Map<String, Object>> posts, String feedFile) {
File outputFile = new File(destination.getPath() + File.separator + feedFile);
System.out.print("Rendering feed [" + outputFile + "]... ");
Map<String, Object> model = new HashMap<String, Object>();
model.put("posts", posts);
// model.put("posts", posts);
model.put("pubdate", new Date());

try {
Expand All @@ -162,7 +189,7 @@ public void renderArchive(List<Map<String, Object>> posts, String archiveFile) {
File outputFile = new File(destination.getPath() + File.separator + archiveFile);
System.out.print("Rendering archive [" + outputFile + "]... ");
Map<String, Object> model = new HashMap<String, Object>();
model.put("posts", posts);
// model.put("posts", posts);

try {
render(model, config.getString("template.archive.file"), outputFile);
Expand All @@ -185,7 +212,7 @@ public void renderTags(Map<String, List<Map<String, Object>>> tags, String tagPa
model.put("tag", tag);
// TODO: sort posts here
List<Map<String, Object>> posts = Filter.getPublishedPosts(tags.get(tag));
model.put("posts", posts);
model.put("tag_posts", posts);

tag = tag.trim().replace(" ", "-");
File outputFile = new File(destination.getPath() + File.separator + tagPath + File.separator + tag + config.getString("output.extension"));
Expand Down
23 changes: 19 additions & 4 deletions src/test/java/org/jbake/app/RendererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public void render() throws Exception {
if (line.contains("Lorem ipsum dolor sit amet")) {
foundBody = true;
}
if (foundTitle && foundDate && foundBody) {
break;
}
}

Assert.assertTrue(foundTitle);
Expand All @@ -90,9 +93,9 @@ public void render() throws Exception {

@Test
public void renderIndex() throws Exception {
Renderer renderer = new Renderer(sourceFolder, destinationFolder, templateFolder, config);
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.renderIndex(crawler.getPosts(), "index.html");
File outputFile = new File(destinationFolder, "index.html");
Assert.assertTrue(outputFile.exists());
Expand All @@ -108,6 +111,9 @@ public void renderIndex() throws Exception {
if (line.contains("<h4><a href=\"/blog/2013/second-post.html\">Second Post</a></h4>")) {
foundSecondTitle = true;
}
if (foundFirstTitle && foundSecondTitle) {
break;
}
}

Assert.assertTrue(foundFirstTitle);
Expand All @@ -116,9 +122,9 @@ public void renderIndex() throws Exception {

@Test
public void renderFeed() throws Exception {
Renderer renderer = new Renderer(sourceFolder, destinationFolder, templateFolder, config);
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.renderFeed(crawler.getPosts(), "feed.xml");
File outputFile = new File(destinationFolder, "feed.xml");
Assert.assertTrue(outputFile.exists());
Expand All @@ -138,6 +144,9 @@ public void renderFeed() throws Exception {
if (line.contains("<title>First Post</title>")) {
foundSecondTitle = true;
}
if (foundDescription && foundFirstTitle && foundSecondTitle) {
break;
}
}

Assert.assertTrue(foundDescription);
Expand All @@ -147,9 +156,9 @@ public void renderFeed() throws Exception {

@Test
public void renderArchive() throws Exception {
Renderer renderer = new Renderer(sourceFolder, destinationFolder, templateFolder, config);
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.renderArchive(crawler.getPosts(), "archive.html");
File outputFile = new File(destinationFolder, "archive.html");
Assert.assertTrue(outputFile.exists());
Expand All @@ -165,6 +174,9 @@ public void renderArchive() throws Exception {
if (line.contains("<h4>27 February - <a href=\"/blog/2012/first-post.html\">First Post</a></h4>")) {
foundSecondPost = true;
}
if (foundFirstPost && foundSecondPost) {
break;
}
}

Assert.assertTrue(foundFirstPost);
Expand All @@ -173,9 +185,9 @@ public void renderArchive() throws Exception {

@Test
public void renderTags() throws Exception {
Renderer renderer = new Renderer(sourceFolder, destinationFolder, templateFolder, config);
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.renderTags(crawler.getPostsByTags(), "tags");
File outputFile = new File(destinationFolder + File.separator + "tags" + File.separator + "blog.html");
Assert.assertTrue(outputFile.exists());
Expand All @@ -191,6 +203,9 @@ public void renderTags() throws Exception {
if (line.contains("<h4>27 February - <a href=\"/blog/2012/first-post.html\">First Post</a></h4>")) {
foundSecondPost = true;
}
if (foundFirstPost && foundSecondPost) {
break;
}
}

Assert.assertTrue(foundFirstPost);
Expand Down

0 comments on commit 3c4ba20

Please sign in to comment.