Skip to content

Commit

Permalink
Added option for stdout
Browse files Browse the repository at this point in the history
By adding the `-s` or `--stdout` option, you can print to stdout now. Not th emost beuatiful PR, but I think it is pretty efficent and it works for me. Let me know if I can edit anything!

Related to issue thlorenz#100
  • Loading branch information
RichardLitt authored and thlorenz committed Jun 17, 2016
1 parent eab6f48 commit bb4f414
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,8 @@ By default,
- no limit is placed on Markdown-formatted headings,
- whereas headings from embedded HTML are limited to 4 levels.

### Printing to stdout

You can print to stdout by using the `-s` or `--stdout` option.

[ack]: http://beyondgrep.com/
26 changes: 19 additions & 7 deletions doctoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ function cleanPath(path) {
return homeExpanded.replace(/\s/g, '\\ ');
}

function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix) {
function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, stdOut) {
console.log('\n==================\n');

var transformed = files
.map(function (x) {
var content = fs.readFileSync(x.path, 'utf8')
Expand All @@ -27,15 +27,26 @@ function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPref
return result;
});
var changed = transformed.filter(function (x) { return x.transformed; })
, unchanged = transformed.filter(function (x) { return !x.transformed; });
, unchanged = transformed.filter(function (x) { return !x.transformed; })
, toc = transformed.filter(function (x) { return x.toc; })

if (stdOut) {
toc.forEach(function (x) {
console.log(x.toc)
})
}

unchanged.forEach(function (x) {
console.log('"%s" is up to date', x.path);
});

changed.forEach(function (x) {
console.log('"%s" will be updated', x.path);
fs.writeFileSync(x.path, x.data, 'utf8');
if (stdOut) {
console.log('==================\n\n"%s" should be updated', x.path)
} else {
console.log('"%s" will be updated', x.path);
fs.writeFileSync(x.path, x.data, 'utf8');
}
});
}

Expand Down Expand Up @@ -64,7 +75,7 @@ var modes = {
var mode = modes['github'];

var argv = minimist(process.argv.slice(2)
, { boolean: [ 'h', 'help', 'T', 'notitle'].concat(Object.keys(modes))
, { boolean: [ 'h', 'help', 'T', 'notitle', 's', 'stdout'].concat(Object.keys(modes))
, string: [ 'title', 't', 'maxlevel', 'm', 'entryprefix' ]
, unknown: function(a) { return (a[0] == '-' ? (console.error('Unknown option(s): ' + a), printUsageAndExit(true)) : true); }
});
Expand All @@ -82,6 +93,7 @@ for (var key in modes) {
var title = argv.t || argv.title;
var notitle = argv.T || argv.notitle;
var entryPrefix = argv.entryprefix || '-';
var stdOut = argv.s || argv.stdout

var maxHeaderLevel = argv.m || argv.maxlevel;
if (maxHeaderLevel && isNaN(maxHeaderLevel) || maxHeaderLevel < 0) { console.error('Max. heading level specified is not a positive number: ' + maxHeaderLevel), printUsageAndExit(true); }
Expand All @@ -98,7 +110,7 @@ for (var i = 0; i < argv._.length; i++) {
files = [{ path: target }];
}

transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix);
transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, stdOut);

console.log('\nEverything is OK.');
}
16 changes: 16 additions & 0 deletions test/fixtures/stdout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

DocToccing single file "test/fixtures/readme-with-custom-title.md" for github.com.

==================

## Table of Contents

- [Installation](#installation)
- [API](#api)
- [License](#license)

==================

"test/fixtures/readme-with-custom-title.md" should be updated

Everything is OK.
36 changes: 36 additions & 0 deletions test/transform-stdout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
/*jshint asi: true */

var test = require('tap').test,
fs = require('fs'),
exec = require("child_process").exec;

test('\nshould print to stdout with --stdout option', function (t) {

exec('node doctoc.js test/fixtures/readme-with-custom-title.md --stdout', function (error, stdout, stderr) {
if (error) {
console.error('exec error: ', error);
return;
}
t.deepEqual(stdout
, fs.readFileSync(__dirname + '/fixtures/stdout.md', 'utf8')
, 'spits out the correct table of contents')

t.end()
})
})

test('\nshould print to stdout with -s option', function (t) {

exec('node doctoc.js test/fixtures/readme-with-custom-title.md -s', function (error, stdout, stderr) {
if (error) {
console.error('exec error: ', error);
return;
}
t.deepEqual(stdout
, fs.readFileSync(__dirname + '/fixtures/stdout.md', 'utf8')
, 'spits out the correct table of contents')

t.end()
})
})

0 comments on commit bb4f414

Please sign in to comment.