-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.go
More file actions
67 lines (56 loc) · 1.99 KB
/
render.go
File metadata and controls
67 lines (56 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"net/url"
"strings"
)
// escapeMD escapes special markdown characters in table cell content.
func escapeMD(s string) string {
s = strings.ReplaceAll(s, "|", "\\|")
s = strings.ReplaceAll(s, "\n", " ")
return s
}
// RenderTableIndex generates a markdown table-of-contents listing all tables.
func RenderTableIndex(dbName string, tables []Table) string {
var b strings.Builder
fmt.Fprintf(&b, "# %s tables list\n", dbName)
b.WriteString("| Name | Engine | Create_time | Collation | Comment |\n")
b.WriteString("| ---- | ------ | ----------- | --------- | ------- |\n")
for _, table := range tables {
fileName := fmt.Sprintf("%s.%s.md", dbName, table.Name)
fmt.Fprintf(&b, "| [%s](%s) | %s | %s | %s | %s |\n",
escapeMD(table.Name),
url.PathEscape(fileName),
escapeMD(table.Engine),
escapeMD(table.CreateTime),
escapeMD(table.Collation),
escapeMD(table.Comment),
)
}
return b.String()
}
// RenderTableDetail generates the markdown documentation for a single table.
func RenderTableDetail(dbName string, table Table, columns []TableColumn, ddl *TableDDL, includeDDL bool) string {
var b strings.Builder
fmt.Fprintf(&b, "# %s.%s\n> %s\n", dbName, table.Name, escapeMD(table.Comment))
b.WriteString("### COLUMNS\n")
b.WriteString("| COLUMN_NAME | COLUMN_DEFAULT | IS_NULLABLE | COLLATION_NAME | COLUMN_TYPE | COLUMN_KEY | EXTRA | COLUMN_COMMENT |\n")
b.WriteString("| ----------- | -------------- | ----------- | -------------- | ----------- | ---------- | ----- | -------------- |\n")
for _, col := range columns {
fmt.Fprintf(&b, "| %s | %s | %s | %s | %s | %s | %s | %s |\n",
escapeMD(col.ColumnName),
escapeMD(col.ColumnDefault),
escapeMD(col.IsNullable),
escapeMD(col.CollationName),
escapeMD(col.ColumnType),
escapeMD(col.ColumnKey),
escapeMD(col.Extra),
escapeMD(col.ColumnComment),
)
}
if includeDDL && ddl != nil {
b.WriteString("### DDL\n")
fmt.Fprintf(&b, "```sql\n%s\n```\n", ddl.CreateTable)
}
return b.String()
}