Skip to content

Commit

Permalink
update html api doc builder
Browse files Browse the repository at this point in the history
  • Loading branch information
shalousun committed Jul 31, 2020
1 parent 02da8be commit d628336
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/main/java/com/power/doc/builder/HtmlApiDocBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
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.*;

Expand All @@ -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";


Expand Down Expand Up @@ -80,7 +84,7 @@ public static void buildApiDoc(ApiConfig config, JavaProjectBuilder javaProjectB
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);
Expand Down Expand Up @@ -116,8 +120,8 @@ private static void buildIndex(List<ApiDoc> apiDocList, ApiConfig config) {
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())) {
Expand Down Expand Up @@ -149,19 +153,14 @@ private static void buildIndex(List<ApiDoc> apiDocList, ApiConfig config) {
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");
}
}
Expand All @@ -174,14 +173,11 @@ private static void buildDoc(List<ApiDoc> apiDocList, String outPath) {
*/
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");
}
}
Expand All @@ -196,13 +192,20 @@ private static void buildDictionary(List<ApiDocDict> apiDocDictList, String outP
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;
}
}

0 comments on commit d628336

Please sign in to comment.