-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.py
More file actions
90 lines (78 loc) · 3.38 KB
/
read.py
File metadata and controls
90 lines (78 loc) · 3.38 KB
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
"""
Read page content - supports raw HTML, text, and markdown formats
"""
from typing import Literal
from .browser import SentienceBrowser
def read(
browser: SentienceBrowser,
output_format: Literal["raw", "text", "markdown"] = "raw",
enhance_markdown: bool = True,
) -> dict:
"""
Read page content as raw HTML, text, or markdown
Args:
browser: SentienceBrowser instance
output_format: Output format - "raw" (default, returns HTML for external processing),
"text" (plain text), or "markdown" (lightweight or enhanced markdown).
enhance_markdown: If True and output_format is "markdown", uses markdownify for better conversion.
If False, uses the extension's lightweight markdown converter.
Returns:
dict with:
- status: "success" or "error"
- url: Current page URL
- format: "raw", "text", or "markdown"
- content: Page content as string
- length: Content length in characters
- error: Error message if status is "error"
Examples:
# Get raw HTML (default) - can be used with markdownify for better conversion
result = read(browser)
html_content = result["content"]
# Get high-quality markdown (uses markdownify internally)
result = read(browser, output_format="markdown")
markdown = result["content"]
# Get plain text
result = read(browser, output_format="text")
text = result["content"]
"""
if not browser.page:
raise RuntimeError("Browser not started. Call browser.start() first.")
if output_format == "markdown" and enhance_markdown:
# Get raw HTML from the extension first
raw_html_result = browser.page.evaluate(
"""
(options) => {
return window.sentience.read(options);
}
""",
{"format": "raw"},
)
if raw_html_result.get("status") == "success":
html_content = raw_html_result["content"]
try:
# Use markdownify for enhanced markdown conversion
from markdownify import markdownify, MarkdownifyError
markdown_content = markdownify(html_content, heading_style="ATX", wrap=True)
return {
"status": "success",
"url": raw_html_result["url"],
"format": "markdown",
"content": markdown_content,
"length": len(markdown_content),
}
except ImportError:
print("Warning: 'markdownify' not installed. Install with 'pip install markdownify' for enhanced markdown. Falling back to extension's markdown.")
except MarkdownifyError as e:
print(f"Warning: markdownify failed ({e}), falling back to extension's markdown.")
except Exception as e:
print(f"Warning: An unexpected error occurred with markdownify ({e}), falling back to extension's markdown.")
# If not enhanced markdown, or fallback, call extension with requested format
result = browser.page.evaluate(
"""
(options) => {
return window.sentience.read(options);
}
""",
{"format": output_format},
)
return result