source: TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/ETxmlView.py @ 996

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/ETxmlView.py@996
Revision 996, 2.8 KB checked in by lawrence, 14 years ago (diff)

Further changes associated with the browse/discovery
look and feel

Line 
1# Copyright Bryan Lawrence, Rutherford Appleton Laboratory, CCLRC, 2006
2#
3# This code is made available under the GPL, if you don't know what
4# that means, you have no rights to copy it or use it!
5#
6# Code to support xml and elementTree viewing as text and html
7
8import ElementTree as ET
9import re
10
11def et2text(elem,indent='',html=0,space='   '):
12        '''Lightweight pretty printing of elementTree elements'''
13        def estrip(elem):
14                ''' Just want to get rid of unwanted whitespace '''
15                if elem is None:
16                        return ''
17                else:
18                        return elem.strip()
19        strAttrib=''
20        for att in elem.attrib:
21                strAttrib+=' %s="%s"'%(att,elem.attrib[att])
22        result='%s<%s%s>%s'%(indent,elem.tag,strAttrib,estrip(elem.text))
23        children=len(elem)
24        if children:
25                for item in elem:
26                        result+='\n'+et2text(item,indent=indent+space)
27                result+='\n%s%s</%s>'%(indent,estrip(item.tail),elem.tag)
28        else:
29                result+='</%s>'%(elem.tag)
30        return result
31
32
33def et2html(elem,matchList=[],number=0):
34    #this method is NDG code ... copyright CCLRC ...
35    '''Lightweight HTML pretty printing of elementTree elements + highlight
36    any words which occur in the element text (and tails) which occur in matchList,
37    and formatted using a css something like this:
38    ===
39    DIV.xmlElem {PADDING-LEFT: 20px;}
40    .xmlAttrVal {COLOR:Red; }
41    .xmlAttrTyp {COLOR:Green; }
42    .xmlElemTag {COLOR:Blue; }
43.   highlight {BACKGROUND-COLOR:Yellow; }
44    ===
45    Line number is not yet implemented.
46    '''
47    def span(x,c): return '<span class="%s">%s</span>'%(c,x)
48    def div(x,c): return '<div class="%s">%s</div>'%(c,x)
49    def match(x): 
50        if x is None: return ''
51        for w in matchList: x=re.sub(w,span(w,'highlight'),x)
52        return x
53    lt,gt='<b>&lt;</b>','<b>&gt;</b>'
54    strAttrib=''
55    for att in elem.attrib:
56        strAttrib+=' %s="%s"'%(span(att,'xmlAttrTyp'),span(elem.attrib[att],'xmlAttrVal'))
57    result='%s%s%s%s%s'%(lt,span(elem.tag,"xmlElemTag"),strAttrib,gt,match(elem.text))
58    children=len(elem)
59    if children:
60        for item in elem:
61            result+=et2html(item,matchList)
62        result+='%s%s/%s%s'%(match(item.tail),lt,span(elem.tag,'xmlElemTag'),gt)
63    else:
64        result+='%s/%s%s'%(lt,span(elem.tag,'xmlElemTag'),gt)
65    return div(result,'xmlElem')
66
67def xmlCleanup(string):
68    '''This is a hack to make sure that xml doesn't contain any naughty & characters alone
69    (typically from URL copies)'''
70    #match ? but not (?! if it's followed by alphanumeric \w characters + and a semicolon ;
71    #don't want to use urlencode cos we don't know where it came from ...
72    return re.sub(r'&(?!\w+;)', '&amp;', string)
73
74def xml2text(xmlString):
75    tree=ET.fromstring(xmlCleanup(xmlString))
76    return et2text(tree)
77
78def xml2HTML(xmlString,**kw):
79    tree=ET.fromstring(xmlCleanup(xmlString))
80    return et2HTML(tree,**kw)
Note: See TracBrowser for help on using the repository browser.