This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
convert_javadoc.html
302 lines (258 loc) · 9.15 KB
/
convert_javadoc.html
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE html>
<html>
<head>
</head>
<body>
In: (docstring contents, may be followed by a function declaration without a { character)<br />
<textarea id="inbox" rows="10" cols="100"></textarea>
<p>
Out: python docstring, optionally with a function definition<br />
<input type="radio" name="mtype" value="normal" onclick="xfer()" checked /> normal
<input type="radio" name="mtype" value="yamlgen" onclick="xfer()" /> yamlgen
<input type="radio" name="mtype" value="cstring" onclick="xfer()" /> C string
<br />
<textarea id="outbox" rows="10" cols="100"></textarea>
<p>
Out: sphinx variable doc<br />
<textarea id="sphinxbox" rows="10" cols="100"></textarea>
<script>
function getOutputMethodType() {
var radios = document.getElementsByName('mtype');
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
return radios[i].value;
}
}
}
function trimLines(lines) {
return lines.join('\r\n').trim().split('\r\n');
}
function processDoc(txt) {
var params = [];
var returns = [];
var paramidx = -1;
var foundReturns = false;
var lines = txt.split(/\r?\n/);
for (i = 0; i < lines.length; i++) {
var line = lines[i];
line = line.trim();
line = line.replace("/*", "").replace("*/", "");
if (line.startsWith("*")) {
line = line.slice(1);
}
line = line.replace('<p>', '');
var p = line.match(/@param\W+(\w+)/);
if (p != null) {
line = line.replace(/@param\W+\w+(\W+)?/, '');
paramidx += 1;
params.push([p[1], []]);
} else {
var r = line.match(/@returns\W*|@return\W*/);
if (r != null) {
line = line.replace(/@returns\W*|@return\W*/, '');
foundReturns = true;
}
}
line = line.replace(/{@link\W+#(\w+?)\(.*?\)\W*}/g, `:meth:\`.$1\``);
line = line.replace(/{@link\W+(\w+)#(\w+)\(.*?\)\W*}/g, `:meth:\`.$1.$2\``);
line = line.replace(/{@link\W+(\w+?)\W*}/g, `:class:\`.$1\``);
line = line.trim();
if (foundReturns) {
lines[i] = "";
returns.push(line);
} else if (paramidx != -1) {
lines[i] = "";
params[paramidx][1].push(line);
} else {
lines[i] = line;
}
}
var text = lines.join('\r\n').trim() + '\r\n';
text = text.replace(/(\r\n){2,}/gm, '\r\n\r\n');
// indent each parameter evenly
var pindent = 0;
for (i = 0; i < params.length; i++) {
pindent = Math.max(pindent, params[i][0].length + 9);
}
pindent = ' '.repeat(pindent);
// indent each parameter and append
for (i = 0; i < params.length; i++) {
var pname = ':param ' + params[i][0] + ': ';
var pp = trimLines(params[i][1]);
if (pp.length == 0) {
continue;
}
text += '\r\n' + pname + ' '.repeat(pindent.length - pname.length) + pp[0] + "\r\n";
for (j = 1; j < pp.length; j++) {
if (pp[j]) {
text += pindent + pp[j] + "\r\n";
} else {
text += "\r\n";
}
}
}
if (returns.length > 0) {
returns = trimLines(returns);
// returns always needs a newline before it
text += "\r\n:returns: " + returns[0];
for (i = 1; i < returns.length; i++) {
if (returns[i]) {
text += ' ' + returns[i] + "\r\n";
} else {
text += "\r\n";
}
}
}
return text;
}
function javaTypeToPython(type) {
if (type == "void")
return "None";
if (type == "byte[]")
return "bytes";
var isArray = false;
if (type.endsWith("[]")) {
isArray = true;
type = type.slice(0, -2);
}
type = type.split(/(\w+)/g);
for (var i = 0; i < type.length; i++) {
switch (type[i]) {
case "ArrayList":
type[i] = "List";
break;
case "<":
type[i] = "[";
break;
case ">":
type[i] = "]";
break;
case "boolean": case "Boolean":
type[i] = "bool";
break;
case "Integer":
case "long": case "Long":
case "short": case "Short":
case "byte": case "Byte":
type[i] = "int";
break;
case "double": case "Double":
case "Float":
type[i] = "float";
break;
case "String":
type[i] = "str";
break;
case "ByteBuffer": // java.nio
type[i] = "bytearray";
break;
}
}
type = type.join("");
if (isArray) {
type = "List[" + type + "]";
}
return type;
}
var indent = ' ';
var indentRe = new RegExp(indent, "g");
function formatDocstring(text) {
// indent all
var lines = text.split(/\r?\n/);
for (var i = 1; i < lines.length; i++) {
if (lines[i])
lines[i] = indent + lines[i];
}
if (lines.length > 1)
lines.push(indent);
lines[lines.length - 1] = lines[lines.length - 1] + '"""';
return '"""' + lines.join('\r\n');
}
function process(txt) {
// remove diff formatting if present
txt = txt.replace(/^\+/gm, '');
// try to find a function definition
var s = txt.split(/(.*)\n\s*((?:(?:public|protected|private|static|final|synchronized|abstract|default|native)\s+)+)(?:([\w<>[\]]+)\s+)?(\w+)\s*\(([^)]*)\)/);
var t = processDoc(s[0]);
if (getOutputMethodType() == "yamlgen") {
t = ' doc: |\r\n ' + t.replace(/\r\n/g, '\r\n ');
return t;
} else if (getOutputMethodType() == "cstring") {
var nta = [];
var ta = t.split(/\r\n/g);
for (var i = 0; i < ta.length; i++) {
if (ta[i].startsWith(":param") || ta[i].startsWith(":returns:")) {
ta[i] = ta[i] + "\\n";
}
if (i != 0 && ta[i] === "") {
if (!nta[nta.length - 1].startsWith(":param")) {
nta[nta.length - 1] = nta[nta.length - 1] + "\\n\\n";
}
} else {
nta.push(ta[i]);
}
}
for (var i = 0; i < nta.length; i++) {
if (!nta[i].endsWith("\\n")) {
nta[i] = nta[i] + " ";
}
}
// remove trailing \n
if (nta.length > 0) {
var lastLine = nta[nta.length - 1];
while (lastLine.endsWith("\\n")) {
lastLine = lastLine.slice(0, -2);
}
nta[nta.length - 1] = lastLine
}
t = '"' + nta.join('"\r\n"') + '"';
} else {
t = formatDocstring(t);
}
// add the function definition in if present
if (s.length > 1) {
var modifiers = s[2];
var retType = s[3];
var funcName = s[4];
var args = s[5];
if (args) {
var pyArgs = args.split(", ").map(function (arg) {
var split = arg.split(" ");
var argName, argType;
if (split[0] != "final") {
argType = split[0];
argName = split[1];
} else {
argType = split[1];
argName = split[2];
}
return argName + ": " + javaTypeToPython(argType);
});
args = ", " + pyArgs.join(", ");
}
if (retType) {
retType = javaTypeToPython(retType);
} else {
funcName = "__init__";
retType = "None";
}
if (modifiers.includes("static")) {
t = "@classmethod\n def " + funcName + "(cls" + args +
") -> " + retType + ":\n " + t;
} else {
t = "def " + funcName + "(self" + args +
") -> " + retType + ":\n " + t;
}
}
return t;
}
function xfer() {
var txt = process(inbox.value);
outbox.value = txt;
//sphinxbox.value = '#: ' + lines.join('\r\n').replace(indentRe, indent + '#: ');
}
inbox.oninput = inbox.onpropertychange = inbox.onpaste = xfer;
xfer();
</script>
</body>
</html>