update html api doc builder

This commit is contained in:
oppofind 2020-07-31 10:13:45 +08:00
parent 02da8be1f5
commit d628336519
1 changed files with 27 additions and 24 deletions

View File

@ -40,7 +40,9 @@ import com.thoughtworks.qdox.JavaProjectBuilder;
import org.apache.commons.lang3.StringUtils;
import org.beetl.core.Template;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.power.doc.constants.DocGlobalConstants.*;
@ -52,6 +54,8 @@ public class HtmlApiDocBuilder {
private static long now = System.currentTimeMillis();
private static final String STR_TIME = DateTimeUtil.long2Str(now, DateTimeUtil.DATE_FORMAT_SECOND);
private static String INDEX_HTML = "index.html";
@ -80,7 +84,7 @@ public class HtmlApiDocBuilder {
if (config.isAllInOne()) {
Template indexCssTemplate = BeetlTemplateUtil.getByName(ALL_IN_ONE_CSS);
FileUtil.nioWriteFile(indexCssTemplate.render(), config.getOutPath() + FILE_SEPARATOR + ALL_IN_ONE_CSS);
if(StringUtils.isNotEmpty(config.getAllInOneDocFileName())){
if (StringUtils.isNotEmpty(config.getAllInOneDocFileName())) {
INDEX_HTML = config.getAllInOneDocFileName();
}
builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, ALL_IN_ONE_HTML_TPL, INDEX_HTML);
@ -116,8 +120,8 @@ public class HtmlApiDocBuilder {
ApiDoc doc = apiDocList.get(0);
String homePage = doc.getAlias();
indexTemplate.binding(TemplateVariable.HOME_PAGE.getVariable(), homePage);
indexTemplate.binding(TemplateVariable.API_DOC_LIST.getVariable(), apiDocList);
indexTemplate.binding(TemplateVariable.VERSION.getVariable(), now);
indexTemplate.binding(TemplateVariable.API_DOC_LIST.getVariable(), apiDocList);
indexTemplate.binding(TemplateVariable.ERROR_CODE_LIST.getVariable(), config.getErrorCodes());
indexTemplate.binding(TemplateVariable.DICT_LIST.getVariable(), config.getDataDictionaries());
if (CollectionUtil.isEmpty(config.getErrorCodes())) {
@ -149,19 +153,14 @@ public class HtmlApiDocBuilder {
private static void buildDoc(List<ApiDoc> apiDocList, String outPath) {
FileUtil.mkdirs(outPath);
Template htmlApiDoc;
String strTime = DateTimeUtil.long2Str(now, DateTimeUtil.DATE_FORMAT_SECOND);
for (ApiDoc doc : apiDocList) {
Template apiTemplate = BeetlTemplateUtil.getByName(API_DOC_MD_TPL);
apiTemplate.binding(TemplateVariable.DESC.getVariable(), doc.getDesc());
apiTemplate.binding(TemplateVariable.NAME.getVariable(), doc.getName());
apiTemplate.binding(TemplateVariable.LIST.getVariable(), doc.getList());//类名
String html = MarkDownUtil.toHtml(apiTemplate.render());
htmlApiDoc = BeetlTemplateUtil.getByName(HTML_API_DOC_TPL);
htmlApiDoc.binding(TemplateVariable.HTML.getVariable(), html);
htmlApiDoc.binding(TemplateVariable.TITLE.getVariable(), doc.getDesc());
htmlApiDoc.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
htmlApiDoc.binding(TemplateVariable.VERSION.getVariable(), now);
Map<String, Object> templateVariables = new HashMap<>();
templateVariables.put(TemplateVariable.TITLE.getVariable(), doc.getDesc());
htmlApiDoc = initTemplate(apiTemplate, HTML_API_DOC_TPL, templateVariables);
FileUtil.nioWriteFile(htmlApiDoc.render(), outPath + FILE_SEPARATOR + doc.getAlias() + ".html");
}
}
@ -174,14 +173,11 @@ public class HtmlApiDocBuilder {
*/
private static void buildErrorCodeDoc(List<ApiErrorCode> errorCodeList, String outPath) {
if (CollectionUtil.isNotEmpty(errorCodeList)) {
Template error = BeetlTemplateUtil.getByName(ERROR_CODE_LIST_MD_TPL);
error.binding(TemplateVariable.LIST.getVariable(), errorCodeList);
String errorHtml = MarkDownUtil.toHtml(error.render());
Template errorCodeDoc = BeetlTemplateUtil.getByName(HTML_API_DOC_TPL);
errorCodeDoc.binding(TemplateVariable.VERSION.getVariable(), now);
errorCodeDoc.binding(TemplateVariable.TITLE.getVariable(), ERROR_CODE_LIST_EN_TITLE);
errorCodeDoc.binding(TemplateVariable.HTML.getVariable(), errorHtml);
errorCodeDoc.binding(TemplateVariable.CREATE_TIME.getVariable(), DateTimeUtil.long2Str(now, DateTimeUtil.DATE_FORMAT_SECOND));
Template errorTemplate = BeetlTemplateUtil.getByName(ERROR_CODE_LIST_MD_TPL);
errorTemplate.binding(TemplateVariable.LIST.getVariable(), errorCodeList);
Map<String, Object> templateVariables = new HashMap<>();
templateVariables.put(TemplateVariable.TITLE.getVariable(), ERROR_CODE_LIST_EN_TITLE);
Template errorCodeDoc = initTemplate(errorTemplate, HTML_API_DOC_TPL, templateVariables);
FileUtil.nioWriteFile(errorCodeDoc.render(), outPath + FILE_SEPARATOR + "error_code.html");
}
}
@ -196,13 +192,20 @@ public class HtmlApiDocBuilder {
if (CollectionUtil.isNotEmpty(apiDocDictList)) {
Template template = BeetlTemplateUtil.getByName(DICT_LIST_MD_TPL);
template.binding(TemplateVariable.DICT_LIST.getVariable(), apiDocDictList);
String dictHtml = MarkDownUtil.toHtml(template.render());
Template dictTpl = BeetlTemplateUtil.getByName(HTML_API_DOC_TPL);
dictTpl.binding(TemplateVariable.VERSION.getVariable(), now);
dictTpl.binding(TemplateVariable.TITLE.getVariable(), DICT_EN_TITLE);
dictTpl.binding(TemplateVariable.HTML.getVariable(), dictHtml);
dictTpl.binding(TemplateVariable.CREATE_TIME.getVariable(), DateTimeUtil.long2Str(now, DateTimeUtil.DATE_FORMAT_SECOND));
Map<String, Object> templateVariables = new HashMap<>();
templateVariables.put(TemplateVariable.TITLE.getVariable(), DICT_EN_TITLE);
Template dictTpl = initTemplate(template, HTML_API_DOC_TPL, templateVariables);
FileUtil.nioWriteFile(dictTpl.render(), outPath + FILE_SEPARATOR + "dict.html");
}
}
private static Template initTemplate(Template template, String templateName, Map<String, Object> templateVariables) {
String errorHtml = MarkDownUtil.toHtml(template.render());
Template template1 = BeetlTemplateUtil.getByName(templateName);
template1.binding(TemplateVariable.VERSION.getVariable(), now);
template1.binding(TemplateVariable.HTML.getVariable(), errorHtml);
template1.binding(TemplateVariable.CREATE_TIME.getVariable(), STR_TIME);
template1.binding(templateVariables);
return template1;
}
}