forked from jdan/cleaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a promise-based Cleaver service
- Loading branch information
Showing
3 changed files
with
90 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"dependencies" : { | ||
"optimist": "0.3.x", | ||
"mustache": "0.7.x", | ||
"q": "0.9.6", | ||
"node-markdown": "0.1.x" | ||
} | ||
} |