#-*- coding:UTF-8 -*-
# Version 1.0.0 #
"Transforme un script Python en HTM, \
par Wouf 2008."


import re # Expressions régulières

      
# -------------------------------------------------------------
# Cette fonction reçoit en paramètre une string et renvoie une liste
# de 3 strings : la première chaine ne contient pas de string
# La deuxième en contient une exactement
# la troisième c'est le reste

def megasplit (txt):
    #il faut tester le triple quote
    txt=txt.replace('"""','<') # ce caractère a été remplacé par son equivalent html
    marqueur=("<",'"',"'")
    lindex=0
    debut=txt
    chaine=""
    fin=""
    for i in txt:
         if i=="#" :
             #commentaires, pas de chaine
             fin=""
             break
         if i in marqueur:
             bon="non"
             try :
                 caractere=txt[lindex-1]
                 if caractere<>u"\\" :
                     bon="oui"
             except :
                 bon="oui"
                
             if bon=="oui":    
                 debut=txt[:lindex]
                 fin=(txt[lindex:])
                 break
         lindex=lindex+1
      
    lindex=2
    if i<>"#":
        for j in fin[1:]:
            if i==j:
                 bon="non"
                 try :
                     caractere=fin[lindex-2]
                     if caractere<>"\\" :
                         bon="oui"
                    
                 except :
                     bon="oui"
                    
                 if bon=="oui":
                     break
                    
            lindex=lindex+1
        chaine,fin= fin[:lindex],fin[lindex:]
    chaine=chaine.replace("<",'<')
    return [debut,chaine,fin]

#----------------------------------------------------------------
# reçoit une string et renvoie une liste, les éléments impairs
#  étant des chaines

def enchaine (txt):
    a=megasplit(txt)
    while a[len(a)-1]<>"":
        a[len(a)-1:]=megasplit(a[len(a)-1])
    return a

#----------------------------------------------------------------
def existe (fname): # existence du fichier
    try:
        f = open(fname,'r')
        f.close()
        return 1
    except:
        return 0
#------------------------------------------------------------------

def transform (source):
    orange="and","assert","break","class","continue","def","del","elif","else", \
        "except","finally","for","from","global","if","import","in","is","lambda", \
        "not","or","pass", "print", "raise", "return", "try" ,"while", "yield"

    purple="__import__","abs","any","basestring","all","bool","callable","classmethod","cmp", \
        "compile","complex","delattr","dict","dir","divmod","enumerate","eval","exefile", \
        "file","filter","float","frozenset","getattr","globals","hasattr","hash","help", \
        "hex","id","int","isinstance","issubclass","iter","len","list", \
        "locals","long","map","max","min","object","oct","open","ord","pow","property", \
        "range","raw_input","input","reduce","reload","repr","reversed","round","set","setatt", \
        "slice","sorted","staticmethod","str","sum","super","tuple","type", "unichr", \
        "unicode","vars","xrange","zip"



    #Initialisations des variables locales
    result=source+".html" # nom du fichier resultat
    multiligne="" # pour gérer l'antislash en fin de ligne
    grandechaine=False
    
    header="""
<div style="font-family:Courier; font-size:10px">"""

    
    footer="""
    <p style="color:red">
    # Ce script, qui transforme un programme Python en html est proposé par <a href="http://netvibes.com/wouf">Wouf</a>.
    Il est utilisable gratuitement et sans condition</p>
    </div>
    """

    
    fs = open(source, 'r')
    fd = open(result, 'w')
    fd.write(header)
    while 1:
        txt = fs.readline()
        if txt =="":
            break
        if txt[-2:]=="\\\n" :
            multiligne=multiligne+txt
        elif txt.find('"""')<>-1 and not grandechaine:
            multiligne=multiligne+txt
            grandechaine = True
        elif txt.find('"""')==-1 and grandechaine:
            multiligne=multiligne+txt
        else :
            grandechaine=False
            txt=multiligne+txt
            multiligne=""
            #Caractères spéciaux :
            txt=txt.replace("&","&amp;")
            txt=txt.replace("<","&lt;")
            txt=txt.replace(">","&gt;")
            txt=txt.replace("  ","&nbsp;&nbsp;")
            

            nochaine=0
            
            for txtsplit in enchaine(txt):
                if nochaine%2==0: # pas une chaine
                                      
                    # gestion des mots clefs
                    for mot in orange:
                       pattern=r'\b'+mot+r'\b'
                       txtsplit=re.sub( pattern,"<span style=\"color:orange\">"+ mot +"</span>",txtsplit)
                      
                    # gestion des fonctions prédéfinies
                    for mot in purple:
                       pattern=r'\b'+mot+r'\b'
                       txtsplit=re.sub( pattern,"<span style=\"color:purple\">"+ mot +"</span>",txtsplit)
                      
                    # gestion des commentaires
                    txtsplit=re.sub("(#.*$)","<span style=\"color:red\">\\1</span>",txtsplit)

                    #gestion des fonctions
                    txtsplit=re.sub("def</span>(.*)\(","def </span> <span style=\"color:blue\">\\1</span> (",txtsplit)
                    
                else: # c'est une chaine
                    if txtsplit<>"" :
                        txtsplit="<span style=\"color:green\">"+txtsplit+"</span>"
                nochaine=nochaine+1
                txtsplit=txtsplit.replace("\n","<br />\n")
                fd.write(txtsplit),
            
    fd.write(footer)      
    fs.close()
    fd.close()


source=raw_input(" Fichier source :")
while  not existe(source):
    print(u"Fichier non trouvé !")
    source=raw_input(" Fichier source :")


transform(source)

print 'ok'

# Ce script, qui transforme un programme Python en html est proposé par Wouf. Il est utilisable gratuitement et sans condition