|
| 1 | +package io.swagger.codegen.languages.html; |
| 2 | + |
| 3 | +import io.swagger.codegen.CliOption; |
| 4 | +import io.swagger.codegen.CodegenConfig; |
| 5 | +import io.swagger.codegen.CodegenConstants; |
| 6 | +import io.swagger.codegen.CodegenModel; |
| 7 | +import io.swagger.codegen.CodegenOperation; |
| 8 | +import io.swagger.codegen.CodegenParameter; |
| 9 | +import io.swagger.codegen.CodegenProperty; |
| 10 | +import io.swagger.codegen.CodegenResponse; |
| 11 | +import io.swagger.codegen.CodegenType; |
| 12 | +import io.swagger.codegen.SupportingFile; |
| 13 | +import io.swagger.codegen.languages.DefaultCodegenConfig; |
| 14 | +import io.swagger.codegen.utils.Markdown; |
| 15 | +import io.swagger.v3.oas.models.OpenAPI; |
| 16 | +import io.swagger.v3.oas.models.info.Info; |
| 17 | +import io.swagger.v3.oas.models.media.ArraySchema; |
| 18 | +import io.swagger.v3.oas.models.media.MapSchema; |
| 19 | +import io.swagger.v3.oas.models.media.Schema; |
| 20 | +import org.apache.commons.lang3.StringUtils; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +public class StaticHtmlCodegen extends DefaultCodegenConfig implements CodegenConfig { |
| 27 | + protected String invokerPackage = "io.swagger.client"; |
| 28 | + protected String groupId = "io.swagger"; |
| 29 | + protected String artifactId = "swagger-client"; |
| 30 | + protected String artifactVersion = "1.0.0"; |
| 31 | + |
| 32 | + public StaticHtmlCodegen() { |
| 33 | + super(); |
| 34 | + outputFolder = "docs"; |
| 35 | + |
| 36 | + defaultIncludes = new HashSet<String>(); |
| 37 | + |
| 38 | + cliOptions.add(new CliOption("appName", "short name of the application")); |
| 39 | + cliOptions.add(new CliOption("appDescription", "description of the application")); |
| 40 | + cliOptions.add(new CliOption("infoUrl", "a URL where users can get more information about the application")); |
| 41 | + cliOptions.add(new CliOption("infoEmail", "an email address to contact for inquiries about the application")); |
| 42 | + cliOptions.add(new CliOption("licenseInfo", "a short description of the license")); |
| 43 | + cliOptions.add(new CliOption("licenseUrl", "a URL pointing to the full license")); |
| 44 | + cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, CodegenConstants.INVOKER_PACKAGE_DESC)); |
| 45 | + cliOptions.add(new CliOption(CodegenConstants.GROUP_ID, CodegenConstants.GROUP_ID_DESC)); |
| 46 | + cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_ID, CodegenConstants.ARTIFACT_ID_DESC)); |
| 47 | + cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_VERSION, CodegenConstants.ARTIFACT_VERSION_DESC)); |
| 48 | + |
| 49 | + additionalProperties.put("appName", "Swagger Sample"); |
| 50 | + additionalProperties.put("appDescription", "A sample swagger server"); |
| 51 | + additionalProperties.put("infoUrl", "https://helloreverb.com"); |
| 52 | + additionalProperties. put( "infoEmail", "[email protected]"); |
| 53 | + additionalProperties.put("licenseInfo", "All rights reserved"); |
| 54 | + additionalProperties.put("licenseUrl", "http://apache.org/licenses/LICENSE-2.0.html"); |
| 55 | + additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage); |
| 56 | + additionalProperties.put(CodegenConstants.GROUP_ID, groupId); |
| 57 | + additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId); |
| 58 | + additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion); |
| 59 | + |
| 60 | + supportingFiles.add(new SupportingFile("index.mustache", "", "index.html")); |
| 61 | + reservedWords = new HashSet<String>(); |
| 62 | + |
| 63 | + languageSpecificPrimitives = new HashSet<String>(); |
| 64 | + importMapping = new HashMap<String, String>(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Convert Markdown (CommonMark) to HTML. This class also disables normal HTML |
| 69 | + * escaping in the Mustache engine (see processCompiler(Compiler) above.) |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public String escapeText(String input) { |
| 73 | + // newline escaping disabled for HTML documentation for markdown to work correctly |
| 74 | + return toHtml(input); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public CodegenType getTag() { |
| 79 | + return CodegenType.DOCUMENTATION; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String getArgumentsLocation() { |
| 84 | + return ""; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String getName() { |
| 89 | + return "html"; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String getHelp() { |
| 94 | + return "Generates a static HTML file."; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String getTypeDeclaration(Schema propertySchema) { |
| 99 | + if (propertySchema instanceof ArraySchema) { |
| 100 | + Schema inner = ((ArraySchema) propertySchema).getItems(); |
| 101 | + return String.format("%s[%s]", getSchemaType(propertySchema), getTypeDeclaration(inner)); |
| 102 | + } |
| 103 | + else if (propertySchema instanceof MapSchema) { |
| 104 | + Schema inner = (Schema) propertySchema.getAdditionalProperties(); |
| 105 | + return String.format("%s[String, %s]", getSchemaType(propertySchema), getTypeDeclaration(inner)); |
| 106 | + } |
| 107 | + return super.getTypeDeclaration(propertySchema); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Map<String, Object> postProcessOperations(Map<String, Object> objs) { |
| 112 | + Map<String, Object> operations = (Map<String, Object>) objs.get("operations"); |
| 113 | + List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation"); |
| 114 | + for (CodegenOperation op : operationList) { |
| 115 | + op.httpMethod = op.httpMethod.toLowerCase(); |
| 116 | + for (CodegenResponse response : op.responses) { |
| 117 | + if ("0".equals(response.code)) { |
| 118 | + response.code = "default"; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + return objs; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void processOpts() { |
| 127 | + super.processOpts(); |
| 128 | + |
| 129 | + String templateVersion = getTemplateVersion(); |
| 130 | + if (StringUtils.isNotBlank(templateVersion)) { |
| 131 | + embeddedTemplateDir = templateDir = String.format("%s/htmlDocs", templateVersion); |
| 132 | + } |
| 133 | + else { |
| 134 | + embeddedTemplateDir = templateDir = String.format("%s/htmlDocs", DEFAULT_TEMPLATE_VERSION); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public String escapeQuotationMark(String input) { |
| 140 | + // just return the original string |
| 141 | + return input; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public String escapeUnsafeCharacters(String input) { |
| 146 | + // just return the original string |
| 147 | + return input; |
| 148 | + } |
| 149 | + |
| 150 | + private Markdown markdownConverter = new Markdown(); |
| 151 | + |
| 152 | + /** |
| 153 | + * Convert Markdown text to HTML |
| 154 | + * |
| 155 | + * @param input |
| 156 | + * text in Markdown; may be null. |
| 157 | + * @return the text, converted to Markdown. For null input, "" is returned. |
| 158 | + */ |
| 159 | + public String toHtml(String input) { |
| 160 | + if (input == null) |
| 161 | + return ""; |
| 162 | + return markdownConverter.toHtml(input); |
| 163 | + } |
| 164 | + |
| 165 | + // DefaultCodegen converts model names to UpperCamelCase |
| 166 | + // but for static HTML, we want the names to be preserved as coded in the OpenApi |
| 167 | + // so HTML links work |
| 168 | + @Override |
| 169 | + public String toModelName(final String name) { |
| 170 | + return name; |
| 171 | + } |
| 172 | + |
| 173 | + public void preprocessOpenAPI(OpenAPI openAPI) { |
| 174 | + Info info = openAPI.getInfo(); |
| 175 | + info.setDescription(toHtml(info.getDescription())); |
| 176 | + info.setTitle(toHtml(info.getTitle())); |
| 177 | + if (openAPI.getComponents() == null || openAPI.getComponents().getSchemas() == null) { |
| 178 | + return; |
| 179 | + } |
| 180 | + Map<String, Schema> schemas = openAPI.getComponents().getSchemas(); |
| 181 | + for (Schema schema : schemas.values()) { |
| 182 | + schema.setDescription(toHtml(schema.getDescription())); |
| 183 | + schema.setTitle(toHtml(schema.getTitle())); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + // override to post-process any parameters |
| 188 | + public void postProcessParameter(CodegenParameter parameter) { |
| 189 | + parameter.description = toHtml(parameter.description); |
| 190 | + parameter.unescapedDescription = toHtml(parameter.unescapedDescription); |
| 191 | + } |
| 192 | + |
| 193 | + // override to post-process any model properties |
| 194 | + public void postProcessModelProperty(CodegenModel model, CodegenProperty property) { |
| 195 | + property.description = toHtml(property.description); |
| 196 | + property.unescapedDescription = toHtml(property.unescapedDescription); |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments