|
| 1 | +var escapeHTML = require("escape-html"); |
| 2 | + |
| 3 | +module.exports = function (docMap, options, getCurrent, helpers, OtherHandlebars) { |
| 4 | + var docMapInfo = new DocMapInfo(docMap, getCurrent); |
| 5 | + |
| 6 | + return { |
| 7 | + "makeSignature": function (code) { |
| 8 | + if (code) { |
| 9 | + return escapeHTML(code); |
| 10 | + } |
| 11 | + |
| 12 | + var sig = ""; |
| 13 | + if (this.type === "module") { |
| 14 | + sig = "__" + this.name + "__ "; |
| 15 | + } |
| 16 | + if (this.types) { |
| 17 | + return sig + helpers.makeTypes(this.types); |
| 18 | + } |
| 19 | + |
| 20 | + if (! /function|constructor/i.test(this.type) && !this.params && !this.returns) { |
| 21 | + return sig + helpers.makeType(this); |
| 22 | + } |
| 23 | + |
| 24 | + // if it's a constructor add new |
| 25 | + if (this.type === "constructor") { |
| 26 | + sig += "new "; |
| 27 | + } |
| 28 | + |
| 29 | + // get the name part right |
| 30 | + var parent = docMap[this.parent]; |
| 31 | + if (parent) { |
| 32 | + if (parent.type == "prototype") { |
| 33 | + var parentParent = docMap[parent.parent]; |
| 34 | + sig += (parentParent.alias || (lastPartOfName(parentParent.name) + ".")).toLowerCase(); |
| 35 | + |
| 36 | + } else { |
| 37 | + sig += (parent.alias || lastPartOfName(parent.name) + "."); |
| 38 | + } |
| 39 | + |
| 40 | + sig += (lastPartOfName(this.name) || "function"); |
| 41 | + } else { |
| 42 | + sig += "function"; |
| 43 | + } |
| 44 | + |
| 45 | + sig += "(" + helpers.makeParamsString(this.params) + ")"; |
| 46 | + |
| 47 | + return sig; |
| 48 | + }, |
| 49 | + "makeParams": function () { |
| 50 | + var result = "<b>" + this.name + "</b>"; |
| 51 | + if (this.types) { |
| 52 | + result += " <code>" + helpers.makeTypesString(this.types) + "</code>"; |
| 53 | + } |
| 54 | + return result; |
| 55 | + }, |
| 56 | + makeReturn: function () { |
| 57 | + if (this.types) { |
| 58 | + return " <code>" + helpers.makeTypesString(this.types) + "</code>"; |
| 59 | + } |
| 60 | + }, |
| 61 | + makeTypesString: function (types) { |
| 62 | + if (types && types.length) { |
| 63 | + var txt = "{" + helpers.makeTypes(types); |
| 64 | + return txt + "}"; |
| 65 | + } else { |
| 66 | + return ''; |
| 67 | + } |
| 68 | + }, |
| 69 | + makeTypes: function (types) { |
| 70 | + if (types.length) { |
| 71 | + // turns [{type: 'Object'}, {type: 'String'}] into '{Object | String}' |
| 72 | + return types.map(helpers.makeType).join('|'); |
| 73 | + } else { |
| 74 | + return ''; |
| 75 | + } |
| 76 | + }, |
| 77 | + makeType: function (t) { |
| 78 | + if (t.type === "function") { |
| 79 | + var fn = t.params && t.params.length ? |
| 80 | + "(" + helpers.makeParamsString(t.params) + ")" : ""; |
| 81 | + |
| 82 | + if (t.constructs && t.constructs.types) { |
| 83 | + fn = "constructor" + fn; |
| 84 | + fn += " => " + helpers.makeTypes(t.constructs.types); |
| 85 | + } else { |
| 86 | + fn = "function" + fn; |
| 87 | + } |
| 88 | + |
| 89 | + return fn; |
| 90 | + } |
| 91 | + |
| 92 | + var type = docMap[t.type]; |
| 93 | + var title = type && type.title || undefined; |
| 94 | + var txt = helpers.linkTo(t.type, title); |
| 95 | + var params; |
| 96 | + |
| 97 | + if (t.template && t.template.length) { |
| 98 | + txt += "<" + t.template.map(function (templateItem) { |
| 99 | + return helpers.makeTypes(templateItem.types); |
| 100 | + }).join(",") + ">"; |
| 101 | + } |
| 102 | + |
| 103 | + if (type) { |
| 104 | + if (type.type === "function" && (type.params || type.signatures)) { |
| 105 | + params = type.params || (type.signatures[0] && type.signatures[0].params) || []; |
| 106 | + } else if (type.type === "typedef" && type.types && type.types[0] && type.types[0].type == "function") { |
| 107 | + params = type.types[0].params; |
| 108 | + } |
| 109 | + if (params) { |
| 110 | + txt += "(" + helpers.makeParamsString(params) + ")"; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return txt; |
| 115 | + }, |
| 116 | + makeParamsString: function (params) { |
| 117 | + if (!params || !params.length) { |
| 118 | + return ""; |
| 119 | + } |
| 120 | + |
| 121 | + return params.map(function (param) { |
| 122 | + // try to look up the title |
| 123 | + var type = param.types && param.types[0] && param.types[0].type; |
| 124 | + return helpers.linkTo(type, param.name) + |
| 125 | + (param.variable ? "..." : ""); |
| 126 | + }).join(", "); |
| 127 | + }, |
| 128 | + }; |
| 129 | +}; |
| 130 | + |
| 131 | +var DocMapInfo = function (docMap, getCurrent) { |
| 132 | + this.docMap = docMap; |
| 133 | + this.getCurrent = getCurrent; |
| 134 | +}; |
0 commit comments