|  | 
| 1 | 1 | package com.hsjfans.github.generator; | 
| 2 | 2 | 
 | 
|  | 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 | + | 
| 3 | 14 | /** | 
| 4 | 15 |  * | 
| 5 | 16 |  * html 文档生成器 | 
| 6 | 17 |  * | 
| 7 | 18 |  * @author hsjfans[hsjfans.scholar@gmail.com] | 
| 8 | 19 |  */ | 
| 9 | 20 | 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 | + } | 
| 10 | 139 | } | 
0 commit comments