Skip to content

Commit

Permalink
add help module, takes a file input (md, js or cs) and man a generate…
Browse files Browse the repository at this point in the history
…d manpage
  • Loading branch information
mklabs committed Oct 22, 2011
1 parent 5a8fd4d commit 11d5d70
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var fs = require('fs'),
//
// Simply log in the callback to show completion output.

exports.complete = function complete(name, completer, cb) {
function complete(name, completer, cb) {

// cb not there, assume callback is completer and
// the completer is the executable itself
Expand Down
96 changes: 96 additions & 0 deletions lib/docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

var fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
Ronn = require('ronn').Ronn;

// known markdown extensions
var markdowns = ['.md', '.markdown', '.mkd'],
// the reg used to parse markdown from source files
matcher = {
coffee: /^\s*#\s?/,
js: /^\s*\/\/\s?/
};


var manfront = [
":cmd-:page(1) -- documentation for :page",
"==========================================================================================================",
"",
"## :page \n"
].join('\n');

// ## help
//
// This file is a special task file which uses [ronnjs](https://github.com/kapouer/ronnjs) to
// automatically generate manpage from the source file.
//
// It will basically output the comments of a source file, originally done to be docco compliant,
// using `man`. The source files are parsed and their markdown content is runned through ronnjs,
// then executed through the `man` executable.
//
//
// Output documentation, generated from source files or markdown
// directly.
//

exports.help = function(file, cb, cmd) {
var extname = path.extname(file),
basename = path.basename(file).replace(extname, ''),
md = !!~markdowns.indexOf(extname);

if(basename === 'Cakefile') extname = '.coffee';

path.exists(file, function(state) {
if(!state) return cb(new Error(file + ' does not exist.'));

fs.readFile(file, 'utf8', function(err, content) {
if(err) return cb(err);
var ronn = md ? content : parse(extname, content);
man(ronn, { page: basename, cmd: cmd || 'manman' }, cb);
});
});
};

function handleFront(input, data) {
var front = tmpl(manfront, data);
return front + input;
}

function tmpl(s,d){return s.replace(/:([a-z]+)/g, function(w,m){return d[m];});}

function man(output, options, cb) {
var ronn = new Ronn(handleFront(output, options)),
manpath = path.join(__dirname, '.man.swp');

fs.writeFile(manpath, ronn.roff(), function(err) {
if(err) return cb(err);

var ch = spawn('man', [manpath], {
// soon deprecated, will have to use stdinStream, stdoutStream, and stderrStream instead
customFds: [process.stdin, process.stdout, process.stderr]
});

ch.on('exit', cb);
});
}

function parse(ext, code) {
var lines = code.split('\n'),
sections = [],
reg = matcher[ext.replace('.', '')],
save = function (line, i, arr) {
// push a new line if the prev line is not a comment
if(reg && reg.test(arr[i - 2])) {
sections.push('\n');
}

if(reg && reg.test(line)) {
sections.push(line.replace(reg, ''));
}
};

lines.forEach(save);

return sections.join('\n');
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"engines": {
"node": "> 0.4"
},
"dependencies": {},
"dependencies": {
"ronn": "~0.3.7"
},
"devDependencies": {
"nopt": "~1.0.10",
"commander": "~0.2.0",
Expand Down

0 comments on commit 11d5d70

Please sign in to comment.