Spaces:
Paused
Paused
Peng Ding commited on
Commit ·
0ae4e28
1
Parent(s): 7921f85
chore: bump vendored config 0.3.0→0.3.1, httpserver 0.1.0→0.2.1, readability 0.1.0→0.2.0
Browse files- src/veilrender/_vendor/config.py +21 -11
- src/veilrender/_vendor/httpserver.py +148 -48
- src/veilrender/_vendor/readability.py +43 -30
src/veilrender/_vendor/config.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
# /// zerodep
|
| 2 |
-
# version = "0.3.
|
| 3 |
-
# deps = ["dotenv", "yaml", "
|
| 4 |
# tier = "subsystem"
|
| 5 |
# category = "config"
|
| 6 |
-
# note = "Install/update via
|
| 7 |
# ///
|
| 8 |
|
| 9 |
"""Unified configuration loader — zero dependencies, stdlib only, Python 3.10+.
|
|
@@ -96,15 +96,25 @@ def _load_yaml_loader() -> Callable[[str], Any]:
|
|
| 96 |
return yaml_load
|
| 97 |
|
| 98 |
|
| 99 |
-
def
|
| 100 |
-
"""Load the sibling ``
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
try:
|
| 104 |
-
from
|
| 105 |
except ImportError:
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
# ── Stdlib tomllib (Python 3.11+) ───────────────────────────────────────────
|
|
@@ -280,7 +290,7 @@ def _load_jsonc_file(path: str | os.PathLike[str]) -> dict[str, Any]:
|
|
| 280 |
"""Load a JSONC file, falling back to plain JSON if jsonc module is unavailable."""
|
| 281 |
with open(path, encoding="utf-8") as f:
|
| 282 |
text = f.read()
|
| 283 |
-
jsonc_loads =
|
| 284 |
if jsonc_loads is not None:
|
| 285 |
data = jsonc_loads(text)
|
| 286 |
else:
|
|
|
|
| 1 |
# /// zerodep
|
| 2 |
+
# version = "0.3.1"
|
| 3 |
+
# deps = ["dotenv", "yaml", "jsonx"]
|
| 4 |
# tier = "subsystem"
|
| 5 |
# category = "config"
|
| 6 |
+
# note = "Install/update via: https://zerodep.readthedocs.io/en/latest/guide/cli/"
|
| 7 |
# ///
|
| 8 |
|
| 9 |
"""Unified configuration loader — zero dependencies, stdlib only, Python 3.10+.
|
|
|
|
| 96 |
return yaml_load
|
| 97 |
|
| 98 |
|
| 99 |
+
def _load_jsonx_loader() -> Callable[[str], Any] | None:
|
| 100 |
+
"""Load the sibling ``jsonx`` (or legacy ``jsonc``) module's loader.
|
| 101 |
+
|
| 102 |
+
Returns the ``loads`` function if available, else ``None``.
|
| 103 |
+
"""
|
| 104 |
+
# Try jsonx first (renamed from jsonc)
|
| 105 |
+
_ensure_sibling_path("jsonx")
|
| 106 |
+
sys.modules.pop("jsonx", None)
|
| 107 |
try:
|
| 108 |
+
from jsonx import loads as jsonx_loads
|
| 109 |
except ImportError:
|
| 110 |
+
# Fall back to legacy jsonc for backward compatibility
|
| 111 |
+
_ensure_sibling_path("jsonc")
|
| 112 |
+
sys.modules.pop("jsonc", None)
|
| 113 |
+
try:
|
| 114 |
+
from jsonc import loads as jsonx_loads
|
| 115 |
+
except ImportError:
|
| 116 |
+
return None
|
| 117 |
+
return jsonx_loads
|
| 118 |
|
| 119 |
|
| 120 |
# ── Stdlib tomllib (Python 3.11+) ───────────────────────────────────────────
|
|
|
|
| 290 |
"""Load a JSONC file, falling back to plain JSON if jsonc module is unavailable."""
|
| 291 |
with open(path, encoding="utf-8") as f:
|
| 292 |
text = f.read()
|
| 293 |
+
jsonc_loads = _load_jsonx_loader()
|
| 294 |
if jsonc_loads is not None:
|
| 295 |
data = jsonc_loads(text)
|
| 296 |
else:
|
src/veilrender/_vendor/httpserver.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
# /// zerodep
|
| 2 |
-
# version = "0.
|
| 3 |
# deps = []
|
| 4 |
# tier = "subsystem"
|
| 5 |
# category = "network"
|
| 6 |
-
# note = "Install/update via
|
| 7 |
# ///
|
| 8 |
|
| 9 |
"""Zero-dependency async HTTP server with decorator-based routing.
|
|
@@ -299,9 +299,10 @@ class JSONResponse(Response):
|
|
| 299 |
class StreamingResponse:
|
| 300 |
"""HTTP response streamed from an async generator.
|
| 301 |
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
|
|
|
| 305 |
|
| 306 |
Args:
|
| 307 |
generator: Async iterator yielding ``bytes`` or ``str`` chunks.
|
|
@@ -330,9 +331,8 @@ class StreamingResponse:
|
|
| 330 |
is_sse = self.content_type.startswith("text/event-stream")
|
| 331 |
|
| 332 |
self.headers["Content-Type"] = self.content_type
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
else:
|
| 336 |
self.headers.setdefault("Cache-Control", "no-cache")
|
| 337 |
self.headers.setdefault("Date", _http_date())
|
| 338 |
self.headers.setdefault("Connection", "close")
|
|
@@ -349,16 +349,12 @@ class StreamingResponse:
|
|
| 349 |
async for chunk in self._generator:
|
| 350 |
if isinstance(chunk, str):
|
| 351 |
chunk = chunk.encode("utf-8")
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
writer.write(f"{len(chunk):x}\r\n".encode("latin-1"))
|
| 356 |
-
writer.write(chunk)
|
| 357 |
-
writer.write(b"\r\n")
|
| 358 |
-
await writer.drain()
|
| 359 |
-
if not is_sse:
|
| 360 |
-
writer.write(b"0\r\n\r\n")
|
| 361 |
await writer.drain()
|
|
|
|
|
|
|
| 362 |
except (BrokenPipeError, ConnectionResetError, ConnectionAbortedError):
|
| 363 |
logger.debug("Client disconnected during streaming")
|
| 364 |
finally:
|
|
@@ -878,6 +874,22 @@ class App:
|
|
| 878 |
|
| 879 |
# ── Connection Handling ───────────────────────────────────────────────
|
| 880 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 881 |
async def _handle_connection(
|
| 882 |
self,
|
| 883 |
reader: asyncio.StreamReader,
|
|
@@ -893,30 +905,43 @@ class App:
|
|
| 893 |
)
|
| 894 |
except _BadRequest as exc:
|
| 895 |
logger.debug("Bad request from %s: %s", client_addr, exc)
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
|
|
|
|
|
|
|
|
|
| 900 |
)
|
| 901 |
-
try:
|
| 902 |
-
await response._write(writer)
|
| 903 |
-
except (BrokenPipeError, ConnectionResetError):
|
| 904 |
-
pass
|
| 905 |
-
writer.close()
|
| 906 |
return
|
| 907 |
except HTTPException as exc:
|
| 908 |
-
|
| 909 |
-
|
| 910 |
-
|
| 911 |
-
|
| 912 |
-
pass
|
| 913 |
-
writer.close()
|
| 914 |
return
|
| 915 |
-
except
|
| 916 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 917 |
return
|
| 918 |
except Exception:
|
| 919 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 920 |
return
|
| 921 |
|
| 922 |
request = Request(
|
|
@@ -951,33 +976,44 @@ class App:
|
|
| 951 |
self,
|
| 952 |
host: str = DEFAULT_HOST,
|
| 953 |
port: int = DEFAULT_PORT,
|
|
|
|
|
|
|
| 954 |
) -> None:
|
| 955 |
"""Start the server (blocking).
|
| 956 |
|
| 957 |
Args:
|
| 958 |
-
host: Bind address.
|
| 959 |
-
port: Bind port. Use ``0`` for OS-assigned port
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 960 |
"""
|
| 961 |
try:
|
| 962 |
-
asyncio.run(self._serve(host, port))
|
| 963 |
except KeyboardInterrupt:
|
| 964 |
pass
|
| 965 |
|
| 966 |
-
async def _serve(self, host: str, port: int) -> None:
|
| 967 |
"""Internal async server loop."""
|
| 968 |
self._shutdown_event = asyncio.Event()
|
|
|
|
| 969 |
|
| 970 |
-
|
| 971 |
-
self.
|
| 972 |
-
|
| 973 |
-
|
| 974 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 975 |
|
| 976 |
self._server = server
|
| 977 |
-
addrs = server.sockets[0].getsockname() if server.sockets else (host, port)
|
| 978 |
-
self.host = addrs[0]
|
| 979 |
-
self.port = addrs[1]
|
| 980 |
-
logger.info("Serving on %s:%d", self.host, self.port)
|
| 981 |
|
| 982 |
loop = asyncio.get_running_loop()
|
| 983 |
if sys.platform != "win32":
|
|
@@ -987,6 +1023,70 @@ class App:
|
|
| 987 |
async with server:
|
| 988 |
await self._shutdown_event.wait()
|
| 989 |
logger.info("Shutting down server")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 990 |
|
| 991 |
def shutdown(self) -> None:
|
| 992 |
"""Request a graceful server shutdown.
|
|
|
|
| 1 |
# /// zerodep
|
| 2 |
+
# version = "0.2.1"
|
| 3 |
# deps = []
|
| 4 |
# tier = "subsystem"
|
| 5 |
# category = "network"
|
| 6 |
+
# note = "Install/update via: https://zerodep.readthedocs.io/en/latest/guide/cli/"
|
| 7 |
# ///
|
| 8 |
|
| 9 |
"""Zero-dependency async HTTP server with decorator-based routing.
|
|
|
|
| 299 |
class StreamingResponse:
|
| 300 |
"""HTTP response streamed from an async generator.
|
| 301 |
|
| 302 |
+
All streaming responses use ``Transfer-Encoding: chunked`` for
|
| 303 |
+
maximum compatibility with reverse proxies and intermediaries.
|
| 304 |
+
SSE (``text/event-stream``) responses additionally set
|
| 305 |
+
``Cache-Control: no-cache``.
|
| 306 |
|
| 307 |
Args:
|
| 308 |
generator: Async iterator yielding ``bytes`` or ``str`` chunks.
|
|
|
|
| 331 |
is_sse = self.content_type.startswith("text/event-stream")
|
| 332 |
|
| 333 |
self.headers["Content-Type"] = self.content_type
|
| 334 |
+
self.headers.setdefault("Transfer-Encoding", "chunked")
|
| 335 |
+
if is_sse:
|
|
|
|
| 336 |
self.headers.setdefault("Cache-Control", "no-cache")
|
| 337 |
self.headers.setdefault("Date", _http_date())
|
| 338 |
self.headers.setdefault("Connection", "close")
|
|
|
|
| 349 |
async for chunk in self._generator:
|
| 350 |
if isinstance(chunk, str):
|
| 351 |
chunk = chunk.encode("utf-8")
|
| 352 |
+
writer.write(f"{len(chunk):x}\r\n".encode("latin-1"))
|
| 353 |
+
writer.write(chunk)
|
| 354 |
+
writer.write(b"\r\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
await writer.drain()
|
| 356 |
+
writer.write(b"0\r\n\r\n")
|
| 357 |
+
await writer.drain()
|
| 358 |
except (BrokenPipeError, ConnectionResetError, ConnectionAbortedError):
|
| 359 |
logger.debug("Client disconnected during streaming")
|
| 360 |
finally:
|
|
|
|
| 874 |
|
| 875 |
# ── Connection Handling ───────────────────────────────────────────────
|
| 876 |
|
| 877 |
+
@staticmethod
|
| 878 |
+
async def _send_error_and_close(
|
| 879 |
+
writer: asyncio.StreamWriter,
|
| 880 |
+
response: Response | JSONResponse,
|
| 881 |
+
) -> None:
|
| 882 |
+
"""Write an error response to the client and close the connection.
|
| 883 |
+
|
| 884 |
+
Swallows broken-pipe / reset errors that occur when the client has
|
| 885 |
+
already disconnected.
|
| 886 |
+
"""
|
| 887 |
+
try:
|
| 888 |
+
await response._write(writer)
|
| 889 |
+
except (BrokenPipeError, ConnectionResetError):
|
| 890 |
+
pass
|
| 891 |
+
writer.close()
|
| 892 |
+
|
| 893 |
async def _handle_connection(
|
| 894 |
self,
|
| 895 |
reader: asyncio.StreamReader,
|
|
|
|
| 905 |
)
|
| 906 |
except _BadRequest as exc:
|
| 907 |
logger.debug("Bad request from %s: %s", client_addr, exc)
|
| 908 |
+
await self._send_error_and_close(
|
| 909 |
+
writer,
|
| 910 |
+
Response(
|
| 911 |
+
body=str(exc),
|
| 912 |
+
status_code=400,
|
| 913 |
+
content_type="text/plain; charset=utf-8",
|
| 914 |
+
),
|
| 915 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 916 |
return
|
| 917 |
except HTTPException as exc:
|
| 918 |
+
await self._send_error_and_close(
|
| 919 |
+
writer,
|
| 920 |
+
JSONResponse({"error": exc.message}, status_code=exc.status_code),
|
| 921 |
+
)
|
|
|
|
|
|
|
| 922 |
return
|
| 923 |
+
except asyncio.TimeoutError:
|
| 924 |
+
logger.debug("Request read timed out from %s", client_addr)
|
| 925 |
+
await self._send_error_and_close(
|
| 926 |
+
writer,
|
| 927 |
+
JSONResponse({"error": "Request Timeout"}, status_code=408),
|
| 928 |
+
)
|
| 929 |
+
return
|
| 930 |
+
except asyncio.IncompleteReadError:
|
| 931 |
+
logger.debug("Incomplete request body from %s", client_addr)
|
| 932 |
+
await self._send_error_and_close(
|
| 933 |
+
writer,
|
| 934 |
+
JSONResponse(
|
| 935 |
+
{"error": "Bad Request: incomplete body"}, status_code=400
|
| 936 |
+
),
|
| 937 |
+
)
|
| 938 |
return
|
| 939 |
except Exception:
|
| 940 |
+
logger.debug("Failed to read request from %s", client_addr, exc_info=True)
|
| 941 |
+
await self._send_error_and_close(
|
| 942 |
+
writer,
|
| 943 |
+
JSONResponse({"error": "Internal Server Error"}, status_code=500),
|
| 944 |
+
)
|
| 945 |
return
|
| 946 |
|
| 947 |
request = Request(
|
|
|
|
| 976 |
self,
|
| 977 |
host: str = DEFAULT_HOST,
|
| 978 |
port: int = DEFAULT_PORT,
|
| 979 |
+
*,
|
| 980 |
+
socket: str | None = None,
|
| 981 |
) -> None:
|
| 982 |
"""Start the server (blocking).
|
| 983 |
|
| 984 |
Args:
|
| 985 |
+
host: Bind address (ignored when *socket* is set).
|
| 986 |
+
port: Bind port. Use ``0`` for OS-assigned port (ignored when
|
| 987 |
+
*socket* is set).
|
| 988 |
+
socket: Unix domain socket path. When set, the server listens
|
| 989 |
+
on a Unix socket instead of TCP. The socket file permissions
|
| 990 |
+
are restricted to owner-only (``0o600``) after creation.
|
| 991 |
+
Only available on Unix-like systems.
|
| 992 |
"""
|
| 993 |
try:
|
| 994 |
+
asyncio.run(self._serve(host, port, socket=socket))
|
| 995 |
except KeyboardInterrupt:
|
| 996 |
pass
|
| 997 |
|
| 998 |
+
async def _serve(self, host: str, port: int, *, socket: str | None = None) -> None:
|
| 999 |
"""Internal async server loop."""
|
| 1000 |
self._shutdown_event = asyncio.Event()
|
| 1001 |
+
self._socket_path: str | None = None
|
| 1002 |
|
| 1003 |
+
if socket:
|
| 1004 |
+
server = await self._start_unix_socket(socket)
|
| 1005 |
+
else:
|
| 1006 |
+
server = await asyncio.start_server(
|
| 1007 |
+
self._handle_connection,
|
| 1008 |
+
host,
|
| 1009 |
+
port,
|
| 1010 |
+
)
|
| 1011 |
+
addrs = server.sockets[0].getsockname() if server.sockets else (host, port)
|
| 1012 |
+
self.host = addrs[0]
|
| 1013 |
+
self.port = addrs[1]
|
| 1014 |
+
logger.info("Serving on %s:%d", self.host, self.port)
|
| 1015 |
|
| 1016 |
self._server = server
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1017 |
|
| 1018 |
loop = asyncio.get_running_loop()
|
| 1019 |
if sys.platform != "win32":
|
|
|
|
| 1023 |
async with server:
|
| 1024 |
await self._shutdown_event.wait()
|
| 1025 |
logger.info("Shutting down server")
|
| 1026 |
+
if self._socket_path:
|
| 1027 |
+
self._cleanup_socket()
|
| 1028 |
+
|
| 1029 |
+
async def _start_unix_socket(self, socket_path: str) -> asyncio.Server:
|
| 1030 |
+
"""Start listening on a Unix domain socket.
|
| 1031 |
+
|
| 1032 |
+
Handles stale socket cleanup, permission hardening (``0o600``),
|
| 1033 |
+
and registers the path for shutdown cleanup.
|
| 1034 |
+
|
| 1035 |
+
Args:
|
| 1036 |
+
socket_path: Path for the Unix domain socket file.
|
| 1037 |
+
|
| 1038 |
+
Returns:
|
| 1039 |
+
The ``asyncio.Server`` instance.
|
| 1040 |
+
|
| 1041 |
+
Raises:
|
| 1042 |
+
SystemExit: If the path exists but is not a socket, or the
|
| 1043 |
+
parent directory does not exist.
|
| 1044 |
+
"""
|
| 1045 |
+
import stat as stat_mod
|
| 1046 |
+
|
| 1047 |
+
path = os.path.realpath(socket_path)
|
| 1048 |
+
|
| 1049 |
+
# Remove stale socket if present
|
| 1050 |
+
if os.path.exists(path):
|
| 1051 |
+
try:
|
| 1052 |
+
st = os.stat(path)
|
| 1053 |
+
if stat_mod.S_ISSOCK(st.st_mode):
|
| 1054 |
+
os.unlink(path)
|
| 1055 |
+
logger.info("Removed stale socket: %s", path)
|
| 1056 |
+
else:
|
| 1057 |
+
logger.error("Socket path exists and is not a socket: %s", path)
|
| 1058 |
+
sys.exit(1)
|
| 1059 |
+
except OSError as exc:
|
| 1060 |
+
logger.error("Cannot remove stale socket %s: %s", path, exc)
|
| 1061 |
+
sys.exit(1)
|
| 1062 |
+
|
| 1063 |
+
# Ensure parent directory exists
|
| 1064 |
+
parent = os.path.dirname(path)
|
| 1065 |
+
if not os.path.isdir(parent):
|
| 1066 |
+
logger.error("Socket parent directory does not exist: %s", parent)
|
| 1067 |
+
sys.exit(1)
|
| 1068 |
+
|
| 1069 |
+
server = await asyncio.start_unix_server(
|
| 1070 |
+
self._handle_connection,
|
| 1071 |
+
path=path,
|
| 1072 |
+
)
|
| 1073 |
+
|
| 1074 |
+
# Restrict permissions to owner-only
|
| 1075 |
+
if os.path.exists(path):
|
| 1076 |
+
os.chmod(path, 0o600)
|
| 1077 |
+
|
| 1078 |
+
self._socket_path = path
|
| 1079 |
+
logger.info("Serving on unix:%s (mode 0600)", path)
|
| 1080 |
+
return server
|
| 1081 |
+
|
| 1082 |
+
def _cleanup_socket(self) -> None:
|
| 1083 |
+
"""Remove the Unix socket file on shutdown."""
|
| 1084 |
+
if self._socket_path and os.path.exists(self._socket_path):
|
| 1085 |
+
try:
|
| 1086 |
+
os.unlink(self._socket_path)
|
| 1087 |
+
logger.info("Removed socket: %s", self._socket_path)
|
| 1088 |
+
except OSError:
|
| 1089 |
+
pass
|
| 1090 |
|
| 1091 |
def shutdown(self) -> None:
|
| 1092 |
"""Request a graceful server shutdown.
|
src/veilrender/_vendor/readability.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
# /// zerodep
|
| 2 |
-
# version = "0.
|
| 3 |
# deps = ["soup"]
|
| 4 |
# tier = "medium"
|
| 5 |
# category = "text"
|
| 6 |
-
# note = "Install/update via
|
| 7 |
# ///
|
| 8 |
|
| 9 |
"""HTML readability content extractor — zero-dep, stdlib only, Python 3.10+.
|
|
@@ -231,6 +231,10 @@ class ReadabilityResult:
|
|
| 231 |
lang: Language code from ``<html lang="...">``, or ``None``.
|
| 232 |
dir: Text direction (``"ltr"`` / ``"rtl"``), or ``None``.
|
| 233 |
length: Character count of *text*.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
"""
|
| 235 |
|
| 236 |
title: str
|
|
@@ -243,6 +247,7 @@ class ReadabilityResult:
|
|
| 243 |
lang: str | None = None
|
| 244 |
dir: str | None = None
|
| 245 |
length: int = 0
|
|
|
|
| 246 |
|
| 247 |
|
| 248 |
# ── Public API ───────────────────────────────────────────────────────────────
|
|
@@ -360,7 +365,7 @@ class _Readability:
|
|
| 360 |
# Grab article content. The first iteration reuses self._soup
|
| 361 |
# (removing non-content tags in-place); retries use a faster
|
| 362 |
# parse that skips those tags during tree construction.
|
| 363 |
-
article_html, article_text = self._grab_article()
|
| 364 |
|
| 365 |
# If metadata title is empty, try to derive from article headings.
|
| 366 |
title = metadata.get("title", "")
|
|
@@ -379,17 +384,19 @@ class _Readability:
|
|
| 379 |
lang=lang,
|
| 380 |
dir=direction,
|
| 381 |
length=len(text),
|
|
|
|
| 382 |
)
|
| 383 |
|
| 384 |
# ── Article grabbing (with retry) ────────────────────────────────────
|
| 385 |
|
| 386 |
_PRE_CLEAN_TAGS = ["script", "style", "link", "noscript"]
|
| 387 |
|
| 388 |
-
def _grab_article(self) -> tuple[str, str]:
|
| 389 |
"""Extract article content, retrying with relaxed rules if needed.
|
| 390 |
|
| 391 |
Returns:
|
| 392 |
-
``(article_html, article_text)`` tuple.
|
|
|
|
| 393 |
"""
|
| 394 |
ruthless = True
|
| 395 |
|
|
@@ -417,7 +424,7 @@ class _Readability:
|
|
| 417 |
article_text = article_tag.get_text(separator=" ", strip=True)
|
| 418 |
|
| 419 |
if len(article_text) >= RETRY_LENGTH or not ruthless:
|
| 420 |
-
return article_html, article_text
|
| 421 |
|
| 422 |
# Too short — retry without ruthless filtering.
|
| 423 |
log.debug(
|
|
@@ -438,8 +445,8 @@ class _Readability:
|
|
| 438 |
# Final fallback: return body content as-is.
|
| 439 |
body = self._soup.find("body")
|
| 440 |
if body is not None:
|
| 441 |
-
return body.to_html(), body.get_text(separator=" ", strip=True)
|
| 442 |
-
return "", ""
|
| 443 |
|
| 444 |
# ── Pre-cleaning ─────────────────────────────────────────────────────
|
| 445 |
|
|
@@ -688,11 +695,17 @@ class _Readability:
|
|
| 688 |
|
| 689 |
# ── Scoring ──────────────────────────────────────────────────────────
|
| 690 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 691 |
def _score_paragraphs(self) -> dict[int, dict[str, Any]]:
|
| 692 |
"""Score paragraph-like nodes and propagate to ancestors.
|
| 693 |
|
| 694 |
Returns:
|
| 695 |
-
Dict mapping ``id(tag)``
|
| 696 |
"""
|
| 697 |
candidates: dict[int, dict[str, Any]] = {}
|
| 698 |
|
|
@@ -701,21 +714,22 @@ class _Readability:
|
|
| 701 |
if len(inner_text) < MIN_PARAGRAPH_LENGTH:
|
| 702 |
continue
|
| 703 |
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
# Ensure
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
|
|
|
| 719 |
|
| 720 |
# Content score for this paragraph.
|
| 721 |
inner_len = len(inner_text)
|
|
@@ -723,11 +737,10 @@ class _Readability:
|
|
| 723 |
content_score += len(COMMAS_RE.findall(inner_text))
|
| 724 |
content_score += min(inner_len / 100.0, 3.0)
|
| 725 |
|
| 726 |
-
# Propagate to
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
candidates[id(grandparent)]["score"] += content_score / 2.0
|
| 731 |
|
| 732 |
# Scale scores by link density.
|
| 733 |
for entry in candidates.values():
|
|
@@ -797,7 +810,7 @@ class _Readability:
|
|
| 797 |
|
| 798 |
# Create an article wrapper.
|
| 799 |
article = self._Tag("div")
|
| 800 |
-
sibling_threshold = max(10.0, best["score"] * 0.
|
| 801 |
|
| 802 |
# If there's no parent, use the candidate itself.
|
| 803 |
if parent is None:
|
|
|
|
| 1 |
# /// zerodep
|
| 2 |
+
# version = "0.2.0"
|
| 3 |
# deps = ["soup"]
|
| 4 |
# tier = "medium"
|
| 5 |
# category = "text"
|
| 6 |
+
# note = "Install/update via: https://zerodep.readthedocs.io/en/latest/guide/cli/"
|
| 7 |
# ///
|
| 8 |
|
| 9 |
"""HTML readability content extractor — zero-dep, stdlib only, Python 3.10+.
|
|
|
|
| 231 |
lang: Language code from ``<html lang="...">``, or ``None``.
|
| 232 |
dir: Text direction (``"ltr"`` / ``"rtl"``), or ``None``.
|
| 233 |
length: Character count of *text*.
|
| 234 |
+
score: Readability score of the best candidate container. Higher
|
| 235 |
+
values indicate stronger confidence that the extracted content
|
| 236 |
+
is a real article rather than navigation / boilerplate. Zero
|
| 237 |
+
when no scored candidate was found (body fallback).
|
| 238 |
"""
|
| 239 |
|
| 240 |
title: str
|
|
|
|
| 247 |
lang: str | None = None
|
| 248 |
dir: str | None = None
|
| 249 |
length: int = 0
|
| 250 |
+
score: float = 0.0
|
| 251 |
|
| 252 |
|
| 253 |
# ── Public API ───────────────────────────────────────────────────────────────
|
|
|
|
| 365 |
# Grab article content. The first iteration reuses self._soup
|
| 366 |
# (removing non-content tags in-place); retries use a faster
|
| 367 |
# parse that skips those tags during tree construction.
|
| 368 |
+
article_html, article_text, article_score = self._grab_article()
|
| 369 |
|
| 370 |
# If metadata title is empty, try to derive from article headings.
|
| 371 |
title = metadata.get("title", "")
|
|
|
|
| 384 |
lang=lang,
|
| 385 |
dir=direction,
|
| 386 |
length=len(text),
|
| 387 |
+
score=article_score,
|
| 388 |
)
|
| 389 |
|
| 390 |
# ── Article grabbing (with retry) ────────────────────────────────────
|
| 391 |
|
| 392 |
_PRE_CLEAN_TAGS = ["script", "style", "link", "noscript"]
|
| 393 |
|
| 394 |
+
def _grab_article(self) -> tuple[str, str, float]:
|
| 395 |
"""Extract article content, retrying with relaxed rules if needed.
|
| 396 |
|
| 397 |
Returns:
|
| 398 |
+
``(article_html, article_text, score)`` tuple. *score* is the
|
| 399 |
+
readability score of the best candidate (0.0 for body fallback).
|
| 400 |
"""
|
| 401 |
ruthless = True
|
| 402 |
|
|
|
|
| 424 |
article_text = article_tag.get_text(separator=" ", strip=True)
|
| 425 |
|
| 426 |
if len(article_text) >= RETRY_LENGTH or not ruthless:
|
| 427 |
+
return article_html, article_text, best["score"]
|
| 428 |
|
| 429 |
# Too short — retry without ruthless filtering.
|
| 430 |
log.debug(
|
|
|
|
| 445 |
# Final fallback: return body content as-is.
|
| 446 |
body = self._soup.find("body")
|
| 447 |
if body is not None:
|
| 448 |
+
return body.to_html(), body.get_text(separator=" ", strip=True), 0.0
|
| 449 |
+
return "", "", 0.0
|
| 450 |
|
| 451 |
# ── Pre-cleaning ─────────────────────────────────────────────────────
|
| 452 |
|
|
|
|
| 695 |
|
| 696 |
# ── Scoring ──────────────────────────────────────────────────────────
|
| 697 |
|
| 698 |
+
# Maximum ancestor levels for score propagation. The original
|
| 699 |
+
# algorithm only propagated to parent (1x) and grandparent (0.5x).
|
| 700 |
+
# Modern SPA-rendered pages nest content 3-5 levels deep in wrapper
|
| 701 |
+
# divs, so we extend propagation with diminishing weights.
|
| 702 |
+
_ANCESTOR_WEIGHTS = [1.0, 0.5, 0.333, 0.25]
|
| 703 |
+
|
| 704 |
def _score_paragraphs(self) -> dict[int, dict[str, Any]]:
|
| 705 |
"""Score paragraph-like nodes and propagate to ancestors.
|
| 706 |
|
| 707 |
Returns:
|
| 708 |
+
Dict mapping ``id(tag)`` -> ``{"tag": tag, "score": float}``.
|
| 709 |
"""
|
| 710 |
candidates: dict[int, dict[str, Any]] = {}
|
| 711 |
|
|
|
|
| 714 |
if len(inner_text) < MIN_PARAGRAPH_LENGTH:
|
| 715 |
continue
|
| 716 |
|
| 717 |
+
# Collect ancestors up to the propagation depth.
|
| 718 |
+
ancestors: list[Any] = []
|
| 719 |
+
cur = tag.parent
|
| 720 |
+
for _ in range(len(self._ANCESTOR_WEIGHTS)):
|
| 721 |
+
if cur is None or cur.name in ("html", "body", "[document]"):
|
| 722 |
+
break
|
| 723 |
+
ancestors.append(cur)
|
| 724 |
+
cur = cur.parent
|
| 725 |
+
|
| 726 |
+
# Ensure each ancestor is initialised in the candidate map.
|
| 727 |
+
for anc in ancestors:
|
| 728 |
+
if id(anc) not in candidates:
|
| 729 |
+
candidates[id(anc)] = {
|
| 730 |
+
"tag": anc,
|
| 731 |
+
"score": self._init_score(anc),
|
| 732 |
+
}
|
| 733 |
|
| 734 |
# Content score for this paragraph.
|
| 735 |
inner_len = len(inner_text)
|
|
|
|
| 737 |
content_score += len(COMMAS_RE.findall(inner_text))
|
| 738 |
content_score += min(inner_len / 100.0, 3.0)
|
| 739 |
|
| 740 |
+
# Propagate to ancestors with diminishing weights.
|
| 741 |
+
for i, anc in enumerate(ancestors):
|
| 742 |
+
weight = self._ANCESTOR_WEIGHTS[i]
|
| 743 |
+
candidates[id(anc)]["score"] += content_score * weight
|
|
|
|
| 744 |
|
| 745 |
# Scale scores by link density.
|
| 746 |
for entry in candidates.values():
|
|
|
|
| 810 |
|
| 811 |
# Create an article wrapper.
|
| 812 |
article = self._Tag("div")
|
| 813 |
+
sibling_threshold = max(10.0, best["score"] * 0.1)
|
| 814 |
|
| 815 |
# If there's no parent, use the candidate itself.
|
| 816 |
if parent is None:
|