Changeset 961


Ignore:
Timestamp:
19/05/06 11:30:26 (13 years ago)
Author:
domlowe
Message:

Added prettyprintHTML function to cgi parser

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TI02-CSML/trunk/parser/parseTest.py

    r955 r961  
    1111"""Test for GML/CSML parsing code 
    1212""" 
     13def PrettyPrintHTML(elem,matchList=[]): 
     14    '''Lightweight HTML pretty printing of elementTree elements + highlight 
     15    any words which occur in the element text (and tails) which occur in matchList, 
     16    and formatted using a css something like this: 
     17    === 
     18    DIV.xmlElem {PADDING-LEFT: 20px;} 
     19    .xmlAttrVal {COLOR:Red; } 
     20    .xmlAttrTyp {COLOR:Green; } 
     21    .xmlElemTag {COLOR:Blue; } 
     22.   highlight {BACKGROUND-COLOR:Yellow; } 
     23    === 
     24    ''' 
     25    def span(x,c): return '<span class="%s">%s</span>'%(c,x) 
     26    def div(x,c): return '<div class="%s">%s</div>'%(c,x) 
     27    def match(x): 
     28        if x is None: return '' 
     29        for w in matchList: x=re.sub(w,span(w,'highlight'),x) 
     30        return x 
     31    lt,gt='<b>&lt;</b>','<b>&gt;</b>' 
     32    strAttrib='' 
     33    for att in elem.attrib: 
     34        strAttrib+=' %s="%s"'%(span(att,'xmlAttrTyp'),span(elem.attrib[att],'xmlAttrVal')) 
     35    result='%s%s%s%s%s'%(lt,span(elem.tag,"xmlElemTag"),strAttrib,gt,match(elem.text)) 
     36    children=len(elem) 
     37    if children: 
     38        for item in elem: 
     39            result+=PrettyPrintHTML(item) 
     40        result+='%s%s/%s%s'%(match(item.tail),lt,span(elem.tag,'xmlElemTag'),gt) 
     41    else: 
     42        result+='%s/%s%s'%(lt,span(elem.tag,'xmlElemTag'),gt) 
     43    return div(result,'xmlElem') 
     44 
     45 
     46 
     47 
    1348 
    1449def main(): 
     
    1651    print "Content-type: text/html" 
    1752    print  
     53    print "<HEAD>" 
     54    print '<STYLE TYPE="text/css">'  
     55    print 'DIV.xmlElem {PADDING-LEFT: 20px;}' 
     56    #print '.highlight {BACKGROUND-COLOR:Yellow; }' 
     57    print '.xmlAttrVal {COLOR:Red; }' 
     58    print '.xmlAttrTyp {COLOR:Green; }' 
     59    print '.xmlElemTag {COLOR:Blue; }' 
     60    print "</STYLE>" 
    1861    print "<title>CSML Test Parser</title>" 
    1962    print "<h1>CSML Test Parser</h1>" 
    2063    print "<p>Please note this is experimental, any questions please contact d.lowe@rl.ac.uk</p>" 
    21     print "<p>Parser version - 18 May 2006</p>" 
     64    print "<p>Parser version - 19 May 2006</p>" 
    2265    print "<h2>Load a CSML file to test:</h2>" 
    2366     
     
    75118                sys.exit() 
    76119             
    77             strCSML=parser_extra.PrettyPrint(csml) 
     120 
     121            strCSML= PrettyPrintHTML(csml) 
    78122            strCSML=parser_extra.removeInlineNS(strCSML) 
    79             strCSML=strCSML.replace('<','&lt ') 
    80             strCSML=strCSML.replace('>',' &gt') 
    81             listCSML=strCSML.split('&gt') 
    82             print "<h3>CSML Parsed OK - Note, this does not automatically mean it is valid CSML.. this is not a CSML validator..please check the output matches the input. </h3>" 
    83  
    84             for item in listCSML[:-1]:   
    85                 item=item+'&gt' 
    86                 print "<p>" + item + "</p>" 
    87             print "<h3> Sorry about the formatting...</h3>"     
     123            print strCSML 
     124             
     125            print "</HEAD>" 
    88126main() 
    89127 
Note: See TracChangeset for help on using the changeset viewer.