Skip to content

Commit

Permalink
Style changes for JSHint and npm test command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbeletsky authored and jdan committed Dec 6, 2013
1 parent 16983ec commit 4a3275f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 46 deletions.
32 changes: 32 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"asi" : false,
"bitwise" : true,
"boss" : false,
"browser" : true,
"curly" : true,
"debug": false,
"devel": false,
"eqeqeq": true,
"evil": false,
"expr": true,
"forin": false,
"immed": true,
"latedef" : false,
"laxbreak": false,
"loopfunc": true,
"multistr": true,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": true,
"onevar": false,
"plusplus": false,
"regexp": false,
"strict": false,
"globalstrict": true,
"sub": false,
"trailing" : true,
"undef": true,
"unused": "vars",
"node": true
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Themes may be specified by one of the following options:

* An absolute or relative path to a directory
* A URL to a directory
* A github repostitory in the form of *username/reponame*
* A github repository in the form of *username/reponame*

### Overriding Themes

Expand Down
2 changes: 1 addition & 1 deletion bin/cleaver
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

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

if (process.argv[2] === 'watch') {
Expand Down
46 changes: 25 additions & 21 deletions lib/cleaver.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var fs = require('fs');
var Q = require('q');
var path = require('path');
var marked = require('marked');
Expand All @@ -8,7 +7,10 @@ var mustache = require('mustache');
var helper;

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

this.file = path.resolve(file);

helper = require('./helper')(this.file);
Expand Down Expand Up @@ -50,13 +52,11 @@ function Cleaver(file) {
* @return {Promise}
*/
Cleaver.prototype.loadDocument = function () {
var self = this;

return Q.all([
helper.load(this.external, true),
helper.load(this.templates)
]);
}
};


/**
Expand All @@ -69,8 +69,9 @@ Cleaver.prototype.renderSlides = function () {
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(this.renderSlide(slices[i]));
}

// insert an author slide (if necessary) at the end
if (this.metadata.author) {
Expand All @@ -82,11 +83,12 @@ Cleaver.prototype.renderSlides = function () {
}

// 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);
}
};


/**
Expand Down Expand Up @@ -114,7 +116,7 @@ Cleaver.prototype.populateResources = function () {
}

return Q.all(promises);
}
};


/**
Expand All @@ -127,7 +129,7 @@ Cleaver.prototype.populateThemeResources = function () {
if (this.metadata.theme) {
return helper.loadTheme(this.metadata.theme, this);
}
}
};


/**
Expand All @@ -140,7 +142,7 @@ Cleaver.prototype.loadStaticAssets = function () {
return Q.all([
helper.load(this.resources)
]);
}
};


/**
Expand Down Expand Up @@ -196,7 +198,7 @@ Cleaver.prototype.renderSlideshow = function () {
// 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));
}
};


/**
Expand All @@ -207,7 +209,7 @@ Cleaver.prototype.renderSlideshow = function () {
*/
Cleaver.prototype.renderSlide = function (content) {
return marked(content);
}
};


/**
Expand All @@ -218,7 +220,7 @@ Cleaver.prototype.renderSlide = function (content) {
*/
Cleaver.prototype.renderAuthorSlide = function (content) {
return mustache.render(this.templates.loaded.author, content);
}
};


/**
Expand All @@ -236,16 +238,18 @@ Cleaver.prototype.renderAgendaSlide = function (slices) {
matches = /^(#{3,})\s+(.+)$/.exec(firstLine);

// Only index non-title slides (begins with 3 ###)
if (!matches) continue;
if (!matches) {
continue;
}

if (lastTitle != matches[2]) {
if (lastTitle !== matches[2]) {
lastTitle = matches[2];
titles.push(lastTitle);
}
}

return mustache.render(this.templates.loaded.agenda, titles);
}
};


/**
Expand All @@ -267,16 +271,16 @@ Cleaver.prototype.slice = function(document) {
* empty slide at the beginning (to represent empty metadata), followed
* by the first slice (with the -- chopped off).
*/
if (i == 0 && cuts[i].match(/^--/)) {
slices.push("");
if (i === 0 && cuts[i].match(/^--/)) {
slices.push('');
cuts[i] = cuts[i].slice(2);
}

slices.push(cuts[i].trim());
}

return slices;
}
};


/**
Expand All @@ -301,7 +305,7 @@ Cleaver.prototype.run = function () {
.fail(function (err) {
console.log('!!', err.message);
});
}
};


module.exports = Cleaver;
36 changes: 24 additions & 12 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,20 @@ function load(map, external) {
var filename;

for (var key in map) {
if (!map[key]) continue;
if (!map[key]) {
continue;
}

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

promises.push(loadSingle(filename)
.then((function (_key) {
return function (data) {
loaded[_key] = data;
}
};
})(key)));
}

Expand Down Expand Up @@ -108,7 +112,7 @@ function load(map, external) {
* @return {Promise}
*/
function loadTheme(source, ctx) {
var promises = [], stats;
var promises = [];

if (!fs.existsSync(source) && source.match(/^[\w-]+\/[\w-]+$/)) {
source = 'https://raw.github.com/' + source + '/master/';
Expand Down Expand Up @@ -188,12 +192,12 @@ function readFilePromise(filename, failsafe) {
* @return {Promise.<string>} The body of the response
*/
function httpGetPromise(url, failsafe) {
var deferred = Q.defer(), cb, get;
var deferred = Q.defer(), get;

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

if (res.statusCode != 200) {
if (res.statusCode !== 200) {
if (failsafe) {
deferred.resolve();
} else {
Expand All @@ -209,7 +213,7 @@ function httpGetPromise(url, failsafe) {
res.on('end', function () {
deferred.resolve(data);
});
}
};

if (url.match(/^https/)) {
get = https.get(url, cb);
Expand Down Expand Up @@ -241,12 +245,20 @@ function getUserHome() {

module.exports = function (inputPath) {
normalizePath = function (pathname) {
if (pathname[0] == '~') pathname = pathname.replace('~', getUserHome());
if (pathname[0] == '/') return pathname;
if (path.resolve(pathname) == pathname) return pathname;
if (pathname[0] === '~') {
pathname = pathname.replace('~', getUserHome());
}

if (pathname[0] === '/') {
return pathname;
}

if (path.resolve(pathname) === pathname) {
return pathname;
}

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

return {
loadSingle: loadSingle,
Expand All @@ -255,4 +267,4 @@ module.exports = function (inputPath) {
loadTheme: loadTheme,
save: save
};
}
};
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@
"highlight.js": "~7.3.0",
"marked": "~0.2.9"
},
"devDependencies": {
"jshint": "~2.3.0"
},
"scripts": {
"test": "./node_modules/.bin/jshint lib/ resources/script.js bin/cleaver"
},
"license": "MIT"
}
24 changes: 13 additions & 11 deletions resources/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
function goBack() {
var wrapper = document.querySelector('#wrapper');
var lastSlide = wrapper.lastChild;
while (lastSlide != null && lastSlide.nodeType !== 1) {
while (lastSlide !== null && lastSlide.nodeType !== 1) {
lastSlide = lastSlide.previousSibling;
}

Expand All @@ -22,7 +22,7 @@ function goBack() {
function goForward() {
var wrapper = document.querySelector('#wrapper');
var firstSlide = wrapper.firstChild;
while (firstSlide != null && firstSlide.nodeType !== 1) {
while (firstSlide !== null && firstSlide.nodeType !== 1) {
firstSlide = firstSlide.nextSibling;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ function setCurrentProgress() {

if (progressBar !== null) {
var pagesNumber = wrapper.querySelectorAll('section').length;
var currentNumber = parseInt(currentPage());
var currentNumber = parseInt(currentPage(), 10);
var currentPercent = pagesNumber === 1 ? 100 : 100 * currentNumber / (pagesNumber - 1);
progressBar.style.width = currentPercent.toString() + '%';
}
Expand All @@ -95,7 +95,7 @@ function goToPage(page) {
if (targetSlide) {
var numSlides = allSlides().length;

for (var i = 0; currentPage() != page && i < numSlides; i++) {
for (var i = 0; currentPage() !== page && i < numSlides; i++) {
goForward();
}
}
Expand Down Expand Up @@ -130,30 +130,32 @@ window.onload = function () {

// If the location hash specifies a page number, go to it.
var page = window.location.hash.slice(1);
if (page) goToPage(page);
if (page) {
goToPage(page);
}

document.onkeydown = function (e) {
var kc = e.keyCode;

// left, down, H, J, backspace, PgUp - BACK
// up, right, K, L, space, enter, PgDn - FORWARD
if (kc == 37 || kc == 40 || kc == 8 || kc == 72 || kc == 74 || kc == 33) {
if (kc === 37 || kc === 40 || kc === 8 || kc === 72 || kc === 74 || kc === 33) {
goBack();
} else if (kc == 38 || kc == 39 || kc == 13 || kc == 32 || kc == 75 || kc == 76 || kc == 34) {
} else if (kc === 38 || kc === 39 || kc === 13 || kc === 32 || kc === 75 || kc === 76 || kc === 34) {
goForward();
}
}
};

if (document.querySelector('.next') && document.querySelector('.prev')) {
document.querySelector('.next').onclick = function (e) {
e.preventDefault();
goForward();
}
};

document.querySelector('.prev').onclick = function (e) {
e.preventDefault();
goBack();
}
};
}

}
};

0 comments on commit 4a3275f

Please sign in to comment.