-
Notifications
You must be signed in to change notification settings - Fork 0
/
docu.mjs
executable file
·261 lines (224 loc) · 7.3 KB
/
docu.mjs
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env node
/* This commentaries are made by hand to be processed by https://github.com/StringManolo/docu */
/* lang -> javascript
* name -> docu.mjs
* title -> Docu - Documentation
* filetype -> ES6 module
* description -> Docu is a code geneator CLI tool. Is intended to generate documentation in html, markdown and bbcode from code commentaries.
* summary -> CLI tool to create documents from commentaries in html, markdown and bbcode.
* dependency -> node https://node.org,fwl.mjs https://github.com/StringManolo/fwl
*/
import fs from "fs";
import fwl from "./fwl.mjs";
fwl(global);
if (!process.argv[2]) {
console.log(`Usage:
node docu.mjs myCodeFile html > documentation.html
node docu.mjs myCodeFile markdown > documentation.md
node docu.mjs myCodeFile bbcode > documentation.txt
`);
process.exit(0);
}
if (process.argv[3]) {
documentType = process.argv[3];
}
const filename = process.argv[2];
let file;
try {
file = fs.readFileSync(filename, { encoding: "utf-8" });
} catch(e) {
console.log(`Unable to load the file: ${e}`);
process.exit(0);
}
let stringContext = false;
let commentContext = false;
let commentStartPos = false;
let lineCounter = 1;
let comments = [];
let commentLines = [];
for (let i in file) {
if (file[i] == "\n") {
++lineCounter;
}
if (file[i - 1] != "\\" && file[i] == '"' || file[i] == "'" || file[i] == "`") {
if (commentContext == false) {
switch(file[i]) {
case '"':
stringContext = (stringContext == '"' ? false : '"');
break;
case "'":
stringContext = (stringContext == "'" ? false : "'");
break;
case "`":
stringContext = (stringContext == "`" ? false : "`");
}
}
} else if(file[i] == "/" && file[1 + +i] == "*") {
if (stringContext == false) {
commentContext = true;
commentStartPos = i;
}
} else if(file[i] == "*" && file[1 + +i] == "/") {
if (stringContext == false) {
if (commentStartPos) {
commentLines.push(lineCounter);
comments.push(file.substring(commentStartPos, +i + 2));
commentStartPos = false;
stringContext = false;
commentContext = false;
}
}
}
}
let lang = "unknown";
let sourceLink = "";
const processParts = (part, lineNumber) => {
let aux = "";
switch(part[0]) {
case "lang":
lang = part[1];
return p (`code: ${part[1]}`);
case "title":
t = title (`${part[1]}`);
break;
case "sourceLink":
sourceLink = part[1];
break;
case "name": return h1 (`${part[1]}`) + hr ``;
case "filetype": return h3 (`${part[1]}`) + hr ``;
case "description": return h4 (`${part[1]}`) + hr ``;
case "dependency": {
const dLinks = part[1].split(",");
let dGenerated = "";
for (let i in dLinks) {
dGenerated += link(dLinks[i]) + ",";
}
return list(dGenerated.substr(0, dGenerated.length-1));
}
case "variable":
case "object":
case "function":
case "property":
case "method": {
let lineNumberLink;
if (documentType == "html") {
lineNumberLink = link(`${lineNumber} #L${lineNumber}`);
} else {
lineNumberLink = link(`${lineNumber} ${sourceLink}#L${lineNumber}`);
}
return p("") + br("") + h5(italic (`${part[0]}`) + " " + bold (`${part[1]}`) + " " + lineNumberLink) + br ``;
}
case "type": return br (`${italic(part[1])}`);
case "summary": return br (`${part[1]}`);
case "return":
if(part.length > 3) {
return "Return: " + bold (`${part[1]}`) + " " + italic (`${part[2]}`) + " " + br (` ${part[3]}`);
} else if (part.length < 4) {
return "Return: " + italic (`${part[2]}`) + " " + br ``;
}
break;
case "param":
if (part.length > 3) {
return "Argument: " + bold (`${part[1]}`) + " " + italic (`${part[2]}`) + " " + br (`${part[3]}`);
} else {
return "Argument: " + bold (`${part[1]}`) + br ``;
}
case "values": return br (`Values: ${part[1]}`);
case "example": {
const formatedCode = `${lang}
${part[1].trim()}`;
return code(formatedCode);
}
}
return "";
}
let premarkup = "",
premarkup2 = "",
markup = "",
t = "",
postmarkup = "";
if (documentType == "html") {
premarkup = html `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
`;
premarkup2 = html `</head>
<body>
`;
postmarkup = html `</body>
</html>`;
}
const parseComment = (comment, lineNumber) => {
if (/(\n|\r\n)/g.test(comment)) {
let lines = comment.split("\n");
for (let i in lines) {
if (/\-\>/g.test(lines[i])) {
if (/\/\*/g.test(lines[i]) || /\*/g.test(lines[i])) {
lines[i] = lines[i].split("*").splice(1, lines[i].length).join("*").trim();
let parts = lines[i].split("->").map(part => part.trim());
if (parts[0] == "example") {
//maybe missing some part if -> found again?
parts = lines.splice(i, lines.length - (1 + +i)).join("\n").split("->");
parts[0] = parts[0].trim();
/*
let aux = parts[1].split("\n");
for (let i = 0; i < aux.length; ++i) {
if (i == 0) {
aux[i] = aux[i].trim();
}
let precomputedIndex = (1 + +i).toString().length;
let separator = "";
let numberOfSpaces = 3 - precomputedIndex;
for (let j = 0; j < numberOfSpaces; ++j) {
separator += " ";
}
separator += "| ";
aux[i] = `${1 + +i}${separator}${aux[i]}`;
}
parts[1] = aux.join("\n");
*/
}
markup += processParts(parts, lineNumber);
} else {
//console.log("Not finding star");
}
} else {
//console.log("Not finding arrow");
}
}
} else {
//console.log(`Not finding line breaks`);
}
}
for (let i in comments) {
parseComment(comments[i], +commentLines[i] + 1);
}
const replaceAll = (str, pattern, newStr) => {
while (pattern.test(str)) {
str = str.replace(pattern, newStr);
}
return str;
}
const htmlEntities = s => {
const r = "replace";
const a = s[r](/ /g," ")[r](/!/g,"!")[r](/"/g,""")[r](/%/g,"%")[r](/'/g,"'")[r](/\(/g,"(")[r](/\)/g,")")[r](/</g,"<")[r](/>/g,">")[r](/`/g,"`")[r](/a/g,"a")[r](/A/g,"A")[r](/e/g,"e")[r](/E/g,"E")[r](/i/g,"i")[r](/I/g,"I")[r](/o/g,"o")[r](/O/g,"O")[r](/u/g,"u")[r](/U/g,"U")[r](/{/g,"{")[r](/}/g,"}")[r](/‘/g,"‘")[r](/’/g,"’")[r](/‚/g,"‚")[r](/“/g,"“")[r](/”/g,"”")[r](/„/g,"„")[r](/′/g,"′")[r](/″/g,"‴")[r](/‹/g,"‹")[r](/›/g,"›")[r](/s/g,"s")[r](/S/g,"S")[r](/\./g,".");
return a;
}
let endFile, auxFile;
if (documentType == "html") {
endFile = file.split("\n");
auxFile = "html\n";
for (let i in endFile) {
auxFile += `<span id="L${+i + 1}">${htmlEntities(endFile[i])}</span>\n`;
}
}
let generated;
if (documentType == "html") {
generated = (premarkup + t + premarkup2 + markup + style_default() + p`` + text `Source:` + rawCode(auxFile) + postmarkup);
} else {
generated = (premarkup + t + premarkup2 + markup + style_default() +
p`` + postmarkup);
}
generated = replaceAll(generated, /REMOVETHISLINE/g, "");
console.log(generated);