Commits

bworkman  committed b9043eb

support singleFile mode, add functional elements

  • Participants
  • Parent commits b8c83c7

Comments (0)

Files changed (1)

 import re
 from pprint import pprint, pformat
 
-PROC_CSS = """html, body
-{
-        width:100%;
-        height:100%;
-        margin:0px;
-}
+__CSS = "html,body{ width:100%; height:100%; margin:0px}body{ width:578px; max-width:578px; margin:24px auto 0px 36px}p,table{ margin:0px 0px 0px 12px}nav{ display:block; margin-bottom:6px; text-align:center}nav.main a{ font-size:1.05em}h1{ margin-top:0px}h1,h4{ margin-bottom:0px}div#author{ text-align:right; font-style:italic}span.date{ font:bold 14px Monospace,small-caps}table{ font-family:'Trebuchet MS',Arial,Helvetica,sans-serif; width:100%; border-collapse:collapse}table td,table th { font-size:1em; border:1px solid SlateGray; padding:3px 7px 2px 7px}table th{ font-size:1.1em; text-align:left; padding-top:5px; padding-bottom:4px; background-color:LightSlateGray; color:#fff}table tr.alt td { color:#000; background-color:#C9CFD6}"
 
-body
-{
-        width:578px;
-        max-width:578px;
-        margin:24px auto 0px 36px;
-}
-
-p, table
-{
-        margin:0px 0px 0px 12px;
-}
-
-nav
-{
-        display:block;
-        margin-bottom:6px;
-        text-align:center;
-}
-
-nav.main a
-{
-        font-size:1.05em;
-}
-
-h1
-{
-        margin-top:0px;
-}
-
-h1, h4
-{
-        margin-bottom:0px;
-}
-
-div#author
-{
-        text-align:right;
-        font-style:italic;
-}
-
-span.date
-{
-        font:bold 14px Monospace,small-caps;
-}
-
-table
-{
-        font-family:'Trebuchet MS', Arial, Helvetica, sans-serif;
-        width:100%;
-        border-collapse:collapse;
-}
-
-table td, table th 
-{
-        font-size:1em;
-        border:1px solid SlateGray;
-        padding:3px 7px 2px 7px;
-}
-
-table th
-{
-        font-size:1.1em;
-        text-align:left;
-        padding-top:5px;
-        padding-bottom:4px;
-        background-color:LightSlateGray;
-        color:#ffffff;
-}
-
-table tr.alt td 
-{
-        color:#000000;
-        background-color:#C9CFD6;
-}
-"""
-PROC_HTML = """<!DOCTYPE html>
+__PROC_HTML = """<!DOCTYPE html>
 <html>
         <head>
-                <title>pdb: ${proc_name}</title>
+                <title>pdb: ${name}</title>
 
-                <meta name="description" content="${proc_blurb}">
+                <meta name="description" content="${blurb}">
                 <meta name="author" content="Code Fox">
                 <meta charset="UTF-8">
 
-                <link rel="stylesheet" type="text/css" href="proc.css">
+                <link rel="stylesheet" type="text/css" href="../main.css">
         </head>
         <body>
-                <nav>
-                        <a href="${prev_link}" style="float:left;">previous</a>
-                        <a href="${index_link}">index</a>
-                        <a href="${next_link}" style="float:right;">next</a>
-                </nav>
-                <hr style="margin-bottom:0px;"/>
-                <div style="display:inline-block;">
-                        <h1>
-                                ${proc_name}
-                        </h1>
-                        <div id="author">
-                                ${proc_author}
-                        </div>
-                </div>
-                <hr style="margin-bottom:0px;"/>
-                <p id="copyright" style="margin:0">
-                        <span class="date">${proc_date}</span><br>
-                        ${proc_copyright}
-                </p>
-                <h4>Description</h4>
-                <p id="description">
-                        ${proc_blurb}
-                </p>
-                <h4>Help</h4>
-                <p id="help">
-                        ${proc_help}
-                </p>
-                <hr />
-                <h4>Type</h4>
-                <p id="type">
-                        ${proc_type}
-                </p>
-                <h4>Parameters</h4>
-                ${parameters}
-                <h4>Return</h4>
-                ${results}
-                <hr style="margin-bottom:0px;" />
-                <nav>
-                        <a href="${prev_link}" style="float:left;">previous</a>
-                        <a href="${index_link}">index</a>
-                        <a href="${next_link}" style="float:right;">next</a>
-                </nav>
+                ${body}
         </body>
 </html>
 """
-TABLE_HTML = """
-                <table>
-                        <thead>
-                                <tr>
-                                        <th>Type</th>
-                                        <th>Name</th>
-                                        <th>Description</th>
-                                </tr>
-                        </thead>
-                        <tbody>
-                                ${body}
-                        </tbody>
-                </table>
-"""
-TR_HTML = """
-                                <tr ${class}>
-                                        <td>
-                                                ${type}
-                                        </td>
-                                        <td>
-                                                ${name}
-                                        </td>
-                                        <td>
-                                                ${desc}
-                                        </td>
-                                </tr>
-
-"""
-INDEX_TEMPLATE = """<!DOCTYPE html>
+__INDEX_HTML = """<!DOCTYPE html>
 <html>
         <head>
                 <title>pdb: index</title>
                 <meta name="author" content="Code Fox">
                 <meta charset="UTF-8">
 
-                <link rel="stylesheet" type="text/css" href="main.css">
-
-                </style>
+                ${css}
         </head>
         <body>
                 <a id="top"></a>
                         <div style="display:inline-block; margin:0px auto 0px auto;">
                                 <h1>Gimp Plugin Database</h1>
                                 <div id="author">
-                                        <span class="date">
-                                                ${date}
-                                        </span>
+                                        <span class="date">${date}</span>
                                 </div>
                         </div>
                 </div>
 
         return html.encode('ascii', 'ignore')
 
-def slugify(value):
+def slugify(procedureName):
         """
         Normalizes string, converts to lowercase, removes non-alpha characters,
         and converts spaces to hyphens.
 
         Based on code in: Django
         """
-        value = unicodedata.normalize('NFKD', unicode(value))
-        value = value.encode('ascii', 'ignore')
-        value = re.sub('[^\w\s-]', '', value).strip().lower()
-        value = re.sub('[-\s]+', '-', value)
-        return value
+        procedureName = unicodedata.normalize('NFKD', unicode(procedureName))
+        procedureName = procedureName.encode('ascii', 'ignore')
+        procedureName = re.sub('[^\w\s-]', '', procedureName).strip().lower()
+        procedureName = re.sub('[-\s]+', '-', procedureName)
+        return procedureName
 
 def get_dir() :
 
 
         return response
 
-def prepare(root) :
+def variable_type(t) :
 
-        # create sub directory if it doesnt exist
-        outDirectory = os.path.join(
-                        root,
-                        "pdb"
-        )
 
-        if not os.path.exists(outDirectory):
-                os.makedirs(outDirectory)
-
-        with open(os.path.join(root, "main.css"),'w') as f:
-                f.write(PROC_CSS)
-
-        return None
-
-
-def var_type(t) :
         """
         in the documentation, but not defined in gimpfu
         PF_REGION:"PF_REGION"
         """
-        if (t not in {PF_INT8:"PF_INT8",PF_INT16:"PF_INT16",PF_INT32:"PF_INT32",PF_INT:"PF_INT",PF_FLOAT:"PF_FLOAT",PF_STRING:"PF_STRING",PF_VALUE:"PF_VALUE",PF_COLOR:"PF_COLOR",PF_COLOUR:"PF_COLOUR",PF_IMAGE:"PF_IMAGE",PF_LAYER:"PF_LAYER",PF_CHANNEL:"PF_CHANNEL",PF_DRAWABLE:"PF_DRAWABLE",PF_TOGGLE:"PF_TOGGLE",PF_BOOL:"PF_BOOL",PF_RADIO:"PF_RADIO",PF_SLIDER:"PF_SLIDER",PF_SPINNER:"PF_SPINNER",PF_ADJUSTMENT:"PF_ADJUSTMENT",PF_FONT:"PF_FONT",PF_FILE:"PF_FILE",PF_BRUSH:"PF_BRUSH",PF_PATTERN:"PF_PATTERN",PF_GRADIENT:"PF_GRADIENT",PF_PALETTE:"PF_PALETTE"}) :
+        types = {PF_INT8:"PF_INT8",PF_INT16:"PF_INT16",PF_INT32:"PF_INT32",PF_INT:"PF_INT",PF_FLOAT:"PF_FLOAT",PF_STRING:"PF_STRING",PF_VALUE:"PF_VALUE",PF_COLOR:"PF_COLOR",PF_COLOUR:"PF_COLOUR",PF_IMAGE:"PF_IMAGE",PF_LAYER:"PF_LAYER",PF_CHANNEL:"PF_CHANNEL",PF_DRAWABLE:"PF_DRAWABLE",PF_TOGGLE:"PF_TOGGLE",PF_BOOL:"PF_BOOL",PF_RADIO:"PF_RADIO",PF_SLIDER:"PF_SLIDER",PF_SPINNER:"PF_SPINNER",PF_ADJUSTMENT:"PF_ADJUSTMENT",PF_FONT:"PF_FONT",PF_FILE:"PF_FILE",PF_BRUSH:"PF_BRUSH",PF_PATTERN:"PF_PATTERN",PF_GRADIENT:"PF_GRADIENT",PF_PALETTE:"PF_PALETTE"}
+
+        if (t not in types) :
                 return "unknown"
 
-        return VAR_TYPES[t]
+        return types[t]
 
-def proc_type(t) :
+def procedure_type(t) :
 
-        if (t not in {PLUGIN:"PROC_PLUG_IN",EXTENSION:"PROC_EXTENSION",TEMPORARY:"PROC_TEMPORARY"}) :
+        types = {PLUGIN:"PROC_PLUG_IN",EXTENSION:"PROC_EXTENSION",TEMPORARY:"PROC_TEMPORARY"}
+
+        if (t not in types) :
                 return "unknown"
 
-        return PROC_TYPES[t]
+        return types[t]
 
-def table(lst) :
+def alt_table(variables) :
 
-        row = dict()
+        if(0 == len(variables)):
+                return "<p>None</p>"
+
+        table = '${offset}<table>\n${offset}\t<thead>\n${offset}\t\t<tr>\n${offset}\t\t\t<th>Type</th>\n${offset}\t\t\t<th>Name</th>\n${offset}\t\t\t<th>Description</th>\n${offset}\t\t</tr>\n${offset}\t</thead>\n${offset}\t<tbody>${body}\n${offset}\t</tbody>\n${offset}</table>'
+
+        tr = '\n${offset}<tr ${class}>\n${offset}\t<td>${type}</td>\n${offset}\t<td>${name}</td>\n${offset}\t<td>${desc}</td>\n${offset}</tr>'
+
+        row = {"offset":"\t\t\t\t"}
         html = ""
         alt = False
-        for item in lst :
+        for item in variables :
                 row["class"] = ""
-                if (True == alt) :
+                if (alt is True) :
                         row["class"] = ' class="alt"'
-                row["type"] = dump_var_type(item[0])
+                row["type"] = variable_type(item[0])
                 row["name"] = escape(item[1])
                 row["desc"] = escape(item[2])
 
-                html += Template(TR_HTML).safe_substitute(row)
+                html += Template(tr).safe_substitute(row)
                 alt = not alt
 
-        return Template(TABLE_HTML).safe_substitute({"body":html})
+        return Template(table).safe_substitute({"body":html, "offset":"\t\t\t"})
 
-def file_name(name) :
+def index_table(links, maxRowLen=5):
 
-        return "%s.html"%(slugify(name))
+        if(0 == len(links)):
+                return ""
 
-def plain_table(heading, lst, max_row_length=5):
+        buf = '\t\t<table>\n\t\t\t<tr>\n'
+        count = 0
+        for link in links:
+                count += 1
 
-        buf = ""
-        buf += Template('\t\t<a id="${heading}"><h4>${heading}</h4></a>\n').safe_substitute({"heading":heading})
-        buf += '\t\t<table>\n\t\t\t<tr>\n'
-        count = 0
-        for item in lst:
-                count += 1
-                # special
-                buf += Template('\t\t\t\t<td><a href="pdb/${file}.html" title="${item}">${item}</a></td>\n').safe_substitute({"file":file_name(item), "item":item})
-                # special
-                if 0 == (count % max_row_length):
+                buf += Template('\t\t\t\t<td><a href="${href}" title="${name}">${name}</a></td>\n').safe_substitute(link)
+
+                if 0 == (count % maxRowLen):
                         buf += '\t\t\t</tr>\n\t\t\t<tr>\n'
 
-        buf += '\t\t\t</tr>\n\t\t</table>\n\t\t<nav><a href="#top">top</a></nav>\n'
-
-        if (0 == count) :
-                return ""
+        buf += '\t\t\t</tr>\n\t\t</table>'
 
         return buf
 
 
         return ns_list
 
-def make_header_filter(procs):
+def make_heading_filter(procedureNames):
+
+        procedureNames = list(procedureNames)
 
         def filter_headings(ns):
 
-                lst = filter(lambda name: name.startswith(ns),procs)
+                lst = filter(lambda name: name.startswith(ns),procedureNames)
 
                 if( 0 == len(lst)):
                         return None
 
                 for tmp in lst:
-                        procs.remove(tmp)
+                        procedureNames.remove(tmp)
 
                 return True
 
         return filter_headings
 
-def headings(procs, delimiter="-", minSize=5):
+def headings(procedureNames, delimiter="-", minSize=7):
 
         root = ("root", list())
 
-        for proc in procs:
-                insert(root, proc.split(delimiter))
+        for pname in procedureNames:
+                insert(root, pname.split(delimiter))
 
         heading_list = list()
         for node in root[1]:
                 heading_list.extend(namespaces(node, delimiter, minSize))
 
-        return filter(make_header_filter(list(procs)),heading_list)
+        return filter(make_heading_filter(procedureNames),heading_list)
+
+
+
+def transform(procedure) :
+
+        dct = dict()
+
+        d["name"] = escape(procedure.proc_name)
+
+        "<br>".join(map(escape_html,procedure.proc_author.split(',')))
+
+        d["authors"] = ""
+        authors = 
+        for author in authors :
+                d["authors"] += "%s<br>"%escape(author.strip())
+
+        d["date"] = escape(procedure.proc_date)
+        d["copyright"] = escape(procedure.proc_copyright)
+        d["blurb"] = escape(procedure.proc_blurb)
+        d["help"] = escape(procedure.proc_help)
+        d["type"] = procedure_type(escape(procedure.proc_type))
+
+        d["parameters"] = alt_table(procedure.params)
+        d["results"] = alt_table(procedure.return_vals)
+
+
+        return dct
+
+def link(singleFile) :
+        def link_transform(pname):
+                if(singleFile):
+                        return {"name":pname,"href":"#%s"%slugify(pname)}
+                return {"name":pname,"href":"pdb/%s.html"%slugify(pname)}
+
+        return link_transform
 
 def dump_pdb(minSize, singleFile) :
 
         if("" == root) :
                 return None
 
-        prepare(root)
+        now = datetime.now().strftime("%A, %d. %B %Y %I:%M%p")
 
-        # loop through PDB
-        procs = gimp.pdb.query()
-        procs.sort()
+        css = Template('<style type="text/css">\n\t\t\t${css}\n\t\t</style>\n').safe_substitute({"css":__CSS})
 
-        # begin index buffering
-        heading_list = headings(procs, '-', minSize)
+        if(singleFile is not True):
+                # create sub directory if it doesnt exist
+                tmp = os.path.join(root,"pdb")
 
-        buf = '\t\t<hr>\n\t\t<nav class="main">'
+                if not os.path.exists(tmp):
+                        os.makedirs(tmp)
+
+                with open(os.path.join(root, "main.css"),'w') as f:
+                        f.write(__CSS)
+
+                css = '<link rel="stylesheet" type="text/css" href="main.css">'
+
+        # get all procedure names from PDB
+        procedure_names = gimp.pdb.query()
+        procedure_names.sort()
+
+        # get comprehensive list of namespaces
+        heading_list = headings(procedure_names, '-', minSize)
+
+        # for every namespace, create a top index link
+        index_buf = '\t\t<hr>\n\t\t<nav class="main">\n\t\t\t'
         for heading in heading_list :
-                buf += Template('<a href="#${heading}">${heading}</a>').safe_substitute({"heading":heading})
+                index_buf += Template('<a href="#${heading}">${heading}</a>').safe_substitute({"heading":heading})
 
                 if(heading != heading_list[-1]):
-                        buf += " &bull; "
+                        index_buf += " &bull; "
 
-        buf += "</nav>\n\t\t<hr>\n"
+        index_buf += "\n\t\t</nav>\n\t\t<hr>\n"
+        proc_buf = ""
 
+        # for every namespace, create a heading, anchor and index table
         for heading in heading_list :
-                buf += plain_table(heading, filter(lambda name: name.startswith(heading),procs))
-                procs = filter(lambda name: not name.startswith(heading),procs)
+
+                index_buf += Template('\t\t<a id="${heading}"><h4>${heading}</h4></a>\n').safe_substitute({"heading":heading})
+
+                space = filter(lambda pname: pname.startswith(heading),procedure_names)
+
+                index_buf += index_table(map(link(singleFile),space))
+                index_buf += '\n\t\t<nav><a href="#top">top</a></nav>\n'
+
+                # buffer procedure output for this namespace
+                for pname in space:
+
+                        proc_buf += "<h1>%s</h1><p><h4>face-palm</h4></p>"%pname
+
+                        if (singleFile is not True):
+                                with open(os.path.join(root,"pdb","%s.html"%slugify(pname)), "w+") as f :
+                                        f.write(Template(__PROC_HTML).safe_substitute({"body":proc_buf}))
+                                        proc_buf = ""
+                # end procedure buffering
+
+                # mark all procedures in this namespace as done
+                procedure_names = filter(lambda pname: pname not in space,procedure_names)
         # end index buffering
 
+        # write index, if not singleFile, proc_buff will be empty!
         with open(os.path.join(root,"index.html"), "w+") as f :
-                f.write(Template(INDEX_TEMPLATE).safe_substitute({"date":datetime.now().strftime("%A, %d. %B %Y %I:%M%p"),"body":buf}))
+                f.write(Template(__INDEX_HTML).safe_substitute({"date":now,"body":index_buf + proc_buf, "css":css}))
+
+        """
+                <nav>
+                        <a href="${prev_link}" style="float:left;">previous</a>
+                        <a href="${index_link}">index</a>
+                        <a href="${next_link}" style="float:right;">next</a>
+                </nav>
+
+
+                <hr style="margin-bottom:0px;"/>
+                <div style="display:inline-block;">
+                        <h1>
+                                ${name}
+                        </h1>
+                        <div id="author">
+                                ${authors}
+                        </div>
+                </div>
+                <hr style="margin-bottom:0px;"/>
+                <p id="copyright" style="margin:0">
+                        <span class="date">${date}</span><br>
+                        ${copyright}
+                </p>
+                <h4>Description</h4>
+                <p id="description">
+                        ${blurb}
+                </p>
+                <h4>Help</h4>
+                <p id="help">
+                        ${help}
+                </p>
+                <hr />
+                <h4>Type</h4>
+                <p id="type">
+                        ${type}
+                </p>
+                <h4>Parameters</h4>
+                ${parameters}
+                <h4>Return</h4>
+                ${results}
+                <hr style="margin-bottom:0px;" />
+
+
+
+
+
+                <nav>
+                        <a href="${prev_link}" style="float:left;">previous</a>
+                        <a href="${index_link}">index</a>
+                        <a href="${next_link}" style="float:right;">next</a>
+                </nav>
+
+        """
+
+
 
         return None
 
         None,
         #params
         [
-                (PF_INT, "scale", "scale", 5),
+                (PF_INT, "scale", "scale", 7),
                 (PF_BOOL, "single-file", "single-file", False)
         ],
         #results