#!/usr/bin/python

import re, string, os, sys

printXMLFile = 1

if printXMLFile :
    outFile = open("Data/css2reference.xml",'w')
    sys.stdout = outFile 


class Property:
    def __init__(self):
        self.elements={}
        pass

    def addPropertyHeader(self,mainSection,name,standard,anchor):
        self.mainSection=mainSection
        self.name=name
        self.standard=standard
        self.anchor=anchor
        
    def printProperty(self):
        names = [self.name]
        keys = ['-left','-right','-bottom']
        if re.compile('-top').search(self.name):
            for k in keys:
                names.append(re.compile('-top').sub(k,self.name))


        for n in names:
            print "<property section='"+self.mainSection+"'  name='"+n+"' standard='"+self.standard+'#'+self.anchor+"'>"
            for e in self.elements.items():
                print "<"+e[0]+">",
                print e[1],
                print "</"+e[0]+">"
            print "<description></description>\n</property>"

        

propertyList=[]

originalDirectory = "Original/"
mainSection=''
currentFile=''


def values(str):
    str = re.compile("(<.*?>)").sub('',str)
    str = re.compile("\&lt;'?(.*?)'?\&gt;").sub("<prop name='\g<1>'/>",str)
    return string.strip(str)

def tableRows(dataList):
    for d in dataList:

        elName = string.lower(re.compile("(.*?)[: ]").match(d).group(1))
        if elName=="applies":
            elName="appliesTo"
            
        propertyList[len(propertyList)-1].elements[elName] =  values(re.compile(".*?<td>(.*)").match(d).group(1))


def propdef(dataList):
    global propertyList
    
    for item in dataList:
        d = re.compile("\n",re.S).sub('',item)
        d = re.compile("(<tr|</table>)").sub('\n\g<1>',d)
        hit = re.compile("<strong>'(.*?)'</strong>").search(d)
        anchor=re.compile('.*?<a name="([\w-]+)"',re.S).match(d).group(1)
        propertyList.append(Property())
        propertyList[len(propertyList)-1].addPropertyHeader(mainSection,hit.group(1),currentFile,anchor)
        tableRows(re.compile("<tr.*?<em>(.*)").findall(d))


for orig in  os.listdir(originalDirectory):
    if not re.compile("\.html\Z").search(orig):
        continue
    input = open(originalDirectory+orig) 
    data = input.read()
    input.close()

    currentFile = orig

    
    mainSection  = re.compile("<title>(.*?)</title>",re.I).search(data).group(1)
    mainSection = re.compile("[ ,]").sub('',mainSection)[0:20]

    propdef(re.compile("<div\s+class\s*=\s*\"propdef\">(.*?)</div>",re.S).findall(data))
    

print """ <!-- generated by run.py -->\n<!DOCTYPE CSS2Reference [
<!ENTITY exampleList SYSTEM 'exampleList.xml'>
<!ENTITY aural_examples SYSTEM 'aural_examples.xml'>
<!ENTITY examples SYSTEM 'examples.xml'>
<!ATTLIST example id ID #IMPLIED idrefs IDREFS #IMPLIED>]>"""

print "<CSS2Reference>"

for p in propertyList:
    p.printProperty()

print """
&exampleList;
&examples;
&aural_examples;
</CSS2Reference>"""


if printXMLFile:
    outFile.close()
    os.system("saxonclient -o Output/index.html Data/css2reference.xml XSLT/stylesheet.xslt pwd=${PWD}/")






