Comparing version 0.0.5 to 0.1.0
130
lib/bake.js
@@ -5,6 +5,6 @@ var fs = require("fs"), | ||
dive = require("dive"), | ||
marked = require("marked"); | ||
ejs = require("ejs"); | ||
// Main function | ||
var bake = function(dir, hooks) { | ||
var bake = function(confFile, hooks) { | ||
@@ -19,79 +19,103 @@ // File counter | ||
// Load the configuration file | ||
var conf = cjson.load(dir + "/conf/bake.json"); | ||
var conf = cjson.load(confFile); | ||
console.log("Successfully loaded configuration file."); | ||
// Set values for `bakeDir` and `tplDir` | ||
var bakeDir = conf.directories.bake || "pub", | ||
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("\nBeginning to bake " + dir + "\n"); | ||
console.log("Beginning to bake " + bakeDir + ".\n"); | ||
// Dive into the public directory | ||
dive(dir + "/pub", function(err, file) { | ||
dive(bakeDir, function(err, master) { | ||
// Throw errors | ||
if (err) throw err; | ||
// Match the file's name against markdown file extensions | ||
if (file.match(/\.(mkd|md|markdown)$/i)) { | ||
// 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 file's contents | ||
fs.readFile(file, "utf8", function(err, data) { | ||
// Read the master-file's contents | ||
fs.readFile(master, "utf8", function(err, data) { | ||
// Throw errors | ||
if (err) throw err; | ||
// Get the properties of the file | ||
var p = props(data); | ||
// Get the properties | ||
// `file` is the file specific property object | ||
var file = props(data); | ||
// `global` is the global property object | ||
var global = conf.properties; | ||
// Parse markdown | ||
var content = marked(p.__content); | ||
// Assert that `file.template` is set | ||
if (file.template == undefined) | ||
file.template = global.defaultTemplate || "default"; | ||
// Assert p.template is set | ||
if (p.template == undefined) | ||
p.template = conf.defaultTemplate; | ||
// Assert p.author is set | ||
if (p.author == undefined) | ||
p.author = conf.defaultAuthor; | ||
// Read the template file | ||
fs.readFile(dir + "/tpl/" + p.template + ".tpl", "utf8", | ||
function(err, tpl) { | ||
fs.readFile(tplDir + "/" + file.template + ".tpl", "utf8", | ||
function(err, result) { | ||
// Throw errors | ||
if (err) throw err; | ||
// Insert the content | ||
tpl = tpl.replace("{__content}", content); | ||
// For all keys of `global` | ||
for (var key in global) | ||
// If a hook is defined, apply it | ||
if (hooks[key] != undefined) | ||
global[key] = hooks[key](global); | ||
// For all keys of conf.properties in tpl | ||
if (conf.properties != undefined) | ||
for (var key in conf.properties) | ||
// If a hook is defined, apply it | ||
if (hooks[key] != undefined) | ||
tpl = hooks[key](tpl, p, conf.properties); | ||
// Otherwise simply replace the key | ||
else | ||
tpl = tpl.replace(new RegExp("{" + key + "}", "g"), | ||
conf.properties[key]); | ||
// For all keys of `file` | ||
for (var key in file) | ||
// If a hook is defined, apply it | ||
if (hooks[key] != undefined) | ||
file[key] = hooks[key](file); | ||
// For all keys of p in tpl | ||
for (var key in p) | ||
// exept for p.__content | ||
if (key != "__content") | ||
// If a hook is defined, apply it | ||
if (hooks[key] != undefined) | ||
tpl = hooks[key](tpl, p, conf.properties); | ||
// Otherwise simply replace the key | ||
else | ||
tpl = tpl.replace(new RegExp("{" + key + "}", "g"), | ||
p[key]); | ||
// (Pre-)Insert the content (so there may be ejs-tags in | ||
// `file.__content` are parsed, too. | ||
result = result.replace(/<%= +file.__content +%>/g, | ||
file.__content); | ||
// New filename | ||
htmlFile = file.replace(/\.(mkd|md|markdown)$/i, "") + ".html"; | ||
// Result's filename | ||
var resultFilename = master.replace(fileExtPattern, | ||
"." + fileExt[masterExt]); | ||
// Export `file` and `global` to locals object for use in the | ||
// template. | ||
var locals = { | ||
file: file, | ||
global: global | ||
}; | ||
// Render ejs-template | ||
result = ejs.render(result, { locals: locals }); | ||
// Write contents | ||
fs.writeFile(htmlFile, tpl, function(err) { | ||
fs.writeFile(resultFilename, result, function(err) { | ||
// Throw errors | ||
if (err) throw err; | ||
// Log on success | ||
console.log(htmlFile + " written"); | ||
if (!--todo) | ||
console.log("Everything has been successfully baked!\n"); | ||
// Log status on success | ||
console.log(" " + resultFilename + " written.\n"); | ||
// When file counter is zero | ||
if (!--todo) { | ||
if (hooks["__complete"] != undefined) | ||
// Call the completion hook | ||
hooks["__complete"](); | ||
// State final message | ||
console.log("Everything has been successfully baked!"); | ||
} | ||
}); | ||
@@ -98,0 +122,0 @@ }); |
{ | ||
"name": "bake", | ||
"description": "javascript website bakery for markdown files", | ||
"tags": ["website", "blog", "markdown", "template", "git"], | ||
"version": "0.0.5", | ||
"description": "bakery for static files that supports embedded JS templates", | ||
"tags": [ "ejs", "template" ], | ||
"version": "0.1.0", | ||
"author": "Paul Vorbach <paul@vorb.de> (http://vorb.de)", | ||
@@ -22,4 +22,4 @@ "repository": { | ||
"props": ">=0.0.3", | ||
"marked": ">=0.0.1" | ||
"ejs": ">=0.4.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7965
5
99
131
+ Addedejs@>=0.4.3
+ Addedansi-styles@4.3.0(transitive)
+ Addedasync@3.2.6(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.112.0.1(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedejs@3.1.10(transitive)
+ Addedfilelist@1.0.4(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedjake@10.9.2(transitive)
+ Addedminimatch@3.1.25.1.6(transitive)
+ Addedsupports-color@7.2.0(transitive)
- Removedmarked@>=0.0.1
- Removedmarked@14.1.3(transitive)