Skip to content

Commit

Permalink
Merge pull request jdan#50 from jdan/populate-helper
Browse files Browse the repository at this point in the history
Refactor all day every day
  • Loading branch information
jdan committed Sep 28, 2013
2 parents 157633e + 65fd5d4 commit 2e8e767
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
test_slides/
*.html
92 changes: 51 additions & 41 deletions lib/cleaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,71 +34,65 @@ function Cleaver(file) {


/**
* Renders the document
* Loads the document and required templates.
*
* @return {Promise}
*/
Cleaver.prototype._parseDocument = function () {
Cleaver.prototype._loadDocument = function () {
var self = this;

return Q.all([
helper.load(this.external),
helper.load(this.templates, 'templates')
])
.then(self._populateResources.bind(self));
}


/**
* Populates `slides` and some extra loaded content, based on the metadata
* listed in the document
* Parses the metadata and renders the slides.
*
* @param {Array.<Object>} data The data returned by fetching the assets
* listed in `external` and `templates`
* @return {Promise.<Array.<Object>>}
* @return {Promise}
*/
Cleaver.prototype._populateResources = function (data) {
Cleaver.prototype._renderSlides = function () {
var self = this;
var slices = this._slice(self.external.loaded.document);
this.metadata = yaml.safeLoad(slices[0]);

for (var i = 1; i < slices.length; i++) {
for (var i = 1; i < slices.length; i++)
this.slides.push(md(slices[i]));
}

// insert an author slide (if necessary) at the end
if (this.metadata.author) {
if (this.metadata.author)
this.slides.push(this._renderAuthorSlide(this.metadata.author));
}

// insert an agenda slide (if necessary) as our second slide
if (this.metadata.agenda) {
if (this.metadata.agenda)
this.slides.splice(1, 0, this._renderAgendaSlide(slices));
}

return Q.resolve(true);
}


/**
* Populates `slides` and some extra loaded content, based on the metadata
* listed in the document.
*
* @return {Promise}
*/
Cleaver.prototype._populateResources = function () {
var promises = [];

// maybe load an external stylesheet
if (this.metadata.style) {
promises.push(helper.loadSingle(this.metadata.style)
.then(function (data) {
self.external.loaded.style = data;
}));
}
if (this.metadata.style)
promises.push(helper.populateSingle(this.metadata.style, this.external, 'style'));

// maybe load an external template
if (this.metadata.template) {
promises.push(helper.loadSingle(this.metadata.template)
.then(function (data) {
self.templates.loaded.slides = data;
}));
}
if (this.metadata.template)
promises.push(helper.populateSingle(this.metadata.template, this.templates, 'slides'));

// maybe load an external layout
if (this.metadata.layout) {
promises.push(helper.loadSingle(this.metadata.layout)
.then(function (data) {
self.templates.loaded.layout = data;
}));
}
if (this.metadata.layout)
promises.push(helper.populateSingle(this.metadata.layout, this.templates, 'layout'));

return Q.all(promises);
}
Expand All @@ -107,17 +101,20 @@ Cleaver.prototype._populateResources = function (data) {
/**
* Loads the templates, default stylesheet, and any external stylesheets
* as defined by the user.
*
* @return {Promise.<Array.<Object>>}
*/
Cleaver.prototype._loadAssets = function () {
Cleaver.prototype._loadStaticAssets = function () {
return Q.all([
helper.load(this.resources, 'resources')
]);
}


/**
* Renders the slideshow
* Renders the slideshow.
*
* @return {Promise}
*/
Cleaver.prototype._renderSlideshow = function () {
var putControls = this.metadata.controls || (this.metadata.controls === undefined);
Expand Down Expand Up @@ -151,7 +148,8 @@ Cleaver.prototype._renderSlideshow = function () {


/**
* Renders the author slide
* Renders the author slide.
*
* @param {string} authorData The author field of the slideshow metadata
* @return {string} The formatted author slide
*/
Expand All @@ -160,7 +158,8 @@ Cleaver.prototype._renderAuthorSlide = function (authorData) {
}

/*
* Renders the agenda slide
* Renders the agenda slide.
*
* @param {string} slices The set of slices that had been loaded from the input file
* @return {string} The formatted agenda slide
*/
Expand All @@ -185,7 +184,8 @@ Cleaver.prototype._renderAgendaSlide = function (slices) {
}

/**
* Returns a chopped up document that's easy to parse
* Returns a chopped up document that's easy to parse.
*
* @param {string} The full document
* @return {Array.<string>} A list of all slides
*/
Expand All @@ -202,18 +202,28 @@ Cleaver.prototype._slice = function(document) {


/**
* Method to run the whole show
* Method to run the whole show.
*
* @return {Promise}
*/
Cleaver.prototype.run = function () {
var self = this;

Q.all([this._parseDocument(), this._loadAssets()])
// Load document -> Parse Metadata / Render Slides -> Populate Resources
var documentChain = this._loadDocument()
.then(self._renderSlides.bind(self))
.then(self._populateResources.bind(self));

// Load static assets
var assetChain = this._loadStaticAssets();

return Q.all([documentChain, assetChain])
.then(self._renderSlideshow.bind(self))
.fail(function (err) {
throw err;
})
.done();
}


/* exports */
module.exports = Cleaver;
23 changes: 22 additions & 1 deletion lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ function loadSingle(filename) {
}


/**
* Loads a single asset and sets the returned data to the `loaded` subobject
* of a given resource map.
*
* Usage: helper.populateSingle(this.metadata.template, this.templates, 'slides')
* helper.populateSingle(this.metadata.layout, this.templates, 'layout')
*
* @param {string} filename The name and location of the file to load
* @param {Object} destination The destination map to store the loaded data
* @param {string} key The key to use for storing the loaded data in destination
* @return {Promise}
*/
function populateSingle(filename, destination, key) {
return loadSingle(filename)
.then(function (data) {
destination.loaded[key] = data;
});
}


/**
* Loads files from a given map and places them in that map's `loaded` field
* @param {Object} map The map of labels to filenames
Expand Down Expand Up @@ -58,7 +78,8 @@ function save(filename, contents) {
return Q.nfcall(fs.writeFile, filename, contents);
}


/* exports */
exports.loadSingle = loadSingle;
exports.populateSingle = populateSingle;
exports.load = load;
exports.save = save;

0 comments on commit 2e8e767

Please sign in to comment.