Skip to content

Commit

Permalink
expose API to accept an input string and resolve the produced slidesh…
Browse files Browse the repository at this point in the history
…ow instead of reading/writing file contents
  • Loading branch information
jdan committed Mar 8, 2014
1 parent 35047de commit 0b8ff3d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 51 deletions.
35 changes: 25 additions & 10 deletions bin/cleaver
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ var fs = require('fs');
var Cleaver = require(path.join(__dirname,'../lib/cleaver.js'));
var program = require('commander');

/**
* Helper function to use the cleaver API to create and save a new
* presentation
*/
function createAndSave(file) {
fs.readFile(file, 'utf-8', function (err, contents) {
var presentation = new Cleaver(contents, path.resolve(path.dirname(file)));
var promise = presentation.run();

promise
.then(function (product) {
var outputFile = presentation.metadata.output;
fs.writeFile(outputFile, product);
})
.fail(function (err) {
process.stderr.write('!! ' + err.message + '\n');
process.exit(1);
});
});
}

program
.version(require('../package.json').version)
.usage('[command] [file]');
Expand All @@ -13,13 +34,13 @@ program
.command('watch')
.description('Watch for changes on markdown file')
.action(function(){
file = program.args[0];
new Cleaver(file).run();
var file = program.args[0];
createAndSave(file);

console.log('Watching for changes on ' + file + '. Ctrl-C to abort.');
fs.watchFile(file, { persistent: true, interval: 100 }, function () {
console.log('Rebuilding: ' + new Date());
new Cleaver(file).run();
createAndSave(file);
});
});

Expand All @@ -28,12 +49,6 @@ program.parse(process.argv);
if (!program.args.length) {
program.help();
} else {
var file = program.args[0];
var cleaver = new Cleaver(file);
cleaver.run()
.fail(function (err) {
console.log('!!', err.message);
process.exit(1);
});
createAndSave(program.args[0]);
}

30 changes: 16 additions & 14 deletions lib/cleaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ var yaml = require('js-yaml');
var mustache = require('mustache');
var helper;

function Cleaver(file) {
if (!file) {
throw '!! Please specify a file to parse';
}

this.file = path.resolve(file);

helper = require('./helper')(this.file);
/**
* Constructor accepts a document (as a string) and a path from which to
* include external dependencies (stylesheets, scripts, etc)
*
* @param {string} document The input document
* @param {!string} includePath Optional resource path (default: '.')
* @constructor
*/
function Cleaver(document, includePath) {
this.document = document.toString();
this.path = path.resolve(includePath || '.');
helper = require('./helper')(this.path);

this.templates = {
layout: 'templates/layout.mustache',
Expand All @@ -28,7 +33,6 @@ function Cleaver(file) {
};

this.external = {
document: this.file,
style: []
};

Expand All @@ -52,7 +56,7 @@ function Cleaver(file) {
*/
Cleaver.prototype.loadDocument = function () {
return Q.all([
helper.load(this.external, true),
helper.load(this.external, { external: true }),
helper.load(this.templates)
]);
};
Expand All @@ -64,8 +68,7 @@ Cleaver.prototype.loadDocument = function () {
* @return {Promise}
*/
Cleaver.prototype.renderSlides = function () {
var self = this;
var slices = this.slice(self.external.loaded.document);
var slices = this.slice(this.document);
var i;

this.metadata = yaml.safeLoad(slices[0].content) || {};
Expand Down Expand Up @@ -202,9 +205,8 @@ Cleaver.prototype.renderSlideshow = function () {
metadata: this.metadata
};

// Render the main layout
var outputLocation = this.metadata.output || path.basename(this.file, '.md') + '-cleaver.html';
return helper.save(outputLocation, mustache.render(this.templates.loaded.layout, layoutData));
/* Return the rendered slideshow */
return mustache.render(this.templates.loaded.layout, layoutData);
};


Expand Down
34 changes: 7 additions & 27 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,23 @@ function populateSingle(filename, destination, key, failsafe) {
/**
* Loads files from a given map and places them in that map's `loaded` field
* @param {Object} map The map of labels to filenames
* @param {!Boolean} external Whether or not the file is external to the project
* @param {!Object} options Load options
* @return {Promise.<Array.<Object>>} The same map with a `loaded` field populated with file contents
*/
function load(map, external) {
function load(map, options) {
var promises = [];
var loaded = {};
var filename;

options = options || {};

for (var key in map) {
if (!map[key] || map[key].length === 0) {
continue;
}

filename = map[key];
if (!external) {
if (!options.external) {
filename = path.resolve(ROOT_DIR, filename);
}

Expand Down Expand Up @@ -164,27 +166,6 @@ function loadSettings(source, ctx) {
}


/**
* Saves contents to a specified location
* @param {string} filename The destination
* @param {string} contents The contents of the file
* @return {Promise}
*/
function save(filename, contents) {
var deferred = Q.defer();

fs.writeFile(filename, contents, function (err, contents) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(path.resolve(filename));
}
});

return deferred.promise;
}


/**
* Promise to load a files contents
* @param {string} filename The file to load
Expand Down Expand Up @@ -280,14 +261,13 @@ module.exports = function (inputPath) {
return pathname;
}

return path.resolve(path.dirname(inputPath), pathname);
return path.resolve(inputPath, pathname);
};

return {
loadSingle: loadSingle,
populateSingle: populateSingle,
load: load,
loadTheme: loadTheme,
save: save
loadTheme: loadTheme
};
};

0 comments on commit 0b8ff3d

Please sign in to comment.