Skip to content

Commit

Permalink
Merge pull request #855 from linwumingshi/master
Browse files Browse the repository at this point in the history
feat: ✨ Add WebSocket html, ascii doc support
  • Loading branch information
shalousun authored Jul 22, 2024
2 parents 9983318 + 5c8f9a0 commit 95ea2aa
Show file tree
Hide file tree
Showing 43 changed files with 1,524 additions and 695 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/ly/doc/builder/AdocDocBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void buildApiDoc(ApiConfig config, JavaProjectBuilder javaProjectB
Objects.requireNonNull(docBuildTemplate, "doc build template is null");
List<ApiDoc> apiDocList = docBuildTemplate.getApiData(configBuilder).getApiDatas();
if (config.isAllInOne()) {
String docName = builderTemplate.allInOneDocName(config, INDEX_DOC, ".adoc");
String docName = builderTemplate.allInOneDocName(config, INDEX_DOC, DocGlobalConstants.ASCIIDOC_EXTENSION);
apiDocList = docBuildTemplate.handleApiGroup(apiDocList, config);
builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder,
DocGlobalConstants.ALL_IN_ONE_ADOC_TPL, docName);
Expand Down
131 changes: 35 additions & 96 deletions src/main/java/com/ly/doc/builder/DocBuilderTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
*/
package com.ly.doc.builder;

import com.ly.doc.constants.*;
import com.ly.doc.constants.DocGlobalConstants;
import com.ly.doc.constants.HighlightStyle;
import com.ly.doc.constants.TemplateVariable;
import com.ly.doc.constants.TornaConstants;
import com.ly.doc.factory.BuildTemplateFactory;
import com.ly.doc.model.*;
import com.ly.doc.template.IDocBuildTemplate;
Expand All @@ -43,7 +46,7 @@
*
* @author yu 2019/9/26.
*/
public class DocBuilderTemplate implements IBaseDocBuilderTemplate {
public class DocBuilderTemplate implements IBaseDocBuilderTemplate<ApiDoc> {

/**
* get all api data
Expand Down Expand Up @@ -73,7 +76,7 @@ public ApiAllData getApiData(ApiConfig config, JavaProjectBuilder javaProjectBui
public void buildApiDoc(List<ApiDoc> apiDocList, ApiConfig config, String template, String fileExtension) {
FileUtil.mkdirs(config.getOutPath());
for (ApiDoc doc : apiDocList) {
Template mapper = buildApiDocTemplate(doc, config, template);
Template mapper = this.buildApiDocTemplate(doc, config, template);
FileUtil.nioWriteFile(mapper.render(),
config.getOutPath() + DocGlobalConstants.FILE_SEPARATOR + doc.getName() + fileExtension);
}
Expand Down Expand Up @@ -106,16 +109,11 @@ public Template buildApiDocTemplate(ApiDoc doc, ApiConfig config, String templat
*/
public void buildAllInOne(List<ApiDoc> apiDocList, ApiConfig config, JavaProjectBuilder javaProjectBuilder,
String template, String outPutFileName) {
buildDoc(apiDocList, config, javaProjectBuilder, template, outPutFileName, null, null);
}

public void buildWebSocket(List<WebSocketDoc> webSocketDocList, ApiConfig config,
JavaProjectBuilder javaProjectBuilder, String template, String outPutFileName) {
buildWebSocketDoc(webSocketDocList, config, javaProjectBuilder, template, outPutFileName, null);
this.buildDoc(apiDocList, config, javaProjectBuilder, template, outPutFileName, null, null);
}

/**
* get render doc template
* get render doc template.
* @param apiDocList list data of Api doc
* @param config api config
* @param javaProjectBuilder JavaProjectBuilder
Expand Down Expand Up @@ -145,9 +143,9 @@ public Template buildAllRenderDocTemplate(List<ApiDoc> apiDocList, ApiConfig con
tpl.binding(TemplateVariable.RESPONSE_EXAMPLE.getVariable(), config.isResponseExample());
tpl.binding(TemplateVariable.DISPLAY_REQUEST_PARAMS.getVariable(), config.isRequestParamsTable());
tpl.binding(TemplateVariable.DISPLAY_RESPONSE_PARAMS.getVariable(), config.isResponseParamsTable());
setCssCDN(config, tpl);
this.setCssCDN(config, tpl);

setDirectoryLanguageVariable(config, tpl);
this.setDirectoryLanguageVariable(config, tpl);
List<ApiDocDict> apiDocDictList = DocUtil.buildDictionary(config, javaProjectBuilder);
tpl.binding(TemplateVariable.DICT_LIST.getVariable(), apiDocDictList);

Expand Down Expand Up @@ -189,34 +187,7 @@ public Template buildAllRenderDocTemplate(List<ApiDoc> apiDocList, ApiConfig con
}

/**
* get render doc template
* @param webSocketDocList list data of webSocket doc
* @param config api config
* @param javaProjectBuilder JavaProjectBuilder
* @param template template
* @param index index html
* @return Template
*/
public Template buildAllWebSocketDocTemplate(List<WebSocketDoc> webSocketDocList, ApiConfig config,
JavaProjectBuilder javaProjectBuilder, String template, String index) {
String strTime = DateTimeUtil.long2Str(NOW, DateTimeUtil.DATE_FORMAT_SECOND);
Template tpl = BeetlTemplateUtil.getByName(template);
String style = config.getStyle();
tpl.binding(TemplateVariable.VERSION_LIST.getVariable(), config.getRevisionLogs());
tpl.binding(TemplateVariable.STYLE.getVariable(), style);
tpl.binding(TemplateVariable.HIGH_LIGHT_CSS_LINK.getVariable(), config.getHighlightStyleLink());
tpl.binding(TemplateVariable.BACKGROUND.getVariable(), HighlightStyle.getBackgroundColor(style));
tpl.binding(TemplateVariable.WEBSOCKET_DOC_LIST.getVariable(), webSocketDocList);
tpl.binding(TemplateVariable.LANGUAGE.getVariable(), config.getLanguage());
tpl.binding(TemplateVariable.VERSION.getVariable(), NOW);
tpl.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
tpl.binding(TemplateVariable.PROJECT_NAME.getVariable(), config.getProjectName());
setCssCDN(config, tpl);
return tpl;
}

/**
* Merge all api doc into one document
* Merge all api doc into one document.
* @param apiDocList list data of Api doc
* @param config api config
* @param javaProjectBuilder JavaProjectBuilder
Expand All @@ -229,18 +200,17 @@ public void buildDoc(List<ApiDoc> apiDocList, ApiConfig config, JavaProjectBuild
String template, String outPutFileName, ApiDoc apiDoc, String index) {
String outPath = config.getOutPath();
FileUtil.mkdirs(outPath);
Template tpl = buildAllRenderDocTemplate(apiDocList, config, javaProjectBuilder, template, apiDoc, index);
FileUtil.nioWriteFile(tpl.render(), outPath + DocGlobalConstants.FILE_SEPARATOR + outPutFileName);
}

public void buildWebSocketDoc(List<WebSocketDoc> webSocketDocList, ApiConfig config,
JavaProjectBuilder javaProjectBuilder, String template, String outPutFileName, String index) {
String outPath = config.getOutPath();
FileUtil.mkdirs(outPath);
Template tpl = buildAllWebSocketDocTemplate(webSocketDocList, config, javaProjectBuilder, template, index);
Template tpl = this.buildAllRenderDocTemplate(apiDocList, config, javaProjectBuilder, template, apiDoc, index);
FileUtil.nioWriteFile(tpl.render(), outPath + DocGlobalConstants.FILE_SEPARATOR + outPutFileName);
}

/**
* build search js.
* @param config api config
* @param javaProjectBuilder JavaProjectBuilder
* @param apiDocList list data of Api doc
* @param template template
*/
public void buildSearchJs(ApiConfig config, JavaProjectBuilder javaProjectBuilder, List<ApiDoc> apiDocList,
String template) {
List<ApiErrorCode> errorCodeList = DocUtil.errorCodeDictToList(config, javaProjectBuilder);
Expand All @@ -253,7 +223,7 @@ public void buildSearchJs(ApiConfig config, JavaProjectBuilder javaProjectBuilde
}

boolean isOnlyDefaultGroup = apiDocList.size() == 1;
Map<String, String> titleMap = setDirectoryLanguageVariable(config, tpl);
Map<String, String> titleMap = this.setDirectoryLanguageVariable(config, tpl);
// set error code
if (CollectionUtil.isNotEmpty(errorCodeList)) {
ApiDoc apiDoc1 = new ApiDoc();
Expand Down Expand Up @@ -333,38 +303,7 @@ public void buildSearchJs(ApiConfig config, JavaProjectBuilder javaProjectBuilde
}

/**
* build error_code adoc
* @param config api config
* @param template template
* @param outPutFileName output file
* @param javaProjectBuilder javaProjectBuilder
*/
public void buildErrorCodeDoc(ApiConfig config, String template, String outPutFileName,
JavaProjectBuilder javaProjectBuilder) {
Template tpl = buildErrorCodeDocTemplate(config, template, javaProjectBuilder);
FileUtil.nioWriteFile(tpl.render(), config.getOutPath() + DocGlobalConstants.FILE_SEPARATOR + outPutFileName);
}

/**
* build errorCode adoc template
* @param config api config
* @param template template
* @param javaProjectBuilder javaProjectBuilder
* @return template
*/
public Template buildErrorCodeDocTemplate(ApiConfig config, String template,
JavaProjectBuilder javaProjectBuilder) {
List<ApiErrorCode> errorCodeList = DocUtil.errorCodeDictToList(config, javaProjectBuilder);
String strTime = DateTimeUtil.long2Str(NOW, DateTimeUtil.DATE_FORMAT_SECOND);
Template tpl = BeetlTemplateUtil.getByName(template);
setCssCDN(config, tpl);
tpl.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
tpl.binding(TemplateVariable.LIST.getVariable(), errorCodeList);
return tpl;
}

/**
* build error_code html
* build error_code html.
* @param config api config
* @param javaProjectBuilder javaProjectBuilder
* @param apiDocList list data of Api doc
Expand All @@ -388,7 +327,7 @@ public void buildErrorCodeDoc(ApiConfig config, JavaProjectBuilder javaProjectBu
errorTemplate.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 2);
}
// set css cdn
setCssCDN(config, errorTemplate);
this.setCssCDN(config, errorTemplate);
List<ApiDocDict> apiDocDictList = DocUtil.buildDictionary(config, javaProjectBuilder);
errorTemplate.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
errorTemplate.binding(TemplateVariable.VERSION.getVariable(), NOW);
Expand All @@ -404,7 +343,7 @@ public void buildErrorCodeDoc(ApiConfig config, JavaProjectBuilder javaProjectBu
}

/**
* build common_data doc
* build common_data doc.
* @param config api config
* @param javaProjectBuilder JavaProjectBuilder
* @param apiDocList list data of Api doc
Expand All @@ -423,13 +362,7 @@ public void buildDirectoryDataDoc(ApiConfig config, JavaProjectBuilder javaProje
mapper.binding(TemplateVariable.STYLE.getVariable(), style);
List<ApiErrorCode> errorCodeList = DocUtil.errorCodeDictToList(config, javaProjectBuilder);
// set css cdn
setCssCDN(config, mapper);
if (DocLanguage.CHINESE.equals(config.getLanguage())) {
mapper.binding(TemplateVariable.CSS_CND.getVariable(), DocGlobalConstants.CSS_CDN_CH);
}
else {
mapper.binding(TemplateVariable.CSS_CND.getVariable(), DocGlobalConstants.CSS_CDN);
}
this.setCssCDN(config, mapper);
if (CollectionUtil.isNotEmpty(errorCodeList)) {
mapper.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 2);
}
Expand All @@ -443,14 +376,14 @@ public void buildDirectoryDataDoc(ApiConfig config, JavaProjectBuilder javaProje
mapper.binding(TemplateVariable.INDEX_ALIAS.getVariable(), indexAlias);
mapper.binding(TemplateVariable.BACKGROUND.getVariable(), HighlightStyle.getBackgroundColor(style));
mapper.binding(TemplateVariable.ERROR_CODE_LIST.getVariable(), errorCodeList);
setDirectoryLanguageVariable(config, mapper);
this.setDirectoryLanguageVariable(config, mapper);
mapper.binding(TemplateVariable.DICT_LIST.getVariable(), directoryList);
FileUtil.nioWriteFile(mapper.render(),
config.getOutPath() + DocGlobalConstants.FILE_SEPARATOR + outPutFileName);
}

/**
* build common_data doc
* build common_data doc.
* @param config api config
* @param javaProjectBuilder JavaProjectBuilder
* @param template template
Expand All @@ -464,7 +397,7 @@ public void buildDirectoryDataDoc(ApiConfig config, JavaProjectBuilder javaProje
}

/**
* build common_data doc Template
* build common_data doc Template.
* @param config api config
* @param javaProjectBuilder JavaProjectBuilder
* @param template template
Expand All @@ -474,15 +407,21 @@ public Template buildDirectoryDataDocTemplate(ApiConfig config, JavaProjectBuild
String template) {
List<ApiDocDict> directoryList = DocUtil.buildDictionary(config, javaProjectBuilder);
Template mapper = BeetlTemplateUtil.getByName(template);
setDirectoryLanguageVariable(config, mapper);
this.setDirectoryLanguageVariable(config, mapper);
// set css cdn
setCssCDN(config, mapper);
this.setCssCDN(config, mapper);
String strTime = DateTimeUtil.long2Str(NOW, DateTimeUtil.DATE_FORMAT_SECOND);
mapper.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
mapper.binding(TemplateVariable.DICT_LIST.getVariable(), directoryList);
return mapper;
}

/**
* get api data.
* @param config api config
* @param javaProjectBuilder JavaProjectBuilder
* @return ApiData list
*/
private List<ApiDoc> listOfApiData(ApiConfig config, JavaProjectBuilder javaProjectBuilder) {
this.checkAndInitForGetApiData(config);
config.setMd5EncryptedHtmlName(true);
Expand Down
73 changes: 64 additions & 9 deletions src/main/java/com/ly/doc/builder/IBaseDocBuilderTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
import com.ly.doc.constants.DocLanguage;
import com.ly.doc.constants.FrameworkEnum;
import com.ly.doc.constants.TemplateVariable;
import com.ly.doc.model.ApiConfig;
import com.ly.doc.model.ApiDocDict;
import com.ly.doc.model.ApiErrorCode;
import com.ly.doc.model.RevisionLog;
import com.ly.doc.factory.BuildTemplateFactory;
import com.ly.doc.model.*;
import com.ly.doc.template.IDocBuildTemplate;
import com.ly.doc.utils.BeetlTemplateUtil;
import com.ly.doc.utils.DocUtil;
import com.power.common.util.CollectionUtil;
Expand All @@ -48,19 +47,44 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Logger;

/**
* doc builder template interface.
*
* @author yu 2020/5/16.
*/
public interface IBaseDocBuilderTemplate {
public interface IBaseDocBuilderTemplate<T extends IDoc> {

/**
* Logger for the class.
*/
Logger log = Logger.getLogger(IBaseDocBuilderTemplate.class.getName());

/**
* now currentTimeMillis.
*/
long NOW = System.currentTimeMillis();

/**
* get all doc api data
* @param config ApiConfig
* @param javaProjectBuilder JavaProjectBuilder
* @return ApiAllData
*/
default List<T> getApiDoc(boolean adoc, boolean showJavaType, boolean paramsDataToTree, ApiConfig config,
JavaProjectBuilder javaProjectBuilder) {
config.setAdoc(adoc);
config.setShowJavaType(showJavaType);
config.setParamsDataToTree(paramsDataToTree);
this.checkAndInit(config, Boolean.TRUE);
ProjectDocConfigBuilder configBuilder = new ProjectDocConfigBuilder(config, javaProjectBuilder);
IDocBuildTemplate<T> docBuildTemplate = BuildTemplateFactory.getDocBuildTemplate(config.getFramework(),
config.getClassLoader());
Objects.requireNonNull(docBuildTemplate, "doc build template is null");
return docBuildTemplate.getApiData(configBuilder).getApiDatas();
}

/**
* check condition and init
* @param config Api config
Expand Down Expand Up @@ -193,8 +217,8 @@ default void bindingCommonVariable(ApiConfig config, JavaProjectBuilder javaProj
if (CollectionUtil.isNotEmpty(apiDocDictList)) {
template.binding(TemplateVariable.DICT_ORDER.getVariable(), ++codeIndex);
}
setDirectoryLanguageVariable(config, template);
setCssCDN(config, template);
this.setDirectoryLanguageVariable(config, template);
this.setCssCDN(config, template);
}

/**
Expand All @@ -206,7 +230,7 @@ default void bindingCommonVariable(ApiConfig config, JavaProjectBuilder javaProj
*/
default String allInOneDocName(ApiConfig apiConfig, String fileName, String suffix) {
String allInOneName = apiConfig.getAllInOneDocFileName();
if (StringUtils.isNotEmpty(apiConfig.getAllInOneDocFileName())) {
if (StringUtils.isNotEmpty(allInOneName)) {
if (allInOneName.endsWith(suffix)) {
return allInOneName;
}
Expand Down Expand Up @@ -269,8 +293,39 @@ static void copyJarFile(String source, String target) {
bufferedWriter.close();
}
catch (IOException e) {
e.printStackTrace();
log.warning("copy jar file error:" + e.getMessage());
}
}

/**
* build error_code adoc.
* @param config api config
* @param template template
* @param outPutFileName output file
* @param javaProjectBuilder javaProjectBuilder
*/
default void buildErrorCodeDoc(ApiConfig config, String template, String outPutFileName,
JavaProjectBuilder javaProjectBuilder) {
Template tpl = this.buildErrorCodeDocTemplate(config, template, javaProjectBuilder);
FileUtil.nioWriteFile(tpl.render(), config.getOutPath() + DocGlobalConstants.FILE_SEPARATOR + outPutFileName);
}

/**
* build errorCode adoc template.
* @param config api config
* @param template template
* @param javaProjectBuilder javaProjectBuilder
* @return template
*/
default Template buildErrorCodeDocTemplate(ApiConfig config, String template,
JavaProjectBuilder javaProjectBuilder) {
List<ApiErrorCode> errorCodeList = DocUtil.errorCodeDictToList(config, javaProjectBuilder);
String strTime = DateTimeUtil.long2Str(NOW, DateTimeUtil.DATE_FORMAT_SECOND);
Template tpl = BeetlTemplateUtil.getByName(template);
this.setCssCDN(config, tpl);
tpl.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
tpl.binding(TemplateVariable.LIST.getVariable(), errorCodeList);
return tpl;
}

}
Loading

0 comments on commit 95ea2aa

Please sign in to comment.