Skip to content

Commit

Permalink
added support for multiple input documents
Browse files Browse the repository at this point in the history
  • Loading branch information
jdan committed Aug 27, 2014
1 parent e848dfb commit 8e075bc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
39 changes: 20 additions & 19 deletions bin/cleaver
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ var Cleaver;
* Helper function to use the cleaver API to create and save a new
* presentation
*/
function createAndSave(file, options) {
fs.readFile(file, 'utf-8', function (err, contents) {
var presentation = new Cleaver(contents, options, path.resolve(path.dirname(file)));
var promise = presentation.run();
function createAndSave(files, options) {

This comment has been minimized.

Copy link
@sudodoki

sudodoki Aug 27, 2014

Collaborator

options in this context refer to command line options? Or are all presentations going to share same options that came in first file in markdown?

This comment has been minimized.

Copy link
@jdan

jdan Aug 27, 2014

Author Owner

Options are the command-line options :) I'll add a docstring.

files.forEach(function (file) {
fs.readFile(file, 'utf-8', function (err, contents) {
var presentation = new Cleaver(contents, options, path.resolve(path.dirname(file)));

promise
.then(function (product) {
var outputFile = presentation.options.output || path.basename(file, '.md') + '-cleaver.html';
fs.writeFile(outputFile, product);
})
.fail(function (err) {
process.stderr.write('!! ' + err.message + '\n');
if (program.debug) {
process.stderr.write(err.stack);
}
process.exit(1);
});
presentation.run()
.then(function (product) {
var outputFile = presentation.options.output || path.basename(file, '.md') + '-cleaver.html';
fs.writeFile(outputFile, product);
})
.fail(function (err) {
process.stderr.write('!! ' + err.message + '\n');
if (program.debug) {
process.stderr.write(err.stack);
}
process.exit(1);
});
});
});
}

Expand All @@ -38,12 +39,12 @@ program
.description('Watch for changes on markdown file')
.action(function () {
var file = program.args[0];
createAndSave(file, parsedOpts);
createAndSave([file], parsedOpts);

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

Expand Down Expand Up @@ -103,5 +104,5 @@ if (!program.args.length) {
// Load cleaver with the new ENV for debugging
// TODO: This seems a little janky, maybe handle this in lib/
Cleaver = require('../');
createAndSave(program.args[0], parsedOpts);
createAndSave(program.args, parsedOpts);
}
15 changes: 15 additions & 0 deletions lib/clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Clones an object
*/
function clone(obj) {
var key, clone = {};
if (!obj) return;

for (key in obj) {
clone[key] = obj[key];

This comment has been minimized.

Copy link
@sudodoki

sudodoki Aug 27, 2014

Collaborator

If obj[key] will be object, it would pass references to those, so they will be mutable & change in obj/clone simultaneously. Might it be the problem? If there's no functions in obj you can use old clone = JSON.parse(JSON.stringify(obj)) otherwise would have to iterate those & copy some manually. Just something to consider in case it's this usecase.

This comment has been minimized.

Copy link
@jdan

jdan Aug 27, 2014

Author Owner

You're absolutely right. This doesn't support nested objects. When we support the author tag in command-line options, I'll have to figure something out. JSON.parse(JSON.stringify(obj)) is pretty neat.

}

return clone;
};

module.exports = clone;
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var marked = require('marked');
var hljs = require('highlight.js');
var yaml = require('js-yaml');
var mustache = require('mustache');

var clone = require('./clone');
var helper;


Expand All @@ -20,7 +22,7 @@ var helper;
*/
function Cleaver(document, options, includePath) {
this.document = document.toString();
this.options = options || {};
this.options = clone(options) || {};
this.path = path.resolve(includePath || '.');
/**
* Require helper using `this.path` as a context for where to locate any
Expand Down

0 comments on commit 8e075bc

Please sign in to comment.