# MySQL Workbench Plugin
# <description>
# Written in MySQL Workbench 6.3.4

from wb import *
import grt
#import mforms

ModuleInfo = DefineModule("ModelDocumentation", author="Hieu Le", version="1.0.0", description="Generate Markdown documentation from a model")

# This plugin takes no arguments
@ModuleInfo.plugin("info.hieule.wb.documentation", caption="Generate documentation (Markdown)", description="description", input=[wbinputs.currentCatalog()], pluginMenu="Utilities")
@ModuleInfo.export(grt.INT, grt.classes.db_Catalog)
def documentation(catalog):
    text = "#Schema documentation\n\n"
    text += "Generated by MySQL Workbench Model Documentation v1.0.0 - Copyright (c) 2015 Hieu Le\n\n";

    schema = catalog.schemata[0]
    for table in schema.tables:
        text += writeTableDoc(table)
        for column in table.columns:
            text += writeColumnDoc(column)
        text += "\n\n"

    print text

    return 0

def writeTableDoc(table):
    text = "##Table: `" + table.name + "`\n\n"

    text += "**Description:**\n\n"

    text += table.comment + "\n\n"

    text += "**Columns:**\n\n"


    text += "|Column|Data type|Description\n|-|-|-|\n"
    return text


def writeColumnDoc(column):
    text = "|`" + column.name + "`|" + column.simpleType.name

    if column.length != -1:
        text += "(" + str(column.length) + ")"

    text += "|" + (nl2br(column.comment) if column.comment else "*no description*") +  "|" + "\n"
    return text

def nl2br(text):
    return "<br />".join(map(lambda x: x.strip(), text.split("\n")))
# MySQL Workbench Plugin
# <description>
# Written in MySQL Workbench 6.3.4

from wb import *
import grt
#import mforms

ModuleInfo = DefineModule("ModelDocumentation", author="Hieu Le", version="1.0.0", description="Generate Markdown documentation from a model")

# This plugin takes no arguments
@ModuleInfo.plugin("info.hieule.wb.documentation", caption="Generate documentation (Markdown)", description="description", input=[wbinputs.currentCatalog()], pluginMenu="Utilities")
@ModuleInfo.export(grt.INT, grt.classes.db_Catalog)
def documentation(catalog):
    text = "#Schema documentation\n\n"
    text += "Generated by MySQL Workbench Model Documentation v1.0.0 - Copyright (c) 2015 Hieu Le\n\n";

    schema = catalog.schemata[0]
    for table in schema.tables:
        text += writeTableDoc(table)
        for column in table.columns:
            text += writeColumnDoc(column, table)
        text += "\n\n"

    print text

    return 0

def writeTableDoc(table):
    text = "##Table: `" + table.name + "`\n\n"

    text += "**Description:**\n\n"

    text += table.comment + "\n\n"

    text += "**Columns:**\n\n"


    text += "|Column|Data type|Attributes|Default|Description\n|-|-|-|-|-|\n"
    return text


def writeColumnDoc(column, table):
    # column name
    text = "|`" + column.name

    # column type name
    text += "`|" + column.simpleType.name

    # column max lenght if any
    if column.length != -1:
        text += "(" + str(column.length) + ")"

    text += "|"

    # 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 += ", ".join(attribs)

    # column default value
    text += "|" + (("`" + column.defaultValue + "`") if column.defaultValue else " ")

    # column description
    text += "|" + (nl2br(column.comment) if column.comment else "*no description*") +  "|" + "\n"
    return text

def nl2br(text):
    return "<br />".join(map(lambda x: x.strip(), text.split("\n")))
Add column attributes