Skip to content

Commit

Permalink
added --debug option, removed janky options everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
jdan committed Apr 26, 2014
1 parent 07ab1dd commit 8a4df4e
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 53 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ cleaver watch path/to/something-changing.md
# Rebuilding: Thu Nov 07 2013 00:16:09 GMT-0500 (EST)
```

Use the `--debug` flag to display debug information:

```bash
$ cleaver --debug examples/basic.md
cleaver loaded input document +0ms
helper read /Users/jordan/Projects/cleaver/templates/layout.mustache +0ms
helper read /Users/jordan/Projects/cleaver/templates/author.mustache +0ms
helper read /Users/jordan/Projects/cleaver/templates/default.mustache +0ms
cleaver loaded templates +3ms
cleaver parsed metadata +4ms
helper read /Users/jordan/Projects/cleaver/resources/default.css +13ms
helper read /Users/jordan/Projects/cleaver/resources/github.css +0ms
helper read /Users/jordan/Projects/cleaver/resources/script.js +0ms
cleaver loaded static assets +9ms
cleaver rendered slides +1ms
cleaver rendered presentation +1ms
```

## More Info

**Cleaver** is a one-stop shop for generating HTML presentations in
Expand Down
26 changes: 23 additions & 3 deletions bin/cleaver
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

var path = require('path');
var fs = require('fs');
var Cleaver = require(path.join(__dirname,'../lib/cleaver.js'));
var program = require('commander');
var Cleaver;

/**
* Helper function to use the cleaver API to create and save a new
Expand All @@ -21,6 +21,9 @@ function createAndSave(file) {
})
.fail(function (err) {
process.stderr.write('!! ' + err.message + '\n');
if (program.debug) {
process.stderr.write(err.stack);
}
process.exit(1);
});
});
Expand All @@ -33,7 +36,7 @@ program
program
.command('watch')
.description('Watch for changes on markdown file')
.action(function(){
.action(function () {
var file = program.args[0];
createAndSave(file);

Expand All @@ -44,11 +47,28 @@ program
});
});

program
.option('--debug', 'Enable debug output');

program.parse(process.argv);

if (!program.args.length) {
program.help();
} else {
/**
* Enable debugging
*
* For finer-tuned debugging you can ignore the --debug flag and
* instead use the DEBUG environment variable that the `debug` module
* reads automatically.
*
* $ DEBUG=[cleaver,helper] cleaver path/to/presentation.md
*/
if (program.debug) {
process.env.DEBUG = '*';
}

/* Load cleaver with the new ENV for debugging */
Cleaver = require(path.join(__dirname,'../lib/cleaver.js'));
createAndSave(program.args[0]);
}

43 changes: 34 additions & 9 deletions lib/cleaver.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Q = require('q');
var debug = require('debug')('cleaver');
var path = require('path');
var marked = require('marked');
var hljs = require('highlight.js');
Expand Down Expand Up @@ -56,8 +57,12 @@ function Cleaver(document, includePath) {
*/
Cleaver.prototype.loadDocument = function () {
return Q.all([
helper.load(this.external, { external: true }),
helper.load(this.templates)
helper.load(this.external, { external: true }).then(function () {
debug('loaded input document');
}),
helper.load(this.templates).then(function () {
debug('loaded templates');
})
]);
};

Expand All @@ -72,6 +77,7 @@ Cleaver.prototype.renderSlides = function () {
var i;

this.metadata = yaml.safeLoad(slices[0].content) || {};
debug('parsed metadata');

for (i = 1; i < slices.length; i++) {
this.slides.push({
Expand Down Expand Up @@ -111,17 +117,26 @@ Cleaver.prototype.populateResources = function () {

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

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

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

return Q.all(promises);
Expand All @@ -136,7 +151,9 @@ Cleaver.prototype.populateResources = function () {
Cleaver.prototype.populateThemeResources = function () {
// maybe load a theme
if (this.metadata.theme) {
return helper.loadTheme(this.metadata.theme, this);
return helper.loadTheme(this.metadata.theme, this).then(function () {
debug('loaded theme');
});
}
};

Expand All @@ -150,7 +167,9 @@ Cleaver.prototype.populateThemeResources = function () {
Cleaver.prototype.loadStaticAssets = function () {
return Q.all([
helper.load(this.resources)
]);
]).then(function () {
debug('loaded static assets');
});
};


Expand All @@ -162,7 +181,7 @@ Cleaver.prototype.loadStaticAssets = function () {
Cleaver.prototype.renderSlideshow = function () {
var putControls = this.metadata.controls || (this.metadata.controls === undefined);
var putProgress = this.metadata.progress || (this.metadata.progress === undefined);
var style, script;
var style, script, output;

// Render the slides in a template (maybe as specified by the user)
var slideshow = mustache.render(this.templates.loaded.slides, {
Expand All @@ -171,6 +190,8 @@ Cleaver.prototype.renderSlideshow = function () {
progress: putProgress
});

debug('rendered slides');

// TODO: handle defaults gracefully
var title = this.metadata.title || 'Untitled';
var encoding = this.metadata.encoding || 'utf-8';
Expand Down Expand Up @@ -206,7 +227,11 @@ Cleaver.prototype.renderSlideshow = function () {
};

/* Return the rendered slideshow */
return mustache.render(this.templates.loaded.layout, layoutData);
output = mustache.render(this.templates.loaded.layout, layoutData);

debug('rendered presentation');

return output;
};


Expand Down
71 changes: 31 additions & 40 deletions lib/helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Q = require('q');
var debug = require('debug')('helper');
var http = require('http');
var https = require('https');
var fs = require('fs');
Expand All @@ -16,16 +17,15 @@ var ROOT_DIR = path.resolve(__dirname, '..');
/**
* Loads a single static asset from either a filename or URL. Returns a promise.
* @param {string} source The source document to load
* @param {!Boolean} failsafe Option to fail gracefully, without raising errors
* @return {Promise.<string>} The file contents
*/
function loadSingle(source, failsafe) {
function loadSingle(source) {
var promise;

if (source.match(/^https?:\/\//)) {
promise = httpGetPromise(source, failsafe);
promise = httpGetPromise(source);
} else {
promise = readFilePromise(normalizePath(source), failsafe);
promise = readFilePromise(normalizePath(source));
}

return promise;
Expand All @@ -42,11 +42,10 @@ function loadSingle(source, failsafe) {
* @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
* @param {!Boolean} failsafe Option to fail gracefully, without raising errors
* @return {Promise}
*/
function populateSingle(filename, destination, key, failsafe) {
return loadSingle(filename, failsafe)
function populateSingle(filename, destination, key) {
return loadSingle(filename)
.then(function (data) {
if (data) {
/**
Expand Down Expand Up @@ -139,10 +138,10 @@ function loadTheme(source, ctx) {

promises = [
loadSettings(source + 'settings.json', ctx),
populateSingle(source + 'style.css', ctx.external, 'style', true),
populateSingle(source + 'template.mustache', ctx.templates, 'slides', true),
populateSingle(source + 'layout.mustache', ctx.templates, 'layout', true),
populateSingle(source + 'script.js', ctx.external, 'script', true)
populateSingle(source + 'style.css', ctx.external, 'style'),
populateSingle(source + 'template.mustache', ctx.templates, 'slides'),
populateSingle(source + 'layout.mustache', ctx.templates, 'layout'),
populateSingle(source + 'script.js', ctx.external, 'script')
];

return Q.all(promises);
Expand All @@ -157,7 +156,7 @@ function loadTheme(source, ctx) {
* @return {Promise}
*/
function loadSettings(source, ctx) {
return loadSingle(source, true).then(function (data) {
return loadSingle(source).then(function (data) {
if (data) {
data = JSON.parse(data);
ctx.override = data.override;
Expand All @@ -169,53 +168,49 @@ function loadSettings(source, ctx) {
/**
* Promise to load a files contents
* @param {string} filename The file to load
* @param {!Boolean} failsafe Option to fail gracefully, without raising errors
* @return {Promise.<string>} The file's contents
*/
function readFilePromise(filename, failsafe) {
function readFilePromise(filename) {
var deferred;

if (!failsafe) {
return Q.nfcall(fs.readFile, filename, 'utf-8');
} else {
deferred = Q.defer();
deferred = Q.defer();

fs.readFile(filename, 'utf-8', function (err, contents) {
fs.readFile(filename, 'utf-8', function (err, contents) {
if (err) {
debug(err + ' ' + filename);
} else {
debug('read ' + filename);
deferred.resolve(contents);
});
}
});

return deferred.promise;
}
return deferred.promise;
}


/**
* Promise to perform a get request and return the result
* @param {string} url The url to request
* @param {!Boolean} failsafe Option to fail gracefully, without raising errors
* @return {Promise.<string>} The body of the response
*/
function httpGetPromise(url, failsafe) {
function httpGetPromise(url) {
var deferred = Q.defer(), get;

var cb = function (res) {
var data = '';

if (res.statusCode !== 200) {
if (failsafe) {
deferred.resolve();
} else {
deferred.reject(new Error(
'The url (' + url + ') returned with status ' + res.statusCode));
}
}

res.on('data', function (chunk) {
data += chunk;
});

res.on('end', function () {
deferred.resolve(data);
if (res.statusCode !== 200) {
debug(res.statusCode + ': ' + url);
deferred.resolve();
} else {
debug('fetched ' + url);
deferred.resolve(data);
}
});
};

Expand All @@ -226,12 +221,8 @@ function httpGetPromise(url, failsafe) {
}

get.on('error', function (err) {
if (failsafe) {
deferred.resolve();
} else {
deferred.reject(new Error(
'The url (' + url + ') caused an error'));
}
deferred.resolve();
debug(err + ': ' + url);
});

return deferred.promise;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cleaver",
"preferGlobal": true,
"version": "0.6.3",
"version": "0.6.4",
"author": "Jordan Scales <[email protected]>",
"description": "30-second slideshows for hackers",
"keywords": [
Expand All @@ -28,6 +28,7 @@
"commander": "~2.1.0"
},
"devDependencies": {
"debug": "~0.8.0",
"jshint": "~2.3.0"
},
"scripts": {
Expand Down

0 comments on commit 8a4df4e

Please sign in to comment.