Browse Source

tools: parse documentation metadata

This commit introduces the Documentation YAML metadata concept to the
documentation.

- Parses added or Added for HTML
- Parses all data for JSON

Ref: https://github.com/nodejs/node/pull/3867
Ref: https://github.com/nodejs/node/issues/3713
Ref: https://github.com/nodejs/node/issues/6470
PR-URL: https://github.com/nodejs/node/pull/6495
Reviewed-By: Robert Jefe Lindstaedt <robert.lindstaedt@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
v6.x
Tristian Flanagan 9 years ago
committed by Evan Lucas
parent
commit
cea17775e0
  1. 13
      doc/api_assets/style.css
  2. 27
      tools/doc/README.md
  3. 21
      tools/doc/common.js
  4. 12
      tools/doc/generate.js
  5. 26
      tools/doc/html.js
  6. 8
      tools/doc/json.js

13
doc/api_assets/style.css

@ -108,6 +108,19 @@ em code {
background-color: #0084B6; background-color: #0084B6;
} }
.api_metadata {
font-size: .75em;
margin-bottom: 1em;
}
.api_metadata span {
margin-right: 1em;
}
.api_metadata span:last-child {
margin-right: 0px;
}
ul.plain { ul.plain {
list-style: none; list-style: none;
} }

27
tools/doc/README.md

@ -6,18 +6,27 @@ Each type of heading has a description block.
## module ## module
<!-- YAML
added: v0.10.0
-->
Stability: 3 - Stable Stability: 3 - Stable
description and examples. description and examples.
### module.property ### module.property
<!-- YAML
added: v0.10.0
-->
* Type * Type
description of the property. description of the property.
### module.someFunction(x, y, [z=100]) ### module.someFunction(x, y, [z=100])
<!-- YAML
added: v0.10.0
-->
* `x` {String} the description of the string * `x` {String} the description of the string
* `y` {Boolean} Should I stay or should I go? * `y` {Boolean} Should I stay or should I go?
@ -26,6 +35,9 @@ Each type of heading has a description block.
A description of the function. A description of the function.
### Event: 'blerg' ### Event: 'blerg'
<!-- YAML
added: v0.10.0
-->
* Argument: SomeClass object. * Argument: SomeClass object.
@ -33,10 +45,16 @@ Each type of heading has a description block.
only exception. only exception.
## Class: SomeClass ## Class: SomeClass
<!-- YAML
added: v0.10.0
-->
description of the class. description of the class.
### Class Method: SomeClass.classMethod(anArg) ### Class Method: SomeClass.classMethod(anArg)
<!-- YAML
added: v0.10.0
-->
* `anArg` {Object} Just an argument * `anArg` {Object} Just an argument
* `field` {String} anArg can have this field. * `field` {String} anArg can have this field.
@ -46,16 +64,25 @@ Each type of heading has a description block.
Description of the method for humans. Description of the method for humans.
### someClass.nextSibling() ### someClass.nextSibling()
<!-- YAML
added: v0.10.0
-->
* Return: {SomeClass object | null} The next someClass in line. * Return: {SomeClass object | null} The next someClass in line.
### someClass.someProperty ### someClass.someProperty
<!-- YAML
added: v0.10.0
-->
* String * String
The indication of what someProperty is. The indication of what someProperty is.
### Event: 'grelb' ### Event: 'grelb'
<!-- YAML
added: v0.10.0
-->
* `isBlerg` {Boolean} * `isBlerg` {Boolean}

21
tools/doc/common.js

@ -0,0 +1,21 @@
'use strict';
const yaml = require('js-yaml');
function isYAMLBlock(text) {
return !!text.match(/^<!-- YAML/);
}
exports.isYAMLBlock = isYAMLBlock;
function extractAndParseYAML(text) {
text = text.trim();
text = text.replace(/^<!-- YAML/, '')
.replace(/-->$/, '');
// js-yaml.safeLoad() throws on error
return yaml.safeLoad(text);
}
exports.extractAndParseYAML = extractAndParseYAML;

12
tools/doc/generate.js

@ -1,15 +1,15 @@
'use strict'; 'use strict';
var processIncludes = require('./preprocess.js'); const processIncludes = require('./preprocess.js');
var fs = require('fs'); const fs = require('fs');
// parse the args. // parse the args.
// Don't use nopt or whatever for this. It's simple enough. // Don't use nopt or whatever for this. It's simple enough.
var args = process.argv.slice(2); const args = process.argv.slice(2);
var format = 'json'; let format = 'json';
var template = null; let template = null;
var inputFile = null; let inputFile = null;
args.forEach(function(arg) { args.forEach(function(arg) {
if (!arg.match(/^\-\-/)) { if (!arg.match(/^\-\-/)) {

26
tools/doc/html.js

@ -1,10 +1,11 @@
'use strict'; 'use strict';
var fs = require('fs'); const common = require('./common.js');
var marked = require('marked'); const fs = require('fs');
var path = require('path'); const marked = require('marked');
var preprocess = require('./preprocess.js'); const path = require('path');
var typeParser = require('./type-parser.js'); const preprocess = require('./preprocess.js');
const typeParser = require('./type-parser.js');
module.exports = toHTML; module.exports = toHTML;
@ -148,6 +149,9 @@ function parseLists(input) {
output.push(tok); output.push(tok);
return; return;
} }
if (tok.type === 'html' && common.isYAMLBlock(tok.text)) {
tok.text = parseYAML(tok.text);
}
state = null; state = null;
output.push(tok); output.push(tok);
return; return;
@ -174,6 +178,18 @@ function parseLists(input) {
return output; return output;
} }
function parseYAML(text) {
const meta = common.extractAndParseYAML(text);
let html = '<div class="api_metadata">';
if (meta.added || meta.Added) {
meta.added = meta.added || meta.Added;
html += '<span>Added: ' + meta.added + '</span>';
}
return html + '</div>';
}
// Syscalls which appear in the docs, but which only exist in BSD / OSX // Syscalls which appear in the docs, but which only exist in BSD / OSX
var BSD_ONLY_SYSCALLS = new Set(['lchmod']); var BSD_ONLY_SYSCALLS = new Set(['lchmod']);

8
tools/doc/json.js

@ -5,7 +5,8 @@ module.exports = doJSON;
// Take the lexed input, and return a JSON-encoded object // Take the lexed input, and return a JSON-encoded object
// A module looks like this: https://gist.github.com/1777387 // A module looks like this: https://gist.github.com/1777387
var marked = require('marked'); const common = require('./common.js');
const marked = require('marked');
function doJSON(input, filename, cb) { function doJSON(input, filename, cb) {
var root = {source: filename}; var root = {source: filename};
@ -91,6 +92,8 @@ function doJSON(input, filename, cb) {
current.list = current.list || []; current.list = current.list || [];
current.list.push(tok); current.list.push(tok);
current.list.level = 1; current.list.level = 1;
} else if (type === 'html' && common.isYAMLBlock(tok.text)) {
current.meta = parseYAML(tok.text);
} else { } else {
current.desc = current.desc || []; current.desc = current.desc || [];
if (!Array.isArray(current.desc)) { if (!Array.isArray(current.desc)) {
@ -274,6 +277,9 @@ function processList(section) {
delete section.list; delete section.list;
} }
function parseYAML(text) {
return common.extractAndParseYAML(text);
}
// textRaw = "someobject.someMethod(a[, b=100][, c])" // textRaw = "someobject.someMethod(a[, b=100][, c])"
function parseSignature(text, sig) { function parseSignature(text, sig) {

Loading…
Cancel
Save