Browse Source

add basic prototype of custom formatter

formatter
Brett Langdon 11 years ago
parent
commit
40ee3ab5ab
7 changed files with 130 additions and 13 deletions
  1. +1
    -0
      .gitignore
  2. +17
    -12
      bin/docast
  3. +76
    -0
      lib/formatter.js
  4. +9
    -0
      lib/index.jade
  5. +5
    -0
      lib/index.js
  6. +18
    -0
      lib/template.jade
  7. +4
    -1
      package.json

+ 1
- 0
.gitignore View File

@ -1 +1,2 @@
node_modules
build

+ 17
- 12
bin/docast View File

@ -9,32 +9,37 @@ var doc = [
' docast (-h | --help)',
' docast (-v | --version)',
' docast extract [-o <output>] <input_files>...',
' docast generate <formatter> <input_files>...',
' docast generate [-f <formatter>] [-b <output>] <input_files>...',
'',
'Options:',
' -h --help Show this help text',
' -v --version Show docast version information',
' -h --help Show this help text',
' -v --version Show docast version information',
'Extract:',
' Parse docs from javascript files and output as json to a file',
' -o --output <output> File to output to [default: out.json]',
' <input_files> List of javascript files to fetch docs from',
' -o --output <output> File to output to [default: out.json]',
' <input_files> List of javascript files to fetch docs from',
'Generate:',
' Provide a script used to generate documentation from the parsed docs',
' <formatter> Script which exports a `function(comments)` used to generate docs from comments',
' <input_files> List of javascript files to fetch docs from',
' -b --build <output> Folderto output to [default: build]',
' -f --formatter <formatter> Script to import as formatter, default uses internal',
' <input_files> List of javascript files to fetch docs from',
].join('\r\n');
var args = docopt.docopt(doc, {version: require('../package.json').version});
var comments = [];
var comments = {};
args['<input_files>'].forEach(function(file){
comments = comments.concat(docast.parse(file));
comments[file] = docast.parse(file);
});
if(args.extract){
fs.writeFileSync(args['--output'], JSON.stringify(comments));
} else if(args.generate){
var loc = path.join(process.cwd(), args['<formatter>']);
var formatter = require(loc);
formatter(comments);
var options = {build: args['--build']};
var formatter = new docast.formatter(options);
if(args['--formatter']){
var loc = path.join(process.cwd(), args['--formatter']);
formatter = new (require(loc))(options);
}
formatter.format(comments);
}

+ 76
- 0
lib/formatter.js View File

@ -0,0 +1,76 @@
var fs = require('fs');
var jade = require('jade');
var mkdirp = require('mkdirp');
var path = require('path');
var yaml = require('js-yaml');
var Formatter = function(options){
this.options = options || {};
this.build = options.build || 'build';
this.yaml = (options.yaml === undefined)? true : options.yaml;
var defaultTemplate = path.join(__dirname, 'template.jade');
this.template = (options.template === undefined)? defaultTemplate : options.template;
this.renderer = JSON.stringify;
if(this.template){
this.renderer = jade.compileFile(this.template);
}
var defaultIndexTemplate = path.join(__dirname, 'index.jade');
this.indexTemplate = (options.indexTemplate === undefined)? defaultIndexTemplate : options.indexTemplate;
this.indexRenderer = JSON.stringify;
if(this.indexTemplate){
this.indexRenderer = jade.compileFile(this.indexTemplate);
}
this.output = path.join(process.cwd(), this.build);
mkdirp.sync(this.output);
};
Formatter.prototype.format = function(data){
for(var filename in data){
this.formatFile(filename, data[filename]);
}
var data = {
index: this.buildIndex(this.output),
};
fs.writeFileSync(path.join(this.output, 'index.html'), this.indexRenderer(data));
};
Formatter.prototype.buildIndex = function(dir){
var files = fs.readdirSync(dir);
var index = [];
files.forEach(function(filename){
absFilename = path.join(dir, filename);
var stats = fs.statSync(absFilename);
if(stats.isDirectory()){
index = index.concat(this.buildIndex(absFilename));
} else {
index.push(absFilename.replace(this.output + '/', ''));
}
}.bind(this));
return index;
};
Formatter.prototype.formatFile = function(filename, comments){
var out = path.join(this.output, filename);
var dir = path.dirname(out);
var base = path.basename(out, '.js');
out = path.join(dir, base + '.html');
mkdirp.sync(path.dirname(out));
if(this.yaml){
for(var i in comments){
comments[i].doc = yaml.safeLoad(comments[i].doc);
}
}
var data = {
filename: filename,
comments: comments,
};
var rendered = this.renderer(data);
fs.writeFileSync(out, rendered);
};
module.exports = Formatter;

+ 9
- 0
lib/index.jade View File

@ -0,0 +1,9 @@
doctype html
html
head
body
h1=dir
ul
each file in index
li
a(href=file)=file

+ 5
- 0
lib/index.js View File

@ -15,6 +15,10 @@ var traverseForType = function(type, expr, callback){
if(expr.type === 'BlockStatement'){
expr.body.forEach(function(expr){
if(expr.type === type){
// probably a blank return or throw statement
if(!expr.argument){
return;
}
if(expr.argument.type === 'NewExpression'){
callback(expr.argument.callee.name);
} else if(expr.argument.type === 'Literal'){
@ -183,3 +187,4 @@ module.exports.parseContents = function(contents, options){
};
module.exports.parse = module.exports.parseFile;
module.exports.formatter = require('./formatter');

+ 18
- 0
lib/template.jade View File

@ -0,0 +1,18 @@
doctype html
html
body
h1=filename
each comment in comments
h3=comment.name + '(' + comment.params.join(', ') + ')'
pre=comment.doc.comment
if comment.doc.params
h5 Params
ul
each param in comment.doc.params
li=param.name + ' [' + param.type + ']'
pre=param.comment
if comment.doc.returns
h5 Returns
ul
li='[' + comment.doc.returns.type + ']'
pre=comment.doc.returns.comment

+ 4
- 1
package.json View File

@ -18,7 +18,10 @@
"homepage": "https://github.com/brettlangdon/docast",
"dependencies": {
"docopt": "^0.4.1",
"esprima": "^1.2.3"
"esprima": "^1.2.3",
"jade": "^1.9.1",
"mkdirp": "^0.5.0",
"yaml": "^0.2.3"
},
"devDependencies": {
"mocha": "^2.1.0"


Loading…
Cancel
Save