publish.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* eslint-disable indent, no-nested-ternary, space-infix-ops */
  2. /**
  3. @overview Builds a tree-like JSON string from the doclet data.
  4. @version 0.0.3
  5. @example
  6. ./jsdoc scratch/jsdoc_test.js -t templates/haruki -d console -q format=xml
  7. */
  8. const xml = require('js2xmlparser');
  9. const hasOwnProp = Object.prototype.hasOwnProperty;
  10. function graft(parentNode, childNodes, parentLongname) {
  11. childNodes
  12. .filter(({memberof}) => memberof === parentLongname)
  13. .forEach(element => {
  14. let i;
  15. let len;
  16. let thisClass;
  17. let thisEvent;
  18. let thisFunction;
  19. let thisMixin;
  20. let thisNamespace;
  21. if (element.kind === 'namespace') {
  22. if (!parentNode.namespaces) {
  23. parentNode.namespaces = [];
  24. }
  25. thisNamespace = {
  26. 'name': element.name,
  27. 'description': element.description || '',
  28. 'access': element.access || '',
  29. 'virtual': Boolean(element.virtual)
  30. };
  31. parentNode.namespaces.push(thisNamespace);
  32. graft(thisNamespace, childNodes, element.longname);
  33. }
  34. else if (element.kind === 'mixin') {
  35. if (!parentNode.mixins) {
  36. parentNode.mixins = [];
  37. }
  38. thisMixin = {
  39. 'name': element.name,
  40. 'description': element.description || '',
  41. 'access': element.access || '',
  42. 'virtual': Boolean(element.virtual)
  43. };
  44. parentNode.mixins.push(thisMixin);
  45. graft(thisMixin, childNodes, element.longname);
  46. }
  47. else if (element.kind === 'function') {
  48. if (!parentNode.functions) {
  49. parentNode.functions = [];
  50. }
  51. thisFunction = {
  52. 'name': element.name,
  53. 'access': element.access || '',
  54. 'virtual': Boolean(element.virtual),
  55. 'description': element.description || '',
  56. 'parameters': [],
  57. 'examples': []
  58. };
  59. parentNode.functions.push(thisFunction);
  60. if (element.returns) {
  61. thisFunction.returns = {
  62. 'type': element.returns[0].type? (element.returns[0].type.names.length === 1? element.returns[0].type.names[0] : element.returns[0].type.names) : '',
  63. 'description': element.returns[0].description || ''
  64. };
  65. }
  66. if (element.examples) {
  67. for (i = 0, len = element.examples.length; i < len; i++) {
  68. thisFunction.examples.push(element.examples[i]);
  69. }
  70. }
  71. if (element.params) {
  72. for (i = 0, len = element.params.length; i < len; i++) {
  73. thisFunction.parameters.push({
  74. 'name': element.params[i].name,
  75. 'type': element.params[i].type? (element.params[i].type.names.length === 1? element.params[i].type.names[0] : element.params[i].type.names) : '',
  76. 'description': element.params[i].description || '',
  77. 'default': hasOwnProp.call(element.params[i], 'defaultvalue') ? element.params[i].defaultvalue : '',
  78. 'optional': typeof element.params[i].optional === 'boolean'? element.params[i].optional : '',
  79. 'nullable': typeof element.params[i].nullable === 'boolean'? element.params[i].nullable : ''
  80. });
  81. }
  82. }
  83. }
  84. else if (element.kind === 'member') {
  85. if (!parentNode.properties) {
  86. parentNode.properties = [];
  87. }
  88. parentNode.properties.push({
  89. 'name': element.name,
  90. 'access': element.access || '',
  91. 'virtual': Boolean(element.virtual),
  92. 'description': element.description || '',
  93. 'type': element.type? (element.type.length === 1? element.type[0] : element.type) : ''
  94. });
  95. }
  96. else if (element.kind === 'event') {
  97. if (!parentNode.events) {
  98. parentNode.events = [];
  99. }
  100. thisEvent = {
  101. 'name': element.name,
  102. 'access': element.access || '',
  103. 'virtual': Boolean(element.virtual),
  104. 'description': element.description || '',
  105. 'parameters': [],
  106. 'examples': []
  107. };
  108. parentNode.events.push(thisEvent);
  109. if (element.returns) {
  110. thisEvent.returns = {
  111. 'type': element.returns.type ? (element.returns.type.names.length === 1 ? element.returns.type.names[0] : element.returns.type.names) : '',
  112. 'description': element.returns.description || ''
  113. };
  114. }
  115. if (element.examples) {
  116. for (i = 0, len = element.examples.length; i < len; i++) {
  117. thisEvent.examples.push(element.examples[i]);
  118. }
  119. }
  120. if (element.params) {
  121. for (i = 0, len = element.params.length; i < len; i++) {
  122. thisEvent.parameters.push({
  123. 'name': element.params[i].name,
  124. 'type': element.params[i].type? (element.params[i].type.names.length === 1? element.params[i].type.names[0] : element.params[i].type.names) : '',
  125. 'description': element.params[i].description || '',
  126. 'default': hasOwnProp.call(element.params[i], 'defaultvalue') ? element.params[i].defaultvalue : '',
  127. 'optional': typeof element.params[i].optional === 'boolean'? element.params[i].optional : '',
  128. 'nullable': typeof element.params[i].nullable === 'boolean'? element.params[i].nullable : ''
  129. });
  130. }
  131. }
  132. }
  133. else if (element.kind === 'class') {
  134. if (!parentNode.classes) {
  135. parentNode.classes = [];
  136. }
  137. thisClass = {
  138. 'name': element.name,
  139. 'description': element.classdesc || '',
  140. 'extends': element.augments || [],
  141. 'access': element.access || '',
  142. 'virtual': Boolean(element.virtual),
  143. 'fires': element.fires || '',
  144. 'constructor': {
  145. 'name': element.name,
  146. 'description': element.description || '',
  147. 'parameters': [
  148. ],
  149. 'examples': []
  150. }
  151. };
  152. parentNode.classes.push(thisClass);
  153. if (element.examples) {
  154. for (i = 0, len = element.examples.length; i < len; i++) {
  155. thisClass.constructor.examples.push(element.examples[i]);
  156. }
  157. }
  158. if (element.params) {
  159. for (i = 0, len = element.params.length; i < len; i++) {
  160. thisClass.constructor.parameters.push({
  161. 'name': element.params[i].name,
  162. 'type': element.params[i].type? (element.params[i].type.names.length === 1? element.params[i].type.names[0] : element.params[i].type.names) : '',
  163. 'description': element.params[i].description || '',
  164. 'default': hasOwnProp.call(element.params[i], 'defaultvalue') ? element.params[i].defaultvalue : '',
  165. 'optional': typeof element.params[i].optional === 'boolean'? element.params[i].optional : '',
  166. 'nullable': typeof element.params[i].nullable === 'boolean'? element.params[i].nullable : ''
  167. });
  168. }
  169. }
  170. graft(thisClass, childNodes, element.longname);
  171. }
  172. });
  173. }
  174. /**
  175. @param {TAFFY} data
  176. @param {object} opts
  177. */
  178. exports.publish = (data, {destination, query}) => {
  179. let docs;
  180. const root = {};
  181. data({undocumented: true}).remove();
  182. docs = data().get(); // <-- an array of Doclet objects
  183. graft(root, docs);
  184. if (destination === 'console') {
  185. if (query && query.format === 'xml') {
  186. console.log( xml.parse('jsdoc', root) );
  187. }
  188. else {
  189. console.log( require('jsdoc/util/dumper').dump(root) );
  190. }
  191. }
  192. else {
  193. console.log('This template only supports output to the console. Use the option "-d console" when you run JSDoc.');
  194. }
  195. };