Changeset 157 for table_parser


Ignore:
Timestamp:
21/11/10 02:15:17 (18 months ago)
Author:
nigel.sim
Message:

Added the parsing of anchors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • table_parser/table_parser.py

    r156 r157  
    2626    pass 
    2727 
    28 class Cell(object): 
    29     def __init__(self): 
    30         self.data = None 
    31         return 
     28class Cell(list): 
     29    """The cell holds components of text and HTML anchors""" 
    3230    def append(self,item): 
    33         if self.data != None: 
    34             print "Overwriting %s"%self.data 
    35         self.data = item 
     31        s = super(Cell, self) 
     32        if isinstance(item, str): 
     33            if len(self) == 0 or isinstance(top(self), str): 
     34                if len(self) > 0: 
     35                    i = len(self)-1 
     36                    self[i] = self[i] + item 
     37                else: 
     38                    s.append(item) 
     39            elif isinstance(top(self), Anchor): 
     40                s.append(item) 
     41        else: 
     42            s.append(item) 
     43 
     44class Anchor(object): 
     45    """HTML Anchor""" 
     46    def __init__(self, href): 
     47        self.href = href 
     48        self.text = None 
     49    def append(self, text): 
     50        self.text = text 
     51    def __str__(self): 
     52        return '<a href="%s">%s</a>'%(self.href, self.text) 
    3653 
    3754# Get the item on the top of a stack 
     
    4057 
    4158class TableParser(HTMLParser): 
    42     def __init__(self, parser=None): 
     59    def __init__(self, parser=None, anchors=True): 
    4360        """ 
    4461        The parser is a method which will be passed the doc at the end 
     
    5471        self._parser = parser 
    5572        self.reset() 
     73        self.anchors = anchors 
    5674        return 
    5775 
     
    7088        self._tag = tag 
    7189        self._attrs = attrs 
     90        attrs = dict(attrs) 
    7291        if lower(tag) == 'table': 
    7392            self._buf = '' 
     
    7998            self._buf = '' 
    8099            self._stack.append(Cell()) 
     100        elif lower(tag) == 'a' and self.anchors and isinstance(top(self._stack), Cell): 
     101            # add the text we already have 
     102            top(self._stack).append(self._buf) 
     103            self._buf = '' 
     104            self._stack.append(Anchor(attrs['href'])) 
    81105 
    82106        #print "Encountered the beginning of a %s tag" % tag 
     
    103127            t = top(self._stack) 
    104128            if isinstance(t, Row): 
    105                 # We can not currently have text and other table elements in the same cell. 
    106                 # Table elements get precedence 
    107                 if c.data == None: 
    108                     t.append(self._buf) 
    109                 else: 
    110                     t.append(c.data) 
     129                c.append(self._buf) 
     130                t.append(c) 
    111131            else: 
    112132                print "Cell not in a row, rather in a %s"%t 
     133        elif lower(tag) == 'a' and self.anchors and isinstance(top(self._stack), Anchor): 
     134            a = None 
     135            while not isinstance(a, Anchor): 
     136                a = self._stack.pop() 
     137            c = top(self._stack) 
     138            if isinstance(c, Cell): 
     139                a.append(self._buf) 
     140                self._buf = '' 
     141                c.append(a) 
     142            else: 
     143                print "anchor should be in a cell" 
    113144        self._tag = None 
    114145        #print "Encountered the end of a %s tag" % tag 
Note: See TracChangeset for help on using the changeset viewer.