Commits

James Sumners committed 651a178

Initial version of njsBaker.js added.

Comments (0)

Files changed (1)

+/*global
+	__filename:false,
+	console:false,
+	process:false,
+	require:false
+*/
+
+(function() {
+
+var argv = process.argv,
+	baker, // The parsed Baker.json
+	bakerJSON = argv[2] || 'Baker.json',
+	cwd = process.cwd(),
+	fs = require('fs'),
+	stats = {};
+
+try {
+	stats = fs.statSync(bakerJSON);
+} catch(e) {
+	stats.isFile = function() { return false; };
+}
+
+if (!stats.isFile()) {
+	console.log('A descriptor file is required.');
+	console.log('Either create a Baker.json file or specify one.');
+	console.log('Example:');
+	console.log('\tnode ' + __filename + ' myBaker.json');
+	process.exit(1);
+}
+
+// Read the Baker file and parse it
+baker = JSON.parse(fs.readFileSync(bakerJSON));
+
+// Load the snippets
+var snippets = {};
+for (var snippet in baker.snippets) {
+	if (baker.snippets.hasOwnProperty(snippet)) {
+		snippets[snippet] = fs.readFileSync(
+			baker.config.templateDir + '/' + baker.snippets[snippet]) + '';
+	}
+}
+
+// Now we will parse through the "pages" and compile them
+var buffer = '',
+	re,
+	tokens = baker.tokens || [],
+	tokenDelimA = baker.config.tokenDelimA,
+	tokenDelimB = baker.config.tokenDelimB;
+for (var page in baker.pages) {
+	if (!baker.pages.hasOwnProperty(page)) {
+		continue;
+	}
+
+	page = baker.pages[page];
+
+	// Read in the template file
+	buffer = fs.readFileSync(baker.config.templateDir + '/' + page.template);
+	buffer = buffer.toString();
+
+	// Look through the template for tokens and replace them
+	for (var i = 0, j = tokens.length; i < j; i += 1) {
+		re = new RegExp(tokenDelimA + tokens[i] + tokenDelimB);
+		buffer = buffer.replace(re, snippets[tokens[i]], 'g');
+	}
+
+	// Replace the content token
+	re = new RegExp(tokenDelimA + "content" + tokenDelimB);
+	buffer = buffer.replace(re, snippets[page.content]);
+
+	fs.writeFileSync(baker.config.buildDir + '/' + page.file, buffer);
+}
+
+}());