Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Create a directory index when caching sections

  • Loading branch information...
commit 5ae787ff8279f55b54bf1ae9383574549979925d 1 parent 2b4051a
@laktek authored
View
20 lib/cache_store.js
@@ -2,18 +2,30 @@ var path = require("path");
var fs = require("fs");
var DeepFstream = require("./utils/deep_fstream");
+var module_utils = require("./utils/module_utils");
module.exports = {
outputDir: null,
+ templates: null,
+
+ contents: null,
+
setup: function(config){
var self = this;
self.outputDir = config.output_dir;
+
+ self.templates = module_utils.requireAndSetup(config.plugins.template_handler, config);
+ self.contents = module_utils.requireAndSetup(config.plugins.content_handler, config);
},
stat: function(request_basename, file_extension, request_options, callback) {
var self = this;
+ if (self.templates.isSection(request_basename) || self.contents.isSection(request_basename)) {
+ request_basename = path.join(request_basename, "index");
+ };
+
var file_path = path.join(self.outputDir, (request_basename + file_extension));
fs.stat(file_path, function(err, stat) {
@@ -31,6 +43,10 @@ module.exports = {
var cache_obj = {"body": null, "options": {"header": header}};
var encoding = (file_extension === ".html") ? "utf8" : "binary";
+ if (self.templates.isSection(request_basename) || self.contents.isSection(request_basename)) {
+ request_basename = path.join(request_basename, "index");
+ };
+
var file_path = path.join(self.outputDir, (request_basename + file_extension));
fs.stat(file_path, function(err, stat){
@@ -56,6 +72,10 @@ module.exports = {
var header = (rendered_obj.options && rendered_obj.options.header) || {};
var encoding = (file_extension === ".html") ? "utf8" : "binary";
+ if (self.templates.isSection(request_basename) || self.contents.isSection(request_basename)) {
+ request_basename = path.join(request_basename, "index");
+ };
+
var file_path = path.join(self.outputDir, (request_basename + file_extension));
var dir_path_portions = path.dirname(file_path).split(path.sep || "/");
View
5 lib/site_generator.js
@@ -107,8 +107,8 @@ module.exports = {
collectPathsForSection: function(section, callback) {
var self = this;
- self.contents.getContentPaths(section, function(err, content_paths){
- self.getStaticAndCompilableTemplates(section, function(err, template_paths){
+ self.contents.getContentPaths(section, function(err, content_paths) {
+ self.getStaticAndCompilableTemplates(section, function(err, template_paths) {
return callback(null, _.union(content_paths, template_paths));
});
});
@@ -182,7 +182,6 @@ module.exports = {
renderEachPath: function(paths, callback) {
var self = this;
- console.log(paths);
var move_to_next_path = function() {
if (paths.length) {
View
123 spec/cache_store.spec.js
@@ -3,10 +3,36 @@ var path = require("path");
var cache_store = require("../lib/cache_store.js");
+var module_utils = require("../lib/utils/module_utils.js");
+
describe("setup", function(){
+ it("setup the templates handler", function() {
+ spyOn(module_utils, "requireAndSetup").andCallFake(function(id, config) {
+ return { "id": id };
+ });
+
+ cache_store.setup({ "plugins": { "template_handler": "sample_template_handler" } });
+
+ expect(cache_store.templates.id).toEqual("sample_template_handler");
+ });
+
+ it("setup the contents handler", function() {
+ spyOn(module_utils, "requireAndSetup").andCallFake(function(id, config) {
+ return { "id": id };
+ });
+
+ cache_store.setup({ "plugins": { "content_handler": "sample_content_handler" } });
+
+ expect(cache_store.contents.id).toEqual("sample_content_handler");
+ });
+
it("set the output directory", function(){
- cache_store.setup({"output_dir": "output_dir"});
+ spyOn(module_utils, "requireAndSetup").andCallFake(function(id, config) {
+ return { "id": id };
+ });
+
+ cache_store.setup({ "output_dir": "output_dir", "plugins": {} });
expect(cache_store.outputDir).toEqual("output_dir");
});
@@ -15,6 +41,11 @@ describe("setup", function(){
describe("stat", function(){
it("call the callback with the file's modified time", function(){
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(false);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
cache_store.outputDir = "output_dir";
@@ -31,6 +62,11 @@ describe("stat", function(){
});
it("call the callback with an error if file doesn't exist", function(){
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(false);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
cache_store.outputDir = "output_dir";
@@ -49,9 +85,32 @@ describe("stat", function(){
describe("get", function(){
+ it("correct the given path if it's a section", function() {
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(true);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
+
+ cache_store.outputDir = "output_dir";
+
+ spyOn(fs, "stat");
+
+ var spyCallback = jasmine.createSpy();
+ cache_store.get("path/test", ".html", { "options": {} }, {}, spyCallback);
+
+ expect(fs.stat).toHaveBeenCalledWith("output_dir/path/test/index.html", jasmine.any(Function));
+ });
+
it("read the file with the correct encoding", function() {
var cached_content = new Buffer("cached content");
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(false);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
+
cache_store.outputDir = "output_dir";
spyOn(fs, "stat").andCallFake(function(file_path, callback){
@@ -70,6 +129,12 @@ describe("get", function(){
var cached_content = new Buffer("cached content");
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(false);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
+
spyOn(fs, "stat").andCallFake(function(file_path, callback){
return callback(null, { "mtime": new Date(2012, 6, 21), "size": 567 });
});
@@ -87,6 +152,12 @@ describe("get", function(){
it("call the callback with the error if there's an error reading the file", function(){
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(false);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
+
spyOn(fs, "stat").andCallFake(function(file_path, callback){
return callback(null, { "mtime": new Date(2012, 6, 21), "size": 567 });
});
@@ -107,6 +178,12 @@ describe("get", function(){
describe("update", function(){
it("create missing directories", function(){
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(false);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
+
spyOn(fs, "stat").andCallFake(function(dirpath, callback){
return callback(null, {"isDirectory": function(){ return false }});
});
@@ -127,6 +204,12 @@ describe("update", function(){
});
it("write file to the correct path", function(){
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(false);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
+
spyOn(fs, "stat").andCallFake(function(dirpath, callback){
return callback(null, {"isDirectory": function(){ return true }});
});
@@ -139,7 +222,34 @@ describe("update", function(){
expect(fs.writeFile).toHaveBeenCalledWith("output_dir/path/subdir/test.html", "test", "utf8", jasmine.any(Function));
});
+ it("correct the given path if it's a section", function() {
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(true);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
+
+ cache_store.outputDir = "output_dir";
+
+ spyOn(fs, "stat").andCallFake(function(dirpath, callback){
+ return callback(null, {"isDirectory": function(){ return true }});
+ });
+
+ spyOn(fs, "writeFile");
+
+ var spyCallback = jasmine.createSpy();
+ cache_store.update("path/subdir/test", ".html", { "body": "test", "options": { "header": {} } }, {}, spyCallback);
+
+ expect(fs.writeFile).toHaveBeenCalledWith("output_dir/path/subdir/test/index.html", "test", "utf8", jasmine.any(Function));
+ });
+
it("set the correct encoding", function(){
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(false);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
+
spyOn(fs, "stat").andCallFake(function(dirpath, callback){
return callback(null, {"isDirectory": function(){ return true }});
});
@@ -153,6 +263,12 @@ describe("update", function(){
});
it("call the callback with the error if there's an error in writing the file", function(){
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(false);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
+
spyOn(fs, "stat").andCallFake(function(dirpath, callback){
return callback(null, {"isDirectory": function(){ return true }});
});
@@ -168,6 +284,11 @@ describe("update", function(){
});
it("call the callback a valid cache object", function(){
+ var spyIsSection = jasmine.createSpy();
+ spyIsSection.andReturn(false);
+
+ cache_store.templates = { "isSection": spyIsSection };
+ cache_store.contents = { "isSection": spyIsSection };
spyOn(fs, "stat").andCallFake(function(dirpath, callback) {
return callback(null, { "isDirectory": function(){ return true } });
Please sign in to comment.
Something went wrong with that request. Please try again.