import os,re

sorting = lambda x: x[3][2] # by key
#sorting = lambda x: x[3][3] # by group

tab=[];

def addToList(lst,v):
    v=v.replace("tr\"","")
    v=v.replace("\"","")
    v=v.replace("KeyEvent.","")
    v=v.replace("Shortcut.","")
    lst.append(v.strip())

def check(a,b,plugFlag):
    global nm,tab
    try:
        f = open(a, "r",encoding='utf-8') #Opens the file in read-mode
        text = f.read() #Reads the file and assigns the value to a variable
        f.close()
    except:
        return
    ms=re.findall("\.registerShortcut[^\;]*\;",text,re.S+re.M)
    #print("\n",b)
    for s in ms:
        vs=[]
        m=re.search("\.registerShortcut([^\;]*)",s)
        q=m.groups(0)[0]
        v=""; n=0
        for c in q:
            if c=="(":
                 n=n+1;
            elif c==")":
                 n=n-1;
                 if n==0:
                    addToList(vs,v)
            elif c==",":
                 if n==1:
                    addToList(vs,v)
                 v=""
            else:
                 if c!="\n":
                     v=v+c
            if n==0:
                break;
#        print("vs:",vs)
        if vs:
            tab.append((plugFlag,a,b,vs))

def saveHtml():
    global sortColumn
    f2 = open("shortcuts.html","w",encoding='utf-8')
    f2.write("""<html><body>
    """)

    f2.write("""<head>
    <style type="text/css">

    table
    {
    color:#000;
    background: #DFD;
    border-collapse:collapse;
    width:647px;
    border:5px solid lightseagreen;
    }

    td
    {
    padding:.5em 1em;
    }
    tr:nth-child(even) {background: yellowgreen}

    tr.C { font-weight: bold }

    tfoot
    {
    }
    tfoot td
    {
    padding-bottom:1.5em;
    }
    </style>

    </head>""");

    f2.write("""<table>
    <thead>
    <th>N</th>
    <th>File</th>
    <th>name</th>
    <th>title</th>
    <th>key</th>
    <th>group</th>
    <th>mod</th>
    </thead><tboby>
    """)
    num=0
    tab.sort(key=sorting)
    for t in tab:
        num=num+1
        #print(t)
        f2.write("<tr class=\""+t[0]+"\">")
        f2.write("<td>%d</td>\n" % num);
        f2.write("<td><a href=\""+t[1]+"\">"+t[2]+"</a></td>\n")
        for td in t[3]:
           f2.write("<td>"+td+"</td>\n")
        f2.write("</tr>")

    f2.write("""
    </table></tboby>""")

    f2.write("""
    </body></html>""")
    f2.close()


for root, dirs, names in os.walk("core/src"):
    for name in names:
        if re.match(".*\.java",name):
            full=os.path.join(root, name);
            check(full,name,'C');
for root, dirs, names in os.walk("plugins"):
    for name in names:
        if re.match(".*\.java",name):
            plugName=re.search(r"plugins\\([^\\]*)\\",root)
            if plugName:
                pName=plugName.groups(0)[0]
            full=os.path.join(root, name);
            check(full,pName,'P');
saveHtml()



