File size: 4,917 Bytes
1a48d6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from __future__ import annotations
import re
from html.parser import HTMLParser
from typing import Any


class _Tables(HTMLParser):
    def __init__(self, source: str) -> None:
        super().__init__(convert_charrefs=True)
        self.source = source
        self.offsets = [0]
        self.offsets.extend((match.end() for match in re.finditer("\n", source)))
        self.tables: list[list[dict[str, Any]]] = []
        self.table: list[dict[str, Any]] | None
        self.row: list[str] | None
        self.cell: list[str] | None
        self.cell_tag: str | None
        self.cell_parts: list[tuple[str, int]] | None
        self.row_spans: list[list[int] | None] | None
        self.table = self.row = self.cell = None
        self.cell_tag = self.cell_parts = self.row_spans = None
        self.row_start: int | None = None
        self.depth = 0
        self.invalid = False

    def position(self) -> int:
        (line, column) = self.getpos()
        return self.offsets[line - 1] + column

    def handle_starttag(
        self, tag: str, attrs: list[tuple[str, str | None]] | dict[str, str | None]
    ) -> None:
        if tag == "table":
            self.depth += 1
            if self.depth != 1:
                self.invalid = True
            else:
                self.table = []
        if not self.depth:
            return
        attrs = dict(attrs)
        if (
            "hidden" in attrs
            or (attrs.get("aria-hidden") or "").lower() == "true"
            or re.search(
                "(?:display\\s*:\\s*none|visibility\\s*:\\s*hidden)",
                attrs.get("style") or "",
                re.IGNORECASE,
            )
            or (tag in ("script", "style"))
        ):
            self.invalid = True
        if tag == "tr":
            if self.row is not None or self.cell is not None:
                self.invalid = True
            (self.row, self.row_start, self.row_spans) = ([], self.position(), [])
        elif tag in ("td", "th"):
            if self.row is None or self.cell is not None:
                self.invalid = True
            if attrs.get("rowspan", "1") != "1" or attrs.get("colspan", "1") != "1":
                self.invalid = True
            (self.cell, self.cell_tag, self.cell_parts) = ([], tag, [])
        elif tag in ("br", "p", "div", "ul", "ol", "li") and self.cell is not None:
            self.cell.append(" ")

    def handle_data(self, data: str) -> None:
        if self.cell is not None:
            self.cell.append(data)
            self.cell_parts.append((data, self.position()))
        elif self.row is not None and data.strip():
            self.invalid = True

    def handle_endtag(self, tag: str) -> None:
        if not self.depth:
            return
        if tag in ("td", "th"):
            if self.cell is None or self.row is None or tag != self.cell_tag:
                self.invalid = True
            else:
                value = re.sub("\\s+", " ", "".join(self.cell)).strip()
                self.row.append(value)
                matches = [
                    position + data.find(value)
                    for (data, position) in self.cell_parts
                    if value
                    and data.count(value) == 1
                    and self.source.startswith(value, position + data.find(value))
                ]
                self.row_spans.append(
                    [matches[0], matches[0] + len(value)] if len(matches) == 1 else None
                )
            self.cell = self.cell_tag = self.cell_parts = None
        elif tag == "tr":
            if self.row is None or self.cell is not None or self.table is None:
                self.invalid = True
            else:
                end = self.source.find(">", self.position()) + 1
                self.table.append(
                    {
                        "cells": self.row,
                        "line": self.source[self.row_start : end],
                        "offset": self.row_start,
                        "spans": self.row_spans,
                    }
                )
            self.row = self.row_start = self.row_spans = None
        elif tag == "table":
            if self.depth == 1:
                if self.row is not None or self.cell is not None:
                    self.invalid = True
                self.tables.append(self.table)
                self.table = None
            self.depth -= 1
        elif tag in ("p", "div", "ul", "ol", "li") and self.cell is not None:
            self.cell.append(" ")


def tables(source: str) -> list[list[dict[str, Any]]] | None:
    parser = _Tables(source)
    try:
        parser.feed(source)
        parser.close()
    except (ValueError, TypeError):
        return None
    if (
        parser.invalid
        or parser.depth
        or parser.row is not None
        or (parser.cell is not None)
    ):
        return None
    return parser.tables