alirezarezvani/claude-skills / engineering/book-to-skill/skills/book-to-skill
book-to-skill
UnverifiedUse when turning a book, docs folder or spec collection (PDF, EPUB, DOCX, HTML, Markdown) into an agent skill, before summarizing it by hand. Extracts with extract_document.py, compiles chapters, glossary and cheatsheet, validates with book_skill_validator.py and enforces a rights gate.
scripts/book_to_skill/parsers/epub.py
View on GitHubfrom __future__ import annotations
import posixpath
import re
import zipfile
import sys
from book_to_skill.parsers.html_text import _HTMLTextExtractor
from book_to_skill.exceptions import ExtractionError
from book_to_skill.zip_safety import _Budget, safe_read, validate_zip_xml_safety
def extract_with_ebooklib(epub_path: str) -> str | None:
"""Extract via ebooklib, after the same XML guard DOCX gets.
ebooklib parses container.xml, the OPF package document and the content docs
with a third-party XML stack whose entity handling this code does not
control. The archive is an untrusted file the user was handed, so it is
screened for DTD/entity declarations before ebooklib sees it — upstream ran
this check on DOCX only.
"""
validate_zip_xml_safety(epub_path, label="EPUB")
try:
import ebooklib
from ebooklib import epub
from bs4 import BeautifulSoup
book = epub.read_epub(epub_path)
parts = []
for item in book.get_items_of_type(ebooklib.ITEM_DOCUMENT):
soup = BeautifulSoup(item.get_content(), "html.parser")
parts.append(soup.get_text(separator="\n"))
return "\n\n".join(parts)
except ImportError:
return None
except Exception as e:
print(f" [warn] extract_with_ebooklib failed: {type(e).__name__}: {e}", file=sys.stderr)
return None
def _find_opf_path(zf: zipfile.ZipFile) -> str | None:
"""Locate the OPF package document inside an EPUB archive.
First tries ``META-INF/container.xml`` (the spec-defined entry point),
then falls back to scanning the archive for any ``.opf`` file.
"""
# Spec-defined: read container.xml for the rootfile path
try:
container = safe_read(zf, "META-INF/container.xml").decode("utf-8", errors="replace")
match = re.search(r'full-path=["\']([^"\']+\.opf)["\']', container)
if match:
return match.group(1)
except ExtractionError:
# A size/ratio refusal from safe_read is a verdict, not a parse failure:
# let it reach the caller instead of silently falling through to the .opf
# glob below. Upstream wrote `except (KeyError, Exception)`, which is just
# `except Exception` and swallowed everything.
raise
except (KeyError, OSError, UnicodeDecodeError, zipfile.BadZipFile):
# container.xml absent or malformed — the glob fallback is the answer.
pass
# Fallback: glob for any .opf file
opf_files = [n for n in zf.namelist() if n.endswith(".opf")]
return opf_files[0] if opf_files else None
def extract_with_zipfile(epub_path: str) -> str | None:
"""stdlib-only EPUB extractor: unzip → parse HTML files."""
try:
with zipfile.ZipFile(epub_path) as zf:
budget = _Budget()
names = zf.namelist()
# Locate OPF and determine its directory for resolving relative hrefs
opf_path = _find_opf_path(zf)
opf_dir = posixpath.dirname(opf_path) if opf_path else ""
# Build reading order from the OPF spine (not the manifest's href
# order), then append any remaining content docs as a safety net.
spine_order: list[str] = []
seen: set[str] = set()
if opf_path:
opf_text = safe_read(zf, opf_path, budget).decode("utf-8", errors="replace")
# Manifest: item id -> resolved href. Parse each <item> opening
# tag so attribute order (id before/after href) does not matter;
# both self-closing <item .../> and <item ...></item> forms work
# because all attributes live in the opening tag.
manifest: dict[str, str] = {}
for item_tag in re.findall(r"<item\b[^>]*?/?>", opf_text):
id_m = re.search(r'\bid=["\']([^"\']+)["\']', item_tag)
href_m = re.search(r'\bhref=["\']([^"\']+)["\']', item_tag)
if id_m and href_m:
href = href_m.group(1)
resolved = posixpath.normpath(posixpath.join(opf_dir, href)) if opf_dir else href
manifest[id_m.group(1)] = resolved
# Spine: ordered idrefs -> hrefs (true reading order).
for idref in re.findall(r'<itemref\b[^>]*?\bidref=["\']([^"\']+)["\']', opf_text):
href = manifest.get(idref)
if href and href not in seen:
spine_order.append(href)
seen.add(href)
# Safety net: append remaining manifest content documents (e.g. a
# nav doc not in the spine) in manifest order, so nothing is lost.
for href in manifest.values():
if href.endswith((".html", ".xhtml")) and href not in seen:
spine_order.append(href)
seen.add(href)
html_files = spine_order or sorted(
n for n in names if n.endswith((".html", ".xhtml"))
)
if not html_files:
return None
parts = []
for name in html_files:
try:
raw = safe_read(zf, name, budget).decode("utf-8", errors="replace")
parser = _HTMLTextExtractor()
parser.feed(raw)
parts.append(parser.get_text())
except Exception:
continue
return "\n\n".join(parts) if parts else None
except Exception as e:
print(f" [warn] extract_with_zipfile failed: {type(e).__name__}: {e}", file=sys.stderr)
return None
def count_epub_chapters(epub_path: str) -> int:
"""Count spine items (approximate chapter count) without dependencies."""
try:
with zipfile.ZipFile(epub_path) as zf:
opf_path = _find_opf_path(zf)
if not opf_path:
return 0
opf_text = safe_read(zf, opf_path).decode("utf-8", errors="replace")
return len(re.findall(r'<itemref\b', opf_text))
except Exception:
return 0
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| canvas-designanthropics/skillsCreate beautiful visual art in .png and .pdf documents using design philosophy. You should use this skill when the user asks to create a poster, piece of art, design, or other static piece. Create original visual designs, never copying existing artists' work to avoid copyright violations. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| docxanthropics/skillsUse this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include: any mention of 'Word doc', 'word document', '.docx', '.dotx', or requests to produce professional documents with formatting like tables of contents, headings, page numbers, or letterheads. Also use when extracting or reorganizing content from .docx or .dotx files, inserting or replacing images in documents, performing find-and-replace in Word files, working with tracked changes or comments, or converting content into a polished Word document. If the user asks for a 'report', 'memo', 'letter', 'template', or similar deliverable as a Word or .docx file, use this skill. Do NOT use for PDFs, spreadsheets, Google Docs, or general coding tasks unrelated to document generation. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| pdfanthropics/skillsUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple PDFs into one, splitting PDFs apart, rotating pages, adding watermarks, creating new PDFs, filling PDF forms, encrypting/decrypting PDFs, extracting images, and OCR on scanned PDFs to make them searchable. If the user mentions a .pdf file or asks to produce one, use this skill. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| pptxanthropics/skillsUse this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or presentations; reading, parsing, or extracting text from any .pptx or .potx file (even if the extracted content will be used elsewhere, like in an email or summary); editing, modifying, or updating existing presentations; combining or splitting slide files; working with templates (.potx), layouts, speaker notes, or comments. Trigger whenever the user mentions "deck," "slides," "presentation," or references a .pptx or .potx filename, regardless of what they plan to do with the content afterward. If a .pptx or .potx file needs to be opened, created, or touched, use this skill. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |