+
+
+
diff --git a/tools/doc/LICENSE b/tools/doc/LICENSE
new file mode 100644
index 000000000..e3d4e695a
--- /dev/null
+++ b/tools/doc/LICENSE
@@ -0,0 +1,18 @@
+Copyright Joyent, Inc. and other Node contributors. All rights reserved.
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
diff --git a/tools/doc/README.md b/tools/doc/README.md
new file mode 100644
index 000000000..743bfaae1
--- /dev/null
+++ b/tools/doc/README.md
@@ -0,0 +1,76 @@
+Here's how the node docs work.
+
+Each type of heading has a description block.
+
+
+ ## module
+
+ Stability: 3 - Stable
+
+ description and examples.
+
+ ### module.property
+
+ * Type
+
+ description of the property.
+
+ ### module.someFunction(x, y, [z=100])
+
+ * `x` {String} the description of the string
+ * `y` {Boolean} Should I stay or should I go?
+ * `z` {Number} How many zebras to bring.
+
+ A description of the function.
+
+ ### Event: 'blerg'
+
+ * Argument: SomeClass object.
+
+ Modules don't usually raise events on themselves. `cluster` is the
+ only exception.
+
+ ## Class: SomeClass
+
+ description of the class.
+
+ ### Class Method: SomeClass.classMethod(anArg)
+
+ * `anArg` {Object} Just an argument
+ * `field` {String} anArg can have this field.
+ * `field2` {Boolean} Another field. Default: `false`.
+ * Return: {Boolean} `true` if it worked.
+
+ Description of the method for humans.
+
+ ### someClass.nextSibling()
+
+ * Return: {SomeClass object | null} The next someClass in line.
+
+ ### someClass.someProperty
+
+ * String
+
+ The indication of what someProperty is.
+
+ ### Event: 'grelb'
+
+ * `isBlerg` {Boolean}
+
+ This event is emitted on instances of SomeClass, not on the module itself.
+
+
+* Modules have (description, Properties, Functions, Classes, Examples)
+* Properties have (type, description)
+* Functions have (list of arguments, description)
+* Classes have (description, Properties, Methods, Events)
+* Events have (list of arguments, description)
+* Methods have (list of arguments, description)
+* Properties have (type, description)
+
+# CLI usage
+
+Run the following from the etherpad-lite root directory:
+```sh
+$ node tools/doc/generate doc/all.md --format=html --template=doc/template.html > out.htm
+```
\ No newline at end of file
diff --git a/tools/doc/generate.js b/tools/doc/generate.js
new file mode 100644
index 000000000..8d52e101e
--- /dev/null
+++ b/tools/doc/generate.js
@@ -0,0 +1,120 @@
+#!/usr/bin/env node
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var marked = require('marked');
+var fs = require('fs');
+var path = require('path');
+
+// parse the args.
+// Don't use nopt or whatever for this. It's simple enough.
+
+var args = process.argv.slice(2);
+var format = 'json';
+var template = null;
+var inputFile = null;
+
+args.forEach(function (arg) {
+ if (!arg.match(/^\-\-/)) {
+ inputFile = arg;
+ } else if (arg.match(/^\-\-format=/)) {
+ format = arg.replace(/^\-\-format=/, '');
+ } else if (arg.match(/^\-\-template=/)) {
+ template = arg.replace(/^\-\-template=/, '');
+ }
+})
+
+
+if (!inputFile) {
+ throw new Error('No input file specified');
+}
+
+
+console.error('Input file = %s', inputFile);
+fs.readFile(inputFile, 'utf8', function(er, input) {
+ if (er) throw er;
+ // process the input for @include lines
+ processIncludes(inputFile, input, next);
+});
+
+
+var includeExpr = /^@include\s+([A-Za-z0-9-_\/]+)(?:\.)?([a-zA-Z]*)$/gmi;
+var includeData = {};
+function processIncludes(inputFile, input, cb) {
+ var includes = input.match(includeExpr);
+ if (includes === null) return cb(null, input);
+ var errState = null;
+ console.error(includes);
+ var incCount = includes.length;
+ if (incCount === 0) cb(null, input);
+
+ includes.forEach(function(include) {
+ var fname = include.replace(/^@include\s+/, '');
+ if (!fname.match(/\.md$/)) fname += '.md';
+
+ if (includeData.hasOwnProperty(fname)) {
+ input = input.split(include).join(includeData[fname]);
+ incCount--;
+ if (incCount === 0) {
+ return cb(null, input);
+ }
+ }
+
+ var fullFname = path.resolve(path.dirname(inputFile), fname);
+ fs.readFile(fullFname, 'utf8', function(er, inc) {
+ if (errState) return;
+ if (er) return cb(errState = er);
+ processIncludes(fullFname, inc, function(er, inc) {
+ if (errState) return;
+ if (er) return cb(errState = er);
+ incCount--;
+ includeData[fname] = inc;
+ input = input.split(include).join(includeData[fname]);
+ if (incCount === 0) {
+ return cb(null, input);
+ }
+ });
+ });
+ });
+}
+
+
+function next(er, input) {
+ if (er) throw er;
+ switch (format) {
+ case 'json':
+ require('./json.js')(input, inputFile, function(er, obj) {
+ console.log(JSON.stringify(obj, null, 2));
+ if (er) throw er;
+ });
+ break;
+
+ case 'html':
+ require('./html.js')(input, inputFile, template, function(er, html) {
+ if (er) throw er;
+ console.log(html);
+ });
+ break;
+
+ default:
+ throw new Error('Invalid format: ' + format);
+ }
+}
diff --git a/tools/doc/html.js b/tools/doc/html.js
new file mode 100644
index 000000000..c52fff70a
--- /dev/null
+++ b/tools/doc/html.js
@@ -0,0 +1,174 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var fs = require('fs');
+var marked = require('marked');
+var path = require('path');
+
+module.exports = toHTML;
+
+function toHTML(input, filename, template, cb) {
+ var lexed = marked.lexer(input);
+ fs.readFile(template, 'utf8', function(er, template) {
+ if (er) return cb(er);
+ render(lexed, filename, template, cb);
+ });
+}
+
+function render(lexed, filename, template, cb) {
+ // get the section
+ var section = getSection(lexed);
+
+ filename = path.basename(filename, '.md');
+
+ lexed = parseLists(lexed);
+
+ // generate the table of contents.
+ // this mutates the lexed contents in-place.
+ buildToc(lexed, filename, function(er, toc) {
+ if (er) return cb(er);
+
+ template = template.replace(/__FILENAME__/g, filename);
+ template = template.replace(/__SECTION__/g, section);
+ template = template.replace(/__VERSION__/g, process.version);
+ template = template.replace(/__TOC__/g, toc);
+
+ // content has to be the last thing we do with
+ // the lexed tokens, because it's destructive.
+ content = marked.parser(lexed);
+ template = template.replace(/__CONTENT__/g, content);
+
+ cb(null, template);
+ });
+}
+
+
+// just update the list item text in-place.
+// lists that come right after a heading are what we're after.
+function parseLists(input) {
+ var state = null;
+ var depth = 0;
+ var output = [];
+ output.links = input.links;
+ input.forEach(function(tok) {
+ if (state === null) {
+ if (tok.type === 'heading') {
+ state = 'AFTERHEADING';
+ }
+ output.push(tok);
+ return;
+ }
+ if (state === 'AFTERHEADING') {
+ if (tok.type === 'list_start') {
+ state = 'LIST';
+ if (depth === 0) {
+ output.push({ type:'html', text: '