From 9d4d228bb66ed086fa4b05ca1d9d93537b8e016a Mon Sep 17 00:00:00 2001 From: Robert Jefe Lindstaedt Date: Wed, 25 May 2016 16:20:47 +0200 Subject: [PATCH 01/17] doc, tools: extract linkManPages --- tools/doc/html.js | 21 ++------------------- tools/doc/lib/linkManPages.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 tools/doc/lib/linkManPages.js diff --git a/tools/doc/html.js b/tools/doc/html.js index ef7d78d5b70ab3..5ac032ec38ecd8 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -7,6 +7,8 @@ const path = require('path'); const preprocess = require('./preprocess.js'); const typeParser = require('./type-parser.js'); +const linkManPages = require('./lib/linkManPages') + module.exports = toHTML; // customized heading without id attribute @@ -216,25 +218,6 @@ function parseYAML(text) { return html.join('\n'); } -// Syscalls which appear in the docs, but which only exist in BSD / OSX -var BSD_ONLY_SYSCALLS = new Set(['lchmod']); - -// Handle references to man pages, eg "open(2)" or "lchmod(2)" -// Returns modified text, with such refs replace with HTML links, for example -// 'open(2)' -function linkManPages(text) { - return text.replace(/ ([a-z]+)\((\d)\)/gm, function(match, name, number) { - // name consists of lowercase letters, number is a single digit - var displayAs = name + '(' + number + ')'; - if (BSD_ONLY_SYSCALLS.has(name)) { - return ' ' + displayAs + ''; - } else { - return ' ' + displayAs + ''; - } - }); -} function linkJsTypeDocs(text) { var parts = text.split('`'); diff --git a/tools/doc/lib/linkManPages.js b/tools/doc/lib/linkManPages.js new file mode 100644 index 00000000000000..00052ed43a2cde --- /dev/null +++ b/tools/doc/lib/linkManPages.js @@ -0,0 +1,19 @@ +// Syscalls which appear in the docs, but which only exist in BSD / OSX +var BSD_ONLY_SYSCALLS = new Set(['lchmod']); + +// Handle references to man pages, eg "open(2)" or "lchmod(2)" +// Returns modified text, with such refs replace with HTML links, for example +// 'open(2)' +module.exports = function linkManPages(text) { + return text.replace(/ ([a-z]+)\((\d)\)/gm, function(match, name, number) { + // name consists of lowercase letters, number is a single digit + var displayAs = name + '(' + number + ')'; + if (BSD_ONLY_SYSCALLS.has(name)) { + return ' ' + displayAs + ''; + } else { + return ' ' + displayAs + ''; + } + }); +} From 56ac6af59e8c76d9c79c3cd4c4d96d0c987960c5 Mon Sep 17 00:00:00 2001 From: Robert Jefe Lindstaedt Date: Wed, 25 May 2016 16:22:22 +0200 Subject: [PATCH 02/17] doc: extract parseText --- tools/doc/html.js | 11 +---------- tools/doc/lib/parseText.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 tools/doc/lib/parseText.js diff --git a/tools/doc/html.js b/tools/doc/html.js index 5ac032ec38ecd8..589ffb704e549e 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -8,6 +8,7 @@ const preprocess = require('./preprocess.js'); const typeParser = require('./type-parser.js'); const linkManPages = require('./lib/linkManPages') +const parseText = require('./lib/parseText') module.exports = toHTML; @@ -131,16 +132,6 @@ function render(lexed, filename, template, nodeVersion, cb) { }); } -// handle general body-text replacements -// for example, link man page references to the actual page -function parseText(lexed) { - lexed.forEach(function(tok) { - if (tok.text && tok.type !== 'code') { - tok.text = linkManPages(tok.text); - tok.text = linkJsTypeDocs(tok.text); - } - }); -} // just update the list item text in-place. // lists that come right after a heading are what we're after. diff --git a/tools/doc/lib/parseText.js b/tools/doc/lib/parseText.js new file mode 100644 index 00000000000000..e6d754a11fea8c --- /dev/null +++ b/tools/doc/lib/parseText.js @@ -0,0 +1,10 @@ +// handle general body-text replacements +// for example, link man page references to the actual page +module.exports = function parseText(lexed) { + lexed.forEach(function(tok) { + if (tok.text && tok.type !== 'code') { + tok.text = linkManPages(tok.text); + tok.text = linkJsTypeDocs(tok.text); + } + }); +} From eccdbfc4db7871998613d175cbe114328c7647c8 Mon Sep 17 00:00:00 2001 From: Robert Jefe Lindstaedt Date: Wed, 25 May 2016 16:23:55 +0200 Subject: [PATCH 03/17] doc, tools: extract toHTML --- tools/doc/html.js | 35 +---------------------------------- tools/doc/lib/toHTML.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 34 deletions(-) create mode 100644 tools/doc/lib/toHTML.js diff --git a/tools/doc/html.js b/tools/doc/html.js index 589ffb704e549e..64f0637238d9d0 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -9,6 +9,7 @@ const typeParser = require('./type-parser.js'); const linkManPages = require('./lib/linkManPages') const parseText = require('./lib/parseText') +const toHTML = require('./lib/toHTML') module.exports = toHTML; @@ -33,40 +34,6 @@ var gtocPath = path.resolve(path.join( var gtocLoading = null; var gtocData = null; -function toHTML(input, filename, template, nodeVersion, cb) { - if (typeof nodeVersion === 'function') { - cb = nodeVersion; - nodeVersion = null; - } - nodeVersion = nodeVersion || process.version; - - if (gtocData) { - return onGtocLoaded(); - } - - if (gtocLoading === null) { - gtocLoading = [onGtocLoaded]; - return loadGtoc(function(err, data) { - if (err) throw err; - gtocData = data; - gtocLoading.forEach(function(xs) { - xs(); - }); - }); - } - - if (gtocLoading) { - return gtocLoading.push(onGtocLoaded); - } - - function onGtocLoaded() { - var lexed = marked.lexer(input); - fs.readFile(template, 'utf8', function(er, template) { - if (er) return cb(er); - render(lexed, filename, template, nodeVersion, cb); - }); - } -} function loadGtoc(cb) { fs.readFile(gtocPath, 'utf8', function(err, data) { diff --git a/tools/doc/lib/toHTML.js b/tools/doc/lib/toHTML.js new file mode 100644 index 00000000000000..f5c5aabf959f1e --- /dev/null +++ b/tools/doc/lib/toHTML.js @@ -0,0 +1,34 @@ +module.exports = function toHTML(input, filename, template, nodeVersion, cb) { + if (typeof nodeVersion === 'function') { + cb = nodeVersion; + nodeVersion = null; + } + nodeVersion = nodeVersion || process.version; + + if (gtocData) { + return onGtocLoaded(); + } + + if (gtocLoading === null) { + gtocLoading = [onGtocLoaded]; + return loadGtoc(function(err, data) { + if (err) throw err; + gtocData = data; + gtocLoading.forEach(function(xs) { + xs(); + }); + }); + } + + if (gtocLoading) { + return gtocLoading.push(onGtocLoaded); + } + + function onGtocLoaded() { + var lexed = marked.lexer(input); + fs.readFile(template, 'utf8', function(er, template) { + if (er) return cb(er); + render(lexed, filename, template, nodeVersion, cb); + }); + } +} From fe25e1a3d130766cb6c2775fc153a767849ec510 Mon Sep 17 00:00:00 2001 From: Robert Jefe Lindstaedt Date: Wed, 25 May 2016 16:26:40 +0200 Subject: [PATCH 04/17] doc, tools: extract loadGtoc --- tools/doc/html.js | 28 +--------------------------- tools/doc/lib/loadGtoc.js | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 tools/doc/lib/loadGtoc.js diff --git a/tools/doc/html.js b/tools/doc/html.js index 64f0637238d9d0..ec60954cfb6653 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -10,6 +10,7 @@ const typeParser = require('./type-parser.js'); const linkManPages = require('./lib/linkManPages') const parseText = require('./lib/parseText') const toHTML = require('./lib/toHTML') +const loadGtoc = require('./lib/loadGtoc') module.exports = toHTML; @@ -22,33 +23,6 @@ marked.setOptions({ renderer: renderer }); -// TODO(chrisdickinson): never stop vomitting / fix this. -var gtocPath = path.resolve(path.join( - __dirname, - '..', - '..', - 'doc', - 'api', - '_toc.md' -)); -var gtocLoading = null; -var gtocData = null; - - -function loadGtoc(cb) { - fs.readFile(gtocPath, 'utf8', function(err, data) { - if (err) return cb(err); - - preprocess(gtocPath, data, function(err, data) { - if (err) return cb(err); - - data = marked(data).replace(/ Date: Wed, 25 May 2016 16:28:05 +0200 Subject: [PATCH 05/17] doc, tools: extract toID --- tools/doc/html.js | 7 +------ tools/doc/lib/toID.js | 6 ++++++ 2 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 tools/doc/lib/toID.js diff --git a/tools/doc/html.js b/tools/doc/html.js index ec60954cfb6653..51dd7f1628244f 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -11,6 +11,7 @@ const linkManPages = require('./lib/linkManPages') const parseText = require('./lib/parseText') const toHTML = require('./lib/toHTML') const loadGtoc = require('./lib/loadGtoc') +const toID = require('./lib/toID') module.exports = toHTML; @@ -24,12 +25,6 @@ marked.setOptions({ }); -function toID(filename) { - return filename - .replace('.html', '') - .replace(/[^\w\-]/g, '-') - .replace(/-+/g, '-'); -} function render(lexed, filename, template, nodeVersion, cb) { if (typeof nodeVersion === 'function') { diff --git a/tools/doc/lib/toID.js b/tools/doc/lib/toID.js new file mode 100644 index 00000000000000..408c625079e24e --- /dev/null +++ b/tools/doc/lib/toID.js @@ -0,0 +1,6 @@ +module.exports = function toID(filename) { + return filename + .replace('.html', '') + .replace(/[^\w\-]/g, '-') + .replace(/-+/g, '-'); +} From db06307421bf24d35b111bae827662578b054181 Mon Sep 17 00:00:00 2001 From: Robert Jefe Lindstaedt Date: Wed, 25 May 2016 16:29:14 +0200 Subject: [PATCH 06/17] doc, tools: extract render --- tools/doc/html.js | 45 +---------------------------------------- tools/doc/lib/render.js | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 44 deletions(-) create mode 100644 tools/doc/lib/render.js diff --git a/tools/doc/html.js b/tools/doc/html.js index 51dd7f1628244f..42a7f87fa94817 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -12,6 +12,7 @@ const parseText = require('./lib/parseText') const toHTML = require('./lib/toHTML') const loadGtoc = require('./lib/loadGtoc') const toID = require('./lib/toID') +const render = require('./lib/render') module.exports = toHTML; @@ -25,50 +26,6 @@ marked.setOptions({ }); - -function render(lexed, filename, template, nodeVersion, cb) { - if (typeof nodeVersion === 'function') { - cb = nodeVersion; - nodeVersion = null; - } - - nodeVersion = nodeVersion || process.version; - - // get the section - var section = getSection(lexed); - - filename = path.basename(filename, '.md'); - - parseText(lexed); - 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); - - var id = toID(path.basename(filename)); - - template = template.replace(/__ID__/g, id); - template = template.replace(/__FILENAME__/g, filename); - template = template.replace(/__SECTION__/g, section); - template = template.replace(/__VERSION__/g, nodeVersion); - template = template.replace(/__TOC__/g, toc); - template = template.replace( - /__GTOC__/g, - gtocData.replace('class="nav-' + id, 'class="nav-' + id + ' active') - ); - - // content has to be the last thing we do with - // the lexed tokens, because it's destructive. - const 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) { diff --git a/tools/doc/lib/render.js b/tools/doc/lib/render.js new file mode 100644 index 00000000000000..ad2bb452fadde0 --- /dev/null +++ b/tools/doc/lib/render.js @@ -0,0 +1,41 @@ +module.exports = function render(lexed, filename, template, nodeVersion, cb) { + if (typeof nodeVersion === 'function') { + cb = nodeVersion; + nodeVersion = null; + } + + nodeVersion = nodeVersion || process.version; + + // get the section + var section = getSection(lexed); + + filename = path.basename(filename, '.md'); + + parseText(lexed); + 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); + + var id = toID(path.basename(filename)); + + template = template.replace(/__ID__/g, id); + template = template.replace(/__FILENAME__/g, filename); + template = template.replace(/__SECTION__/g, section); + template = template.replace(/__VERSION__/g, nodeVersion); + template = template.replace(/__TOC__/g, toc); + template = template.replace( + /__GTOC__/g, + gtocData.replace('class="nav-' + id, 'class="nav-' + id + ' active') + ); + + // content has to be the last thing we do with + // the lexed tokens, because it's destructive. + const content = marked.parser(lexed); + template = template.replace(/__CONTENT__/g, content); + + cb(null, template); + }); +} From 22ce257aa814f6dc4f63a775028565fe94089523 Mon Sep 17 00:00:00 2001 From: Robert Jefe Lindstaedt Date: Wed, 25 May 2016 16:30:33 +0200 Subject: [PATCH 07/17] doc, tools: extract parseLists --- tools/doc/html.js | 61 +------------------------------------ tools/doc/lib/parseLists.js | 60 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 60 deletions(-) create mode 100644 tools/doc/lib/parseLists.js diff --git a/tools/doc/html.js b/tools/doc/html.js index 42a7f87fa94817..65a51d1195de80 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -13,6 +13,7 @@ const toHTML = require('./lib/toHTML') const loadGtoc = require('./lib/loadGtoc') const toID = require('./lib/toID') const render = require('./lib/render') +const parseLists = require('./lib/parseLists') module.exports = toHTML; @@ -26,66 +27,6 @@ marked.setOptions({ }); -// 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 (tok.type === 'code' && tok.text.match(/Stability:.*/g)) { - tok.text = parseAPIHeader(tok.text); - output.push({ type: 'html', text: tok.text }); - return; - } - if (state === null || - (state === 'AFTERHEADING' && tok.type === 'heading')) { - 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: '
' }); - } - depth++; - output.push(tok); - return; - } - if (tok.type === 'html' && common.isYAMLBlock(tok.text)) { - tok.text = parseYAML(tok.text); - } - state = null; - output.push(tok); - return; - } - if (state === 'LIST') { - if (tok.type === 'list_start') { - depth++; - output.push(tok); - return; - } - if (tok.type === 'list_end') { - depth--; - output.push(tok); - if (depth === 0) { - state = null; - output.push({ type: 'html', text: '
' }); - } - return; - } - } - output.push(tok); - }); - - return output; -} - function parseYAML(text) { const meta = common.extractAndParseYAML(text); const html = ['