File size: 8,083 Bytes
27cde0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""HTML processing utilities for ScrapeRL backend."""

import re
from typing import Any, Optional
from bs4 import BeautifulSoup, Tag, NavigableString

from app.utils.logging import get_logger

logger = get_logger(__name__)


def parse_html(html: str, parser: str = "html.parser") -> BeautifulSoup:
    """
    Parse HTML string into a BeautifulSoup object.

    Args:
        html: Raw HTML string
        parser: Parser to use (html.parser, lxml, html5lib)

    Returns:
        Parsed BeautifulSoup object
    """
    return BeautifulSoup(html, parser)


def clean_html(
    html: str,
    remove_scripts: bool = True,
    remove_styles: bool = True,
    remove_comments: bool = True,
    remove_tags: Optional[list[str]] = None,
) -> str:
    """
    Clean HTML by removing unwanted elements.

    Args:
        html: Raw HTML string
        remove_scripts: Remove <script> tags
        remove_styles: Remove <style> tags
        remove_comments: Remove HTML comments
        remove_tags: Additional tags to remove

    Returns:
        Cleaned HTML string
    """
    soup = parse_html(html)

    # Remove script tags
    if remove_scripts:
        for script in soup.find_all("script"):
            script.decompose()

    # Remove style tags
    if remove_styles:
        for style in soup.find_all("style"):
            style.decompose()

    # Remove comments
    if remove_comments:
        from bs4 import Comment

        for comment in soup.find_all(string=lambda text: isinstance(text, Comment)):
            comment.extract()

    # Remove additional specified tags
    if remove_tags:
        for tag_name in remove_tags:
            for tag in soup.find_all(tag_name):
                tag.decompose()

    return str(soup)


def extract_text(
    html: str,
    separator: str = " ",
    strip: bool = True,
) -> str:
    """
    Extract plain text from HTML.

    Args:
        html: Raw HTML string
        separator: String to join text segments
        strip: Strip whitespace from result

    Returns:
        Extracted plain text
    """
    soup = parse_html(html)

    # Remove script and style elements
    for element in soup(["script", "style", "noscript"]):
        element.decompose()

    text = soup.get_text(separator=separator)

    if strip:
        # Normalize whitespace
        text = re.sub(r"\s+", " ", text).strip()

    return text


def semantic_chunk(
    html: str,
    max_chunk_size: int = 4000,
    overlap: int = 200,
) -> list[dict[str, Any]]:
    """
    Split HTML content into semantic chunks based on structure.

    Args:
        html: Raw HTML string
        max_chunk_size: Maximum characters per chunk
        overlap: Number of characters to overlap between chunks

    Returns:
        List of chunk dictionaries with text and metadata
    """
    soup = parse_html(html)
    chunks: list[dict[str, Any]] = []

    # Remove non-content elements
    for element in soup(["script", "style", "noscript", "nav", "footer", "header"]):
        element.decompose()

    # Find semantic boundaries
    semantic_tags = ["article", "section", "div", "p", "h1", "h2", "h3", "h4", "h5", "h6"]

    def get_text_content(element: Tag | NavigableString) -> str:
        if isinstance(element, NavigableString):
            return str(element).strip()
        return element.get_text(separator=" ", strip=True)

    current_chunk = ""
    current_metadata: dict[str, Any] = {"tags": [], "headings": []}

    for element in soup.find_all(semantic_tags):
        text = get_text_content(element)
        if not text:
            continue

        tag_name = element.name if isinstance(element, Tag) else "text"

        # Check if adding this would exceed max size
        if len(current_chunk) + len(text) + 1 > max_chunk_size:
            if current_chunk:
                chunks.append({
                    "text": current_chunk.strip(),
                    "metadata": current_metadata.copy(),
                    "char_count": len(current_chunk),
                })
            # Start new chunk with overlap
            if overlap > 0 and current_chunk:
                current_chunk = current_chunk[-overlap:] + " " + text
            else:
                current_chunk = text
            current_metadata = {"tags": [tag_name], "headings": []}
        else:
            current_chunk += " " + text if current_chunk else text
            current_metadata["tags"].append(tag_name)

        # Track headings
        if tag_name in ["h1", "h2", "h3", "h4", "h5", "h6"]:
            current_metadata["headings"].append(text[:100])

    # Add remaining content
    if current_chunk.strip():
        chunks.append({
            "text": current_chunk.strip(),
            "metadata": current_metadata,
            "char_count": len(current_chunk),
        })

    # If no semantic chunks found, fall back to simple chunking
    if not chunks:
        text = extract_text(html)
        for i in range(0, len(text), max_chunk_size - overlap):
            chunk_text = text[i : i + max_chunk_size]
            if chunk_text.strip():
                chunks.append({
                    "text": chunk_text.strip(),
                    "metadata": {"tags": [], "headings": []},
                    "char_count": len(chunk_text),
                })

    return chunks


def extract_links(
    html: str,
    base_url: Optional[str] = None,
    include_text: bool = True,
) -> list[dict[str, str]]:
    """
    Extract all links from HTML.

    Args:
        html: Raw HTML string
        base_url: Base URL for resolving relative links
        include_text: Include link text in results

    Returns:
        List of link dictionaries with href and optionally text
    """
    from urllib.parse import urljoin

    soup = parse_html(html)
    links: list[dict[str, str]] = []

    for anchor in soup.find_all("a", href=True):
        href = anchor.get("href", "")
        if not href or href.startswith("#") or href.startswith("javascript:"):
            continue

        # Resolve relative URLs
        if base_url and not href.startswith(("http://", "https://", "//")):
            href = urljoin(base_url, href)

        link_data: dict[str, str] = {"href": href}

        if include_text:
            link_data["text"] = anchor.get_text(strip=True)

        # Include title if present
        title = anchor.get("title")
        if title:
            link_data["title"] = title

        links.append(link_data)

    return links


def extract_tables(
    html: str,
    include_headers: bool = True,
) -> list[dict[str, Any]]:
    """
    Extract tables from HTML as structured data.

    Args:
        html: Raw HTML string
        include_headers: Try to identify and include header rows

    Returns:
        List of table dictionaries with headers and rows
    """
    soup = parse_html(html)
    tables: list[dict[str, Any]] = []

    for table in soup.find_all("table"):
        table_data: dict[str, Any] = {
            "headers": [],
            "rows": [],
        }

        # Extract headers from thead or first row
        if include_headers:
            thead = table.find("thead")
            if thead:
                header_row = thead.find("tr")
                if header_row:
                    table_data["headers"] = [
                        th.get_text(strip=True)
                        for th in header_row.find_all(["th", "td"])
                    ]

        # Extract body rows
        tbody = table.find("tbody") or table
        for row in tbody.find_all("tr"):
            cells = row.find_all(["td", "th"])
            row_data = [cell.get_text(strip=True) for cell in cells]

            # If no headers yet and this looks like a header row
            if include_headers and not table_data["headers"] and row.find("th"):
                table_data["headers"] = row_data
            else:
                if row_data:  # Skip empty rows
                    table_data["rows"].append(row_data)

        if table_data["rows"] or table_data["headers"]:
            tables.append(table_data)

    return tables