9 changed files with 149 additions and 12 deletions
@ -1,5 +1,5 @@ |
|||||
dependencies { |
dependencies { |
||||
implementation 'mysql:mysql-connector-java:8.0.27' |
testImplementation 'mysql:mysql-connector-java:8.0.27' |
||||
implementation 'commons-io:commons-io:2.11.0' |
|
||||
implementation 'org.commonmark:commonmark:0.18.1' |
implementation 'org.commonmark:commonmark:0.18.1' |
||||
|
implementation 'org.freemarker:freemarker:2.3.31' |
||||
} |
} |
||||
|
@ -0,0 +1,57 @@ |
|||||
|
package com.databasir.core.render.markdown; |
||||
|
|
||||
|
import com.databasir.core.meta.pojo.DatabaseMeta; |
||||
|
import com.databasir.core.render.Render; |
||||
|
import com.databasir.core.render.RenderConfig; |
||||
|
import freemarker.template.Configuration; |
||||
|
import freemarker.template.Template; |
||||
|
import freemarker.template.TemplateException; |
||||
|
import freemarker.template.TemplateExceptionHandler; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.io.OutputStream; |
||||
|
import java.io.OutputStreamWriter; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* use freemarker template to render markdown |
||||
|
*/ |
||||
|
@RequiredArgsConstructor |
||||
|
public class MarkdownTemplateRender implements Render { |
||||
|
|
||||
|
private final RenderConfig renderConfig; |
||||
|
|
||||
|
private String templatePath = "template/render/markdown/markdown.ftlh"; |
||||
|
|
||||
|
public MarkdownTemplateRender(RenderConfig config, String templatePath) { |
||||
|
this(config); |
||||
|
this.templatePath = templatePath; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void rendering(DatabaseMeta meta, OutputStream outputStream) throws IOException { |
||||
|
doRendering(meta, outputStream); |
||||
|
} |
||||
|
|
||||
|
public void doRendering(DatabaseMeta meta, OutputStream outputStream) throws IOException { |
||||
|
Configuration cfg = new Configuration(Configuration.VERSION_2_3_29); |
||||
|
cfg.setClassForTemplateLoading(getClass(), "/"); |
||||
|
cfg.setDefaultEncoding("UTF-8"); |
||||
|
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); |
||||
|
cfg.setLogTemplateExceptions(false); |
||||
|
cfg.setWrapUncheckedExceptions(true); |
||||
|
cfg.setFallbackOnNullLoopVariable(false); |
||||
|
|
||||
|
Map<String, Object> root = new HashMap<>(); |
||||
|
root.put("meta", meta); |
||||
|
root.put("config", renderConfig); |
||||
|
Template template = cfg.getTemplate(templatePath); |
||||
|
try { |
||||
|
template.process(root, new OutputStreamWriter(outputStream)); |
||||
|
} catch (TemplateException e) { |
||||
|
throw new IllegalStateException(e); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,44 @@ |
|||||
|
# ${meta.databaseName} |
||||
|
|
||||
|
| seq | name | comment | |
||||
|
| ---- | --------------- | ------ | |
||||
|
<#list meta.tables as table> |
||||
|
| ${table_index+1} | [${table.tableName}](#${table.tableName}) | ${table.tableComment!'N/A'} | |
||||
|
</#list> |
||||
|
|
||||
|
<#if config.renderTables> |
||||
|
<#list meta.tables as table> |
||||
|
## ${table.tableName} |
||||
|
|
||||
|
<#if config.renderColumns> |
||||
|
### Columns |
||||
|
|
||||
|
| seq | name | type | nullable | auto increment| default | comment | |
||||
|
| ---- | ------ | ------ | ------ | -------- | ------ | ------ | |
||||
|
<#list table.columns as column> |
||||
|
| ${column_index+1} | ${column.name} | ${column.type} | ${column.isNullable?then('YES','')} | ${column.isAutoIncrement?then('YES', '')} | ${column.isNullable?then(column.defaultValue!'NULL', column.defaultValue!'')} | ${column.comment!''} | |
||||
|
</#list> |
||||
|
</#if> |
||||
|
|
||||
|
<#if config.renderIndexes> |
||||
|
### Indexes |
||||
|
|
||||
|
| seq | name | unique | primary key | columns | |
||||
|
| ---- | ---- | -------- | -------- | ------ | |
||||
|
<#list table.indexes as index> |
||||
|
| ${index_index+1} | ${index.indexName} | ${index.isUniqueKey?then('YES', '')} | ${index.isPrimaryKey?then('YES','')} | ${index.columnNames?join(', ')} | |
||||
|
</#list> |
||||
|
</#if> |
||||
|
|
||||
|
<#if config.renderTriggers> |
||||
|
### Triggers |
||||
|
|
||||
|
| seq | name | timing | statement | created | |
||||
|
| ---- | ---- | -------- | --------- | -------- | |
||||
|
<#list table.triggers as trigger> |
||||
|
| ${trigger_index} | ${trigger.name} | ${trigger.timing + " " + trigger.manipulation } | ${trigger.statement?replace("\n", "<br>")?replace("\r", " ")} | ${trigger.createAt} | |
||||
|
</#list> |
||||
|
</#if> |
||||
|
|
||||
|
</#list> |
||||
|
</#if> |
Loading…
Reference in new issue