Module: pyjsdoc
Python library & command-line tool for documentation and dependency analysis of JavaScript files.
Some of the features this offers:
List all dependencies of a file or files:
>>> CodeBaseDoc(['examples'])['subclass.js'].module.all_dependencies ['module.js', 'module_closure.js', 'class.js', 'subclass.js']
Programmatically access properties of the documentation:
>>> CodeBaseDoc(['examples'])['subclass.js']['public_method'].doc 'A public method.' >>> CodeBaseDoc(['examples'])['subclass.js']['public_method'].is_private False
Generate documentation for a set of files:
>>> CodeBaseDoc(['examples']).save_docs(None, 'js_apidocs')
Tag reference is similar to JSDoc: http://jsdoc.sourceforge.net/#tagref. See usage() for command line options.
Classes
ClassDoc
Documentation for a single class.
CodeBaseDoc
Represents the documentation for an entire codebase.
CommentDoc
Base class for all classes that represent a parsed comment of some sort.
CyclicDependency
Exception raised if there is a cyclic dependency.
FileDoc
Represents documentaion for an entire file. The constructor takes the source text for file, parses it, then provides a class wrapper around the parsed text.
FunctionDoc
Documentation for a single function or method. Takes a parsed comment and provides accessors for accessing the various fields.
MissingDependency
Exception raised if a file references a dependency that doesn't exist.
ModuleDoc
Represents the top-level fileoverview documentation.
ParamDoc
Represents a parameter, option, or parameter-like object, basically anything that has a name, a type, and a description, separated by spaces. This is also used for return types and exceptions, which use an empty string for the name.
Functions
any (iter)
For < Python2.5 compatibility.
build_dependency_graph (start_nodes, js_doc)
Build a graph where nodes are filenames and edges are reverse dependencies (so an edge from jquery.js to jquery.dimensions.js indicates that jquery.js must be included before jquery.dimensions.js). The graph is represented as a dictionary from filename to (in-degree, edges) pair, for ease of topological sorting. Also returns a list of nodes of degree zero.
build_html_page (title, body)
Build the simple tag skeleton for a title and body.
encode_json (val)
find_dependencies (start_nodes, js_doc)
Sort the dependency graph, taking in a list of starting module names and a CodeBaseDoc (or equivalent dictionary). Returns an ordered list of transitive dependencies such that no module appears before its dependencies.
first_sentence (str)
Return the first sentence of a string - everything up to the period, or the whole text if there is no period.
>>> first_sentence('')
''
>>> first_sentence('Incomplete')
''
>>> first_sentence('The first sentence. This is ignored.')
'The first sentence.'
flatten (iter_of_iters)
Flatten an iterator of iterators into a single, long iterator, exhausting each subiterator in turn.
>>> flatten([[1, 2], [3, 4]]) [1, 2, 3, 4]
get_doc_comments (text)
Return a list of all documentation comments in the file text. Each comment is a pair, with the first element being the comment text and the second element being the line after it, which may be needed to guess function & arguments.
>>> get_doc_comments(read_file('examples/module.js'))[0][0][:40]
'/**\n * This is the module documentation.'
>>> get_doc_comments(read_file('examples/module.js'))[1][0][7:50]
'This is documentation for the first method.'
>>> get_doc_comments(read_file('examples/module.js'))[1][1]
'function the_first_function(arg1, arg2) '
>>> get_doc_comments(read_file('examples/module.js'))[2][0]
'/** This is the documentation for the second function. */'
get_file_list (paths)
Return a list of all JS files, given the root paths.
get_path_list (opts)
Return a list of all root paths where JS files can be found, given the command line options (in dict form) for this script.
guess_function_name (next_line, regexps=['function (\\w+)', '(\\w+):\\sfunction', '\\.(\\w+)\\s*=\\s*function'])
Attempt to determine the function name from the first code line following the comment. The patterns recognized are described by regexps, which defaults to FUNCTION_REGEXPS. If a match is successful, returns the function name. Otherwise, returns None.
guess_parameters (next_line)
Attempt to guess parameters based on the presence of a parenthesized group of identifiers. If successful, returns a list of parameter names; otherwise, returns None.
htmlize_paragraphs (text)
Convert paragraphs delimited by blank lines into HTML text enclosed in <p> tags.
is_js_file (filename)
Return true if the filename ends in .js and is not a packed or minified file (no '.pack' or '.min' in the filename)
>>> is_js_file('jquery.min.js')
False
>>> is_js_file('foo.json')
False
>>> is_js_file('ui.combobox.js')
True
list_js_files (dir)
Generator for all JavaScript files in the directory, recursively
>>> 'examples/module.js' in list(list_js_files('examples'))
True
main (args=['./manage.py', 'build_docs'])
Main command-line invocation.
make_index (css_class, entities)
Generate the HTML index (a short description and a link to the full documentation) for a list of FunctionDocs or ClassDocs.
parse_comment (doc_comment, next_line)
Split the raw comment text into a dictionary of tags. The main comment body is included as 'doc'.
>>> comment = get_doc_comments(read_file('examples/module.js'))[4][0]
>>> parse_comment(strip_stars(comment), '')['doc']
'This is the documentation for the fourth function.\n\n Since the function being documented is itself generated from another\n function, its name needs to be specified explicitly. using the @function tag'
>>> parse_comment(strip_stars(comment), '')['function']
'not_auto_discovered'
If there are multiple tags with the same name, they're included as a list:
>>> parse_comment(strip_stars(comment), '')['param']
['{String} arg1 The first argument.', '{Int} arg2 The second argument.']
parse_comments_for_file (filename)
Return a list of all parsed comments in a file. Mostly for testing & interactive use.
printable (id)
Turn a Python identifier into something fit for human consumption.
>>> printable('author')
'Author'
>>> printable('all_dependencies')
'All Dependencies'
read_file (path)
Open a file, reads it into a string, closes the file, and returns the file text.
run_and_exit_if (opts, action, *names)
Run the no-arg function action if any of names appears in the option dict opts.
run_doctests ()
save_file (path, text)
Save a string to a file. If the containing directory(ies) doesn't exist, this creates it.
split_delimited (delimiters, split_by, text)
Generator that walks the text and splits it into an array on split_by, being careful not to break inside a delimiter pair. delimiters should be an even-length string with each pair of matching delimiters listed together, open first.
>>> list(split_delimited('{}[]', ',', ''))
['']
>>> list(split_delimited('', ',', 'foo,bar'))
['foo', 'bar']
>>> list(split_delimited('[]', ',', 'foo,[bar, baz]'))
['foo', '[bar, baz]']
>>> list(split_delimited('{}', ' ', '{Type Name} name Desc'))
['{Type Name}', 'name', 'Desc']
>>> list(split_delimited('[]{}', ',', '[{foo,[bar, baz]}]'))
['[{foo,[bar, baz]}]']
Two adjacent delimiters result in a zero-length string between them:
>>> list(split_delimited('{}', ' ', '{Type Name} Desc'))
['{Type Name}', '', 'Desc']
split_by may be a predicate function instead of a string, in which case it should return true on a character to split.
>>> list(split_delimited('', lambda c: c in '[]{}, ', '[{foo,[bar, baz]}]'))
['', '', 'foo', '', 'bar', '', 'baz', '', '', '']
split_tag (section)
Split the JSDoc tag text (everything following the @) at the first whitespace. Returns a tuple of (tagname, body).
strip_stars (doc_comment)
Strip leading stars from a doc comment.
>>> strip_stars('/** This is a comment. */')
'This is a comment.'
>>> strip_stars('/**\n * This is a\n * multiline comment. */')
'This is a\n multiline comment.'
>>> strip_stars('/** \n\t * This is a\n\t * multiline comment. \n*/')
'This is a\n multiline comment.'
topological_sort (dependencies, start_nodes)
Perform a topological sort on the dependency graph dependencies, starting from list start_nodes.
trim_js_ext (filename)
If filename ends with .js, trims the extension off.
>>> trim_js_ext('foo.js')
'foo'
>>> trim_js_ext('something_else.html')
'something_else.html'
usage ()
warn (format, *args)
Print out a warning on STDERR.
Attributes
FUNCTION_REGEXPS
Value of FUNCTION_REGEXPS
['function (\\w+)', '(\\w+):\\sfunction', '\\.(\\w+)\\s*=\\s*function']