Skip to content

Commit

Permalink
parse ``` and ~~~~ special code marker in markdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed Oct 22, 2011
1 parent 11d5d70 commit 31ee00f
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions lib/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,28 @@ var manfront = [
// directly.
//

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

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

if(!cb) {
cb = cmd;
cmd = 'manman';
}

// noop if no callback supplied
cb = cb || function(){};

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);
var ronn = md ? parseMd(content) : parse(extname, content);
man(ronn, { page: basename, cmd: cmd }, cb);
});
});
};
Expand Down Expand Up @@ -94,3 +102,32 @@ function parse(ext, code) {

return sections.join('\n');
}

function parseMd(content) {
var delim = '<code>',
delimIn = delim + 'in',
delimOut = delim + 'out',
code;

content = content
.replace(/```.+/gm, delimIn + '\n')
.replace(/~~~.+/gm, delimIn + '\n')
.replace(/```/gm, '\n' + delimOut)
.replace(/~~~/gm, '\n' + delimOut);

content = content.split('\n')
.map(function(line) {
if (line.match(delimIn)) {
line = line.replace(delimIn, '');
code = true;
} else if(line.match(delimOut)) {
line = line.replace(delimOut, '');
code = false;
}

return code ? ' ' + line : line;
});


return content.join('\n');
}

0 comments on commit 31ee00f

Please sign in to comment.