| 31 | |
| 32 | |
| 33 | class Node(object): |
| 34 | def __init__(self, parent, name, attr_list, body): |
| 35 | self.parent = parent |
| 36 | self.name = name |
| 37 | self.body = body |
| 38 | self.attr = dict((x[0], x[2]) for x in ATTR.findall(attr_list)) |
| 39 | |
| 40 | if self.name in ['th', 'td']: |
| 41 | self.text = self.strip_tags(self.body) |
| 42 | self.children = [] |
| 43 | else: |
| 44 | self.text = '' |
| 45 | self.children = [Node(self, *g) for g in NODE.findall(self.body)] |
| 46 | |
| 47 | def __repr__(self): |
| 48 | return "<Node('%s')>" % self.name |
| 49 | |
| 50 | def strip_tags(self, html): |
| 51 | if type(html) != str: |
| 52 | html = html.group(3) |
| 53 | return NODE.sub(self.strip_tags, html) |
| 54 | |
| 55 | def traverse(self, depth=0): |
| 56 | print('%s%s: %s %s' % (' ' * depth, self.name, self.attr, self.text)) |
| 57 | |
| 58 | for c in self.children: |
| 59 | c.traverse(depth + 2) |
| 60 | |
| 61 | def get_row_width(self): |
| 62 | total = 0 |
| 63 | assert self.name == 'tr' |
| 64 | for c in self.children: |
| 65 | if 'colspan' in c.attr: |
| 66 | total += int(c.attr['colspan']) |
| 67 | else: |
| 68 | total += 1 |
| 69 | return total |
| 70 | |
| 71 | def scan_format(self, index=0, width=0, rowspan=None): |
| 72 | if rowspan is None: |
| 73 | rowspan = {} |
| 74 | |
| 75 | format_str = '' |
| 76 | |
| 77 | expand_char = 'x' if platform.system() != 'Darwin' else '' |
| 78 | |
| 79 | if self.name in ['th', 'td']: |
| 80 | extend = ((width == 3 and index == 1) or |
| 81 | (width != 3 and width < 5 and index == width - 1)) |
| 82 | |
| 83 | if self.name == 'th': |
| 84 | format_str += 'c%s ' % (expand_char if extend else '') |
| 85 | else: |
| 86 | format_str += 'l%s ' % (expand_char if extend else '') |
| 87 | |
| 88 | if 'colspan' in self.attr: |
| 89 | for i in range(int(self.attr['colspan']) - 1): |
| 90 | format_str += 's ' |
no outgoing calls
no test coverage detected