Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use path.resolve

  • Loading branch information...
commit 415faead3e8cc8ff580398e98176011e1ba639bd 1 parent dcd24c1
@pvorb authored
View
271 bake.js
@@ -1,141 +1,150 @@
-var fs = require("fs"),
- props = require("props"),
- dive = require("dive"),
- ejs = require("ejs");
+var fs = require('fs');
+var path = require('path');
+var props = require('props');
+var dive = require('dive');
+var ejs = require('ejs');
// Main function
var bake = function(conf, hooks) {
- // File counter
- var todo = 0;
-
- // Ensure `conf` is an object
- if (typeof conf === "string")
- conf = JSON.parse(conf);
- if (typeof conf !== "object")
- throw new Error("parameter conf must be a valid configuration object");
+ // File counter
+ var todo = 0;
+
+ // Ensure `conf` is an object
+ if (typeof conf == 'string')
+ conf = JSON.parse(conf);
+ if (typeof conf != 'object')
+ throw new Error('parameter conf must be a valid configuration object');
- // Ensure `hooks` is an object
- if (typeof hooks !== "object")
- hooks = { };
+ // Ensure `hooks` is an object
+ if (typeof hooks != 'object')
+ hooks = { };
- // Set values for `inputDir`, `outputDir` and `tplDir`
- var inputDir = conf.directories.input || "pub",
- outputDir = conf.directories.output || "pub",
- tplDir = conf.directories.templates || "tpl";
+ // Set values for `inputDir`, `outputDir` and `tplDir`
+ var inputDir = conf.directories.input || 'pub';
+ var outputDir = conf.directories.output || 'pub';
+ var tplDir = conf.directories.templates || 'tpl';
- // Set values for `fileExt`
- var fileExt = conf.fileExtensions || { txt: "html" };
- var fileExtPattern
- = new RegExp("\.(" + Object.keys(fileExt).join("|") + ")$", "i");
-
- // Status log
- console.log("Beginning to bake " + inputDir + ".\n");
-
- // Dive into the public directory
- dive(inputDir, function(err, master) {
- // Throw errors
- if (err) throw err;
-
- // Matching variable
- var match;
-
- // Match the master-file's name against enabled file extensions
- if (match = master.match(fileExtPattern)) {
-
- // Get the file extension of the master file
- var masterExt = match[1];
-
- // Increase file counter
- ++todo;
-
- // Read the master-file's contents
- fs.readFile(master, "utf8", function(err, data) {
- // Throw errors
- if (err) throw err;
-
- // Get the properties
- // `prop` is the file specific property object
- var prop = props(data);
-
- // Define `hasOwnProperty` shorthand
- prop.has = prop.hasOwnProperty;
-
- // Amend `prop` by properties in `conf.properties` if defined
- if (conf.properties != undefined)
- for (var key in conf.properties) {
- if (prop[key] == undefined)
- prop[key] = conf.properties[key];
- }
-
- // Assert that `prop.template` is set
- if (prop.template == undefined)
- prop.template = "default.tpl";
-
- // `__propBefore` hook
- if (hooks.__propBefore != undefined)
- prop = hooks.__propBefore(master, prop);
-
- // Various property hooks
- for (var key in prop)
- if (hooks[key] != undefined)
- prop[key] = hooks[key](master, prop);
-
- // `__propAfter` hook
- if (hooks.__propAfter != undefined)
- prop = hooks.__propAfter(master, prop);
-
- // Read the template file
- fs.readFile(tplDir + "/" + prop.template, "utf8",
- function(err, result) {
- // Throw errors
- if (err) throw err;
-
- // (Pre-)Insert the content (so ejs-tags in
- // `prop.__content` are parsed, too.
- result = result.replace(/<%=\s+__content\s+%>/g,
- prop.__content);
-
- // Result's filename
- var resName = master.replace(fileExtPattern,
- "." + fileExt[masterExt]);
-
- // New file's path
- if (prop._id == undefined)
- prop._id = resName.replace(inputDir, "");
-
- // Add output dir
- resName = outputDir + prop._id;
-
- // Render ejs-template
- result = ejs.render(result, { locals: prop });
-
- // Write contents
- fs.writeFile(resName, result, function(err) {
- // Throw errors
- if (err) throw err;
-
- // `__written` hook
- if (hooks.__writeAfter != undefined)
- hooks.__writeAfter(master, prop);
-
- // Log status on success
- console.log(" " + resName + " written.\n");
-
- // When file counter is zero
- if (!--todo) {
- // `__complete` hook
- if (hooks.__complete != undefined)
- hooks.__complete(master, prop);
-
- // State final message
- console.log("Everything has been successfully baked!");
- }
- });
- });
- });
- }
- });
+ inputDir = path.resolve(process.cwd(), inputDir);
+ outputDir = path.resolve(process.cwd(), outputDir);
+ tplDir = path.resolve(process.cwd(), tplDir);
+
+ // Set values for `fileExt`
+ var fileExt = conf.fileExtensions || { txt: 'html' };
+ var fileExtPattern
+ = new RegExp('\.(' + Object.keys(fileExt).join('|') + ')$', 'i');
+
+ // Status log
+ console.log('Beginning to bake ' + inputDir + '.\n');
+
+ // Dive into the public directory
+ dive(inputDir, function(err, master) {
+ // Throw errors
+ if (err) throw err;
+
+ // Matching variable
+ var match;
+
+ // Match the master-file's name against enabled file extensions
+ if (match = master.match(fileExtPattern)) {
+
+ // Get the file extension of the master file
+ var masterExt = match[1];
+
+ // Increase file counter
+ ++todo;
+
+ // Read the master-file's contents
+ fs.readFile(master, 'utf8', function(err, data) {
+ // Throw errors
+ if (err) throw err;
+
+ // Get the properties
+ // `prop` is the file specific property object
+ var prop = props(data);
+
+ // Define `hasOwnProperty` shorthand
+ prop.has = prop.hasOwnProperty;
+
+ // Amend `prop` by properties in `conf.properties` if defined
+ if (conf.properties != undefined)
+ for (var key in conf.properties) {
+ if (prop[key] == undefined)
+ prop[key] = conf.properties[key];
+ }
+
+ // Assert that `prop.template` is set
+ if (prop.template == undefined)
+ prop.template = 'default.tpl';
+
+ // `__propBefore` hook
+ if (hooks.__propBefore != undefined)
+ prop = hooks.__propBefore(master, prop);
+
+ // Various property hooks
+ for (var key in prop)
+ if (hooks[key] != undefined)
+ prop[key] = hooks[key](master, prop);
+
+ // `__propAfter` hook
+ if (hooks.__propAfter != undefined)
+ prop = hooks.__propAfter(master, prop);
+
+ // Read the template file
+ fs.readFile(path.resolve(tplDir, prop.template), 'utf8',
+ function(err, result) {
+ // Throw errors
+ if (err) throw err;
+
+ // (Pre-)Insert the content (so ejs-tags in
+ // `prop.__content` are parsed, too.
+ result = result.replace(/<%=\s+__content\s+%>/g,
+ prop.__content);
+
+ // Result's filename
+ var resName = master.replace(fileExtPattern,
+ '.' + fileExt[masterExt]);
+
+ // New file's path
+ if (prop._id == undefined)
+ prop._id = resName.replace(inputDir, '');
+
+ // Remove first slash
+ if (/^\//, prop._id)
+ prop._id = prop._id.substring(1);
+
+ // Add output dir
+ resName = path.resolve(outputDir, prop._id);
+
+ // Render ejs-template
+ result = ejs.render(result, { locals: prop });
+
+ // Write contents
+ fs.writeFile(resName, result, function(err) {
+ // Throw errors
+ if (err) throw err;
+
+ // `__written` hook
+ if (hooks.__writeAfter != undefined)
+ hooks.__writeAfter(master, prop);
+
+ // Log status on success
+ console.log(' ' + resName + ' written.\n');
+
+ // When file counter is zero
+ if (!--todo) {
+ // `__complete` hook
+ if (hooks.__complete != undefined)
+ hooks.__complete(master, prop);
+
+ // State final message
+ console.log('Everything has been successfully baked!');
+ }
+ });
+ });
+ });
+ }
+ });
};
module.exports = bake;
View
16 example/bake.js
@@ -1,15 +1,17 @@
-var fs = require("fs"),
- marked = require("marked"),
- bake = require("../");
+var fs = require('fs');
+var path = require('path');
+var marked = require('marked');
+var bake = require('../bake.js');
var a = {};
// Read the configuration file
-var conf = fs.readFileSync(process.cwd() + "/conf/bake.json", "utf8");
+var conf = fs.readFileSync(path.resolve(process.cwd(), 'conf/bake.json'),
+ 'utf8');
// Bake some markdown files
bake(conf, {
- __content: function(filename, properties) {
- return marked(properties.__content);
- }
+ __content: function(filename, properties) {
+ return marked(properties.__content);
+ }
});
View
32 example/conf/bake.json
@@ -1,18 +1,18 @@
{
- "directories": {
- "input": "pub",
- "output": "pub",
- "templates": "tpl"
- },
- "fileExtensions": {
- "mkd": "html",
- "md": "html",
- "markdown": "html",
- "txt": "html"
- },
- "properties": {
- "template": "default.tpl",
- "author": "Paul",
- "siteTitle": "My Site"
- }
+ "directories": {
+ "input": "pub",
+ "output": "pub",
+ "templates": "templates"
+ },
+ "fileExtensions": {
+ "mkd": "html",
+ "md": "html",
+ "markdown": "html",
+ "txt": "html"
+ },
+ "properties": {
+ "template": "default.tpl",
+ "author": "Paul",
+ "siteTitle": "My Site"
+ }
}
View
7 example/pub/test.html
@@ -8,7 +8,12 @@
<h1>First post</h1>
<p>Written on August 14th, 2011 by Paul</p>
-<p>This is my <strong>first</strong> blog post.</p> <h2>Here&apos;s a heading</h2> <blockquote><p> And this is a block quote</p></blockquote>
+
+<p>This is my <strong>first</strong> blog post.
+
+</p>
+<h2>Here&apos;s a heading</h2>
+<blockquote><p>And this is a block quote</p></blockquote>
</body>
</html>
View
0  example/tpl/default.tpl → example/templates/default.tpl
File renamed without changes
View
0  example/tpl/post.tpl → example/templates/post.tpl
File renamed without changes
View
43 package.json
@@ -1,23 +1,24 @@
{
- "name": "bake",
- "description": "bakery for static files (like jekyll) that supports embedded JS templates",
- "tags": [ "ejs", "template" ],
- "version": "0.5.3",
- "author": "Paul Vorbach <paul@vorb.de> (http://vorb.de)",
- "repository": {
- "type": "git",
- "url": "git://github.com/pvorb/node-bake.git"
- },
- "bugs": {
- "web": "http://github.com/pvorb/node-bake/issues"
- },
- "engines": {
- "node": ">= 0.4.0"
- },
- "main": "bake.js",
- "dependencies": {
- "dive": ">=0.0.3",
- "props": ">=0.1.0",
- "ejs": ">=0.4.3"
- }
+ "name": "bake",
+ "description": "bakery for static files (like jekyll) that supports embedded JS templates",
+ "tags": [ "ejs", "template" ],
+ "version": "0.5.3",
+ "author": "Paul Vorbach <paul@vorb.de> (http://vorb.de)",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/pvorb/node-bake.git"
+ },
+ "bugs": {
+ "url": "http://github.com/pvorb/node-bake/issues"
+ },
+ "engines": {
+ "node": ">= 0.4.0"
+ },
+ "main": "./bake.js",
+ "dependencies": {
+ "dive": ">=0.0.3",
+ "marked": ">=0.1.7",
+ "props": ">=0.1.0",
+ "ejs": ">=0.4.3"
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.