|
| 1 | +/*global require: true */ |
| 2 | +(function () { |
| 3 | + 'use strict'; |
| 4 | + |
| 5 | + // This file receives data from JSDoc via the `publish` exported function, |
| 6 | + // and converts it into JSON that is written to a file. |
| 7 | + |
| 8 | + var fs = require('jsdoc/fs'); |
| 9 | + var helper = require('jsdoc/util/templateHelper'); |
| 10 | + |
| 11 | + var _ = require("underscore"); |
| 12 | + var stringify = require("canonical-json"); |
| 13 | + |
| 14 | + // This is the big map of name -> data that we'll write to a file. |
| 15 | + var dataContents = {}; |
| 16 | + // List of just the names, which we'll also write to a file. |
| 17 | + var names = []; |
| 18 | + |
| 19 | + /** |
| 20 | + * Get a tag dictionary from the tags field on the object, for custom fields |
| 21 | + * like package |
| 22 | + * @param {JSDocData} data The thing you get in the TaffyDB from JSDoc |
| 23 | + * @return {Object} Keys are the parameter names, values are the values. |
| 24 | + */ |
| 25 | + var getTagDict = function (data) { |
| 26 | + var tagDict = {}; |
| 27 | + |
| 28 | + if (data.tags) { |
| 29 | + _.each(data.tags, function (tag) { |
| 30 | + tagDict[tag.title] = tag.value; |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + return tagDict; |
| 35 | + }; |
| 36 | + |
| 37 | + // Fix up a JSDoc entry and add it to `dataContents`. |
| 38 | + var addToData = function (entry) { |
| 39 | + _.extend(entry, getTagDict(entry)); |
| 40 | + |
| 41 | + // strip properties we don't want |
| 42 | + entry.comment = undefined; |
| 43 | + entry.___id = undefined; |
| 44 | + entry.___s = undefined; |
| 45 | + entry.tags = undefined; |
| 46 | + |
| 47 | + // generate `.filepath` and `.lineno` from `.meta` |
| 48 | + if (entry.meta && entry.meta.path) { |
| 49 | + var packagesFolder = 'packages/'; |
| 50 | + var index = entry.meta.path.indexOf(packagesFolder); |
| 51 | + if (index != -1) { |
| 52 | + var fullFilePath = entry.meta.path.substr(index + packagesFolder.length) + '/' + entry.meta.filename; |
| 53 | + entry.filepath = fullFilePath; |
| 54 | + entry.lineno = entry.meta.lineno; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + entry.meta = undefined; |
| 59 | + |
| 60 | + if (!entry.importfrompackage && entry.filepath) { |
| 61 | + entry.module = entry.filepath.split('/')[0]; |
| 62 | + } else { |
| 63 | + entry.module = entry.importfrompackage; |
| 64 | + } |
| 65 | + |
| 66 | + names.push(entry.longname); |
| 67 | + dataContents[entry.longname] = entry; |
| 68 | + }; |
| 69 | + |
| 70 | + /** |
| 71 | + Entry point where JSDoc calls us. It passes us data in the form of |
| 72 | + a TaffyDB object (which is an in-JS database of sorts that you can |
| 73 | + query for records. |
| 74 | +
|
| 75 | + @param {TAFFY} taffyData See <http://taffydb.com/>. |
| 76 | + @param {object} opts |
| 77 | + @param {Tutorial} tutorials |
| 78 | + */ |
| 79 | + exports.publish = function(taffyData) { |
| 80 | + var data = helper.prune(taffyData); |
| 81 | + |
| 82 | + var namespaces = helper.find(data, {kind: "namespace"}); |
| 83 | + |
| 84 | + // prepare all of the namespaces |
| 85 | + _.each(namespaces, function (namespace) { |
| 86 | + if (namespace.summary) { |
| 87 | + addToData(namespace); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + var properties = helper.find(data, {kind: "member"}); |
| 92 | + |
| 93 | + _.each(properties, function (property) { |
| 94 | + if (property.summary) { |
| 95 | + addToData(property); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + // Callback descriptions are going to be embeded into Function descriptions |
| 100 | + // when they are used as arguments, so we always attach them to reference |
| 101 | + // them later. |
| 102 | + var callbacks = helper.find(data, {kind: "typedef"}); |
| 103 | + _.each(callbacks, function (cb) { |
| 104 | + delete cb.comment; |
| 105 | + addToData(cb); |
| 106 | + }); |
| 107 | + |
| 108 | + var functions = helper.find(data, {kind: "function"}); |
| 109 | + var constructors = helper.find(data, {kind: "class"}); |
| 110 | + |
| 111 | + // we want to do all of the same transformations to classes and functions |
| 112 | + functions = functions.concat(constructors); |
| 113 | + |
| 114 | + // insert all of the function data into the namespaces |
| 115 | + _.each(functions, function (func) { |
| 116 | + if (! func.summary) { |
| 117 | + // we use the @summary tag to indicate that an item is documented |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + func.options = []; |
| 122 | + var filteredParams = []; |
| 123 | + |
| 124 | + // Starting a param with `options.` makes it an option, not a |
| 125 | + // param. Dot (`.`) in this case binds tighter than comma, so |
| 126 | + // `options.foo,bar` will create an option named `foo, bar` |
| 127 | + // (representing two options in the docs). We process pipes so |
| 128 | + // that `options.foo|bar` also results in `foo, bar`. |
| 129 | + _.each(func.params, function (param) { |
| 130 | + param.name = param.name.replace(/,|\|/g, ", "); |
| 131 | + |
| 132 | + var splitName = param.name.split("."); |
| 133 | + |
| 134 | + if (splitName.length < 2 || splitName[0] !== "options") { |
| 135 | + // not an option |
| 136 | + filteredParams.push(param); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + param.name = splitName[1]; |
| 141 | + |
| 142 | + func.options.push(param); |
| 143 | + }); |
| 144 | + |
| 145 | + func.params = filteredParams; |
| 146 | + |
| 147 | + // the entire unparsed doc comment. takes up too much room in the |
| 148 | + // data file. |
| 149 | + delete func.comment; |
| 150 | + |
| 151 | + addToData(func); |
| 152 | + }); |
| 153 | + |
| 154 | + // write full docs JSON |
| 155 | + var jsonString = stringify(dataContents, null, 2); |
| 156 | + var jsString = "module.exports = " + jsonString + ";"; |
| 157 | + jsString = "// This file is automatically generated by JSDoc; regenerate it with scripts/admin/jsdoc/jsdoc.sh\n" + jsString; |
| 158 | + var docsDataFilename = "../data/data.js"; |
| 159 | + fs.writeFileSync(docsDataFilename, jsString); |
| 160 | + |
| 161 | + // write name tree JSON |
| 162 | + jsonString = stringify(names.sort(), null, 2); |
| 163 | + var nameTreeFilename= "../data/names.json"; |
| 164 | + fs.writeFileSync(nameTreeFilename, jsonString); |
| 165 | + }; |
| 166 | +})(); |
0 commit comments