docs2epub 0.1.1__py3-none-any.whl → 0.1.2__py3-none-any.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.
- docs2epub/__init__.py +1 -1
- docs2epub/cli.py +45 -17
- docs2epub/kindle_html.py +19 -12
- docs2epub/pandoc_epub2.py +44 -5
- {docs2epub-0.1.1.dist-info → docs2epub-0.1.2.dist-info}/METADATA +6 -1
- docs2epub-0.1.2.dist-info/RECORD +11 -0
- docs2epub-0.1.1.dist-info/RECORD +0 -11
- {docs2epub-0.1.1.dist-info → docs2epub-0.1.2.dist-info}/WHEEL +0 -0
- {docs2epub-0.1.1.dist-info → docs2epub-0.1.2.dist-info}/entry_points.txt +0 -0
docs2epub/__init__.py
CHANGED
docs2epub/cli.py
CHANGED
|
@@ -2,10 +2,20 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import argparse
|
|
4
4
|
from pathlib import Path
|
|
5
|
+
from urllib.parse import urlparse
|
|
5
6
|
|
|
6
7
|
from .docusaurus_next import DocusaurusNextOptions, iter_docusaurus_next
|
|
7
8
|
from .epub import EpubMetadata, build_epub
|
|
8
|
-
from .pandoc_epub2 import build_epub2_with_pandoc
|
|
9
|
+
from .pandoc_epub2 import PandocEpub2Options, build_epub2_with_pandoc
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _infer_defaults(start_url: str) -> tuple[str, str, str]:
|
|
13
|
+
parsed = urlparse(start_url)
|
|
14
|
+
host = parsed.netloc or "docs"
|
|
15
|
+
title = host
|
|
16
|
+
author = host
|
|
17
|
+
language = "en"
|
|
18
|
+
return title, author, language
|
|
9
19
|
|
|
10
20
|
|
|
11
21
|
def _build_parser() -> argparse.ArgumentParser:
|
|
@@ -49,9 +59,9 @@ def _build_parser() -> argparse.ArgumentParser:
|
|
|
49
59
|
p.add_argument("--max-pages", type=int, default=None)
|
|
50
60
|
p.add_argument("--sleep-s", type=float, default=0.5)
|
|
51
61
|
|
|
52
|
-
p.add_argument("--title",
|
|
53
|
-
p.add_argument("--author",
|
|
54
|
-
p.add_argument("--language", default=
|
|
62
|
+
p.add_argument("--title", default=None)
|
|
63
|
+
p.add_argument("--author", default=None)
|
|
64
|
+
p.add_argument("--language", default=None)
|
|
55
65
|
p.add_argument("--identifier", default=None)
|
|
56
66
|
p.add_argument("--publisher", default=None)
|
|
57
67
|
|
|
@@ -62,6 +72,19 @@ def _build_parser() -> argparse.ArgumentParser:
|
|
|
62
72
|
help="Output format. Default: epub2 (Kindle-friendly).",
|
|
63
73
|
)
|
|
64
74
|
|
|
75
|
+
p.add_argument(
|
|
76
|
+
"--keep-images",
|
|
77
|
+
action="store_true",
|
|
78
|
+
help="Keep and embed remote images (may be slower and can trigger fetch warnings).",
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
p.add_argument(
|
|
82
|
+
"-v",
|
|
83
|
+
"--verbose",
|
|
84
|
+
action="store_true",
|
|
85
|
+
help="Verbose output (shows full pandoc warnings).",
|
|
86
|
+
)
|
|
87
|
+
|
|
65
88
|
return p
|
|
66
89
|
|
|
67
90
|
|
|
@@ -72,10 +95,13 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
72
95
|
out_value = args.out or args.out_pos
|
|
73
96
|
|
|
74
97
|
if not start_url or not out_value:
|
|
75
|
-
raise SystemExit(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
98
|
+
raise SystemExit("Usage: docs2epub <START_URL> <OUT.epub> [options]")
|
|
99
|
+
|
|
100
|
+
inferred_title, inferred_author, inferred_language = _infer_defaults(start_url)
|
|
101
|
+
|
|
102
|
+
title = args.title or inferred_title
|
|
103
|
+
author = args.author or inferred_author
|
|
104
|
+
language = args.language or inferred_language
|
|
79
105
|
|
|
80
106
|
options = DocusaurusNextOptions(
|
|
81
107
|
start_url=start_url,
|
|
@@ -86,26 +112,27 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
86
112
|
|
|
87
113
|
chapters = iter_docusaurus_next(options)
|
|
88
114
|
if not chapters:
|
|
89
|
-
raise SystemExit("No
|
|
115
|
+
raise SystemExit("No pages scraped (did not find article content).")
|
|
90
116
|
|
|
91
|
-
out_path: Path
|
|
92
117
|
out_path_value = Path(out_value)
|
|
93
118
|
|
|
94
119
|
if args.format == "epub2":
|
|
95
120
|
out_path = build_epub2_with_pandoc(
|
|
96
121
|
chapters=chapters,
|
|
97
122
|
out_file=out_path_value,
|
|
98
|
-
title=
|
|
99
|
-
author=
|
|
100
|
-
language=
|
|
123
|
+
title=title,
|
|
124
|
+
author=author,
|
|
125
|
+
language=language,
|
|
101
126
|
publisher=args.publisher,
|
|
102
127
|
identifier=args.identifier,
|
|
128
|
+
verbose=args.verbose,
|
|
129
|
+
options=PandocEpub2Options(keep_images=args.keep_images),
|
|
103
130
|
)
|
|
104
131
|
else:
|
|
105
132
|
meta = EpubMetadata(
|
|
106
|
-
title=
|
|
107
|
-
author=
|
|
108
|
-
language=
|
|
133
|
+
title=title,
|
|
134
|
+
author=author,
|
|
135
|
+
language=language,
|
|
109
136
|
identifier=args.identifier,
|
|
110
137
|
publisher=args.publisher,
|
|
111
138
|
)
|
|
@@ -116,6 +143,7 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
116
143
|
meta=meta,
|
|
117
144
|
)
|
|
118
145
|
|
|
146
|
+
size_mb = out_path.stat().st_size / (1024 * 1024)
|
|
119
147
|
print(f"Scraped {len(chapters)} pages")
|
|
120
|
-
print(f"EPUB written to: {out_path.resolve()}")
|
|
148
|
+
print(f"EPUB written to: {out_path.resolve()} ({size_mb:.2f} MB)")
|
|
121
149
|
return 0
|
docs2epub/kindle_html.py
CHANGED
|
@@ -5,37 +5,45 @@ import re
|
|
|
5
5
|
from bs4 import BeautifulSoup
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
def clean_html_for_kindle_epub2(
|
|
8
|
+
def clean_html_for_kindle_epub2(
|
|
9
|
+
html_fragment: str,
|
|
10
|
+
*,
|
|
11
|
+
keep_images: bool,
|
|
12
|
+
) -> str:
|
|
9
13
|
"""Best-effort HTML cleanup for Kindle-friendly EPUB2.
|
|
10
14
|
|
|
11
15
|
This is intentionally conservative: it strips known-problematic attributes
|
|
12
16
|
and tags that commonly cause Send-to-Kindle conversion issues.
|
|
17
|
+
|
|
18
|
+
By default we drop remote images to avoid pandoc fetch failures.
|
|
13
19
|
"""
|
|
14
20
|
|
|
15
21
|
soup = BeautifulSoup(html_fragment, "lxml")
|
|
16
22
|
|
|
23
|
+
if not keep_images:
|
|
24
|
+
for img in list(soup.find_all("img")):
|
|
25
|
+
src = str(img.get("src") or "")
|
|
26
|
+
if src.startswith("http://") or src.startswith("https://"):
|
|
27
|
+
img.decompose()
|
|
28
|
+
|
|
17
29
|
# EPUB2: <u> tag isn't consistently supported; convert to a span.
|
|
18
30
|
for u in list(soup.find_all("u")):
|
|
19
31
|
span = soup.new_tag("span")
|
|
20
32
|
span["style"] = "text-decoration: underline;"
|
|
21
|
-
span.string = u.get_text() if u.string is None else u.string
|
|
22
33
|
if u.string is None:
|
|
23
|
-
# Keep children by moving them into the span.
|
|
24
34
|
for child in list(u.contents):
|
|
25
35
|
span.append(child)
|
|
36
|
+
else:
|
|
37
|
+
span.string = u.string
|
|
26
38
|
u.replace_with(span)
|
|
27
39
|
|
|
28
40
|
# Remove tabindex attributes (not allowed in EPUB2 XHTML).
|
|
29
41
|
for el in soup.find_all(attrs={"tabindex": True}):
|
|
30
|
-
|
|
31
|
-
del el["tabindex"]
|
|
32
|
-
except KeyError:
|
|
33
|
-
pass
|
|
42
|
+
el.attrs.pop("tabindex", None)
|
|
34
43
|
|
|
35
44
|
# Remove start attribute from ordered lists (not allowed in EPUB2 XHTML).
|
|
36
45
|
for ol in soup.find_all("ol"):
|
|
37
|
-
|
|
38
|
-
del ol["start"]
|
|
46
|
+
ol.attrs.pop("start", None)
|
|
39
47
|
|
|
40
48
|
# Strip duplicate ids in a simple way: if an id repeats, rename it.
|
|
41
49
|
seen_ids: set[str] = set()
|
|
@@ -54,7 +62,6 @@ def clean_html_for_kindle_epub2(html_fragment: str) -> str:
|
|
|
54
62
|
el["id"] = new_id
|
|
55
63
|
seen_ids.add(new_id)
|
|
56
64
|
|
|
57
|
-
# Remove empty fragment links that point to missing ids (best-effort).
|
|
58
65
|
# If href="#something" but no element has id="something", drop href.
|
|
59
66
|
all_ids = {str(el.get("id")) for el in soup.find_all(attrs={"id": True})}
|
|
60
67
|
for a in soup.find_all("a", href=True):
|
|
@@ -62,9 +69,9 @@ def clean_html_for_kindle_epub2(html_fragment: str) -> str:
|
|
|
62
69
|
if href.startswith("#") and len(href) > 1:
|
|
63
70
|
frag = href[1:]
|
|
64
71
|
if frag not in all_ids:
|
|
65
|
-
|
|
72
|
+
a.attrs.pop("href", None)
|
|
66
73
|
|
|
67
|
-
# Normalize
|
|
74
|
+
# Normalize whitespace a bit (helps keep diffs smaller and reduces odd output).
|
|
68
75
|
text = str(soup)
|
|
69
76
|
text = re.sub(r"\s+", " ", text)
|
|
70
77
|
return text.strip()
|
docs2epub/pandoc_epub2.py
CHANGED
|
@@ -15,7 +15,8 @@ from .model import Chapter
|
|
|
15
15
|
class PandocEpub2Options:
|
|
16
16
|
toc: bool = True
|
|
17
17
|
toc_depth: int = 2
|
|
18
|
-
|
|
18
|
+
split_level: int = 1
|
|
19
|
+
keep_images: bool = False
|
|
19
20
|
|
|
20
21
|
|
|
21
22
|
def _wrap_html(title: str, body_html: str) -> str:
|
|
@@ -34,6 +35,26 @@ def _wrap_html(title: str, body_html: str) -> str:
|
|
|
34
35
|
"""
|
|
35
36
|
|
|
36
37
|
|
|
38
|
+
def _summarize_pandoc_warnings(stderr: str) -> str:
|
|
39
|
+
warnings = [line for line in stderr.splitlines() if line.startswith("[WARNING]")]
|
|
40
|
+
if not warnings:
|
|
41
|
+
return ""
|
|
42
|
+
|
|
43
|
+
resource = [w for w in warnings if "Could not fetch resource" in w]
|
|
44
|
+
duplicate = [w for w in warnings if "Duplicate identifier" in w]
|
|
45
|
+
|
|
46
|
+
parts: list[str] = []
|
|
47
|
+
parts.append(f"pandoc warnings: {len(warnings)} (use -v to see full output)")
|
|
48
|
+
if duplicate:
|
|
49
|
+
parts.append(f"- Duplicate identifier: {len(duplicate)} (usually safe; affects internal anchors)")
|
|
50
|
+
if resource:
|
|
51
|
+
parts.append(
|
|
52
|
+
f"- Missing resources: {len(resource)} (some images may be dropped; use --keep-images/-v to inspect)"
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
return "\n".join(parts)
|
|
56
|
+
|
|
57
|
+
|
|
37
58
|
def build_epub2_with_pandoc(
|
|
38
59
|
*,
|
|
39
60
|
chapters: Iterable[Chapter],
|
|
@@ -43,6 +64,7 @@ def build_epub2_with_pandoc(
|
|
|
43
64
|
language: str,
|
|
44
65
|
publisher: str | None,
|
|
45
66
|
identifier: str | None,
|
|
67
|
+
verbose: bool,
|
|
46
68
|
options: PandocEpub2Options | None = None,
|
|
47
69
|
) -> Path:
|
|
48
70
|
pandoc = shutil.which("pandoc")
|
|
@@ -61,7 +83,7 @@ def build_epub2_with_pandoc(
|
|
|
61
83
|
|
|
62
84
|
html_files: list[Path] = []
|
|
63
85
|
for ch in chapters:
|
|
64
|
-
cleaned = clean_html_for_kindle_epub2(ch.html)
|
|
86
|
+
cleaned = clean_html_for_kindle_epub2(ch.html, keep_images=opts.keep_images)
|
|
65
87
|
html_doc = _wrap_html(ch.title, cleaned)
|
|
66
88
|
fp = tmp_path / f"chapter_{ch.index:04d}.html"
|
|
67
89
|
fp.write_text(html_doc, encoding="utf-8")
|
|
@@ -81,14 +103,15 @@ def build_epub2_with_pandoc(
|
|
|
81
103
|
"encoding=UTF-8",
|
|
82
104
|
"--standalone",
|
|
83
105
|
"--split-level",
|
|
84
|
-
str(opts.
|
|
106
|
+
str(opts.split_level),
|
|
85
107
|
]
|
|
86
108
|
|
|
87
109
|
if publisher:
|
|
88
110
|
cmd.extend(["--metadata", f"publisher={publisher}"])
|
|
89
111
|
|
|
90
112
|
if identifier:
|
|
91
|
-
|
|
113
|
+
# Keep identifier stable for Kindle.
|
|
114
|
+
cmd.extend(["--metadata", f"identifier={identifier}"])
|
|
92
115
|
|
|
93
116
|
if opts.toc:
|
|
94
117
|
cmd.extend(["--toc", "--toc-depth", str(opts.toc_depth)])
|
|
@@ -96,6 +119,22 @@ def build_epub2_with_pandoc(
|
|
|
96
119
|
cmd.extend(["-o", str(out_path)])
|
|
97
120
|
cmd.extend([str(p) for p in html_files])
|
|
98
121
|
|
|
99
|
-
subprocess.run(
|
|
122
|
+
proc = subprocess.run(
|
|
123
|
+
cmd,
|
|
124
|
+
stdout=subprocess.PIPE,
|
|
125
|
+
stderr=subprocess.PIPE,
|
|
126
|
+
text=True,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
if proc.returncode != 0:
|
|
130
|
+
# On failure, always show stderr.
|
|
131
|
+
raise RuntimeError(f"pandoc failed (exit {proc.returncode}):\n{proc.stderr.strip()}")
|
|
132
|
+
|
|
133
|
+
if verbose and proc.stderr.strip():
|
|
134
|
+
print(proc.stderr.strip())
|
|
135
|
+
elif proc.stderr.strip():
|
|
136
|
+
summary = _summarize_pandoc_warnings(proc.stderr)
|
|
137
|
+
if summary:
|
|
138
|
+
print(summary)
|
|
100
139
|
|
|
101
140
|
return out_path
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: docs2epub
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Turn documentation sites into an EPUB (Kindle-friendly).
|
|
5
5
|
Author: Breno Brito
|
|
6
6
|
License: MIT
|
|
@@ -34,6 +34,11 @@ uv run docs2epub --help
|
|
|
34
34
|
### uvx (no install)
|
|
35
35
|
|
|
36
36
|
```bash
|
|
37
|
+
uvx docs2epub \
|
|
38
|
+
https://www.techinterviewhandbook.org/software-engineering-interview-guide/ \
|
|
39
|
+
tech-interview-handbook.epub
|
|
40
|
+
|
|
41
|
+
# Optional (override inferred metadata)
|
|
37
42
|
uvx docs2epub \
|
|
38
43
|
https://www.techinterviewhandbook.org/software-engineering-interview-guide/ \
|
|
39
44
|
tech-interview-handbook.epub \
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
docs2epub/__init__.py,sha256=iccyEu4zlubhvd6pM7Z2Gjwn8tPw9IhZ4ABKhbiFjUY,54
|
|
2
|
+
docs2epub/cli.py,sha256=pt1crvrkr2k1ybf_p0m4xSYyoZVluFsDNGuwJ7CykYM,3863
|
|
3
|
+
docs2epub/docusaurus_next.py,sha256=OybL3KPwDZvp2sOL3locE374Zb80M1kubx81SH2GxgQ,3378
|
|
4
|
+
docs2epub/epub.py,sha256=OsPWcPGTgazAeNpWASIE6e4HQ5ILQr2VFO1-Aj3y1kg,2986
|
|
5
|
+
docs2epub/kindle_html.py,sha256=LN0CGj9ap9b8iC_MlZcQLuhJ7FehZr_VbIfMOz78E5c,2297
|
|
6
|
+
docs2epub/model.py,sha256=uL7uwbG6yU0bEGpSFxxIv2pcZHQR9cs2prfqk5iNQwc,160
|
|
7
|
+
docs2epub/pandoc_epub2.py,sha256=l22-QAQcCgJyl7HF0_b5weC3qEGVQLwOhxdbAvd8C2o,3610
|
|
8
|
+
docs2epub-0.1.2.dist-info/METADATA,sha256=UphwnhA-8wtH-DkX1gRRiu4Fu3ukmri0GUGdCFIcU70,1886
|
|
9
|
+
docs2epub-0.1.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
10
|
+
docs2epub-0.1.2.dist-info/entry_points.txt,sha256=DHK4mzthrIXUvM8Y8Vo_3jG2IhegEDDM7T9CvCkUtvw,49
|
|
11
|
+
docs2epub-0.1.2.dist-info/RECORD,,
|
docs2epub-0.1.1.dist-info/RECORD
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
docs2epub/__init__.py,sha256=zCC51i_uxiMh2wmZYBMWo4zCNEe8sfrk0dhrvo6eZ8E,54
|
|
2
|
-
docs2epub/cli.py,sha256=wq0oQP2W9p_eRR9P0W1AvZTn-yrutxfm7u6qU2JCcRA,3056
|
|
3
|
-
docs2epub/docusaurus_next.py,sha256=OybL3KPwDZvp2sOL3locE374Zb80M1kubx81SH2GxgQ,3378
|
|
4
|
-
docs2epub/epub.py,sha256=OsPWcPGTgazAeNpWASIE6e4HQ5ILQr2VFO1-Aj3y1kg,2986
|
|
5
|
-
docs2epub/kindle_html.py,sha256=iymXlYFDfEIRIKC3vaepV_K-SFEvCPpAI5FESj7udOU,2153
|
|
6
|
-
docs2epub/model.py,sha256=uL7uwbG6yU0bEGpSFxxIv2pcZHQR9cs2prfqk5iNQwc,160
|
|
7
|
-
docs2epub/pandoc_epub2.py,sha256=m_9yJeeciyeoXMx5bVJs2qIutGDXifvd8TtFh2DRm-k,2335
|
|
8
|
-
docs2epub-0.1.1.dist-info/METADATA,sha256=QJ8YaNtyyUqoSf8dYMeT9bNAmZ3RCmb2s74_VYvbdvM,1718
|
|
9
|
-
docs2epub-0.1.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
10
|
-
docs2epub-0.1.1.dist-info/entry_points.txt,sha256=DHK4mzthrIXUvM8Y8Vo_3jG2IhegEDDM7T9CvCkUtvw,49
|
|
11
|
-
docs2epub-0.1.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|