Skip to content

Commit

Permalink
feat: add build single word template
Browse files Browse the repository at this point in the history
  • Loading branch information
chenqi146 committed Dec 14, 2023
1 parent 42befd6 commit d390c01
Show file tree
Hide file tree
Showing 6 changed files with 2,714 additions and 10 deletions.
28 changes: 26 additions & 2 deletions src/main/java/com/ly/doc/builder/DocBuilderTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,25 @@ public void buildSearchJs(ApiConfig config, JavaProjectBuilder javaProjectBuilde
* @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
*/
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);
FileUtil.nioWriteFile(tpl.render(), config.getOutPath() + DocGlobalConstants.FILE_SEPARATOR + outPutFileName);
return tpl;
}

/**
Expand Down Expand Up @@ -396,6 +408,18 @@ public void buildDirectoryDataDoc(ApiConfig config, JavaProjectBuilder javaProje
* @param outPutFileName output file
*/
public void buildDirectoryDataDoc(ApiConfig config, JavaProjectBuilder javaProjectBuilder, String template, String outPutFileName) {
Template mapper = buildDirectoryDataDocTemplate(config, javaProjectBuilder, template);
FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + DocGlobalConstants.FILE_SEPARATOR + outPutFileName);
}

/**
* build common_data doc Template
*
* @param config api config
* @param javaProjectBuilder JavaProjectBuilder
* @param template template
*/
public Template buildDirectoryDataDocTemplate(ApiConfig config, JavaProjectBuilder javaProjectBuilder, String template) {
List<ApiDocDict> directoryList = DocUtil.buildDictionary(config, javaProjectBuilder);
Template mapper = BeetlTemplateUtil.getByName(template);
setDirectoryLanguageVariable(config, mapper);
Expand All @@ -404,7 +428,7 @@ public void buildDirectoryDataDoc(ApiConfig config, JavaProjectBuilder javaProje
String strTime = DateTimeUtil.long2Str(now, DateTimeUtil.DATE_FORMAT_SECOND);
mapper.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
mapper.binding(TemplateVariable.DICT_LIST.getVariable(), directoryList);
FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + DocGlobalConstants.FILE_SEPARATOR + outPutFileName);
return mapper;
}

private List<ApiDoc> listOfApiData(ApiConfig config, JavaProjectBuilder javaProjectBuilder) {
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/com/ly/doc/builder/WordDocBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import com.ly.doc.model.ApiConfig;
import com.ly.doc.model.ApiDoc;
import com.ly.doc.template.IDocBuildTemplate;
import com.power.common.util.DateTimeUtil;
import com.power.common.util.FileUtil;
import com.thoughtworks.qdox.JavaProjectBuilder;
import org.beetl.core.Template;
import org.beetl.core.io.IOUtil;
import org.beetl.core.resource.ClasspathResourceLoader;

import java.io.*;
import java.nio.charset.StandardCharsets;
Expand All @@ -27,9 +26,13 @@
* @version 1.0
*/
public class WordDocBuilder {
private static final String BUILD_DOCX = "AllInOne.docx";
private static final String TEMPLATE_DOCX = "template/word/template.docx";


private static final String BUILD_DOCX = "index.docx";
private static final String BUILD_ERROR_DOCX = "error.docx";
private static final String BUILD_DICT_DOCX = "dict.docx";

/**
* build controller api
*
Expand All @@ -55,16 +58,24 @@ public static void buildApiDoc(ApiConfig config, JavaProjectBuilder javaProjectB
List<ApiDoc> apiDocList = docBuildTemplate.getApiData(configBuilder);

if (config.isAllInOne()) {
String docName = builderTemplate.allInOneDocName(config, BUILD_DOCX, ".docx");
String version = config.isCoverOld() ? "" : "-V" + DateTimeUtil.long2Str(System.currentTimeMillis(), DocGlobalConstants.DATE_FORMAT_YYYY_MM_DD_HH_MM);
String docName = builderTemplate.allInOneDocName(config, "AllInOne" + version + ".docx", ".docx");
apiDocList = docBuildTemplate.handleApiGroup(apiDocList, config);
String outPath = config.getOutPath();
FileUtil.mkdirs(outPath);
Template tpl = builderTemplate.buildAllRenderDocTemplate(apiDocList, config, javaProjectBuilder, DocGlobalConstants.ALL_IN_ONE_WORD_XML_TPL, null, null);
replaceDocx(tpl.render(), outPath + DocGlobalConstants.FILE_SEPARATOR + docName);
// todo docs模板还有错误码和字典没有弄
// todo 单个的文档还未渲染
copyAndReplaceDocx(tpl.render(), outPath + DocGlobalConstants.FILE_SEPARATOR + docName);
} else {
FileUtil.mkdir(config.getOutPath());
for (ApiDoc doc : apiDocList) {
Template template = builderTemplate.buildApiDocTemplate(doc, config, DocGlobalConstants.WORD_XML_TPL);
copyAndReplaceDocx(template.render(), config.getOutPath() + DocGlobalConstants.FILE_SEPARATOR + doc.getName() + BUILD_DOCX);
}
Template errorCodeDocTemplate = builderTemplate.buildErrorCodeDocTemplate(config, DocGlobalConstants.WORD_ERROR_XML_TPL, javaProjectBuilder);
copyAndReplaceDocx(errorCodeDocTemplate.render(), config.getOutPath() + DocGlobalConstants.FILE_SEPARATOR + BUILD_ERROR_DOCX);

Template directoryDataDocTemplate = builderTemplate.buildDirectoryDataDocTemplate(config, javaProjectBuilder, DocGlobalConstants.WORD_DICT_XML_TPL);
copyAndReplaceDocx(directoryDataDocTemplate.render(), config.getOutPath() + DocGlobalConstants.FILE_SEPARATOR + BUILD_DICT_DOCX);
}
}

Expand All @@ -77,7 +88,7 @@ public static void buildApiDoc(ApiConfig config, JavaProjectBuilder javaProjectB
* @return
* @since 1.0.0
*/
public static void replaceDocx(String content, String docxOutputPath) throws Exception {
public static void copyAndReplaceDocx(String content, String docxOutputPath) throws Exception {
InputStream resourceAsStream = WordDocBuilder.class.getClassLoader().getResourceAsStream(TEMPLATE_DOCX);
Objects.requireNonNull(resourceAsStream, "word template docx is not found");

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/ly/doc/constants/DocGlobalConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public interface DocGlobalConstants {
String ALL_IN_ONE_HTML_TPL = "AllInOne.html";

String ALL_IN_ONE_WORD_XML_TPL = "word/AllInOneWordTemplate.xml";
String WORD_XML_TPL = "word/index.xml";
String WORD_ERROR_XML_TPL = "word/error.xml";
String WORD_DICT_XML_TPL = "word/dict.xml";

String HTML_API_DOC_TPL = "HtmlApiDoc.html";

Expand Down Expand Up @@ -261,4 +264,6 @@ public interface DocGlobalConstants {
String JAR_TEMP = "./smart-temp/";

String DEFAULT_PRIMITIVE = "defaultPrimitive";

String DATE_FORMAT_YYYY_MM_DD_HH_MM = "yyyyMMddHHmm";
}
Loading

0 comments on commit d390c01

Please sign in to comment.