#!/Users/mh/stools/bin/python

import glob
import os
import inspect

module_head="""<head>
<link rel="stylesheet" type="text/css" href="cx-oracle-demos.css" />
</head>
<body>
<html>
"""

module_tail="""
<p class=footer>
<a href=index.html>index</a> -
<a href=http://code.google.com/p/cx-oracle-demos>home</a> -
<a href=http://cx-oracle.sourceforge.net/html/index.html>cx_Oracle</a>

</html>
</body>
"""
idx_head="""
<h1>cx_Oracle Demo Project</h1>
<p>This is a collection of cx_Oracle demo programs.
<p>
"""

idx_tail="""
<p>
These demos reference the following database objects:
<ul>
<li>table cxdemo_t1
<li>package cxdemo
</ul>
<p>
which are defined here:
<ul>
<li><a href=setup.sql>setup.sql</a>
<li><a href=cleanup.sql>cleanup.sql</a>
</ul>
<p>
This is an open-source project.  To contribute or grab the source, go to:
<ul>
<li><a href=http://code.google.com/p/cx-oracle-demos/>
http://code.google.com/p/cx-oracle-demos</a>
</ul>
<p>
Here's the code template: <a href=Template.txt>Template.txt</a>
"""

def doblock(fd,label,text,tag):
    text=text.strip()
    if len(text) > 0:
        if label is not None:
            fd.write('<h2>%s</h2>\n'%(label))
        fd.write('<%s>%s</%s>\n'%(tag,text,tag))

flist=[os.path.splitext(x)[0] for x in glob.glob('*.py')]
flist.sort()

ixf=open('index.html','w')
ixf.write(module_head)
ixf.write(idx_head)
ixf.write('<table>\n')

for f in flist:
    print '***',f
    m=__import__(f)

    # index row
    ixf.write(' <tr>\n')
    ixf.write('  <td><p><a href=%s.html>%s</a></td>\n'%(m.__name__,m.__name__))
    ixf.write('  <td><p>%s</td>\n'%(m.desc))
    ixf.write(' </tr>\n')

    # module doc
    ht=open('%s.html'%(m.__name__),'w')
    ht.write(module_head)
    ht.write('<h1>%s</h1>\n'%(m.desc))

    doblock(ht,None,m.notes,'p')

    # demo() code
    body=inspect.getsource(m.demo)
    body=body.strip()
    body=body.replace('\n    ','\n')
    ix=body.index('\n')
    body=body[ix+1:]
    ht.write('<pre>%s</pre>\n'%(body))
    ht.write('<p><a href=%s.py>(%s.py)</a>\n'%(m.__name__,m.__name__))

    doblock(ht,'output',m.output,'pre')
    doblock(ht,'setup',m.setup,'pre')
    doblock(ht,'cleanup',m.cleanup,'pre')

    ht.write(module_tail)
    ht.close()

ixf.write('</table>\n')

ixf.write(idx_tail)
ixf.write(module_tail)
ixf.close()
