Coddoc
Description
coddoc is a jsdoc parsing library. Coddoc is different in that it is easily extensible by allowing users to add tag and code parsers through the use of coddoc.addTagHandler and coddoc.addCodeHandler. coddoc also parses source code to be used in APIs.
Coddoc also supports the generation of markdown and html through the use of different templates. Currently the templates use Handlebars but you may use any templating engine you wish
JSDOC Tags
JSDoc tags currently supported are:
- augments|extends : extend an object
- lends : document all members of an anonymous object as members of a declared symbol
- namespace : document a namespace
- parameter|param|arg|argument : parameters of a function
- return|returns : return value of a function
- memberof|memberOf : explicitly document a cod block as a member of an object
- name : explicitly document the name of a symbol
- constructor|constructs : document a symbol as a constructor for a class
- class : documents a symbol as a class
- classdesc : alternate way to add a description to a class
- example : document an example/s of a symbol
- private : document a symbol as private.
- ignore : allows you to document a symbol but ignore it in parsing
- method|function : explicily document a symbol as a method
- field : explicily document a symbol as a field
- type : document the type of a field
- default : document the default value of a field
- throws|throw|exception : document any exception thrown by a method
- property : document a property in the constructor declaration of a class
- borrows : document any filed/methods borrowed from a class
- constant|const : document a field as a constant
- desc|description : alternate way to explicitly document the description of a symbol
- public:explicitly document a symbol as public
Coddoc Tags
Coddoc also has a few additional tags.
<ul>
<li>ignoreCode : ignore the parsed code in the output</li>
<li>includeDoc : include an external doc. External docs can be html or markdown.
Example
@includeDoc [Title Of Doc] ../location/of_doc.md
</li>
<li>projectName : document the projectName</li>
<li>github : url to github project</li>
<li>code : specifies a code block without needing an <code>example</code> tag
Example
{@code var test = "test"; }
</li>
<li>header : allows you to document a header that should appear before
generated documentation</li>
<li>footer : allows you to document a footer that should come after the generated documentation</li>
<li>protected : allows you to document a field as protected</li>
</ul>
Installation
Locally
npm install coddoc
Globally
npm install -g coddoc
Usage
Down doc does not currently create multi file docs instead will output to a single file. You may however implement your own formatter to create multiple files.
Command line options
- -d --directory : the directory that contains your code
- [-f --formatter] : optional formatter to use, if not specified then a JSON object of the symbols will be output
- [-p --pattern] : optional file pattern to use when searching for files
Examples
JSON output
coddoc -d /lib > symbolsjson
To use the markdown formatter
coddoc -d /lib -f markdown > READMEmd
To use the HTML formatter
coddoc -d /lib -f html > indexhtml
To use pragmatically
var coddoc = ;var tree = coddoc;var classes = treeclasses namespaces = treenamespaces;//do something
API
Namespaces
Classes
coddoc
Entry point for parsing code.
addCodeHandler
static function public
Defined parser/code.js Top
Adds a handler for a particular code regular expression. Useful if you want to
match a specific type of code not handled by default. When inside of of the parse function
you can use the RegExp.$
properties to access match sub expressions.
By Default code blocks of the following form are parsed.
// /^function (\w+) *\{/ function(){} // /^var *(\w+) *= *function/ var x = function(){}; // /^(\w+(?:\.\w+)*)\.prototype\.(\w+(?:\.\w+)?) *= *function/ MyObject.prototype.testFunction = function(){}; // /^(\w+(?:\.\w+)*)\.prototype\.(\w+(?:\.\w+)?) *= *([^\n;]+)/ MyObject.prototype.testProperty = "property"; // /^(\w+(?:\.\w+)+) *= *function/ some.object.testFunction = function(){} // /^(\w+(?:\.\w+)+) *= *([^\n;]+)/ some.object.testFunction = ["some", "property"]; // /^var +(\w+) *= *([^\n;]+)/ var testProperty = {my : "property"}; var myObject = { // /^\"?(\w+)\"? *\: *function/ testFunction : function(){}, // /^\"?(\w+)\"? *\: *([^,\n]+)/ testProperty : "some property" }
Example
var util = util;//parse code in the format of var myLocal = name.space.myFunction = function(){};//give it a high priority to allow it to override current handlers.;
Arguments
-
regexp : the regular expression used to match code blocks.
-
[priority=0] : the priority to give this code handler if not provided it is defaulted to 0.
-
parse : a function that returns an object. The object will be set as the
codeObject
on the coddoc.Symbol. The properties of the object will be added to the coddoc.Symbol for processing later.
Source
{ if util parse = priority; priority = 0; handlers; handlers; }
addTagHandler
static function public
Defined parser/tags.js Top
Adds a new tag to be parsed. You can use this to add custom tags. coddoc will not do anything with the new tag by default, however you can add functionality to handle the new tag in the template.
Example
//if a tag is contains a '|' character then each variation will resolve the the same parser function.coddoc;//in the template you can add functionality to handle the new tag. For example://in the html symbol.tmpl you could add a new label to the name header<h3>name#if isStatic<span class="label label-info">Static</span>/if#if isFunction<span class="label label-label">Function</span>/if#if isPrivate<span class="label label-important">Private</span>else #if isProtected<span class="label label-warning">Protected</span> else<span class="label label-success">Public</span> /if/if#if isVoid<span class="label label-label">Void</span>/if</h3>
Arguments
-
tag : the tag to parse, if a tag is contains a '|' character then the string will be split and each variation will resolve to the same parse function. If the tag already exists then the old implementation will be replaced by the new one.
-
parse : a parser function to invoke when a tag that matches the name is encountered.
Source
{ tag; }
getTagRegexp
static function protected
Defined parser/tags.js Top
Returns a regular expression that can be used to parse tags
Returns
RegExp
a regular expression to parse valid tags.
Source
{ return "@(" + Object + ")"; }
parse
static function protected
Defined parser/index.js Top
Parses a string of code into coddoc.Symbols. All processed symbols are added to the coddoc.Tree. This method is not intended to be used directly by user code.
Arguments
-
str : the source code to parse
-
filepath : the relative filepath where the source is located. This is set on the symbol during parsing.
-
tree : the tree which contains all symbols.
-
context : the context which holds information about the current parsing job.
-
emitter :
Returns
Object
Source
{ var l = strlength; var symbols = ; for var i = 0; i < l; i++ var tags = ; var comment = "" c = stri startIndex = i endIndex ret = ; var startCIndex = str; if startCIndex !== -1 i = startCIndex + 2; var endCIndex = str; if endCIndex !== -1 comment = str; emitter; i = endCIndex + 1; //console.log(str.substr(startCIndex, endCIndex - startCIndex)); //console.log(comment); var res = sym = ressymbol; symbols; emitter; var memberof = symmemberof; if !symignore && !symlends tree; else i++; return symbols:symbols code:str; }
parseCode
static function protected
Defined parser/code.js Top
Uses Registered handlers to parse the next block of code from a code fragment. This function is used by coddoc.parse to parse code for comments.
Arguments
-
str : the source string to parse
-
symbol : the symbol to add parsed properties from.
-
context : the current context
Source
{ var l = handlerslength ret = {}; for var i = 0; i < l; i++ var h = handlersi; if h ret = h; break; if ret symbolcodeObject = ret; Object; }
parseTag
static function protected
Defined parser/tags.js Top
Parses a tag and the coresponding comment using a matching tag handler. Each parsed tag could add a new property to the coddoc.Symbol. The parsed tag will be added the the coddoc.Symbol#tags array.
Example
coddoc;//would add a new tag to the symbols property tag : "tagname" props : ... //the props value would also be added to the symbols params array.
Arguments
-
comment : the comment fragment being parsed
-
sym : the symbol that the comment corresponds to. The code object and values will have already been set.
-
context : the currect context object. The context allows tags to set new scopes and namespaces.
-
tag
String
: the tag name being parsed from the comment
Source
{ var tag = comment ret = {}; if tag && taglength === 2 var t = tagstag1; if t t; else throw "Invalid tag " + tag; }
coddoc.Context
A Context object used to keep state when parsing symbols. The context should not be used directly by user code.
Instance
Constructor
Defined context.js Top
Source
{ thisscopes = {}; thisnameSpaces = global:; thisaliases = {}; this; }
activateScope
function public
Defined context.js Top
Activates a scope for.
Arguments
- name : the name of the scope.
Returns
Object
the activated scope object.
Source
{ thisactiveScope = name; return this; }
addNamespace
function public
Defined context.js Top
Adds a namespace the the context object.
Arguments
- name : the name of the namespace
Returns
Object
the object for the namespace.
Source
{ if "undefined" === typeof thisnameSpacesname thisnameSpacesname = {}; return thisnameSpacesname; }
addScope
function public
Defined context.js Top
Adds a scope to the context
Arguments
- name : the name of the scope to add,
Returns
Object
the object for the namespace.
Source
{ if "undefined" === typeof thisscopesname thisscopesname = {}; return thisscopesname; }
getActiveScope
function public
Defined context.js Top
Returns the active scope.
Returns
Object
the scope object
Source
{ return this; }
getActiveScopeName
function public
Defined context.js Top
Returns the name of the active scope.
Returns
String
the active scope name.
Source
{ return thisactiveScope; }
getNamespace
function public
Defined context.js Top
Gets a namespace, creating it if it does not exist.
Arguments
- name : the name of the context
Returns
Object
the object for the namespace.
Source
{ return this; }
getScope
function public
Defined context.js Top
Gets a scope creating it if it does not exist.
Arguments
- name : the name of the scope to get,
Returns
Object
the object for the namespace.
Source
{ return this; }
coddoc.Symbol
A Symbol represents a comment and code pair. Each code handler added through coddoc.addCodeHandler and tag handler added through coddoc.addTagHandler adds/removes properties from a the symbol. Each symbol is added to the coddoc.Tree which is either returned from coddoc or passed into a template handler.
NOTE: This object should not be instantiated by user code
Instance Properties
Property | Type | Default Value | Description |
augments | {Array} |
[]
| Any symbols this symbol augments |
borrows | {Array} |
[]
| Any properties this symbol borrows |
codeObject | {Object} | null | The codeObject of this symbol |
description | {String} | "" | The description of this symbol. |
examples | {Array} |
[]
| The examples for this symbol |
file | {String} | "" | The file where the symbol was found. |
fullname | {String} | "" | The fullname i.e ({memberof}.{name}) |
ignore | {Boolean} | false | Set to true if the symbol should be ignored and not put into coddoc.Tree |
ignoreCode | {Boolean} | false | Set to true if the code object from this symbol should be ignored. |
isConstant | {Boolean} | false | Set to true if this symbol is a constant. |
isConstructor | {Boolean} | false | Set to true is this symbol is a constructor |
isFunction | {Boolean} | false | Set to true if this symbol is a function. |
isPrivate | {Boolean} | false | Set to true if this symbol is private. |
isProtected | {Boolean} | false | Set to true if this symbol is protected. |
isStatic | {Boolean} | false | Set to true if this symbol is static |
memberof | {String} | "" | Who this symbol belongs to. |
name | {String} | "" | The name of this symbol |
params | {Array} |
[]
| The associated params for this symbol if it is a funciton. |
properties | {Array} |
[]
| The associated properties for this symbol |
returns | {Array} |
[]
| Array of return types for this symbol |
see | {Array} |
[]
| Any link for this symbol |
tags | {Array} |
[]
| The associated tags for this symbol |
throws | {Array} |
[]
| Exceptions thrown by this symbol |
type | {*} | null | The type that is symbol represents. |
Constructor
Defined symbol.js Top
Arguments
- options : an object or symbol whos properties will be added to this symbol. Note a deep copy of paraemeters will not be made, so if you pass an array the array will not be cloned.
Source
{ thistags = ; thisparams = ; thisproperties = ; thisexamples = ; thisborrows = ; thisaugments = ; thisincludedDocs = ; thissee = ; thisthrows = ; thisreturns = ; options = options || {}; for var i in options if i in this thisi = optionsi; }
coddoc.Tree
A Tree object which contains symbols.
Instance
Constructor
Defined tree.js Top
Source
{ thissymbols = global:; }
_addSymbol
function private
Defined tree.js Top
Adds a symbol to this tree.
Arguments
- name : the name of the symbol to add.
Returns
Array
Source
{ var ret = thissymbolsname; if !ret ret = thissymbolsname = ; return ret; }
addSymbol
function public
Defined tree.js Top
Entry point to add the symbol
Arguments
-
symbol :
-
path
String
: the path of the symbol. i.e the memberof property of a symbol -
name :
-
obj :
Source
{ var nameParts = utils; var path = namePartsmemberof name = namePartsname; if path === "global" path = name; var sym = this; sym; }
getClasses
function public
Defined tree.js Top
Returns all classes in the tree. The following properties are added to each class symbol.
- staticMethods : all static methods for the class
- staticProperties : all static properties for the class
- instanceMethods : all isntance methods for the class
- instanceProperties : all instance properties for a class
Returns
Array
Source
{ var symbols = thissymbols objects = keys = Object; keys; return objects; }
getMembers
function public
Defined tree.js Top
Gets all members( coddoc.Symbol) for a particular path.
Arguments
- path : the path to look up.
Returns
Array
and array of symbols.
Source
{ var symbols = thissymbols namespaces = keys = Object; keys; return namespaces; }
getNamespaces
function public
Defined tree.js Top
Returns all namespaces in this tree. This method also adds the following values to the namespace.
- properties : all properties that belong to the namespace
- methods : all methods that belong to the namespace
Returns
Array
array of namespaces
Source
{ var symbols = thissymbols namespaces = keys = Object; keys; return namespaces; }
getSymbol
function public
Defined tree.js Top
Returns a symbol from this tree. The Tree will create the symbol if it does not exist.
Arguments
- name : the name of the symbol to get
Returns
Array
the array for the symbol.
Source
{ return this; }
hasSymbol
function public
Defined tree.js Top
Returns true if this tree contains a symbol.
Arguments
- name : the name to test if the tree contains the symbol
Returns
Boolean
true if the tree contains the symbol.
Source
{ var parts = name; return !!thissymbolsname; }
License
MIT LICENSE
Meta
Code: git clone git://github.com/pollenware/coddoc.git