From bf8f3f9f173c496cd4adc240b4cfe45ad741d0d0 Mon Sep 17 00:00:00 2001 From: Robert Kowalski Date: Sun, 25 Jan 2015 15:37:30 +0100 Subject: [PATCH 1/4] fix global leak --- html.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html.js b/html.js index dd9efcb..592a093 100644 --- a/html.js +++ b/html.js @@ -106,7 +106,7 @@ function render(lexed, filename, template, cb) { // content has to be the last thing we do with // the lexed tokens, because it's destructive. - content = marked.parser(lexed); + var content = marked.parser(lexed); template = template.replace(/__CONTENT__/g, content); cb(null, template); From ab16ebd93980f761b413c53c6319d5e80f03a478 Mon Sep 17 00:00:00 2001 From: Robert Kowalski Date: Sun, 25 Jan 2015 15:41:14 +0100 Subject: [PATCH 2/4] add lab to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..67fb6a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/.bin/lab +node_modules/lab/ From 3ee078e42e7e80406ed16a16b5c3470164c5169b Mon Sep 17 00:00:00 2001 From: Robert Kowalski Date: Sun, 25 Jan 2015 15:38:41 +0100 Subject: [PATCH 3/4] add basic tests --- package.json | 7 +- test/basic.js | 44 +++++++++++ .../markdown/include-fixture.markdown | 1 + test/fixtures/markdown/index.markdown | 5 ++ test/fixtures/template.html | 78 +++++++++++++++++++ 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 test/basic.js create mode 100644 test/fixtures/markdown/include-fixture.markdown create mode 100644 test/fixtures/markdown/index.markdown create mode 100644 test/fixtures/template.html diff --git a/package.json b/package.json index 89a9b60..5ad6946 100644 --- a/package.json +++ b/package.json @@ -6,10 +6,15 @@ "engines": { "node": ">=0.6.10" }, + "scripts": { + "test": "lab -v" + }, "dependencies": { "marked": "~0.1.9" }, - "devDependencies": {}, + "devDependencies": { + "lab": "~5.2.1" + }, "optionalDependencies": {}, "bin": "./generate.js" } diff --git a/test/basic.js b/test/basic.js new file mode 100644 index 0000000..364e251 --- /dev/null +++ b/test/basic.js @@ -0,0 +1,44 @@ +var Lab = require('lab'); +var lab = exports.lab = Lab.script(); +var assert = require('assert'); + +var spawn = require('child_process').spawn; + +lab.experiment('doc generation', function() { + lab.test('it renders html from *.markdown files', function(done) { + var files = [__dirname + '/fixtures/markdown/index.markdown']; + + generate('html', files, function (err, buf) { + assert.ok(/

hello from index.markdown/.test(buf.toString())); + done(); + }); + }); + + lab.test('it processes includes from *.markdown files', function(done) { + var files = [__dirname + '/fixtures/markdown/index.markdown']; + + generate('html', files, function (err, buf) { + assert.ok(/

Hello from include with markdown-fileending!/.test(buf.toString())); + done(); + }); + }); +}); + + +function generate(format, files, cb) { + var buffer = '', + child; + + child = spawn(__dirname + '/../generate.js', [ + '--format=' + format, + '--template=' + __dirname + '/fixtures/template.html' + ].concat(files)); + + child.stdout.on('data', function(chunk) { + buffer += chunk; + }); + + child.on('close', function () { + cb(null, buffer); + }); +} diff --git a/test/fixtures/markdown/include-fixture.markdown b/test/fixtures/markdown/include-fixture.markdown new file mode 100644 index 0000000..480fab9 --- /dev/null +++ b/test/fixtures/markdown/include-fixture.markdown @@ -0,0 +1 @@ +## Hello from include with markdown-fileending! diff --git a/test/fixtures/markdown/index.markdown b/test/fixtures/markdown/index.markdown new file mode 100644 index 0000000..a9ef696 --- /dev/null +++ b/test/fixtures/markdown/index.markdown @@ -0,0 +1,5 @@ +# Api Docs + +## hello from index.markdown + +@include include-fixture diff --git a/test/fixtures/template.html b/test/fixtures/template.html new file mode 100644 index 0000000..9c2a0c2 --- /dev/null +++ b/test/fixtures/template.html @@ -0,0 +1,78 @@ + + + + + __SECTION__ Node.js __VERSION__ Manual & Documentation + + + + + +
+ + + +
+
+ + +
+
+

Node.js __VERSION__ Manual & Documentation

+ +
+
+ +
+

Table of Contents

+ __TOC__ +
+ +
+ __CONTENT__ +
+
+
+ + + + + + + + From 812073e4a03fcb65f4c06a1703d6fdacd12091b3 Mon Sep 17 00:00:00 2001 From: Robert Kowalski Date: Fri, 30 Jan 2015 00:17:04 +0100 Subject: [PATCH 4/4] remove hardcoded gtoc path make it configurable and testable --- generate.js | 5 ++++- html.js | 8 +++----- test/basic.js | 1 + 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/generate.js b/generate.js index 768f3b3..3fe7eb0 100755 --- a/generate.js +++ b/generate.js @@ -32,6 +32,7 @@ var args = process.argv.slice(2); var format = 'json'; var template = null; var inputFile = null; +var gtocPath = null; args.forEach(function (arg) { if (!arg.match(/^\-\-/)) { @@ -40,6 +41,8 @@ args.forEach(function (arg) { format = arg.replace(/^\-\-format=/, ''); } else if (arg.match(/^\-\-template=/)) { template = arg.replace(/^\-\-template=/, ''); + } else if (arg.match(/^\-\-gtoc=/)) { + gtocPath = arg.replace(/^\-\-gtoc=/, ''); } }) @@ -70,7 +73,7 @@ function next(er, input) { break; case 'html': - require('./html.js')(input, inputFile, template, function(er, html) { + require('./html.js')(input, inputFile, template, gtocPath, function(er, html) { if (er) throw er; console.log(html); }); diff --git a/html.js b/html.js index 592a093..3332841 100644 --- a/html.js +++ b/html.js @@ -26,19 +26,17 @@ var preprocess = require('./preprocess.js'); module.exports = toHTML; -// TODO(chrisdickinson): never stop vomitting / fix this. -var gtocPath = path.resolve(path.join(__dirname, '..', '..', 'doc', 'api', '_toc.markdown')); var gtocLoading = null; var gtocData = null; -function toHTML(input, filename, template, cb) { +function toHTML(input, filename, template, gtocPath, cb) { if (gtocData) { return onGtocLoaded(); } if (gtocLoading === null) { gtocLoading = [onGtocLoaded]; - return loadGtoc(function(err, data) { + return loadGtoc(gtocPath, function(err, data) { if (err) throw err; gtocData = data; gtocLoading.forEach(function(xs) { @@ -60,7 +58,7 @@ function toHTML(input, filename, template, cb) { } } -function loadGtoc(cb) { +function loadGtoc(gtocPath, cb) { fs.readFile(gtocPath, 'utf8', function(err, data) { if (err) return cb(err); diff --git a/test/basic.js b/test/basic.js index 364e251..e4ba9bb 100644 --- a/test/basic.js +++ b/test/basic.js @@ -31,6 +31,7 @@ function generate(format, files, cb) { child = spawn(__dirname + '/../generate.js', [ '--format=' + format, + '--gtoc=' + __dirname + '/fixtures/markdown/_toc.markdown', '--template=' + __dirname + '/fixtures/template.html' ].concat(files));