-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
122 lines (94 loc) · 3.66 KB
/
generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
subDoc
Copyright (C) Subnodal Technologies. All Rights Reserved.
https://subnodal.com
Licenced by the Subnodal Open-Source Licence, which can be found at LICENCE.md.
*/
var fs = require("fs");
var path = require("path");
const UNICODE_VARSEL_EMOJI = "\uFE0F"
const REFERENCE_SYMBOLS = {
"function": "▶" + UNICODE_VARSEL_EMOJI,
"class": "🎛" + UNICODE_VARSEL_EMOJI,
"method": "⏩" + UNICODE_VARSEL_EMOJI,
"static": "❄️" + UNICODE_VARSEL_EMOJI,
"prop": "🔡" + UNICODE_VARSEL_EMOJI,
"const": "🔒" + UNICODE_VARSEL_EMOJI,
"var": "🔠" + UNICODE_VARSEL_EMOJI,
"generator": "♾" + UNICODE_VARSEL_EMOJI,
};
const REFERENCE_SYMBOL_OTHER = "🔣";
function sortReferences(references) {
return references.sort(function(a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
});
}
function getReferenceSymbol(referenceType) {
return REFERENCE_SYMBOLS[referenceType.split(" ")[0]] || REFERENCE_SYMBOL_OTHER;
}
/*
@name generator.generateMarkdown
Generate a Markdown file given a namespace.
@param namespace <parser.Namespace> Namespace to generate Markdown with
*/
exports.generateMarkdown = function(namespace) {
var sortedReferences = sortReferences(namespace.references);
var markdown = "";
markdown += `# ${namespace.name.trim() == "" ? "(global)" : namespace.name}\n`;
sortedReferences.forEach(function(reference) {
markdown += `## ${getReferenceSymbol(reference.type)} \`${reference.name}\`\n`;
markdown += `\`${reference.type}\``;
if (reference.synopsis.trim() != "") {
markdown += ` · ${reference.synopsis}`;
}
markdown += `\n`;
if (reference.parameters.length > 0) {
markdown += `\n**Parameters:**\n`;
reference.parameters.forEach(function(parameter) {
var typeAnnotation = `\`${parameter.type}\`${parameter.defaultValue != "undefined" ? " = \`" + parameter.defaultValue + "\`" : ""}`;
var descriptionAnnotation = ``;
if (parameter.description.trim != "") {
descriptionAnnotation = `: ${parameter.description.trim()}`;
}
markdown += `* **\`${parameter.identifier}\`** (${typeAnnotation})${descriptionAnnotation}\n`;
});
}
if (reference.returns.type != "undefined") {
if (reference.returns.description.trim() != "") {
markdown += `\n**Returns:** \`${reference.returns.type}\` · ${reference.returns.description}\n`;
} else {
markdown += `\n**Returns:** \`${reference.returns.type}\`\n`;
}
}
markdown += `\n`;
});
markdown = markdown.replace(/\n+$/gs, "");
return markdown;
};
/*
@name generator.createMarkdownFiles
Create Markdown files in the specified directory with given namespaces.
@param outdir <String> Directory to create files in
@param namespaces <[parser.Namespace]> Namespaces to generate Markdown files with
*/
exports.createMarkdownFiles = function(outdir, namespaces) {
namespaces.forEach(function(namespace) {
var filename = namespace.name.trim() + ".md";
if (namespace.name.trim() == "") {
filename = "global.md";
}
if (namespace.references.length == 0) {
return; // Skip this namespace since it's empty
}
if (!fs.existsSync(outdir)) {
fs.mkdirSync(outdir, {recursive: true});
}
fs.writeFileSync(path.join(outdir, filename), exports.generateMarkdown(namespace));
});
};