|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -var sort = require('./lib/sort'), |
| 3 | +var fs = require('fs'), |
| 4 | + sort = require('./lib/sort'), |
4 | 5 | nest = require('./lib/nest'),
|
5 | 6 | filterAccess = require('./lib/filter_access'),
|
6 | 7 | filterJS = require('./lib/filter_js'),
|
@@ -81,50 +82,86 @@ function expandInputs(indexes, options, callback) {
|
81 | 82 | */
|
82 | 83 | module.exports.build = function (indexes, options, callback) {
|
83 | 84 | options = options || {};
|
84 |
| - options.hljs = options.hljs || {}; |
85 | 85 |
|
86 | 86 | if (typeof indexes === 'string') {
|
87 | 87 | indexes = [indexes];
|
88 | 88 | }
|
89 | 89 |
|
90 |
| - if (!options.access) { |
91 |
| - options.access = ['public', 'undefined', 'protected']; |
92 |
| - } |
93 |
| - |
94 |
| - var parseFn = (options.polyglot) ? polyglot : parseJavaScript; |
95 |
| - |
96 | 90 | return expandInputs(indexes, options, function (error, inputs) {
|
97 | 91 | if (error) {
|
98 | 92 | return callback(error);
|
99 | 93 | }
|
100 | 94 | try {
|
101 |
| - callback(null, |
102 |
| - filterAccess(options.access, |
103 |
| - hierarchy( |
104 |
| - sort( |
105 |
| - inputs |
106 |
| - .filter(filterJS(options.extension, options.polyglot)) |
107 |
| - .reduce(function (memo, file) { |
108 |
| - return memo.concat(parseFn(file)); |
109 |
| - }, []) |
110 |
| - .map(pipeline( |
111 |
| - inferName(), |
112 |
| - inferAugments(), |
113 |
| - inferKind(), |
114 |
| - inferParams(), |
115 |
| - inferProperties(), |
116 |
| - inferReturn(), |
117 |
| - inferMembership(), |
118 |
| - nest, |
119 |
| - options.github && github |
120 |
| - )) |
121 |
| - .filter(Boolean), options)))); |
| 95 | + callback(null, buildSync(inputs, options)); |
122 | 96 | } catch (e) {
|
123 | 97 | callback(e);
|
124 | 98 | }
|
125 | 99 | });
|
126 | 100 | };
|
127 | 101 |
|
| 102 | +/** |
| 103 | + * Generate JavaScript documentation given a list of inputs. This internal |
| 104 | + * method does not support require-following and it returns its results |
| 105 | + * synchronously, rather than by calling a callback. |
| 106 | + * |
| 107 | + * @param {Array<string>} indexes files to process |
| 108 | + * @param {Object} options options |
| 109 | + * @param {Array<string>} options.external a string regex / glob match pattern |
| 110 | + * that defines what external modules will be whitelisted and included in the |
| 111 | + * generated documentation. |
| 112 | + * @param {boolean} [options.polyglot=false] parse comments with a regex rather than |
| 113 | + * a proper parser. This enables support of non-JavaScript languages but |
| 114 | + * reduces documentation's ability to infer structure of code. |
| 115 | + * @param {boolean} [options.shallow=false] whether to avoid dependency parsing |
| 116 | + * even in JavaScript code. With the polyglot option set, this has no effect. |
| 117 | + * @param {Array<string|Object>} [options.order=[]] optional array that |
| 118 | + * defines sorting order of documentation |
| 119 | + * @param {Array<string>} [options.access=[]] an array of access levels |
| 120 | + * to output in documentation |
| 121 | + * @param {Object} [options.hljs] hljs optional options |
| 122 | + * @param {boolean} [options.hljs.highlightAuto=false] hljs automatically detect language |
| 123 | + * @param {Array} [options.hljs.languages] languages for hljs to choose from |
| 124 | + * @returns {Object} list of results |
| 125 | + * @public |
| 126 | + */ |
| 127 | +function buildSync(indexes, options) { |
| 128 | + options = options || {}; |
| 129 | + options.hljs = options.hljs || {}; |
| 130 | + |
| 131 | + if (!options.access) { |
| 132 | + options.access = ['public', 'undefined', 'protected']; |
| 133 | + } |
| 134 | + |
| 135 | + var parseFn = (options.polyglot) ? polyglot : parseJavaScript; |
| 136 | + |
| 137 | + return filterAccess(options.access, |
| 138 | + hierarchy( |
| 139 | + sort(indexes.map(function (index) { |
| 140 | + if (typeof index === 'string') { |
| 141 | + return { |
| 142 | + source: fs.readFileSync(index, 'utf8'), |
| 143 | + file: index |
| 144 | + }; |
| 145 | + } |
| 146 | + return index; |
| 147 | + }).filter(filterJS(options.extension, options.polyglot)) |
| 148 | + .reduce(function (memo, file) { |
| 149 | + return memo.concat(parseFn(file)); |
| 150 | + }, []) |
| 151 | + .map(pipeline( |
| 152 | + inferName(), |
| 153 | + inferAugments(), |
| 154 | + inferKind(), |
| 155 | + inferParams(), |
| 156 | + inferProperties(), |
| 157 | + inferReturn(), |
| 158 | + inferMembership(), |
| 159 | + nest, |
| 160 | + options.github && github |
| 161 | + )) |
| 162 | + .filter(Boolean), options))); |
| 163 | +} |
| 164 | + |
128 | 165 | /**
|
129 | 166 | * Lint files for non-standard or incorrect documentation
|
130 | 167 | * information, returning a potentially-empty string
|
@@ -180,6 +217,7 @@ module.exports.lint = function lint(indexes, options, callback) {
|
180 | 217 | };
|
181 | 218 |
|
182 | 219 | module.exports.expandInputs = expandInputs;
|
| 220 | +module.exports.buildSync = buildSync; |
183 | 221 |
|
184 | 222 | module.exports.formats = {
|
185 | 223 | html: require('./lib/output/html'),
|
|
0 commit comments