Skip to content

Commit

Permalink
Created a promise-based Cleaver service
Browse files Browse the repository at this point in the history
  • Loading branch information
jdan committed Aug 7, 2013
1 parent 8b05ac6 commit 52fef9f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 44 deletions.
10 changes: 4 additions & 6 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
var fs = require('fs'),
path = require('path'),
render = require('./render').render;

CLEAVER_ROOT = path.normalize(__dirname + '/../');
Cleaver = require('./render');

exports.init = function(argv) {
if (!argv.file) {
Expand All @@ -14,7 +11,8 @@ exports.init = function(argv) {
fs.readFile(argv.file, 'utf8', function(err, data) {
if (err) throw err;
var contents = JSON.parse(data.replace(/\n/g,''));
render(contents, argv);
});
var cleaver = new Cleaver(contents, argv);

cleaver.run();
});
};
123 changes: 85 additions & 38 deletions lib/render.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,104 @@
var fs = require('fs'),
mustache = require('mustache'),
Q = require('q'),
path = require('path'),
md = require('node-markdown').Markdown;

exports.render = function(json, options) {
var renders = [];
var styles = [];

// push the author slide
if (json.author) {
json.slides.push({
"type": "author",
"name": json.author.name,
"twitter": json.author.twitter,
"url": json.author.url
});
}
function Cleaver(data, options) {
this.data = data;
this.options = options;

this.styleSheets = [];
this.renderedSlides = [];
}

// load default style
fs.readFile(CLEAVER_ROOT + 'styles/default.css', 'utf8', function(err, data) {
if (err) throw err;
styles.push(data);
Cleaver.ROOT_DIR = path.normalize(__dirname + '/../');

Cleaver.prototype.maybeLoadStyle = function (filename) {
var deferred = Q.defer();
var self = this;

fs.readFile(filename, 'utf8', function (err, data) {
if (err) deferred.reject(err);
self.styleSheets.push(data);
deferred.resolve('ok');
});

// load user-defined styles
if (options.style) {
fs.readFile(options.style, 'utf8', function(err, data) {
if (err) throw err;
styles.push(data);
});
}
return deferred.promise
}

Cleaver.prototype.maybeRenderSlides = function () {
var deferred = Q.defer();
var promises = [];
var self = this;

json.slides.forEach(function(v, i) {
fs.readFile(CLEAVER_ROOT + 'templates/_' + v.type + '.mustache', 'utf8', function(err, data) {
if (err) throw err;
if (v.type == 'text')
v.content = md(v.content.replace(/>>/g,'</p><p>'));
for (var i = 0, slide; slide = self.data.slides[i]; i++) {
promises.push(this.maybeRenderSlide(slide));
}

renders[i] = mustache.render(data, v);
return Q.all(promises)
.then(function (renderedSlides) {
self.renderedSlides = renderedSlides
});
}

Cleaver.prototype.maybeRenderSlide = function (slideData) {
var deferred = Q.defer();

fs.readFile(Cleaver.ROOT_DIR + 'templates/_' + slideData.type + '.mustache', 'utf8', function (err, template) {
if (err) deferred.reject(err);
if (slideData.type == 'text') {
// replace >> with </p><p>
// jank.
slideData.content = md(slideData.content.replace(/>>/g, '</p><p>'));
}

deferred.resolve(mustache.render(template, slideData));
});

fs.readFile(CLEAVER_ROOT + 'templates/layout.mustache', 'utf8', function(err, data) {
if (err) throw err;
return deferred.promise;
}

Cleaver.prototype.maybeRenderLayout = function () {
var deferred = Q.defer();
var self = this;

fs.readFile(Cleaver.ROOT_DIR + 'templates/layout.mustache', 'utf8', function(err, template) {
if (err) deferred.reject(err);

var output = mustache.render(template, {
slides: self.renderedSlides,
title: self.data.name,
styles: self.styleSheets,
controls: !(self.options.nocontrols)
});

var output = mustache.render(data, { slides: renders, title: json.name, styles: styles, controls: !(options.nocontrols) });
if (options.debug) {
if (self.options.debug) {
console.log(output);
deferred.resolve('debug');
} else {
if (options.output) {
fs.writeFile(options.output, output);
if (self.options.output) {
fs.writeFile(self.options.output, output, deferred.makeNodeResolver());
} else {
fs.writeFile('output.html', output);
fs.writeFile('output.html', output, deferred.makeNodeResolver());
}
}
});
};

return deferred.promise;
}

Cleaver.prototype.run = function () {
var self = this;

return self.maybeLoadStyle(Cleaver.ROOT_DIR + 'styles/default.css')
.then(function () {
if (self.options.style) return self.maybeLoadStyle(self.options.style);
else return Q.resolve('ok');
})
.then(self.maybeRenderSlides.bind(self))
.then(self.maybeRenderLayout.bind(self))
.done()
}

module.exports = Cleaver
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies" : {
"optimist": "0.3.x",
"mustache": "0.7.x",
"q": "0.9.6",
"node-markdown": "0.1.x"
}
}

0 comments on commit 52fef9f

Please sign in to comment.