diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..2d71137 --- /dev/null +++ b/.jshintrc @@ -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 +} diff --git a/README.md b/README.md index 7a94c64..2932063 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/cleaver b/bin/cleaver index dda0f4c..19d2dfa 100755 --- a/bin/cleaver +++ b/bin/cleaver @@ -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') { diff --git a/lib/cleaver.js b/lib/cleaver.js index d440031..6d9d18c 100644 --- a/lib/cleaver.js +++ b/lib/cleaver.js @@ -1,4 +1,3 @@ -var fs = require('fs'); var Q = require('q'); var path = require('path'); var marked = require('marked'); @@ -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); @@ -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) ]); -} +}; /** @@ -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) { @@ -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); -} +}; /** @@ -114,7 +116,7 @@ Cleaver.prototype.populateResources = function () { } return Q.all(promises); -} +}; /** @@ -127,7 +129,7 @@ Cleaver.prototype.populateThemeResources = function () { if (this.metadata.theme) { return helper.loadTheme(this.metadata.theme, this); } -} +}; /** @@ -140,7 +142,7 @@ Cleaver.prototype.loadStaticAssets = function () { return Q.all([ helper.load(this.resources) ]); -} +}; /** @@ -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)); -} +}; /** @@ -207,7 +209,7 @@ Cleaver.prototype.renderSlideshow = function () { */ Cleaver.prototype.renderSlide = function (content) { return marked(content); -} +}; /** @@ -218,7 +220,7 @@ Cleaver.prototype.renderSlide = function (content) { */ Cleaver.prototype.renderAuthorSlide = function (content) { return mustache.render(this.templates.loaded.author, content); -} +}; /** @@ -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); -} +}; /** @@ -267,8 +271,8 @@ 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); } @@ -276,7 +280,7 @@ Cleaver.prototype.slice = function(document) { } return slices; -} +}; /** @@ -301,7 +305,7 @@ Cleaver.prototype.run = function () { .fail(function (err) { console.log('!!', err.message); }); -} +}; module.exports = Cleaver; diff --git a/lib/helper.js b/lib/helper.js index b95c67b..0a184e8 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -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))); } @@ -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/'; @@ -188,12 +192,12 @@ function readFilePromise(filename, failsafe) { * @return {Promise.} 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 { @@ -209,7 +213,7 @@ function httpGetPromise(url, failsafe) { res.on('end', function () { deferred.resolve(data); }); - } + }; if (url.match(/^https/)) { get = https.get(url, cb); @@ -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, @@ -255,4 +267,4 @@ module.exports = function (inputPath) { loadTheme: loadTheme, save: save }; -} +}; diff --git a/package.json b/package.json index 45278c6..c41f695 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/resources/script.js b/resources/script.js index e095f18..6779451 100644 --- a/resources/script.js +++ b/resources/script.js @@ -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; } @@ -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; } @@ -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() + '%'; } @@ -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(); } } @@ -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(); - } + }; } -} +};