-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql-wbplugin-htmlmodeldoc.py
200 lines (152 loc) · 5.95 KB
/
mysql-wbplugin-htmlmodeldoc.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# MySQL Workbench Plugin
# Generate HTML documentation from a model
# (c) 2018 JSJ
#
# Based on the 'Generate Markdown documentation from a model' by Hieu Le
from wb import *
import grt
import mforms
import cgi
ModuleInfo = DefineModule("HTMLModelDocumentation", author="JSJ",
version="1.0.0", description="Generate HTML documentation from a model")
# This plugin takes no arguments
@ModuleInfo.plugin("es.jsj.wb.htmldocumentation", caption="Generate documentation (HTML)", description="Generate HTML documentation from a model.\n\nBased on script by Hieu Le.", input=[wbinputs.currentDiagram()], pluginMenu="Utilities")
@ModuleInfo.export(grt.INT, grt.classes.db_Catalog)
def documentation(diagram):
text = "<!DOCTYPE html>\n<html>\n"
text += "<head>\n"
text += "<!-- Schema documentation -->\n"
text += "<!-- Generated by MySQL Workbench Model HTML Documentation v1.0.1 - Copyright (c) 2018 JSJ -->\n\n"
text += "<title>MySQL Workbench Model HTML Documentation</title>\n"
text += "<style>\n"
text += " table {\n"
text += " width: 80%;\n"
text += " margin-left: 10%;\n"
text += " }\n"
text += " table, td {\n"
text += " border: 1px solid;\n"
text += " border-collapse: collapse;\n"
text += " }\n"
text += "</style>\n"
text += "</head>\n"
text += "<body>\n\n"
text += "<h1>Model Documentation</h1>\n"
text += "<h2>" + diagram.name + "</h2>\n"
# text += "<p>" + diagram.description + "</p>\n"
text += "\n"
text += "<h3>Tables summary</h3>\n"
text += "<table>\n"
for figure in diagram.figures:
if hasattr(figure, "table") and figure.table:
text += writeTableDesc(figure.table)
text += "</table>\n<br />\n\n"
text += "<h3>Tables detail</h3>\n"
for figure in diagram.figures:
if hasattr(figure, "table") and figure.table:
text += writeTableDoc(figure.table)
text += "\n</body>\n</html>"
mforms.Utilities.set_clipboard_text(text)
mforms.App.get().set_status_text(
"Documentation generated into the clipboard. Paste it to your editor.")
print "Documentation is copied to the clipboard."
return 0
def writeTableDesc(table):
text = "<tr><td><strong>" + table.name + "</strong></td>"
text += "<td>" + table.comment + "</td></tr>\n"
return text
def writeTableDoc(table):
text = "<table>\n"
text += "<tr><td colspan=\"5\"><strong>TABLE:</strong> <i>" + \
table.name + "</i></td></tr>\n"
text += "<tr><td colspan=\"5\"><strong>DESCRIPTION:</strong> "
text += "<br/>" + table.comment + "</td></tr>\n"
text += "<tr><td><strong>Column</strong></td><td><strong>Data type</strong></td><td><strong>Attributes</strong></td><td><strong>Default</strong></td><td><strong>Description</strong></td></tr>\n"
for column in table.columns:
text += "<tr>"
text += writeColumnDoc(column, table)
text += "</tr>"
text += "\n"
if (len(table.indices)):
text += "<tr><td colspan=\"5\"><strong>INDICES</strong></td></tr>\n"
text += "<tr><td><strong>Name</strong></td><td><strong>Columns</strong></td><td><strong>Type</strong></td><td colspan=\"2\"><strong>Description</strong></td></tr>\n"
for index in table.indices:
text += writeIndexDoc(index)
text += "</table>\n<br />\n\n"
return text
def writeColumnDoc(column, table):
# column name
text = "<td><strong>" + column.name + "</strong></td>"
# column type name
if column.simpleType:
text += "<td>" + column.simpleType.name
# column max lenght if any
if column.length != -1:
text += "(" + str(column.length) + ")"
text += "</td>"
else:
text += "<td></td>"
# column attributes
attribs = []
isPrimary = False
isUnique = False
for index in table.indices:
if index.indexType == "PRIMARY":
for c in index.columns:
if c.referencedColumn.name == column.name:
isPrimary = True
break
if index.indexType == "UNIQUE":
for c in index.columns:
if c.referencedColumn.name == column.name:
isUnique = True
break
# primary?
if isPrimary:
attribs.append("PRIMARY")
# auto increment?
if column.autoIncrement == 1:
attribs.append("Auto increments")
# not null?
if column.isNotNull == 1:
attribs.append("Not null")
# unique?
if isUnique:
attribs.append("Unique")
text += "<td>"
text += ", ".join(attribs)
text += "</td>"
# column default value
text += "<td>" + \
(column.defaultValue if column.defaultValue else "") + "</td>"
# column description
text += "<td>" + \
htmlentityencode((nl2br(column.comment) if column.comment else ""))
# foreign key
for fk in table.foreignKeys:
if fk.columns[0].name == column.name:
text += ("<br /><br />" if column.comment else "") + "Foreign key to column <i>" + \
fk.referencedColumns[0].name + "</i> on table <i>" + \
fk.referencedColumns[0].owner.name + "</i>."
break
# finish
text += "</td>"
return text
def writeIndexDoc(index):
text = "<tr>"
# index name
text += "<td>" + index.name + "</td>"
# index columns
text += "<td>" + ", ".join(map(lambda x: "<i>" +
x.referencedColumn.name + "</i>", index.columns)) + "</td>"
# index type
text += "<td>" + index.indexType + "</td>"
# index description
text += "<td colspan=\"2\">" + \
(nl2br(index.comment) if index.comment else " ")
# finish
text += "</td></tr>\n"
return text
def nl2br(text):
return "<br />".join(map(lambda x: x.strip(), text.split("\n")))
def htmlentityencode(s):
return cgi.escape(s).decode('utf-8').encode('ascii', 'xmlcharrefreplace')