Skip to content

Commit 995fd19

Browse files
committed
generate html doc
1 parent d96861b commit 995fd19

File tree

62 files changed

+3279
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3279
-10
lines changed

readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ In filed:
7474

7575
## TODO LIST
7676

77-
- Extend the Java doc
77+
- [x] Extend the Java doc
78+
- [ ] support auto build
79+
- [ ] generator html doc
80+
- [ ] part generator
81+
7882

7983

8084

src/main/java/com/hsjfans/github/config/Config.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ public class Config {
1515

1616
/**
1717
* the path to store the generated docs
18+
*
19+
* default is src/static/
1820
*/
19-
private String outPath;
21+
private String outPath = "src/static/";
2022

2123
/**
2224
* the path of classes files
@@ -33,5 +35,11 @@ public class Config {
3335
private String mvnPath;
3436

3537

38+
/**
39+
* api doc name - xxx 接口文档
40+
*/
41+
private String docName;
42+
43+
3644

3745
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
package com.hsjfans.github.generator;
22

3+
import com.hsjfans.github.config.Config;
4+
import com.hsjfans.github.model.ControllerClass;
5+
import com.hsjfans.github.model.ControllerMethod;
6+
37
/**
48
*
59
* @author hsjfans[[email protected]]
610
*/
711
public abstract class AbstractGenerator implements Generator {
12+
13+
protected Config config;
14+
15+
protected abstract void buildControllerDoc(ControllerClass controllerClass);
16+
17+
protected abstract void buildApiDoc(ControllerClass controllerClass, ControllerMethod controllerMethod);
18+
19+
protected abstract void buildExtraDoc();
20+
821
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.hsjfans.github.generator;
22

3+
import com.hsjfans.github.config.Config;
4+
import com.hsjfans.github.model.ApiTree;
5+
36
/**
47
*
58
* 文档生成器
@@ -8,7 +11,7 @@
811
*/
912
public interface Generator {
1013

11-
14+
void from(ApiTree apiTree, Config config);
1215

1316
}
1417

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,139 @@
11
package com.hsjfans.github.generator;
22

3+
4+
import com.hsjfans.github.config.Config;
5+
import com.hsjfans.github.model.ApiTree;
6+
import com.hsjfans.github.model.ControllerClass;
7+
import com.hsjfans.github.model.ControllerMethod;
8+
import com.hsjfans.github.model.RequestParam;
9+
import com.hsjfans.github.util.CollectionUtil;
10+
import com.hsjfans.github.util.FileUtil;
11+
import com.hsjfans.github.util.StringUtil;
12+
13+
314
/**
415
*
516
* html 文档生成器
617
*
718
* @author hsjfans[[email protected]]
819
*/
920
public class HtmlGenerator extends AbstractGenerator {
21+
22+
23+
private static final String BASE_TPL_PATH = "src/main/resources/tpl/";
24+
private static final String controllerTpl = "api-controller.html";
25+
private static final String Index = "index.html";
26+
private static final String urlTpl = "api-url.html";
27+
private static final String extraTpl = "js.html";
28+
29+
30+
@Override
31+
public void from(ApiTree apiTree, Config config) {
32+
this.config = config;
33+
buildExtraDoc();
34+
StringBuilder controllerList = new StringBuilder();
35+
apiTree.getSet().forEach(
36+
controllerClass -> {
37+
controllerList.append(String.format("\n <li style=\"padding:5px\" > <a href=\"%s\" >%s</a> <span> %s </span> </li>"
38+
,"./"+controllerClass.getName()+".html",controllerClass.getName(),controllerClass.getDescription()));
39+
buildControllerDoc(controllerClass);
40+
}
41+
);
42+
String indexHtml = FileUtil.from(BASE_TPL_PATH+Index);
43+
indexHtml = indexHtml.replace("${api-doc-description}",config.getDocName());
44+
indexHtml = indexHtml.replace("${api-doc-name}",config.getDocName());
45+
indexHtml = indexHtml.replace("${api-controller-item}",controllerList.toString());
46+
indexHtml = indexHtml.replace("${count}",String.valueOf(apiTree.getSet().size()));
47+
FileUtil.to(this.config.getOutPath()+"index.html",indexHtml);
48+
49+
}
50+
51+
52+
@Override
53+
protected void buildControllerDoc(ControllerClass controllerClass) {
54+
55+
StringBuilder controllerHtml = new StringBuilder();
56+
String controller = FileUtil.from(BASE_TPL_PATH+controllerTpl);
57+
// controller = controller.replace("${api-url-description}",controllerClass.getName());
58+
controller = controller.replace("${controller-name}",controllerClass.getName());
59+
controller = controller.replace("${count}",String.valueOf(controllerClass.getControllerMethod().size()));
60+
controller = controller.replace("${controller-description}",controllerClass.getDescription());
61+
controller = controller.replace("${author}",controllerClass.getAuthor());
62+
controller = controller.replace("${baseUrl}", StringUtil.join(controllerClass.getUrl(),","));
63+
controllerClass.getControllerMethod().forEach(controllerMethod -> {
64+
controllerHtml.append(String.format("\n <li style=\"padding:5px\"> <a href=\"%s\" >%s</a> <span> %s </span> </li>"
65+
,"./"+controllerClass.getName()+"_"+controllerMethod.getName()+".html",controllerMethod.getName(),controllerMethod.getName()));
66+
buildApiDoc(controllerClass,controllerMethod);
67+
});
68+
controller = controller.replace("${controller-methods}",controllerHtml.toString());
69+
FileUtil.to(this.config.getOutPath()+controllerClass.getName()+".html",controller);
70+
}
71+
72+
@Override
73+
protected void buildApiDoc(ControllerClass controllerClass, ControllerMethod controllerMethod) {
74+
75+
String method = FileUtil.from(BASE_TPL_PATH+urlTpl);
76+
method = method.replace("${title}",controllerMethod.getName());
77+
method = method.replace("${api-url-name}",controllerMethod.getName());
78+
method = method.replace("${prev-name}",controllerClass.getName());
79+
method = method.replace("${prev-url}",controllerClass.getName()+".html");
80+
if(controllerClass.getUrl().length==0){
81+
controllerClass.setUrl(new String[]{""});
82+
}
83+
if(controllerMethod.getUrl().length==0){
84+
controllerMethod.setUrl(new String[]{""});
85+
}
86+
String[] urls = new String[controllerClass.getUrl().length*controllerMethod.getUrl().length];
87+
int i=0;
88+
for(String baseUrl:controllerClass.getUrl()){
89+
for(String url:controllerMethod.getUrl()){
90+
urls[i++] = baseUrl+url;
91+
}
92+
}
93+
method = method.replace("${urls}",StringUtil.join(urls,","));
94+
method = method.replace("${api-url-description}",controllerMethod.getDescription());
95+
method = method.replace("${methods}", CollectionUtil.requestMethodsToString(controllerMethod.getMethods()));
96+
method = method.replace("${author}",controllerMethod.getAuthor());
97+
98+
StringBuilder params = new StringBuilder();
99+
controllerMethod.getParams().forEach(requestParam->{
100+
if(requestParam.getParams()==null){
101+
params.append(String.format(" \n <tr>\n" +
102+
" <td>%s</td>\n" +
103+
" <td>%s</td>\n" +
104+
" <td>%s</td>\n" +
105+
" <td>%s</td>\n" +
106+
" <td>%s</td>\n" +
107+
" <td>%s</td>\n" +
108+
" </tr> ",requestParam.getName(),requestParam.getType(),
109+
StringUtil.enumToStrs(requestParam.getEnumValues()),requestParam.isNecessary(),requestParam.isFuzzy(),
110+
requestParam.getDescription()));
111+
}else {
112+
// todo
113+
}
114+
});
115+
method = method.replace("${requestParams}", params.toString());
116+
117+
StringBuilder responses = new StringBuilder();
118+
responses.append(String.format(
119+
" <tr>\n" +
120+
" <td>%s</td>\n" +
121+
" <td>%s</td>\n" +
122+
" <td>%s</td>\n" +
123+
" </tr>"
124+
,controllerMethod.getResponseReturn().getName(),
125+
controllerMethod.getResponseReturn().getType(),
126+
controllerMethod.getResponseReturn().getDescription()));
127+
128+
method = method.replace("${responses}", responses.toString());
129+
130+
131+
FileUtil.to(this.config.getOutPath()+controllerClass.getName()+"_"+controllerMethod.getName()+".html",method);
132+
}
133+
134+
@Override
135+
protected void buildExtraDoc() {
136+
String extra = FileUtil.from(BASE_TPL_PATH+extraTpl);
137+
FileUtil.to(this.config.getOutPath()+extraTpl,extra);
138+
}
10139
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
package com.hsjfans.github.generator;
22

3+
import com.hsjfans.github.config.Config;
4+
import com.hsjfans.github.model.ApiTree;
5+
import com.hsjfans.github.model.ControllerClass;
6+
import com.hsjfans.github.model.ControllerMethod;
7+
38
/**
49
*
510
* markdown 文档生成器
611
*
712
* @author hsjfans[[email protected]]
813
*/
914
public class MarkdownGenerator extends AbstractGenerator {
15+
@Override
16+
protected void buildControllerDoc(ControllerClass controllerClass) {
17+
18+
}
19+
20+
@Override
21+
protected void buildApiDoc(ControllerClass controllerClass, ControllerMethod controllerMethod) {
22+
23+
}
24+
25+
@Override
26+
protected void buildExtraDoc() {
27+
28+
}
29+
30+
31+
@Override
32+
public void from(ApiTree apiTree, Config config) {
33+
34+
}
1035
}

src/main/java/com/hsjfans/github/model/ControllerClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class ControllerClass implements Serializable {
4545
/**
4646
* author
4747
*/
48-
private String author;
48+
private String author = "";
4949

5050

5151
private String description;

src/main/java/com/hsjfans/github/model/ControllerMethod.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public class ControllerMethod implements Serializable {
6666
/**
6767
* author
6868
*/
69-
private String author;
69+
private String author = "";
7070

71-
private String description;
71+
private String description = "";
7272

7373

7474

src/main/java/com/hsjfans/github/model/ResponseReturn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ResponseReturn {
1818
/**
1919
* the description
2020
*/
21-
private String description;
21+
private String description = "";
2222

2323

2424
/**

src/main/java/com/hsjfans/github/util/ClassUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import com.github.javaparser.javadoc.JavadocBlockTag;
1313
import com.google.common.collect.Lists;
1414
import com.hsjfans.github.config.Config;
15+
import com.hsjfans.github.generator.Generator;
16+
import com.hsjfans.github.generator.HtmlGenerator;
1517
import com.hsjfans.github.model.*;
1618
import com.hsjfans.github.model.RequestParam;
1719
import com.hsjfans.github.parser.ClassCache;
@@ -206,9 +208,9 @@ public static ControllerMethod parseMethodComment(MethodDeclaration methodDeclar
206208
}
207209

208210
controllerMethod.setParams(requestParams);
209-
responseReturn.setType(method.getReturnType().getTypeName());
211+
responseReturn.setType(method.getReturnType().getSimpleName());
210212
if(responseReturn.getName()==null){
211-
responseReturn.setName(method.getReturnType().getName());
213+
responseReturn.setName(method.getReturnType().getSimpleName());
212214
}
213215
controllerMethod.setResponseReturn(responseReturn);
214216
if(controllerMethod.isIgnore()){return null;}
@@ -383,10 +385,14 @@ public static void main(String[] args) throws ParserException {
383385
String realPath = "/Volumes/doc/projects/java/api";
384386
Config config = new Config();
385387
config.setPackageName(realPath);
388+
config.setDocName("xxx接口文档");
386389
config.setGradle(true);
387390
config.setGradlePath("");
388391
Parser parser = new SpringParser(config);
389-
parser.parse(config.getPackageName(),true);
392+
ApiTree apiTree = parser.parse(config.getPackageName(),true);
393+
// System.out.println(apiTree);
394+
Generator generator = new HtmlGenerator();
395+
generator.from(apiTree,config);
390396

391397
}
392398

0 commit comments

Comments
 (0)