feat: add cover image support for EPUB output#3
Conversation
Add --cover and --cover-from flags to merge_and_build.py: - --cover <path>: Embed a specific cover image in the EPUB - --cover-from <epub>: Auto-extract cover from an original EPUB using OPF metadata (finds the item with id="cover-image") After extraction, the cover is re-embedded via Calibre's ebook-convert. This is necessary because Pandoc wraps cover images in an SVG element, which macOS Quick Look and some e-readers don't render as thumbnails. Calibre re-embeds the cover as a standard <img>, fixing this. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
deusyu
left a comment
There was a problem hiding this comment.
感谢这个 PR!封面支持是个好需求,不过当前实现的范围我想收敛一下。
核心问题
这个 PR 的核心需求其实很简单:给生成的 EPUB 传入封面即可,或者支持从原始 EPUB 自动提取封面后再传入生成流程。当前实现为了修 macOS Quick Look 预览,新增了一次 EPUB 生成后的二次 epub → epub 处理,这个范围有点偏大,也偏离了需求本身。
具体建议
请改成更小的实现:
- 在
merge_and_build.py支持--cover <image> - 可以保留
--cover-from <original.epub>,但它只负责从原始 EPUB 提取封面图片 - 真正的封面接入点应是 HTML → EPUB 的那次
ebook-convert,直接传--cover,不要在生成完 EPUB 之后再做二次转换。calibre_html_publish.py里的 Calibre 命令已经有--preserve-cover-aspect-ratio,加一个--cover参数即可 - 不要引入额外的 EPUB 临时重写流程,也不要从
convert.py反向 import 工具函数(find_calibre_convert已经存在于calibre_html_publish.py)
OPF 解析
如果保留 --cover-from,建议用 xml.etree.ElementTree(标准库)解析 OPF,而非正则。当前正则假设 id="cover-image" 且属性顺序固定,不够健壮。
这样更符合这个项目当前的复杂度,也更接近我想要的功能边界。期待你的更新!
- Pass --cover directly to Calibre during HTML→EPUB conversion instead of doing a separate EPUB→EPUB re-embedding step - Remove embed_cover_in_epub() and the reverse import from convert.py - Replace regex OPF parsing with xml.etree.ElementTree (stdlib) - Add <meta name="cover"> fallback for broader EPUB compatibility Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
感谢 review!你说得对,二次转换确实偏重了。已按建议重构:
净减 14 行代码,逻辑更简单。请再看看 🙏 |
|
补充一个建议:当前 不过还有一个行为点建议一起处理: 建议把 cover 文件也纳入 rebuild 判断,至少覆盖这两种情况:
这也是后续测试里最值得加的一条检查点。 |
Add cover file mtime to generate_format() skip logic so that: 1. Adding --cover to an existing temp dir triggers a rebuild 2. Replacing the cover image triggers a rebuild
|
好建议!已修复。
改动很小(+8 行),核心就是在 |
Summary
--cover <path>flag to embed a specific cover image in the EPUB--cover-from <epub>flag to auto-extract cover from the original EPUB using OPF metadata (id="cover-image")Usage
Why Calibre re-embedding?
Pandoc generates cover pages using
<svg><image xlink:href="..."/></svg>. This is valid EPUB but macOS Quick Look (Finder spacebar preview) doesn't render it as a thumbnail. Running through Calibre'sebook-convertreplaces the SVG wrapper with a standard<img>, which Quick Look recognizes.Test plan
extract_cover_from_epub()with a real EPUB (Animal Spirits) — correctly findsOEBPS/Image00003.jpg(60KB)embed_cover_in_epub()— Calibre re-embedding produces working Quick Look thumbnails🤖 Generated with Claude Code