-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add help module, takes a file input (md, js or cs) and man a generate…
…d manpage
- Loading branch information
Showing
3 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters