Permalink
Browse files

Use the UTF8 encoding for textual files. Fixes #62

  • Loading branch information...
1 parent b170ca9 commit a14174b21efed29718fd560e16fba69b9ed3909c @laktek committed Apr 6, 2013
Showing with 30 additions and 2 deletions.
  1. +10 −1 lib/cache_store.js
  2. +20 −1 spec/cache_store.spec.js
View
@@ -84,7 +84,16 @@ module.exports = {
var body = rendered_obj.body;
var header = (rendered_obj.options && rendered_obj.options.header) || {};
- var encoding = (file_extension === ".html") ? "utf8" : "binary";
+
+ var encoding;
+ // set the correct encoding by the file extension
+ if (file_extension === ".html" || file_extension === ".rss" || file_extension === ".xml" ||
+ file_extension === ".json" || file_extension === ".js" || file_extension === ".css" ||
+ file_extension === ".txt") {
+ encoding = "utf8";
+ } else {
+ encoding = "binary";
+ }
if (self.templates.isSection(request_basename) || self.contents.isSection(request_basename)) {
request_basename = path.join(request_basename, "index");
View
@@ -264,7 +264,7 @@ describe("update", function(){
expect(fs.writeFile).toHaveBeenCalledWith(path.join("output_dir/path/subdir/test/index.html"), "test", "utf8", jasmine.any(Function));
});
- it("set the correct encoding", function(){
+ it("use binary encoding for image files", function(){
var spyIsSection = jasmine.createSpy();
spyIsSection.andReturn(false);
@@ -283,6 +283,25 @@ describe("update", function(){
expect(fs.writeFile).toHaveBeenCalledWith(path.join("output_dir/path/subdir/test.jpg"), "test", "binary", jasmine.any(Function));
});
+ it("use utf8 encoding for text files", 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 }});
+ });
+
+ spyOn(fs, "writeFile");
+
+ var spyCallback = jasmine.createSpy();
+ cache_store.update("path/subdir/test", ".js", { "body": "test", "options": { "header": {} } }, {}, spyCallback);
+
+ expect(fs.writeFile).toHaveBeenCalledWith(path.join("output_dir/path/subdir/test.js"), "test", "utf8", jasmine.any(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);

1 comment on commit a14174b

naholyr commented on a14174b Apr 6, 2013

What about .yml?
There will always be unplanned cases, this should be at least a configurable list of extensions, and at best definable per path.

Please sign in to comment.