This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoc.java
161 lines (146 loc) · 5.72 KB
/
Doc.java
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
//
// Copyright 2016, Yahoo Inc.
// Copyrights licensed under the New BSD License.
// See the accompanying LICENSE file for terms.
//
// [![Build Status](https://travis-ci.org/jminusminus/doc.svg?branch=master)](https://travis-ci.org/jminusminus/doc)
// ## Stability: 0 - Unstable
// This package contains utilities for generating Jmm documentation and serving it via HTTP.
package github.com.jminusminus.doc;
import github.com.jminusminus.markdown.Markdown;
import github.com.jminusminus.core.http.Server;
// This class can be programmatically used to generate Jmm documentation from a Jmm packages source code.
public class Doc {
// ## Command Line Usage
//
// Prints the Markdown for a given Jmm class.
// `doc [jmm_classpath]`
//
// ```
// $ doc github.com.jminusminus.doc.Doc
// // Prints the Markdown for class "github.com.jminusminus.doc.Doc" to stdout.
// ```
//
// Starts a web server with all documentation for the current Jmm workspace.
// `doc`
//
// ```java
// $ doc
// Server started on port 8080, using 4 cores and 8 threads...
// Document server started at http://localhost:8080/
// Serving documentation from /Users/allinson/Java/jmmworkspace
// ```
public static void main(String[] args) {
Doc doc = new Doc();
if (args.length == 1) {
System.out.println("");
System.out.println(doc.getDoc(args[0]).trim());
System.out.println("");
return;
}
doc.startServer(8080);
}
// The current JMM working directory.
protected final String workingDir = System.getenv("JMMPATH");
// Line Feed.
protected static final byte LF = (byte)10;
// Carrage return, line feed.
protected static final String CRLF = "\r\n";
// Start a HTTP server on the given port to serve the documentation for the current Jmm workspace.
public void startServer(int port) {
Server s = Server.createServer((req, res) -> {
String classPath = req.url.substring(1).replace("/", ".");
String md;
if (classPath.length() > 0) {
md = this.getDoc(classPath);
} else {
md = this.toString();
}
res.setHeader("Content", "text/html");
// Replace this with handlebars.
res.end(this.renderHtml(classPath, Markdown.parse(md).toString()));
// res.end("<html><head>" + this.getCss() + "</head><body>" + this.getMenu() + "<div class=\"content\">" + Markdown.parse(md).toString() + "</div></body></html>");
});
s.listen(port);
System.out.println("Document server started at http://localhost:8080/");
System.out.println("Serving documentation from " + this.workingDir);
}
// Returns Markdown documentation for the given Jmm class path in the current Jmm workspace.
public String getDoc(String classPath) {
DocClass dc = new DocClass(this.classPathFilePath(classPath));
if (dc == null) {
return "";
}
return dc.toString();
}
// Returns Markdown with links to all the classes found in the current Jmm workspace.
public String toString() {
String list = "";
for (String file : this.getJavaFiles()) {
if (file.length() > this.workingDir.length() + 10) {
String classPath = file.substring(this.workingDir.length() + 5, file.length() - 5).replace("/", ".");
list += "* [" + classPath + "](/" + classPath + ")" + this.CRLF;
}
}
return "## Package List\n" + list;
}
// Returns a Jmm class path for the given Java package name.
protected String classPathFilePath(String classPath) {
String path = this.workingDir + "/src/" + classPath.replace(".", "/") + ".java";
return path;
}
// Return all .java files in the current workspace.
protected String[] getJavaFiles() {
if (this.workingDir.length() == 0) {
return new String[0];
}
return this.walk(this.workingDir).split(" ");
}
// Walk the given directory and find all .java files.
protected String walk(String dir) {
java.io.File root = new java.io.File(dir);
java.io.File[] list = root.listFiles();
if (list == null) {
return "";
}
String file = "";
String files = "";
for (java.io.File item : list) {
try {
file = item.getCanonicalPath();
} catch (Exception e) {
System.out.println(e);
}
if (item.isDirectory()) {
files += this.walk(file) + " ";
} else {
if (file.endsWith(".java") && !file.endsWith("_test.java")) {
files += file + " ";
}
}
}
return files.trim();
}
protected String renderHtml(String title, String content) {
byte[] data = new byte[0];
try {
data = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(this.workingDir + "/src/github/com/jminusminus/doc/resources/main.html"));
} catch (Exception e) {
return "";
}
String html = new String(data);
html = html.replace("{{title}}", title);
html = html.replace("{{css}}", this.getCss());
html = html.replace("{{content}}", content);
return html;
}
protected String getCss() {
byte[] data = new byte[0];
try {
data = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(this.workingDir + "/src/github/com/jminusminus/doc/resources/main.css"));
} catch (Exception e) {
return "";
}
return new String(data);
}
}