1/* 2 * Author: Heidi Pan <heidi.pan@intel.com> 3 * Copyright (c) 2015 Intel Corporation. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25// dependencies 26var _ = require('lodash'); 27 28 29// generate YuiDocs-style documentation 30function generateDocs(specjs) { 31 var docs = GENERATE_MODULE(specjs.MODULE, ''); 32 GENERATE_TYPE = (function(enums) { 33 return function(type) { 34 return (_.contains(enums, type) ? ('Enum ' + type) : type); 35 } 36 })(_.keys(specjs.ENUMS_BY_GROUP)); 37 docs = _.reduce(specjs.METHODS, function(memo, methodSpec, methodName) { 38 return memo += GENERATE_METHOD(methodName, methodSpec); 39 }, docs); 40 docs = _.reduce(specjs.ENUMS, function(memo, enumSpec, enumName) { 41 return memo += GENERATE_ENUM(enumName, enumSpec); 42 }, docs); 43 if (_.isEmpty(specjs.CLASSGROUPS)) { 44 docs += GENERATE_CLASSES(specjs.CLASSES); 45 } else { 46 docs += GENERATE_MODULE('common', ''); 47 var grouped = _.flatten(_.pluck(_.values(specjs.CLASSGROUPS), 'classes')); 48 var ungrouped = _.difference(_.keys(specjs.CLASSES), grouped); 49 docs += GENERATE_CLASSES(_.pick(specjs.CLASSES, ungrouped), 'common'); 50 _.each(specjs.CLASSGROUPS, function(groupSpec, groupName) { 51 docs += GENERATE_CLASSES(_.pick(specjs.CLASSES, groupSpec.classes), groupName); 52 }); 53 // TODO: figure out why yuidoc won't associate the class with the right module if module definitions are interspersed 54 _.each(specjs.CLASSGROUPS, function(groupSpec, groupName) { 55 docs += GENERATE_MODULE(groupName, groupSpec.description); 56 }); 57 } 58 return docs; 59} 60 61 62// comment wrapper around entire spec 63function GENERATE_DOC(text) { 64 return '/**\n' + text + ' */\n'; 65} 66 67 68// generate module spec 69function GENERATE_MODULE(name, description) { 70 return GENERATE_DOC(description + '\n' 71 + '@module ' + name + '\n'); 72} 73 74 75// generate spec for the given list of classes 76function GENERATE_CLASSES(classes, parent) { 77 return _.reduce(classes, function(memo, classSpec, className) { 78 return memo 79 + GENERATE_CLASS(className, classSpec.description, parent, classSpec.parent) 80 + _.reduce(classSpec.methods, function(memo, methodSpec, methodName) { 81 return memo += GENERATE_METHOD(methodName, methodSpec, className); 82 }, '') 83 + _.reduce(classSpec.variables, function(memo, variableSpec, variableName) { 84 return memo += GENERATE_VAR(variableName, variableSpec, className); 85 }, '') 86 + _.reduce(classSpec.enums, function(memo, enumSpec, enumName) { 87 return memo += GENERATE_ENUM(enumName, enumSpec, className); 88 }, ''); 89 }, ''); 90} 91 92 93// generate class spec 94function GENERATE_CLASS(name, description, namespace, parent) { 95 return GENERATE_DOC(description + '\n' 96 + '@class ' + name + '\n' 97 + (namespace ? ('@module ' + namespace + '\n') : '') 98 /* 99 TODO: leave out until figure out what swig does with inheritance 100 + (parent ? ('@extends ' + parent + '\n') : '') 101 */ 102 ); 103} 104 105 106// generate method spec with parent module/class 107function GENERATE_METHOD(name, spec, parent) { 108 name = name.replace(/!+$/, ''); 109 return GENERATE_DOC(spec.description + '\n' 110 + '@method ' + name + '\n' 111 + (parent ? ('@for ' + parent + '\n') : '@for common\n') 112 + _.reduce(spec.params, function(memo, paramSpec, paramName) { 113 return memo + '@param {' + GENERATE_TYPE(paramSpec.type) + '} ' + paramName + ' ' + paramSpec.description + '\n'; 114 }, '') 115 + ( !_.isEmpty(spec.return) ? ('@return {' + GENERATE_TYPE(spec.return.type) + '} ' + spec.return.description + '\n') : '')); 116} 117 118 119// generate enum spec 120function GENERATE_ENUM(name, spec, parent) { 121 return GENERATE_DOC(spec.description + '\n' 122 + '@property ' + name + '\n' 123 + '@type Enum ' + spec.type + '\n' 124 + '@for ' + (parent ? parent : 'common') + '\n'); 125} 126 127 128// generate variable specs 129function GENERATE_VAR(name, spec, parent) { 130 return GENERATE_DOC(spec.description + '\n' 131 + '@property ' + name + '\n' 132 + '@type ' + spec.type + '\n' 133 + '@for ' + parent + '\n'); 134} 135 136 137// TODO 138// generate link spec 139function GENERATE_LINK(text) { 140 return '{{#crossLink "' + text + '"}}{{/crossLink}}'; 141} 142 143 144module.exports = generateDocs;