-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathHotdocGenerator.java
More file actions
279 lines (241 loc) · 10 KB
/
Copy pathHotdocGenerator.java
File metadata and controls
279 lines (241 loc) · 10 KB
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
package de.peeeq.wurstio.hotdoc;
import com.google.common.collect.Lists;
import de.peeeq.wurstio.WurstCompilerJassImpl;
import de.peeeq.wurstio.utils.FileUtils;
import de.peeeq.wurstscript.RunArgs;
import de.peeeq.wurstscript.WLogger;
import de.peeeq.wurstscript.ast.*;
import de.peeeq.wurstscript.attributes.CompileError;
import de.peeeq.wurstscript.gui.WurstGui;
import de.peeeq.wurstscript.gui.WurstGuiCliImpl;
import de.peeeq.wurstscript.utils.Utils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.eclipse.jdt.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Properties;
public class HotdocGenerator {
private final List<String> files;
private final File outputfolder;
// private WurstModel model;
// private ArrayList<WPackage> packages;
private final VelocityEngine ve;
private final Template variableTemplate;
private final Template navbarTemplate;
private final Template structureTemplate;
public HotdocGenerator(List<String> files) {
this.files = Lists.newArrayList(files);
this.outputfolder = new File(this.files.remove(files.size() - 1));
ve = new VelocityEngine();
Properties p = new Properties();
p.setProperty("runtime.references.strict", "true");
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
ve.init(p);
variableTemplate = ve.getTemplate("/hotdoc/var.html");
navbarTemplate = ve.getTemplate("/hotdoc/navbar.html");
structureTemplate = ve.getTemplate("/hotdoc/structure.html");
}
public void generateDoc() {
try {
WLogger.info("Generating hotdoc into " + outputfolder.getAbsolutePath());
for (String f : files) {
WLogger.info(" input: " + f);
}
if (outputfolder.exists()) {
// clean folder
for (File f : outputfolder.listFiles()) {
if (f.getName().endsWith(".html")) {
f.delete();
}
}
} else {
if (!outputfolder.mkdirs()) {
throw new Error("could not create output directory");
}
}
RunArgs runArgs = new RunArgs();
WurstGui gui = new WurstGuiCliImpl();
WurstCompilerJassImpl compiler = new WurstCompilerJassImpl(null, gui, null, runArgs);
compiler.loadFiles(Utils.getResourceFile("common.j"));
compiler.loadFiles(Utils.getResourceFile("blizzard.j"));
for (String file : files) {
File f = new File(file);
if (!f.exists()) {
WLogger.info("Folder " + f + " does not exist");
throw new RuntimeException(f.getAbsolutePath());
}
if (f.isDirectory()) {
compiler.loadWurstFilesInDir(f);
} else if (Utils.isWurstFile(f)) {
compiler.loadFiles(f);
}
}
WurstModel model = compiler.parseFiles();
if (model == null) {
WLogger.info("Hotdoc model is null.");
for (CompileError e : gui.getErrorList()) {
WLogger.info(e);
}
throw new RuntimeException("Could not analyze program correctly.");
}
ArrayList<WPackage> packages = Lists.newArrayList(model.attrPackages().values());
if (packages.size() == 0) {
throw new RuntimeException("Cannot generate for empty model: " + files.get(0));
}
WLogger.info("Found " + packages.size() + "´packages.");
packages.sort(Comparator.comparing(o -> o.getSource().shortFile()));
createIndex(packages);
for (WPackage p : packages) {
createPackageDoc(p, packages);
}
gui.clearErrors();
} catch (Throwable t) {
System.err.println("Error in creating documentation: ");
t.printStackTrace();
throw new RuntimeException(t);
}
}
private void createIndex(List<WPackage> packages) {
Template t = ve.getTemplate("/hotdoc/document.html");
VelocityContext context = new VelocityContext();
context.put("title", "HotDoc Wurst Documentation");
context.put("navbar", getNavbarWithHighlight(null, packages));
context.put("content", "");
String s = render(t, context);
WLogger.info(s);
// TODO
try {
FileUtils.write(render(t, context), new File(outputfolder + "/index.html"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void createPackageDoc(WPackage pack, List<WPackage> packages) {
Template t = ve.getTemplate("/hotdoc/document.html");
VelocityContext context = new VelocityContext();
context.put("title", pack.getName() + " HotDoc Wurst Documentation");
context.put("navbar", getNavbarWithHighlight(pack, packages));
context.put("content", getPackageContent(pack));
String s = render(t, context);
WLogger.info(s);
// TODO
try {
FileUtils.write(render(t, context), new File(outputfolder + "/" + pack.getName() + ".html"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private String getPackageContent(WPackage pack) {
Template t = ve.getTemplate("/hotdoc/package.html");
VelocityContext context = new VelocityContext();
context.put("currentPackage", pack);
StringWriter writer = new StringWriter();
t.merge(context, writer);
documentVars(getElements(pack, VarDef.class), writer, false);
documentFuncs(getElements(pack, FunctionDefinition.class), writer, false);
documentStructures(pack, writer);
return writer.toString();
}
private void documentStructures(WPackage pack, StringWriter writer) {
List<StructureDef> sorted = Utils.sortByName(getElements(pack, StructureDef.class));
for (StructureDef v : sorted) {
if (!v.attrIsPublic()) {
continue;
}
VelocityContext context = new VelocityContext();
context.put("name", Utils.printElement(v));
context.put("type", "");
context.put("comment", v.attrComment());
context.put("source", v.getSource());
structureTemplate.merge(context, writer);
documentVars(v.getVars(), writer, true);
documentFuncs(v.getMethods(), writer, true);
}
}
private <T extends FunctionDefinition> void documentFuncs(List<T> funcs, StringWriter writer, boolean includeNonPublic) {
funcs = Utils.sortByName(funcs);
for (FunctionDefinition f : funcs) {
if (!f.attrIsPublic()) {
if (!includeNonPublic || f.attrIsPrivate()) {
continue;
}
}
VelocityContext context = new VelocityContext();
StringBuilder descr = new StringBuilder();
descr.append("function ");
if (f instanceof ExtensionFuncDef) {
ExtensionFuncDef ex = (ExtensionFuncDef) f;
descr.append(ex.getExtendedType().attrTyp());
descr.append(".");
}
descr.append(f.getName());
descr.append("(");
boolean first = true;
for (WParameter p : f.getParameters()) {
if (!first) {
descr.append(", ");
}
descr.append(p.attrTyp()).append(" ").append(p.getName());
first = false;
}
descr.append(")");
if (f.attrReturnTyp().isVoid()) {
} else {
descr.append(" returns ");
descr.append(f.attrReturnTyp());
}
context.put("name", descr);
context.put("type", "");
context.put("comment", f.attrComment());
context.put("source", f.getSource());
variableTemplate.merge(context, writer);
}
}
private <T extends VarDef> void documentVars(List<T> vardefs, StringWriter writer, boolean includeNonPublic) {
List<T> sorted = Utils.sortByName(vardefs);
for (VarDef v : sorted) {
if (!v.attrIsPublic()) {
if (!includeNonPublic || v.attrIsPrivate()) {
continue;
}
}
VelocityContext context = new VelocityContext();
context.put("name", v.getName());
context.put("type", v.attrTyp());
context.put("comment", v.attrComment());
context.put("source", v.getSource());
variableTemplate.merge(context, writer);
}
}
private <T> List<T> getElements(WPackage pack, Class<T> clazz) {
List<T> result = Lists.newArrayList();
for (WEntity e : pack.getElements()) {
WLogger.info(Utils.printElement(e));
if (clazz.isAssignableFrom(e.getClass())) {
@SuppressWarnings("unchecked")
T t = (T) e;
result.add(t);
}
}
return result;
}
private String render(Template t, VelocityContext context) {
StringWriter writer = new StringWriter();
t.merge(context, writer);
String s = writer.toString();
return s;
}
private String getNavbarWithHighlight(@Nullable WPackage pack, List<WPackage> packages) {
VelocityContext context = new VelocityContext();
context.put("packages", packages);
context.put("currentPackage", pack);
return render(navbarTemplate, context);
}
}