001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.apidoc;
017
018import com.jfinal.template.Engine;
019
020import java.io.File;
021import java.io.IOException;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026public interface ApiDocRender {
027    ApiDocRender MARKDOWN_RENDER = new ApiDocRender() {
028        private Engine engine = new Engine("apidoc");
029        private String template = "#(\"#\") #(document.value ??)\n" +
030                "\n" +
031                "#(document.notes ??)" +
032                "#for(operation : document.apiOperations)" +
033                "\n" +
034                "\n" +
035                "\n" +
036                "#(\"##\") #(operation.value ??)" +
037                "\n" +
038                "#if(operation.notes)" +
039                "\n\n#(operation.notes ??) \n" +
040                "#end" +
041                "\n" +
042                "#('####') 接口信息:\n" +
043                "- 访问路径: `#(operation.actionKey ??)`\n" +
044                "- 数据类型: `#(operation.contentType.value ??)`\n" +
045
046                "#if(operation.hasParameter())" +
047                "\n#('####') 请求参数:\n" +
048                "\n" +
049                "| 参数 | 名称 | 数据类型 | 是否必须 | 提交方式 | 描述 |  \n" +
050                "| --- | --- | --- | --- | --- | --- |\n" +
051                "#for(parameter : operation.apiParameters)" +
052                "| #(parameter.name ??) | #(parameter.value ??) | `#(parameter.dataType ??)` | #(parameter.require ? '是' : '否') | #(parameter.httpMethodsString ??) | #(parameter.notesString ??) |  \n" +
053                "#end" +
054                "#end" + //参数表格信息
055
056                "\n" +
057                "\n" +
058
059                "#if(operation.paraNotes)" +
060                "> #(operation.paraNotes ??)" +
061                "#end" + //参数配置
062
063                "#if(operation.retType)" +
064                "\n" +
065                "\n" +
066                "#('####') 数据响应:`#(operation.retType ??)`\n\n" +
067
068                "#for(item : operation.retRemarks)" +
069                "#(item.key ??)\n\n" +
070                "| 字段  | 数据类型 | 描述 |  \n" +
071                "| --- | --- | --- | \n" +
072                "#for(info : item.value)" +
073                "| #(info.field ??) | `#(info.classType ??)` | #(info.remarks ??) |  \n" +
074                "#end" +
075                "\n\n" +
076                "#end" + //end 响应字段表格
077
078                "#if(operation.retMockJson)" +
079                "**JSON 示例:**\n" +
080                "```json\n" +
081                "#(operation.retMockJson ??)\n" +
082                "```" +
083                "#end" + //end json示例
084
085                "#end" + // end operation
086                "\n" +
087                "\n" +
088                "#end";
089
090        @Override
091        public void render(List<ApiDocument> apiDocuments, ApiDocConfig config) {
092            try {
093                for (ApiDocument document : apiDocuments) {
094                    doRender(document, config);
095                }
096            } catch (IOException e) {
097                e.printStackTrace();
098            }
099        }
100
101        private void doRender(ApiDocument document, ApiDocConfig config) throws IOException {
102            Map<String, Object> templateParas = new HashMap<>();
103            templateParas.put("config", config);
104            templateParas.put("document", document);
105
106            File file = new File(config.getBasePathAbsolute(), document.getFilePath() + ".md");
107            if (!file.exists()) {
108                file.getParentFile().mkdirs();
109                file.createNewFile();
110            }
111            System.err.println("Jboot generated apidoc -----> " + file.getCanonicalPath());
112            engine.getTemplateByString(template).render(templateParas, file);
113        }
114    };
115
116
117     void render(List<ApiDocument> apiDocuments, ApiDocConfig config);
118}