#!/usr/bin/env python
import cgi, sys, sqlite3
import cgitb; cgitb.enable() 
theform=cgi.FieldStorage()
if not theform.has_key('cmd'):
    thecmd = ''
else: 
    thecmd = theform['cmd'].value
pkgs=('pstricks','pst-plot','pst-node','pstricks-add','pst-nodeExtras','pst-3dplot','pst-tree','multido','pst-eps','pst-bspline','pst-grapha','pst-coil','pst-text')
tab=u'\x09'
bdelim=("{","(","[","")
edelim=("}",")","]","")
bgon='<span class="opt">'
bgoff='</span>'
print "Content-type: text/html\r\n"
pagehead='''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO8859-I" />
<link type="css/text" rel="stylesheet" href="../../psthelp.css" />
<title>PSTricks Help Page</title></head>
<body>
<h1>PSTricks Command Help</h1>
<form action="../../cgi-bin/parsehelp.py" method="get">
    <p id="ask">Command: <input type="text" value="**thecmd**" name="cmd" /><input type="submit" /></p>
</form>
'''
def cnv(s):
    # convert < and > characters to html
    ss=s.replace('<-','&larr;')
    ss=ss.replace('<','&lt;')
    ss=ss.replace('|','| ')
    return ss.replace('>','&gt;')

pagetail='</body></html>\n'
pagebody=''
if thecmd[0:1]<>'\\':
    thecmd='\\'+thecmd
print pagehead.replace('**thecmd**',thecmd)

if thecmd:
    conn = sqlite3.connect('/Library/Webserver/Documents/pstCommands.db') 
    c=conn.cursor()
    t=(thecmd,)
    c.execute('select cmdid, cmdname from Commands where cmdname=?',t)
    x=c.fetchone()
    cmdnum=0
    while not (x is None):
        if x[1]==thecmd:
            cmdnum=int(x[0])
        x=c.fetchone()
    if cmdnum==0:
        print "<p><b>Command not found</b></p> "
        c.execute("select cmdname from Commands where cmdname LIKE '"+thecmd+"%' order by cmdname")
        x=c.fetchone()
        print '<p>Similar commands:</p><p>'
        while not (x is None):
            print ' <a href="../../cgi-bin/parsehelp.py?cmd=%5C'+x[0][1:]+'">',x[0],'</a> '
            x=c.fetchone()
        print '</p>'
    else:
        t=(cmdnum,)
        c.execute('select * from Commands where cmdID=?',t)
        x=c.fetchone()
        #print x---there should be only one
        cmdcat=int(x[1])
        desc=x[6].split(tab) 
        isOpt=x[8]
        ti=[]
        ti.append(thecmd)
        if x[2]==1:
            ti.append(bgon+"*"+bgoff)
        if x[9]==1:
            ti.append(bgon+"[&lt;kwds&gt;]"+bgoff)
        L=len(x[7]) # delimiters
        delim=x[7]
        
        for i in range(0,L):
            k=int(delim[i])
            s=bdelim[k]+desc[i].replace('<-','&larr;')+edelim[k]
            s=cnv(s)
            if isOpt[i]=='1':
                s=bgon+s+bgoff
            ti.append(s)
        if x[10]==1:
            ti.append("(&lt;coords&gt;)"+bgon+"..."+bgoff)
        #ti.append("% "+x[5]+' ['+pkgs[x[11]]+']')
        #print ""
        print '<p id="syntax"><b>Syntax:</b>',''.join(ti),'</p>'
        print '(Gray background indicates optional argument, "&larr;" shows default value, if omitted.)'
        print '<p><strong>Synopsis:</strong> ['+pkgs[x[11]]+']',cnv(x[5])
        if x[9]==1:
            ss="select parName,parPat,parDefault,parDescription from Parameters where (parLimTo="+str(cmdnum)+") or ((parLimTo=0) and (parCat=0 or parCat="+str(x[1])+")) group by parName,parPat,parDefault,parDescription order by parname"
            c.execute(ss)
            print '<table>'
            print '<caption><strong>Keywords for %s</strong></caption>'% thecmd
            print '<tr><th>Keyword</th><th>Kind</th><th>Default</th><th>Description</th></tr>'
            alt=0
            for x in c:
                if alt:
                    print '<tr class="cellcolor">'
                else:
                    print '<tr>'
                print '<td>'+x[0]+'</td><td>'+cnv(x[1])+'</td><td class="center">'+x[2]+'</td><td>'+cnv(x[3])+'</td></tr>'
                alt=1-alt
        print '</table></p>'
        t=(cmdcat,cmdnum)
        if (cmdcat>0) and (cmdcat<>26):
            c.execute('select cmdID, cmdname from Commands where cmdCat=? and cmdID<>?',t)
            x=c.fetchone()
            if not (x is None):
                print '<p>Similar commands: '
                while not(x is None):
                    print ' <a href="../../cgi-bin/parsehelp.py?cmd=%5C'+x[1][1:]+'">',x[1],'</a> '
                    x=c.fetchone()
                print '</p>'


print pagetail

