html-to-markdown-cli 3.23.1__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ """CLI proxy package."""
2
+
3
+ from importlib import metadata
4
+
5
+ try:
6
+ __version__ = metadata.version("html-to-markdown-cli")
7
+ except metadata.PackageNotFoundError:
8
+ __version__ = "0+unknown"
@@ -0,0 +1,45 @@
1
+ """CLI entry point for the html-to-markdown proxy.
2
+
3
+ A platform-specific wheel bundles the native binary under
4
+ ``html_to_markdown_cli/bin/<target>/``; the sdist (and unknown platforms) ship no binary
5
+ and fall back to the runtime downloader. Each wheel contains exactly one ``bin/<target>``
6
+ directory, so the binary is located by globbing rather than by recomputing the target
7
+ triple (which cannot distinguish glibc from musl at runtime).
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import os
13
+ import platform
14
+ import subprocess
15
+ import sys
16
+ from pathlib import Path
17
+
18
+ from .downloader import run
19
+
20
+
21
+ def _find_bundled_binary() -> str | None:
22
+ """Return the path to the bundled native binary if this wheel shipped one."""
23
+ bin_root = Path(__file__).parent / "bin"
24
+ if not bin_root.is_dir():
25
+ return None
26
+
27
+ binary_name = "html-to-markdown.exe" if platform.system().lower() == "windows" else "html-to-markdown"
28
+ for candidate in bin_root.glob(f"*/{binary_name}"):
29
+ if candidate.is_file() and os.access(candidate, os.X_OK):
30
+ return str(candidate)
31
+ return None
32
+
33
+
34
+ def main() -> None:
35
+ """Resolve the native binary (bundled or downloaded) and exec it with forwarded argv."""
36
+ bundled = _find_bundled_binary()
37
+ if bundled:
38
+ completed = subprocess.run([bundled, *sys.argv[1:]], check=False) # noqa: S603
39
+ sys.exit(completed.returncode)
40
+
41
+ sys.exit(run(sys.argv[1:]))
42
+
43
+
44
+ if __name__ == "__main__":
45
+ main()
@@ -0,0 +1,292 @@
1
+ """Resolve, download, verify, and run the native html-to-markdown binary.
2
+
3
+ Fallback for installs that ship no bundled binary (the sdist). Queries the GitHub
4
+ release tagged with this package's own version, picks the asset whose name
5
+ contains the platform target triple, and refuses to install it unless it matches
6
+ ``cli-SHA256SUMS``. All diagnostics go to stderr.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import hashlib
12
+ import json
13
+ import os
14
+ import platform
15
+ import re
16
+ import shutil
17
+ import subprocess
18
+ import sys
19
+ import tarfile
20
+ import tempfile
21
+ import zipfile
22
+ from importlib import metadata
23
+ from pathlib import Path, PurePosixPath
24
+ from typing import IO, TYPE_CHECKING, Any
25
+ from urllib.error import URLError
26
+ from urllib.parse import quote, urlsplit
27
+ from urllib.request import HTTPRedirectHandler, Request, build_opener
28
+
29
+ if TYPE_CHECKING:
30
+ from http.client import HTTPMessage
31
+
32
+ REPO = "xberg-io/html-to-markdown"
33
+ BIN_NAME = "html-to-markdown"
34
+ PKG_NAME = "html-to-markdown-cli"
35
+ VERSION_ENV = "HTML_TO_MARKDOWN_CLI_VERSION"
36
+ _USER_AGENT = "html-to-markdown-cli-python-proxy"
37
+ SUMS_NAME = "cli-SHA256SUMS"
38
+ _PEP440_PRERELEASE = re.compile(r"^(?P<base>\d+\.\d+\.\d+)(?:(?P<kind>a|b|rc)(?P<num>\d+))?$")
39
+ _PRERELEASE_LABEL = {"a": "alpha", "b": "beta", "rc": "rc"}
40
+
41
+
42
+ def release_tag(version: str | None = None) -> str:
43
+ """Return the release tag matching this package's version.
44
+
45
+ The package version is the product version, kept in lockstep by
46
+ scripts/sync_consumer_versions.py, so the tag is derived from it rather
47
+ than resolved as "latest". PEP 440 prereleases (``3.24.0rc1``) map back to the
48
+ semver spelling the release tags use (``v3.24.0-rc.1``).
49
+ """
50
+ if version is None:
51
+ version = metadata.version(PKG_NAME)
52
+ match = _PEP440_PRERELEASE.match(version)
53
+ if not match:
54
+ raise RuntimeError(f"cannot derive a release tag from package version {version!r}")
55
+ if match["kind"] is None:
56
+ return f"v{match['base']}"
57
+ return f"v{match['base']}-{_PRERELEASE_LABEL[match['kind']]}.{match['num']}"
58
+
59
+
60
+ def _target_triple() -> str:
61
+ system = platform.system().lower()
62
+ machine = platform.machine().lower()
63
+
64
+ if system == "windows":
65
+ if machine in {"amd64", "x86_64"}:
66
+ return "x86_64-pc-windows-msvc"
67
+ raise RuntimeError(f"unsupported Windows arch: {machine}")
68
+ if system == "linux":
69
+ if machine in {"amd64", "x86_64"}:
70
+ return "x86_64-unknown-linux-gnu"
71
+ if machine in {"aarch64", "arm64"}:
72
+ return "aarch64-unknown-linux-gnu"
73
+ raise RuntimeError(f"unsupported Linux arch: {machine}")
74
+ if system == "darwin":
75
+ if machine in {"aarch64", "arm64"}:
76
+ return "aarch64-apple-darwin"
77
+ if machine in {"amd64", "x86_64"}:
78
+ return "x86_64-apple-darwin"
79
+ raise RuntimeError(f"unsupported macOS arch: {machine}")
80
+ raise RuntimeError(f"unsupported platform: {system} {machine}")
81
+
82
+
83
+ def _binary_name() -> str:
84
+ return f"{BIN_NAME}.exe" if platform.system().lower() == "windows" else BIN_NAME
85
+
86
+
87
+ class _HttpsOnlyRedirectHandler(HTTPRedirectHandler):
88
+ """Reject any redirect whose target is not https (downgrade/SSRF guard)."""
89
+
90
+ def redirect_request(
91
+ self,
92
+ req: Request,
93
+ fp: IO[bytes],
94
+ code: int,
95
+ msg: str,
96
+ headers: HTTPMessage,
97
+ newurl: str,
98
+ ) -> Request | None:
99
+ if urlsplit(newurl).scheme.lower() != "https":
100
+ raise URLError(f"refusing non-https redirect to: {newurl}")
101
+ return super().redirect_request(req, fp, code, msg, headers, newurl)
102
+
103
+
104
+ _opener = build_opener(_HttpsOnlyRedirectHandler())
105
+
106
+
107
+ def _http_get(url: str, accept: str | None = None) -> bytes:
108
+ if urlsplit(url).scheme.lower() != "https":
109
+ raise RuntimeError(f"refusing non-https URL: {url}")
110
+ headers = {"User-Agent": _USER_AGENT}
111
+ if accept:
112
+ headers["Accept"] = accept
113
+ request = Request(url, headers=headers) # noqa: S310 (https-only enforced above)
114
+ try:
115
+ with _opener.open(request, timeout=60) as response:
116
+ if response.status != 200:
117
+ raise RuntimeError(f"HTTP {response.status} for {url}")
118
+ data: bytes = response.read()
119
+ return data
120
+ except URLError as exc:
121
+ raise RuntimeError(f"failed to fetch {url}: {exc}") from exc
122
+
123
+
124
+ def _asset_score(name: str) -> int:
125
+ lowered = name.lower()
126
+ score = 0
127
+ if BIN_NAME.lower() in lowered:
128
+ score += 2
129
+ if "cli" in lowered:
130
+ score += 1
131
+ return score
132
+
133
+
134
+ def _resolve_release() -> tuple[str, dict[str, Any], dict[str, Any] | None]:
135
+ """Return (tag, archive_asset, checksums_asset_or_none) for this platform."""
136
+ triple = _target_triple()
137
+ pinned = os.getenv(VERSION_ENV) or release_tag()
138
+ api_url = f"https://api.github.com/repos/{REPO}/releases/tags/{quote(pinned, safe='')}"
139
+
140
+ release = json.loads(_http_get(api_url, accept="application/vnd.github+json"))
141
+ assets = release.get("assets") or []
142
+ tag = release.get("tag_name") or pinned
143
+
144
+ archives = [
145
+ a
146
+ for a in assets
147
+ if triple in (a.get("name") or "").lower()
148
+ and ((a.get("name") or "").lower().endswith(".tar.gz") or (a.get("name") or "").lower().endswith(".zip"))
149
+ ]
150
+ if not archives:
151
+ raise RuntimeError(f'no release asset matching target triple "{triple}" in {REPO} release {tag}')
152
+ archives.sort(key=lambda a: _asset_score(a.get("name") or ""), reverse=True)
153
+ archive = archives[0]
154
+
155
+ checksums = next((a for a in assets if a.get("name") == SUMS_NAME), None)
156
+ return tag, archive, checksums
157
+
158
+
159
+ def _expected_digest(text: str, asset_name: str) -> str | None:
160
+ for line in text.splitlines():
161
+ stripped = line.strip()
162
+ if not stripped:
163
+ continue
164
+ parts = stripped.split()
165
+ if len(parts) < 2:
166
+ continue
167
+ name = parts[-1].lstrip("*")
168
+ if name == asset_name:
169
+ return parts[0].lower()
170
+ return None
171
+
172
+
173
+ def _verify_checksum(archive_path: Path, asset_name: str, sums_text: str) -> None:
174
+ expected = _expected_digest(sums_text, asset_name)
175
+ if not expected:
176
+ raise RuntimeError(f"no checksum entry for {asset_name} in {SUMS_NAME} — refusing to install unverified binary")
177
+ digest = hashlib.sha256(archive_path.read_bytes()).hexdigest().lower()
178
+ if digest != expected:
179
+ raise RuntimeError(f"checksum mismatch for {asset_name} (expected {expected}, got {digest})")
180
+
181
+
182
+ def _verify_download(archive_path: Path, asset_name: str, checksums: dict[str, Any] | None) -> None:
183
+ if not checksums:
184
+ raise RuntimeError(f"release has no {SUMS_NAME} asset for {asset_name} — refusing to install unverified binary")
185
+ sums_text = _http_get(checksums["browser_download_url"]).decode("utf-8")
186
+ _verify_checksum(archive_path, asset_name, sums_text)
187
+ print(f"Checksum verified for {asset_name}.", file=sys.stderr) # noqa: T201 (diagnostic to stderr)
188
+
189
+
190
+ def _is_within(base: Path, target: Path) -> bool:
191
+ """Return True if `target`'s resolved path stays inside `base`."""
192
+ base_resolved = base.resolve()
193
+ try:
194
+ target.resolve().relative_to(base_resolved)
195
+ except ValueError:
196
+ return False
197
+ return True
198
+
199
+
200
+ def _reject_unsafe_member(name: str) -> None:
201
+ """Reject absolute paths and any `..` component (zip-slip / tar-slip)."""
202
+ normalized = name.replace("\\", "/")
203
+ pure = PurePosixPath(normalized)
204
+ if pure.is_absolute() or normalized.startswith("/"):
205
+ raise RuntimeError(f"refusing absolute path in archive: {name}")
206
+ if len(normalized) >= 2 and normalized[1] == ":":
207
+ raise RuntimeError(f"refusing absolute path in archive: {name}")
208
+ if any(part == ".." for part in pure.parts):
209
+ raise RuntimeError(f"refusing parent-directory escape in archive: {name}")
210
+
211
+
212
+ def _safe_extract(archive_path: Path, asset_name: str, dest: Path) -> None:
213
+ """Extract a tar/zip, validating every member stays within `dest`.
214
+
215
+ Does not rely on extractall() defaults or the 3.12-only ``filter='data'``:
216
+ each member name is rejected up front if it is absolute or contains ``..``,
217
+ and the resolved destination path is re-checked against ``dest``.
218
+ """
219
+ dest = dest.resolve()
220
+ dest.mkdir(parents=True, exist_ok=True)
221
+
222
+ if asset_name.lower().endswith(".zip"):
223
+ with zipfile.ZipFile(archive_path) as zf:
224
+ for member in zf.namelist():
225
+ _reject_unsafe_member(member)
226
+ if not _is_within(dest, dest / member):
227
+ raise RuntimeError(f"refusing archive entry escaping dest: {member}")
228
+ for member in zf.namelist():
229
+ zf.extract(member, dest)
230
+ else:
231
+ with tarfile.open(archive_path, "r:gz") as tf:
232
+ for tar_member in tf.getmembers():
233
+ _reject_unsafe_member(tar_member.name)
234
+ if not _is_within(dest, dest / tar_member.name):
235
+ raise RuntimeError(f"refusing archive entry escaping dest: {tar_member.name}")
236
+ if tar_member.islnk() or tar_member.issym():
237
+ link_target = dest / tar_member.name
238
+ if not _is_within(dest, link_target.parent / tar_member.linkname):
239
+ raise RuntimeError(f"refusing link escaping dest: {tar_member.name}")
240
+ tf.extractall(dest) # noqa: S202 (members validated above)
241
+
242
+
243
+ def _find_binary(root: Path, name: str) -> Path | None:
244
+ for candidate in root.rglob(name):
245
+ if candidate.is_file():
246
+ return candidate
247
+ return None
248
+
249
+
250
+ def _cache_dir(tag: str) -> Path:
251
+ cache = Path.home() / ".cache" / PKG_NAME / tag
252
+ cache.mkdir(parents=True, exist_ok=True, mode=0o700)
253
+ return cache
254
+
255
+
256
+ def ensure_binary() -> str:
257
+ """Ensure the native binary exists locally, downloading if necessary."""
258
+ override = os.getenv("HTML_TO_MARKDOWN_BINARY")
259
+ if override:
260
+ return override
261
+
262
+ tag, archive, checksums = _resolve_release()
263
+ cache = _cache_dir(tag)
264
+ binary_path = cache / _binary_name()
265
+ if binary_path.exists() and os.access(binary_path, os.X_OK):
266
+ return str(binary_path)
267
+
268
+ print(f"Downloading {BIN_NAME} {tag} asset {archive['name']}...", file=sys.stderr) # noqa: T201
269
+ with tempfile.TemporaryDirectory() as tmpdir:
270
+ tmp = Path(tmpdir)
271
+ archive_path = tmp / Path(archive["name"]).name
272
+ archive_path.write_bytes(_http_get(archive["browser_download_url"]))
273
+ _verify_download(archive_path, archive["name"], checksums)
274
+ extract_dir = tmp / "extract"
275
+ extract_dir.mkdir()
276
+ _safe_extract(archive_path, archive["name"], extract_dir)
277
+ found = _find_binary(extract_dir, _binary_name())
278
+ if not found:
279
+ raise RuntimeError(f"binary {_binary_name()} not found after extracting {archive['name']}")
280
+ shutil.move(str(found), str(binary_path))
281
+
282
+ if platform.system().lower() != "windows":
283
+ binary_path.chmod(0o755)
284
+ print(f"{BIN_NAME} installed.", file=sys.stderr) # noqa: T201 (diagnostic to stderr)
285
+ return str(binary_path)
286
+
287
+
288
+ def run(args: list[str]) -> int:
289
+ """Run the native binary with the given args, returning its exit code."""
290
+ binary_path = ensure_binary()
291
+ completed = subprocess.run([binary_path, *args], check=False) # noqa: S603
292
+ return completed.returncode
@@ -0,0 +1,29 @@
1
+ Metadata-Version: 2.5
2
+ Name: html-to-markdown-cli
3
+ Version: 3.23.1
4
+ Summary: CLI proxy for html-to-markdown — downloads and runs the native html-to-markdown binary from GitHub releases.
5
+ Project-URL: Homepage, https://github.com/xberg-io/html-to-markdown
6
+ Project-URL: Issues, https://github.com/xberg-io/html-to-markdown/issues
7
+ Project-URL: Repository, https://github.com/xberg-io/html-to-markdown.git
8
+ Author-email: Na'aman Hirschfeld <naaman@xberg.io>, Tobias Perlstein <tobias@xberg.io>
9
+ Maintainer-email: Na'aman Hirschfeld <naaman@xberg.io>, Tobias Perlstein <tobias@xberg.io>
10
+ License: MIT
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Environment :: Console
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: 3.14
20
+ Requires-Python: >=3.11
21
+ Description-Content-Type: text/markdown
22
+
23
+ # html-to-markdown-cli
24
+
25
+ CLI proxy for [`html-to-markdown`](https://github.com/xberg-io/html-to-markdown). Installing
26
+ this package provides a `html-to-markdown` command that downloads the matching native
27
+ binary from GitHub releases and runs it.
28
+
29
+ Pin a specific release tag with the `HTML_TO_MARKDOWN_CLI_VERSION` environment variable.
@@ -0,0 +1,8 @@
1
+ html_to_markdown_cli/__init__.py,sha256=bOtgOx_VfZI5AzDGhwk7YEnSwRmx83IPr4KFDHfhQgk,190
2
+ html_to_markdown_cli/cli.py,sha256=9_whfxwSmzm6Qc4t717U2e6mH7zjz-trJKXpuVbjhQk,1456
3
+ html_to_markdown_cli/downloader.py,sha256=Z0jsoQciw51sErXxM90iEIiYEnli1AO2I4JdUU65GDU,11354
4
+ html_to_markdown_cli/bin/x86_64-pc-windows-msvc/html-to-markdown.exe,sha256=Z5ltcXpyQGjXh3x2fGrxJhmAIb_joezZhw-sWX-aVFw,14263808
5
+ html_to_markdown_cli-3.23.1.dist-info/METADATA,sha256=QXhMd4-75fPmkLg4OCBdvGiJxPi0NAyMUD_WVMsjWoM,1423
6
+ html_to_markdown_cli-3.23.1.dist-info/WHEEL,sha256=lpoBGaIUIidvvdZ4qRAP61bBKYJdt4FI3WXFL50Gzyo,94
7
+ html_to_markdown_cli-3.23.1.dist-info/entry_points.txt,sha256=9A503xijprsABIeWzHb088StwsOle-LqiInp5kUfqqI,67
8
+ html_to_markdown_cli-3.23.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: false
4
+ Tag: py3-none-win_amd64
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ html-to-markdown = html_to_markdown_cli.cli:main