Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.
Python agent-skillsai-agentsbook-to-skillcontext-engineeringdocument-processingedtechknowledge-baseknowledge-managementllmpdf-to-markdownragself-studystudy-tools
refactor: modularize extract.py into structured extractor package and update docs
Marcello Mendo3d4c28caaa10
diff --git a/README.md b/README.mdindex 5c81c91..b0ec706 100644--- a/README.md+++ b/README.md@@ -1,7 +1,7 @@<h1 align="center">📚 book-to-skill</h1><p align="center">- <strong>Turn any technical book or document into a Claude Code skill — ready to study, reference, and use while you work.</strong>+ <strong>Turn any technical book, document folder, or collection of sources into a unified Claude Code skill — ready to study, reference, and use while you work.</strong></p><p align="center">@@ -40,7 +40,7 @@ Once installed, you just type `/your-book-slug replication` and Claude reads the## 📦 What it generates-Running `/book-to-skill your-book.pdf` (or `.epub`) creates a full skill at `~/.claude/skills/<slug>/`:+Running `/book-to-skill your-book.pdf` (or a folder, glob, or list of files) creates a full skill at `~/.claude/skills/<slug>/`:| File | Purpose | Size ||------|---------|------|@@ -57,7 +57,7 @@ Running `/book-to-skill your-book.pdf` (or `.epub`) creates a full skill at `~/.## 🚀 Usage```-/book-to-skill <path-to-document> [skill-name-slug]+/book-to-skill <path-to-document-folder-or-glob>... [skill-name-slug]```Supported document formats: PDF, EPUB, DOCX, TXT, Markdown, reStructuredText, AsciiDoc, HTML, RTF, MOBI/AZW/AZW3.@@ -65,14 +65,17 @@ Supported document formats: PDF, EPUB, DOCX, TXT, Markdown, reStructuredText, As**Examples:**```bash-# PDF — derive skill name from filename-/book-to-skill ~/Downloads/designing-data-intensive-applications.pdf+# Process several files together into a unified skill+/book-to-skill ~/papers/paper1.pdf ~/notes/export.txt unified-research-# EPUB — specify a custom slug-/book-to-skill ~/books/clean-code.epub clean-code+# Process all supported files in a folder together+/book-to-skill ~/workspace/project-docs/ project-knowledge-# Full path with explicit name-/book-to-skill /tmp/ddd-evans.pdf domain-driven-design+# Process files matching a glob pattern+/book-to-skill "~/books/*.epub" my-library++# Update/fold new material into an existing skill folder+/book-to-skill ~/articles/new-paper.pdf ~/.claude/skills/project-knowledge```After the skill is created, use it like any other Claude Code skill:@@ -208,9 +211,9 @@ It also shines for books Claude doesn't know at all: niche technical references,**"NotebookLM handles multiple books better."**-Absolutely true — if your workflow is "I have 80 books and I want to search across all of them," NotebookLM is the right tool.+Absolutely true — if your workflow is "I have 80 separate books and I want to search across all of them," NotebookLM is the right tool.-book-to-skill is built for a different job: you want to go deep on one book and have its frameworks embedded in your coding or writing workflow, not in a separate browser tab. It's less "library search" and more "the author is sitting next to you while you work."+book-to-skill is built for a different job: you want to go deep on a specific topic or library, having multiple related documents (papers, chapters, notes) folded into a single unified skill, and even updating it over time as new material arrives! This integrates your customized knowledge base right into your coding or writing workflow, rather than in a separate browser tab.---@@ -222,16 +225,10 @@ Copy this into your Claude Code session:Install book-to-skill: https://raw.githubusercontent.com/virgiliojr94/book-to-skill/master/SKILL.md```-Or manually:+Or manually using standard `git clone` (ensures modular engine files are fetched correctly):```bash-mkdir -p ~/.claude/skills/book-to-skill/scripts--curl -o ~/.claude/skills/book-to-skill/SKILL.md \- https://raw.githubusercontent.com/virgiliojr94/book-to-skill/master/SKILL.md--curl -o ~/.claude/skills/book-to-skill/scripts/extract.py \- https://raw.githubusercontent.com/virgiliojr94/book-to-skill/master/scripts/extract.py+git clone https://github.com/virgiliojr94/book-to-skill.git ~/.claude/skills/book-to-skill```Then in any Claude Code session:@@ -250,7 +247,13 @@ Then in any Claude Code session:book-to-skill/├── SKILL.md # Skill definition + step-by-step instructions├── scripts/-│ └── extract.py # PDF + EPUB extraction (pdftotext / PyPDF2 / pdfminer / ebooklib / zipfile)+│ ├── extract.py # Thin entrypoint wrapper+│ └── extractor/ # Modular extraction package+│ ├── __init__.py+│ ├── config.py+│ ├── dependencies.py+│ ├── utils.py+│ └── parsers/ # Format-specific parser components (pdf, epub, docx, etc.)└── README.md # This file```diff --git a/scripts/extract.py b/scripts/extract.pyindex 4f03851..ef8c30e 100644--- a/scripts/extract.py+++ b/scripts/extract.py@@ -1,947 +1,17 @@#!/usr/bin/env python3"""Extract text from a document file for book-to-skill processing.--PDF extraction tries methods in order:- 1. pdftotext (poppler-utils) — best quality- 2. PyPDF2 — common Python library- 3. pdfminer.six — thorough fallback--EPUB extraction tries methods in order:- 1. ebooklib + BeautifulSoup4 — best quality- 2. zipfile + html.parser — stdlib fallback (no extra deps)--Other supported formats:- - Plain text / Markdown / reStructuredText / AsciiDoc — direct read- - HTML — BeautifulSoup4 if available, then stdlib html.parser- - DOCX — python-docx if available, then stdlib ZIP/XML fallback- - RTF — striprtf if available, then regex fallback- - MOBI/AZW/AZW3 — Calibre ebook-convert if available--Outputs:- <tempdir>/book_skill_work/full_text.txt — full extracted text- <tempdir>/book_skill_work/metadata.json — stats and metadata--Set BOOK_SKILL_WORKDIR to override the output directory.+Modular entrypoint wrapper."""-import glob-import html-import html.parser-import importlib.util-import jsonimport os-import re-import shutil-import subprocessimport sys-import tempfile-import zipfile-from pathlib import Path--OUTPUT_DIR = Path(- os.environ.get(- "BOOK_SKILL_WORKDIR",- str(Path(tempfile.gettempdir()) / "book_skill_work"),- )-)-OUTPUT_TEXT = OUTPUT_DIR / "full_text.txt"-OUTPUT_META = OUTPUT_DIR / "metadata.json"--WORDS_PER_TOKEN = 0.75 # approximate--TEXT_EXTENSIONS = {".txt", ".text", ".md", ".markdown", ".rst", ".adoc", ".asciidoc"}-HTML_EXTENSIONS = {".html", ".htm", ".xhtml"}-CALIBRE_EBOOK_EXTENSIONS = {".mobi", ".azw", ".azw3"}-SUPPORTED_EXTENSIONS = {- ".pdf", ".epub", ".docx", ".rtf",- *TEXT_EXTENSIONS,- *HTML_EXTENSIONS,- *CALIBRE_EBOOK_EXTENSIONS,-}--PYTHON_DEPENDENCIES = {- "docling": "docling",- "PyPDF2": "PyPDF2",- "pdfminer": "pdfminer.six",- "ebooklib": "ebooklib",- "bs4": "beautifulsoup4",- "docx": "python-docx",- "striprtf": "striprtf",-}---def estimate_tokens(text: str) -> int:- return int(len(text.split()) / WORDS_PER_TOKEN)---def supported_formats_message() -> str:- return ", ".join(sorted(SUPPORTED_EXTENSIONS))---def python_module_available(module_name: str) -> bool:- return importlib.util.find_spec(module_name) is not None---def missing_python_packages(module_names: list[str]) -> list[str]:- missing = []- for module_name in module_names:- if not python_module_available(module_name):- missing.append(PYTHON_DEPENDENCIES[module_name])- return missing---def install_python_packages(packages: list[str]) -> bool:- if not packages:- return True-- print(f"Installing missing Python package(s): {', '.join(packages)}")- try:- result = subprocess.run(- [sys.executable, "-m", "pip", "install", *packages],- text=True,- timeout=600,- )- except Exception as exc:- print(f"Package installation failed: {exc}", file=sys.stderr)- return False-- importlib.invalidate_caches()- return result.returncode == 0---def normalize_install_mode(argv: list[str]) -> str:- mode = os.environ.get("BOOK_SKILL_INSTALL_MISSING", "ask").lower()- if "--no-install-missing" in argv:- return "no"- if "--install-missing" in argv:- idx = argv.index("--install-missing")- if idx + 1 < len(argv) and not argv[idx + 1].startswith("--"):- mode = argv[idx + 1].lower()- else:- mode = "yes"- if mode in {"1", "true", "y", "yes", "install"}:- return "yes"- if mode in {"0", "false", "n", "no", "fallback", "skip"}:- return "no"- return "ask"---def offer_dependency_install(- *,- feature: str,- module_names: list[str],- fallback: str | None,- install_mode: str,-) -> None:- packages = missing_python_packages(module_names)- if not packages:- return-- message = f"{feature} uses {', '.join(packages)} if installed"- if fallback:- message += f", otherwise {fallback}"- message += "."- print(message)-- should_install = False- if install_mode == "yes":- should_install = True- elif install_mode == "ask" and sys.stdin.isatty():- answer = input("Missing package(s) detected. Do you want to install? y=install, n=fallback: ").strip().lower()- should_install = answer in {"y", "yes", "install"}- else:- if fallback:- print("Non-interactive mode or install disabled; using fallback.")- else:- print("Non-interactive mode or install disabled; installation skipped.")-- if not should_install:- if fallback:- print(f"Using fallback: {fallback}.")- return-- if install_python_packages(packages):- still_missing = missing_python_packages(module_names)- if not still_missing:- print("Package installation complete.")- return- print(f"Package installation incomplete; still missing: {', '.join(still_missing)}", file=sys.stderr)- else:- print("Package installation failed.", file=sys.stderr)-- if fallback:- print(f"Using fallback: {fallback}.")---def prepare_dependencies(ext: str, extraction_mode: str, install_mode: str) -> None:- if ext == ".pdf" and extraction_mode == "technical":- offer_dependency_install(- feature="Technical PDF extraction",- module_names=["docling"],- fallback="the PDF text fallback chain",- install_mode=install_mode,- )-- if ext == ".pdf" and not shutil.which("pdftotext"):- offer_dependency_install(- feature="PDF text extraction",- module_names=["PyPDF2", "pdfminer"],- fallback="any installed Python PDF parser; extraction fails if none are available",- install_mode=install_mode,- )-- if ext == ".epub":- offer_dependency_install(- feature="EPUB extraction",- module_names=["ebooklib", "bs4"],- fallback="a stdlib ZIP/HTML parser",- install_mode=install_mode,- )-- if ext in HTML_EXTENSIONS:- offer_dependency_install(- feature="HTML extraction",- module_names=["bs4"],- fallback="a stdlib HTML parser",- install_mode=install_mode,- )-- if ext == ".docx":- offer_dependency_install(- feature="DOCX extraction",- module_names=["docx"],- fallback="a stdlib ZIP/XML parser",- install_mode=install_mode,- )-- if ext == ".rtf":- offer_dependency_install(- feature="RTF extraction",- module_names=["striprtf"],- fallback="a basic regex cleanup fallback",- install_mode=install_mode,- )---def read_text_file(path: str) -> str | None:- for encoding in ("utf-8-sig", "utf-8", "cp1252", "latin-1"):- try:- return Path(path).read_text(encoding=encoding)- except UnicodeDecodeError:- continue- except Exception:- return None- return None---def extract_html_content(raw_html: str) -> str:- try:- from bs4 import BeautifulSoup- soup = BeautifulSoup(raw_html, "html.parser")- for element in soup(["script", "style", "head"]):- element.decompose()- return soup.get_text(separator="\n")- except ImportError:- parser = _HTMLTextExtractor()- parser.feed(raw_html)- return parser.get_text()---def extract_html_file(path: str) -> str | None:- raw = read_text_file(path)- if raw is None:- return None- return extract_html_content(raw)---def extract_docx_with_python_docx(docx_path: str) -> str | None:- try:- import docx- document = docx.Document(docx_path)- parts = [paragraph.text for paragraph in document.paragraphs if paragraph.text]- for table in document.tables:- for row in table.rows:- cells = [cell.text.strip() for cell in row.cells]- if any(cells):- parts.append("\t".join(cells))- return "\n".join(parts)- except ImportError:- return None- except Exception:- return None---def extract_docx_with_zipfile(docx_path: str) -> str | None:- try:- import xml.etree.ElementTree as ET-- with zipfile.ZipFile(docx_path) as zf:- xml_bytes = zf.read("word/document.xml")- root = ET.fromstring(xml_bytes)- namespace = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"- parts: list[str] = []- for paragraph in root.iter(f"{namespace}p"):- texts = [node.text for node in paragraph.iter(f"{namespace}t") if node.text]- if texts:- parts.append("".join(texts))- return "\n".join(parts) if parts else None- except Exception:- return None---def extract_docx(docx_path: str) -> tuple[str, str]:- print("Trying python-docx...", end=" ", flush=True)- text = extract_docx_with_python_docx(docx_path)- if text and text.strip():- print("OK")- return text, "python-docx"-- print("not available")- print("Trying stdlib DOCX parser...", end=" ", flush=True)- text = extract_docx_with_zipfile(docx_path)- if text and text.strip():- print("OK")- return text, "zipfile-docx"-- print("FAILED")- print(- "\nERROR: Could not extract text from DOCX.\n"- "Install python-docx for best results:\n"- " pip3 install python-docx",- file=sys.stderr,- )- sys.exit(1)---def strip_rtf_fallback(raw: str) -> str:- raw = re.sub(r"\\'[0-9a-fA-F]{2}", " ", raw)- raw = re.sub(r"\\par[d]?", "\n", raw)- raw = re.sub(r"\\tab", "\t", raw)- raw = re.sub(r"\\[a-zA-Z]+-?\d* ?", "", raw)- raw = raw.replace("{", "").replace("}", "")- return html.unescape(raw)---def extract_rtf(rtf_path: str) -> tuple[str, str]:- raw = read_text_file(rtf_path)- if raw is None:- print("ERROR: Could not read RTF file", file=sys.stderr)- sys.exit(1)-- try:- from striprtf.striprtf import rtf_to_text- text = rtf_to_text(raw)- if text.strip():- return text, "striprtf"- except ImportError:- pass- except Exception:- pass-- return strip_rtf_fallback(raw), "rtf-regex"---def extract_with_ebook_convert(input_path: str) -> str | None:- if not shutil.which("ebook-convert"):- return None- output_path = OUTPUT_DIR / "ebook-convert-output.txt"- try:- result = subprocess.run(- ["ebook-convert", input_path, str(output_path)],- capture_output=True, text=True, timeout=300- )- if result.returncode == 0 and output_path.exists():- text = output_path.read_text(encoding="utf-8", errors="replace")- if text.strip():- return text- except Exception:- pass- return None---def extract_with_pdftotext(pdf_path: str) -> str | None:- if not shutil.which("pdftotext"):- return None- try:- result = subprocess.run(- ["pdftotext", "-layout", pdf_path, "-"],- capture_output=True, text=True, timeout=120- )- if result.returncode == 0 and result.stdout.strip():- return result.stdout- except Exception:- pass- return None---def extract_with_pypdf2(pdf_path: str) -> str | None:- try:- import PyPDF2- text_parts = []- with open(pdf_path, "rb") as f:- reader = PyPDF2.PdfReader(f)- for page in reader.pages:- try:- text_parts.append(page.extract_text() or "")- except Exception:- text_parts.append("")- return "\n".join(text_parts)- except ImportError:- return None- except Exception:- return None---def extract_with_pdfminer(pdf_path: str) -> str | None:- try:- from pdfminer.high_level import extract_text- return extract_text(pdf_path)- except ImportError:- return None- except Exception:- return None---def extract_with_ebooklib(epub_path: str) -> str | None:- 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:- return None---class _HTMLTextExtractor(html.parser.HTMLParser):- """Minimal HTML → plain text converter using stdlib only."""-- SKIP_TAGS = {"script", "style", "head"}-- def __init__(self):- super().__init__()- self._parts: list[str] = []- self._skip_depth = 0- self._current_skip: str | None = None-- def handle_starttag(self, tag, attrs):- if tag in self.SKIP_TAGS:- self._skip_depth += 1- if tag in ("p", "br", "h1", "h2", "h3", "h4", "h5", "h6", "li", "div"):- self._parts.append("\n")-- def handle_endtag(self, tag):- if tag in self.SKIP_TAGS and self._skip_depth:- self._skip_depth -= 1-- def handle_data(self, data):- if not self._skip_depth:- self._parts.append(data)-- def get_text(self) -> str:- return html.unescape("".join(self._parts))---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:- names = zf.namelist()- # Read OPF spine to get reading order, fall back to sorted xhtml files- spine_order: list[str] = []- opf_files = [n for n in names if n.endswith(".opf")]- if opf_files:- opf_text = zf.read(opf_files[0]).decode("utf-8", errors="replace")- spine_order = re.findall(r'href=["\']([^"\']+\.(?:xhtml|html))["\']', opf_text)-- 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 = zf.read(name).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:- return None---def extract_epub(epub_path: str) -> tuple[str, str]:- """Return (text, method) for an EPUB file."""- print("Trying ebooklib + BeautifulSoup4...", end=" ", flush=True)- text = extract_with_ebooklib(epub_path)- if text and text.strip():- print("OK")- return text, "ebooklib"-- print("not available")- print("Trying stdlib zipfile parser...", end=" ", flush=True)- text = extract_with_zipfile(epub_path)- if text and text.strip():- print("OK")- return text, "zipfile"-- print("FAILED")- print(- "\nERROR: Could not extract text from EPUB.\n"- "Install ebooklib + beautifulsoup4 for best results:\n"- " pip3 install ebooklib beautifulsoup4",- file=sys.stderr,- )- sys.exit(1)---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_files = [n for n in zf.namelist() if n.endswith(".opf")]- if not opf_files:- return 0- opf_text = zf.read(opf_files[0]).decode("utf-8", errors="replace")- return len(re.findall(r'<itemref\b', opf_text))- except Exception:- return 0---def count_pages(pdf_path: str) -> int:- # Try pdfinfo first- if shutil.which("pdfinfo"):- try:- result = subprocess.run(- ["pdfinfo", pdf_path], capture_output=True, text=True, timeout=15- )- for line in result.stdout.splitlines():- if line.startswith("Pages:"):- return int(line.split(":")[1].strip())- except Exception:- pass- # Fallback: count form-feed chars (pdftotext -layout uses \f between pages)- try:- import PyPDF2- with open(pdf_path, "rb") as f:- return len(PyPDF2.PdfReader(f).pages)- except Exception:- return 0---def detect_structure(text: str) -> dict:- """Detect chapter count and table of contents presence."""- import re- lines = text[:50000].splitlines()-- # Look for chapter headings- chapter_pattern = re.compile(- r"^\s*(chapter\s+\d+|CHAPTER\s+\d+|ch\.\s*\d+|\d+\.\s+[A-Z])",- re.IGNORECASE- )- chapters_found = [l.strip() for l in lines if chapter_pattern.match(l)]-- # Look for ToC indicators in the first ~30k chars (front matter is often well past 5k:- # copyright pages, praise, dedications, forewords). Require the keyword to appear on- # its own line to avoid false positives like "the contents of this book are...".- toc_pattern = re.compile(- r"^\s*(?:table of contents|contents|índice|sumário)\s*$",- re.IGNORECASE | re.MULTILINE,- )- has_toc = bool(toc_pattern.search(text[:30000]))-- return {- "chapters_detected": len(chapters_found),- "chapter_headings_sample": chapters_found[:10],- "has_toc": has_toc,- }---def extract_with_docling(pdf_path: str) -> str | None:- """Layout-aware extraction using Docling. Best for technical books with tables and code."""- try:- from docling.document_converter import DocumentConverter- from docling.datamodel.pipeline_options import PdfPipelineOptions- from docling.datamodel.base_models import InputFormat- from docling.document_converter import PdfFormatOption-- pipeline_options = PdfPipelineOptions()- pipeline_options.do_ocr = False- pipeline_options.do_table_structure = True-- converter = DocumentConverter(- format_options={- InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)- }- )- result = converter.convert(pdf_path)- return result.document.export_to_markdown()- except ImportError:- return None- except Exception:- return None---def parse_arguments(argv: list[str]) -> tuple[list[str], str, str]:- """Parse argv into (input_paths, extraction_mode, install_mode)."""- input_paths = []- extraction_mode = "text"-- args = argv[1:]- i = 0- while i < len(args):- arg = args[i]- if arg == "--mode":- if i + 1 < len(args):- extraction_mode = args[i+1].lower()- i += 2- else:- i += 1- elif arg == "--install-missing":- if i + 1 < len(args) and not args[i+1].startswith("--"):- i += 2- else:- i += 1- elif arg == "--no-install-missing":- i += 1- elif arg.startswith("-"):- i += 1- else:- input_paths.append(arg)- i += 1-- install_mode = normalize_install_mode(argv)- if extraction_mode not in ("technical", "text"):- extraction_mode = "text"-- return input_paths, extraction_mode, install_mode---def resolve_input_files(paths: list[str]) -> list[Path]:- """Resolve paths including files, directories, and glob patterns to Path objects."""- resolved = []- for path_str in paths:- # Check if it has glob wildcards- if any(char in path_str for char in ("*", "?", "[")):- glob_matches = glob.glob(path_str, recursive=True)- for match in glob_matches:- p = Path(match)- if p.is_file():- resolved.append(p.resolve())- else:- p = Path(path_str)- if p.is_dir():- for root, _, files in os.walk(p):- for file in files:- file_path = Path(root) / file- if file_path.suffix.lower() in SUPPORTED_EXTENSIONS:- resolved.append(file_path.resolve())- else:- # Keep even if it doesn't exist so the error check can report it- resolved.append(p.resolve())-- # Deduplicate while preserving order, then sort by file path for consistency- seen = set()- unique_paths = []- for path in resolved:- resolved_path = path.resolve() if path.exists() else path- if resolved_path not in seen:- seen.add(resolved_path)- unique_paths.append(resolved_path)-- unique_paths.sort(key=lambda x: str(x).lower())- return unique_paths---def extract_single_file(input_path: Path, extraction_mode: str, install_mode: str) -> dict:- """Extract text and metadata from a single file path."""- input_str = str(input_path)-- if not input_path.exists():- print(f"ERROR: File not found: {input_str}", file=sys.stderr)- sys.exit(1)-- ext = input_path.suffix.lower()- document_format = ext.lstrip(".")-- # Sniff magic bytes if suffix is not supported- if ext not in SUPPORTED_EXTENSIONS:- with open(input_str, "rb") as f:- header = f.read(8)- if header[:4] == b"%PDF":- ext = ".pdf"- document_format = "pdf"- elif header[:2] == b"PK":- try:- with zipfile.ZipFile(input_str) as zf:- names = set(zf.namelist())- if "mimetype" in names and zf.read("mimetype").startswith(b"application/epub"):- ext = ".epub"- document_format = "epub"- elif "word/document.xml" in names:- ext = ".docx"- document_format = "docx"- else:- print(- f"ERROR: Unsupported ZIP-based format '{input_path.name}'. Supported: {supported_formats_message()}",- file=sys.stderr,- )- sys.exit(1)- except (zipfile.BadZipFile, KeyError, OSError):- print(- f"ERROR: Unsupported ZIP-based format '{input_path.name}'. Supported: {supported_formats_message()}",- file=sys.stderr,- )- sys.exit(1)- else:- print(- f"ERROR: Unsupported format '{ext or '<none>'}'. Supported: {supported_formats_message()}",- file=sys.stderr,- )- sys.exit(1)-- prepare_dependencies(ext, extraction_mode, install_mode)-- if ext in CALIBRE_EBOOK_EXTENSIONS and not shutil.which("ebook-convert"):- print(- "MOBI/AZW/AZW3 extraction requires Calibre's ebook-convert command. "- "Install Calibre and ensure ebook-convert is on PATH, then rerun this command.",- file=sys.stderr,- )- sys.exit(1)-- text = ""- method = ""- pages = 0- pages_label = "sections"-- if ext == ".epub":- print(f"Extracting EPUB: {input_str}")- text, method = extract_epub(input_str)- pages = count_epub_chapters(input_str)- pages_label = "spine_items"- elif ext == ".pdf":- print(f"Extracting PDF: {input_str}")- if extraction_mode == "technical":- print("Mode: technical — using Docling (layout-aware)...", end=" ", flush=True)- text = extract_with_docling(input_str)- if text:- method = "docling"- print("OK")- else:- print("not available, falling back to pdftotext")- extraction_mode = "text"-- if extraction_mode == "text" or not text:- print("Mode: text — using pdftotext...")- print("Trying pdftotext...", end=" ", flush=True)- text = extract_with_pdftotext(input_str)-- if text:- method = "pdftotext"- print("OK")- else:- print("not available")- print("Trying PyPDF2...", end=" ", flush=True)- text = extract_with_pypdf2(input_str)- if text:- method = "PyPDF2"- print("OK")- else:- print("not available")- print("Trying pdfminer.six...", end=" ", flush=True)- text = extract_with_pdfminer(input_str)- if text:- method = "pdfminer"- print("OK")- else:- print("FAILED")- print(- "\nERROR: Could not extract text from PDF.\n"- "Install one of: poppler-utils (pdftotext), PyPDF2, or pdfminer.six\n"- " sudo apt install poppler-utils\n"- " pip3 install PyPDF2\n"- " pip3 install pdfminer.six",- file=sys.stderr,- )- sys.exit(1)-- pages = count_pages(input_str)- pages_label = "pages"- elif ext in TEXT_EXTENSIONS:- print(f"Extracting text document: {input_str}")- text = read_text_file(input_str)- if text is None or not text.strip():- print("ERROR: Could not read text document", file=sys.stderr)- sys.exit(1)- method = "plain-text"- pages = 0- pages_label = "sections"- elif ext in HTML_EXTENSIONS:- print(f"Extracting HTML: {input_str}")- text = extract_html_file(input_str)- if text is None or not text.strip():- print("ERROR: Could not extract text from HTML", file=sys.stderr)- sys.exit(1)- method = "html-parser"- pages = 0- pages_label = "sections"- elif ext == ".docx":- print(f"Extracting DOCX: {input_str}")- text, method = extract_docx(input_str)- pages = 0- pages_label = "sections"- elif ext == ".rtf":- print(f"Extracting RTF: {input_str}")- text, method = extract_rtf(input_str)- pages = 0- pages_label = "sections"- elif ext in CALIBRE_EBOOK_EXTENSIONS:- print(f"Extracting ebook with Calibre: {input_str}")- text = extract_with_ebook_convert(input_str)- if text is None or not text.strip():- print(- f"ERROR: Could not extract text from {ext}. Install Calibre and ensure ebook-convert is on PATH.",- file=sys.stderr,- )- sys.exit(1)- method = "ebook-convert"- pages = 0- pages_label = "sections"-- tokens = estimate_tokens(text)- structure = detect_structure(text)- file_size_mb = os.path.getsize(input_str) / (1024 * 1024)-- return {- "source_file": str(input_path.resolve()),- "filename": input_path.name,- "format": document_format,- "extraction_method": method,- "file_size_mb": round(file_size_mb, 2),- pages_label: pages,- "pages_label": pages_label,- "pages": pages,- "chars": len(text),- "words": len(text.split()),- "estimated_tokens": tokens,- "text": text,- **structure,- }--def main():- if len(sys.argv) < 2:- print("Usage: extract.py <path-to-document-folder-or-glob>... [--mode technical|text] [--install-missing ask|yes|no]", file=sys.stderr)- print(f"Supported formats: {supported_formats_message()}", file=sys.stderr)- sys.exit(1)-- raw_input_paths, extraction_mode, install_mode = parse_arguments(sys.argv)-- if not raw_input_paths:- print("ERROR: No input document, folder, or glob pattern specified.", file=sys.stderr)- sys.exit(1)-- input_files = resolve_input_files(raw_input_paths)-- if not input_files:- print(f"ERROR: No supported files found matching: {', '.join(raw_input_paths)}", file=sys.stderr)- sys.exit(1)-- OUTPUT_DIR.mkdir(parents=True, exist_ok=True)-- extracted_sources = []- combined_texts = []-- for file_path in input_files:- res = extract_single_file(file_path, extraction_mode, install_mode)- extracted_sources.append(res)-- # Format the text with a clear boundary- separator = f"\n\n{'=' * 80}\nSOURCE: {res['filename']} (Path: {res['source_file']})\n{'=' * 80}\n\n"- combined_texts.append(separator + res["text"])-- # Combine texts- consolidated_text = "".join(combined_texts).strip()-- # Write combined text- OUTPUT_TEXT.write_text(consolidated_text, encoding="utf-8")-- # Consolidate metadata- total_file_size_mb = sum(src["file_size_mb"] for src in extracted_sources)- total_pages = sum(src["pages"] for src in extracted_sources)- total_chars = len(consolidated_text)- total_words = len(consolidated_text.split())- total_tokens = estimate_tokens(consolidated_text)-- # Detect structure on consolidated text- consolidated_structure = detect_structure(consolidated_text)-- metadata = {- "source_file": "Consolidated from multiple sources" if len(extracted_sources) > 1 else extracted_sources[0]["source_file"],- "filename": "multi-source" if len(extracted_sources) > 1 else extracted_sources[0]["filename"],- "format": "mixed" if len(extracted_sources) > 1 else extracted_sources[0]["format"],- "extraction_method": "multi-method" if len(extracted_sources) > 1 else extracted_sources[0]["extraction_method"],- "extraction_mode": extraction_mode,- "file_size_mb": round(total_file_size_mb, 2),- "pages": total_pages,- "chars": total_chars,- "words": total_words,- "estimated_tokens": total_tokens,- "estimated_tokens_human": f"~{total_tokens // 1000}K",- "output_text": str(OUTPUT_TEXT),- "total_sources": len(extracted_sources),- "sources": [- {- "source_file": src["source_file"],- "filename": src["filename"],- "format": src["format"],- "extraction_method": src["extraction_method"],- "file_size_mb": src["file_size_mb"],- "pages": src["pages"],- "pages_label": src["pages_label"],- "chars": src["chars"],- "words": src["words"],- "estimated_tokens": src["estimated_tokens"],- "chapters_detected": src["chapters_detected"],- "has_toc": src["has_toc"]- }- for src in extracted_sources- ],- **consolidated_structure,- }-- OUTPUT_META.write_text(json.dumps(metadata, indent=2, ensure_ascii=False))-- page_line = f" Total Pages: {total_pages}"- print("\nExtraction complete:")- print(f" Sources : {len(extracted_sources)} processed")- print(f" Size : {total_file_size_mb:.2f} MB")- print(page_line)- print(f" Words : {total_words:,}")- print(f" Tokens : ~{total_tokens // 1000}K")- print(f" Chapters: {consolidated_structure['chapters_detected']} detected overall")- print(f" ToC : {'yes' if consolidated_structure['has_toc'] else 'not detected'}")- if not consolidated_structure["has_toc"]:- print(- " WARN : No table of contents detected — chapter mapping in Step 3 "- "will rely on heading scan only, which may miss or duplicate sections."- )- print(f"\n Text -> {OUTPUT_TEXT}")- print(f" Meta -> {OUTPUT_META}")+# Ensure the parent directory of this script's directory (i.e. the repository root)+# is in sys.path so that the modular 'extractor' package can be imported reliably.+sys.path.insert(0, str(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))+from extractor.utils import mainif __name__ == "__main__":main()diff --git a/scripts/extractor/__init__.py b/scripts/extractor/__init__.pynew file mode 100644index 0000000..ca3871b--- /dev/null+++ b/scripts/extractor/__init__.py@@ -0,0 +1 @@+from extractor.utils import resolve_input_files, extract_single_file, maindiff --git a/scripts/extractor/config.py b/scripts/extractor/config.pynew file mode 100644index 0000000..3486742--- /dev/null+++ b/scripts/extractor/config.py@@ -0,0 +1,38 @@+import os+import tempfile+from pathlib import Path++OUTPUT_DIR = Path(+ os.environ.get(+ "BOOK_SKILL_WORKDIR",+ str(Path(tempfile.gettempdir()) / "book_skill_work"),+ )+)+OUTPUT_TEXT = OUTPUT_DIR / "full_text.txt"+OUTPUT_META = OUTPUT_DIR / "metadata.json"++WORDS_PER_TOKEN = 0.75 # approximate++TEXT_EXTENSIONS = {".txt", ".text", ".md", ".markdown", ".rst", ".adoc", ".asciidoc"}+HTML_EXTENSIONS = {".html", ".htm", ".xhtml"}+CALIBRE_EBOOK_EXTENSIONS = {".mobi", ".azw", ".azw3"}+SUPPORTED_EXTENSIONS = {+ ".pdf", ".epub", ".docx", ".rtf",+ *TEXT_EXTENSIONS,+ *HTML_EXTENSIONS,+ *CALIBRE_EBOOK_EXTENSIONS,+}++PYTHON_DEPENDENCIES = {+ "docling": "docling",+ "PyPDF2": "PyPDF2",+ "pdfminer": "pdfminer.six",+ "ebooklib": "ebooklib",+ "bs4": "beautifulsoup4",+ "docx": "python-docx",+ "striprtf": "striprtf",+}+++def supported_formats_message() -> str:+ return ", ".join(sorted(SUPPORTED_EXTENSIONS))diff --git a/scripts/extractor/dependencies.py b/scripts/extractor/dependencies.pynew file mode 100644index 0000000..ca5b6e7--- /dev/null+++ b/scripts/extractor/dependencies.py@@ -0,0 +1,151 @@+import importlib.util+import os+import shutil+import subprocess+import sys+from extractor.config import PYTHON_DEPENDENCIES, HTML_EXTENSIONS+++def python_module_available(module_name: str) -> bool:+ return importlib.util.find_spec(module_name) is not None+++def missing_python_packages(module_names: list[str]) -> list[str]:+ missing = []+ for module_name in module_names:+ if not python_module_available(module_name):+ missing.append(PYTHON_DEPENDENCIES[module_name])+ return missing+++def install_python_packages(packages: list[str]) -> bool:+ if not packages:+ return True++ print(f"Installing missing Python package(s): {', '.join(packages)}")+ try:+ result = subprocess.run(+ [sys.executable, "-m", "pip", "install", *packages],+ text=True,+ timeout=600,+ )+ except Exception as exc:+ print(f"Package installation failed: {exc}", file=sys.stderr)+ return False++ importlib.invalidate_caches()+ return result.returncode == 0+++def normalize_install_mode(argv: list[str]) -> str:+ mode = os.environ.get("BOOK_SKILL_INSTALL_MISSING", "ask").lower()+ if "--no-install-missing" in argv:+ return "no"+ if "--install-missing" in argv:+ idx = argv.index("--install-missing")+ if idx + 1 < len(argv) and not argv[idx + 1].startswith("--"):+ mode = argv[idx + 1].lower()+ else:+ mode = "yes"+ if mode in {"1", "true", "y", "yes", "install"}:+ return "yes"+ if mode in {"0", "false", "n", "no", "fallback", "skip"}:+ return "no"+ return "ask"+++def offer_dependency_install(+ *,+ feature: str,+ module_names: list[str],+ fallback: str | None,+ install_mode: str,+) -> None:+ packages = missing_python_packages(module_names)+ if not packages:+ return++ message = f"{feature} uses {', '.join(packages)} if installed"+ if fallback:+ message += f", otherwise {fallback}"+ message += "."+ print(message)++ should_install = False+ if install_mode == "yes":+ should_install = True+ elif install_mode == "ask" and sys.stdin.isatty():+ answer = input("Missing package(s) detected. Do you want to install? y=install, n=fallback: ").strip().lower()+ should_install = answer in {"y", "yes", "install"}+ else:+ if fallback:+ print("Non-interactive mode or install disabled; using fallback.")+ else:+ print("Non-interactive mode or install disabled; installation skipped.")++ if not should_install:+ if fallback:+ print(f"Using fallback: {fallback}.")+ return++ if install_python_packages(packages):+ still_missing = missing_python_packages(module_names)+ if not still_missing:+ print("Package installation complete.")+ return+ print(f"Package installation incomplete; still missing: {', '.join(still_missing)}", file=sys.stderr)+ else:+ print("Package installation failed.", file=sys.stderr)++ if fallback:+ print(f"Using fallback: {fallback}.")+++def prepare_dependencies(ext: str, extraction_mode: str, install_mode: str) -> None:+ if ext == ".pdf" and extraction_mode == "technical":+ offer_dependency_install(+ feature="Technical PDF extraction",+ module_names=["docling"],+ fallback="the PDF text fallback chain",+ install_mode=install_mode,+ )++ if ext == ".pdf" and not shutil.which("pdftotext"):+ offer_dependency_install(+ feature="PDF text extraction",+ module_names=["PyPDF2", "pdfminer"],+ fallback="any installed Python PDF parser; extraction fails if none are available",+ install_mode=install_mode,+ )++ if ext == ".epub":+ offer_dependency_install(+ feature="EPUB extraction",+ module_names=["ebooklib", "bs4"],+ fallback="a stdlib ZIP/HTML parser",+ install_mode=install_mode,+ )++ if ext in HTML_EXTENSIONS:+ offer_dependency_install(+ feature="HTML extraction",+ module_names=["bs4"],+ fallback="a stdlib HTML parser",+ install_mode=install_mode,+ )++ if ext == ".docx":+ offer_dependency_install(+ feature="DOCX extraction",+ module_names=["docx"],+ fallback="a stdlib ZIP/XML parser",+ install_mode=install_mode,+ )++ if ext == ".rtf":+ offer_dependency_install(+ feature="RTF extraction",+ module_names=["striprtf"],+ fallback="a basic regex cleanup fallback",+ install_mode=install_mode,+ )diff --git a/scripts/extractor/parsers/__init__.py b/scripts/extractor/parsers/__init__.pynew file mode 100644index 0000000..a07ec63--- /dev/null+++ b/scripts/extractor/parsers/__init__.py@@ -0,0 +1 @@+# Parsers packagediff --git a/scripts/extractor/parsers/calibre.py b/scripts/extractor/parsers/calibre.pynew file mode 100644index 0000000..14471c1--- /dev/null+++ b/scripts/extractor/parsers/calibre.py@@ -0,0 +1,21 @@+import shutil+import subprocess+from extractor.config import OUTPUT_DIR+++def extract_with_ebook_convert(input_path: str) -> str | None:+ if not shutil.which("ebook-convert"):+ return None+ output_path = OUTPUT_DIR / "ebook-convert-output.txt"+ try:+ result = subprocess.run(+ ["ebook-convert", input_path, str(output_path)],+ capture_output=True, text=True, timeout=300+ )+ if result.returncode == 0 and output_path.exists():+ text = output_path.read_text(encoding="utf-8", errors="replace")+ if text.strip():+ return text+ except Exception:+ pass+ return Nonediff --git a/scripts/extractor/parsers/docx.py b/scripts/extractor/parsers/docx.pynew file mode 100644index 0000000..c60fba6--- /dev/null+++ b/scripts/extractor/parsers/docx.py@@ -0,0 +1,62 @@+import sys+import zipfile+from extractor.config import supported_formats_message+++def extract_docx_with_python_docx(docx_path: str) -> str | None:+ try:+ import docx+ document = docx.Document(docx_path)+ parts = [paragraph.text for paragraph in document.paragraphs if paragraph.text]+ for table in document.tables:+ for row in table.rows:+ cells = [cell.text.strip() for cell in row.cells]+ if any(cells):+ parts.append("\t".join(cells))+ return "\n".join(parts)+ except ImportError:+ return None+ except Exception:+ return None+++def extract_docx_with_zipfile(docx_path: str) -> str | None:+ try:+ import xml.etree.ElementTree as ET++ with zipfile.ZipFile(docx_path) as zf:+ xml_bytes = zf.read("word/document.xml")+ root = ET.fromstring(xml_bytes)+ namespace = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"+ parts: list[str] = []+ for paragraph in root.iter(f"{namespace}p"):+ texts = [node.text for node in paragraph.iter(f"{namespace}t") if node.text]+ if texts:+ parts.append("".join(texts))+ return "\n".join(parts) if parts else None+ except Exception:+ return None+++def extract_docx(docx_path: str) -> tuple[str, str]:+ print("Trying python-docx...", end=" ", flush=True)+ text = extract_docx_with_python_docx(docx_path)+ if text and text.strip():+ print("OK")+ return text, "python-docx"++ print("not available")+ print("Trying stdlib DOCX parser...", end=" ", flush=True)+ text = extract_docx_with_zipfile(docx_path)+ if text and text.strip():+ print("OK")+ return text, "zipfile-docx"++ print("FAILED")+ print(+ "\nERROR: Could not extract text from DOCX.\n"+ "Install python-docx for best results:\n"+ " pip3 install python-docx",+ file=sys.stderr,+ )+ sys.exit(1)diff --git a/scripts/extractor/parsers/epub.py b/scripts/extractor/parsers/epub.pynew file mode 100644index 0000000..a9cc87a--- /dev/null+++ b/scripts/extractor/parsers/epub.py@@ -0,0 +1,66 @@+import re+import zipfile+from extractor.parsers.html import _HTMLTextExtractor+++def extract_with_ebooklib(epub_path: str) -> str | None:+ 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:+ return 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:+ names = zf.namelist()+ # Read OPF spine to get reading order, fall back to sorted xhtml files+ spine_order: list[str] = []+ opf_files = [n for n in names if n.endswith(".opf")]+ if opf_files:+ opf_text = zf.read(opf_files[0]).decode("utf-8", errors="replace")+ spine_order = re.findall(r'href=["\']([^"\']+\.(?:xhtml|html))["\']', opf_text)++ 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 = zf.read(name).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:+ 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_files = [n for n in zf.namelist() if n.endswith(".opf")]+ if not opf_files:+ return 0+ opf_text = zf.read(opf_files[0]).decode("utf-8", errors="replace")+ return len(re.findall(r'<itemref\b', opf_text))+ except Exception:+ return 0diff --git a/scripts/extractor/parsers/html.py b/scripts/extractor/parsers/html.pynew file mode 100644index 0000000..8e38c84--- /dev/null+++ b/scripts/extractor/parsers/html.py@@ -0,0 +1,52 @@+import html+import html.parser+from extractor.parsers.text import read_text_file+++class _HTMLTextExtractor(html.parser.HTMLParser):+ """Minimal HTML → plain text converter using stdlib only."""++ SKIP_TAGS = {"script", "style", "head"}++ def __init__(self):+ super().__init__()+ self._parts: list[str] = []+ self._skip_depth = 0+ self._current_skip: str | None = None++ def handle_starttag(self, tag, attrs):+ if tag in self.SKIP_TAGS:+ self._skip_depth += 1+ if tag in ("p", "br", "h1", "h2", "h3", "h4", "h5", "h6", "li", "div"):+ self._parts.append("\n")++ def handle_endtag(self, tag):+ if tag in self.SKIP_TAGS and self._skip_depth:+ self._skip_depth -= 1++ def handle_data(self, data):+ if not self._skip_depth:+ self._parts.append(data)++ def get_text(self) -> str:+ return html.unescape("".join(self._parts))+++def extract_html_content(raw_html: str) -> str:+ try:+ from bs4 import BeautifulSoup+ soup = BeautifulSoup(raw_html, "html.parser")+ for element in soup(["script", "style", "head"]):+ element.decompose()+ return soup.get_text(separator="\n")+ except ImportError:+ parser = _HTMLTextExtractor()+ parser.feed(raw_html)+ return parser.get_text()+++def extract_html_file(path: str) -> str | None:+ raw = read_text_file(path)+ if raw is None:+ return None+ return extract_html_content(raw)diff --git a/scripts/extractor/parsers/pdf.py b/scripts/extractor/parsers/pdf.pynew file mode 100644index 0000000..a872ede--- /dev/null+++ b/scripts/extractor/parsers/pdf.py@@ -0,0 +1,93 @@+import shutil+import subprocess+import sys+from extractor.parsers.calibre import extract_with_ebook_convert+++def extract_with_pdftotext(pdf_path: str) -> str | None:+ if not shutil.which("pdftotext"):+ return None+ try:+ result = subprocess.run(+ ["pdftotext", "-layout", pdf_path, "-"],+ capture_output=True, text=True, timeout=120+ )+ if result.returncode == 0 and result.stdout.strip():+ return result.stdout+ except Exception:+ pass+ return None+++def extract_with_pypdf2(pdf_path: str) -> str | None:+ try:+ import PyPDF2+ text_parts = []+ with open(pdf_path, "rb") as f:+ reader = PyPDF2.PdfReader(f)+ for page in reader.pages:+ try:+ text_parts.append(page.extract_text() or "")+ except Exception:+ text_parts.append("")+ return "\n".join(text_parts)+ except ImportError:+ return None+ except Exception:+ return None+++def extract_with_pdfminer(pdf_path: str) -> str | None:+ try:+ from pdfminer.high_level import extract_text+ return extract_text(pdf_path)+ except ImportError:+ return None+ except Exception:+ return None+++def extract_with_docling(pdf_path: str) -> str | None:+ """Layout-aware extraction using Docling. Best for technical books with tables and code."""+ try:+ from docling.document_converter import DocumentConverter+ from docling.datamodel.pipeline_options import PdfPipelineOptions+ from docling.datamodel.base_models import InputFormat+ from docling.document_converter import PdfFormatOption++ pipeline_options = PdfPipelineOptions()+ pipeline_options.do_ocr = False+ pipeline_options.do_table_structure = True++ converter = DocumentConverter(+ format_options={+ InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)+ }+ )+ result = converter.convert(pdf_path)+ return result.document.export_to_markdown()+ except ImportError:+ return None+ except Exception:+ return None+++def count_pages(pdf_path: str) -> int:+ # Try pdfinfo first+ if shutil.which("pdfinfo"):+ try:+ result = subprocess.run(+ ["pdfinfo", pdf_path], capture_output=True, text=True, timeout=15+ )+ for line in result.stdout.splitlines():+ if line.startswith("Pages:"):+ return int(line.split(":")[1].strip())+ except Exception:+ pass+ # Fallback: count form-feed chars (pdftotext -layout uses \f between pages)+ try:+ import PyPDF2+ with open(pdf_path, "rb") as f:+ return len(PyPDF2.PdfReader(f).pages)+ except Exception:+ return 0diff --git a/scripts/extractor/parsers/rtf.py b/scripts/extractor/parsers/rtf.pynew file mode 100644index 0000000..6a105d8--- /dev/null+++ b/scripts/extractor/parsers/rtf.py@@ -0,0 +1,32 @@+import html+import re+import sys+from extractor.parsers.text import read_text_file+++def strip_rtf_fallback(raw: str) -> str:+ raw = re.sub(r"\\'[0-9a-fA-F]{2}", " ", raw)+ raw = re.sub(r"\\par[d]?", "\n", raw)+ raw = re.sub(r"\\tab", "\t", raw)+ raw = re.sub(r"\\[a-zA-Z]+-?\d* ?", "", raw)+ raw = raw.replace("{", "").replace("}", "")+ return html.unescape(raw)+++def extract_rtf(rtf_path: str) -> tuple[str, str]:+ raw = read_text_file(rtf_path)+ if raw is None:+ print("ERROR: Could not read RTF file", file=sys.stderr)+ sys.exit(1)++ try:+ from striprtf.striprtf import rtf_to_text+ text = rtf_to_text(raw)+ if text.strip():+ return text, "striprtf"+ except ImportError:+ pass+ except Exception:+ pass++ return strip_rtf_fallback(raw), "rtf-regex"diff --git a/scripts/extractor/parsers/text.py b/scripts/extractor/parsers/text.pynew file mode 100644index 0000000..7304f81--- /dev/null+++ b/scripts/extractor/parsers/text.py@@ -0,0 +1,12 @@+from pathlib import Path+++def read_text_file(path: str) -> str | None:+ for encoding in ("utf-8-sig", "utf-8", "cp1252", "latin-1"):+ try:+ return Path(path).read_text(encoding=encoding)+ except UnicodeDecodeError:+ continue+ except Exception:+ return None+ return Nonediff --git a/scripts/extractor/utils.py b/scripts/extractor/utils.pynew file mode 100644index 0000000..4dfbf4f--- /dev/null+++ b/scripts/extractor/utils.py@@ -0,0 +1,437 @@+import glob+import json+import os+import re+import sys+import shutil+import zipfile+from pathlib import Path++from extractor.config import (+ OUTPUT_DIR,+ OUTPUT_TEXT,+ OUTPUT_META,+ WORDS_PER_TOKEN,+ SUPPORTED_EXTENSIONS,+ TEXT_EXTENSIONS,+ HTML_EXTENSIONS,+ CALIBRE_EBOOK_EXTENSIONS,+ supported_formats_message,+)+from extractor.dependencies import (+ normalize_install_mode,+ prepare_dependencies,+)+from extractor.parsers.text import read_text_file+from extractor.parsers.html import extract_html_file+from extractor.parsers.docx import extract_docx+from extractor.parsers.rtf import extract_rtf+from extractor.parsers.calibre import extract_with_ebook_convert+from extractor.parsers.pdf import (+ extract_with_docling,+ extract_with_pdftotext,+ extract_with_pypdf2,+ extract_with_pdfminer,+ count_pages,+)+from extractor.parsers.epub import (+ extract_with_ebooklib,+ extract_with_zipfile,+ count_epub_chapters,+)+++def estimate_tokens(text: str) -> int:+ return int(len(text.split()) / WORDS_PER_TOKEN)+++def detect_structure(text: str) -> dict:+ """Detect chapter count and table of contents presence."""+ lines = text[:50000].splitlines()++ # Look for chapter headings+ chapter_pattern = re.compile(+ r"^\s*(chapter\s+\d+|CHAPTER\s+\d+|ch\.\s*\d+|\d+\.\s+[A-Z])",+ re.IGNORECASE+ )+ chapters_found = [l.strip() for l in lines if chapter_pattern.match(l)]++ # Look for ToC indicators in the first ~30k chars+ toc_pattern = re.compile(+ r"^\s*(?:table of contents|contents|índice|sumário)\s*$",+ re.IGNORECASE | re.MULTILINE,+ )+ has_toc = bool(toc_pattern.search(text[:30000]))++ return {+ "chapters_detected": len(chapters_found),+ "chapter_headings_sample": chapters_found[:10],+ "has_toc": has_toc,+ }+++def parse_arguments(argv: list[str]) -> tuple[list[str], str, str]:+ """Parse argv into (input_paths, extraction_mode, install_mode)."""+ input_paths = []+ extraction_mode = "text"++ args = argv[1:]+ i = 0+ while i < len(args):+ arg = args[i]+ if arg == "--mode":+ if i + 1 < len(args):+ extraction_mode = args[i+1].lower()+ i += 2+ else:+ i += 1+ elif arg == "--install-missing":+ if i + 1 < len(args) and not args[i+1].startswith("--"):+ i += 2+ else:+ i += 1+ elif arg == "--no-install-missing":+ i += 1+ elif arg.startswith("-"):+ i += 1+ else:+ input_paths.append(arg)+ i += 1++ install_mode = normalize_install_mode(argv)+ if extraction_mode not in ("technical", "text"):+ extraction_mode = "text"++ return input_paths, extraction_mode, install_mode+++def resolve_input_files(paths: list[str]) -> list[Path]:+ """Resolve paths including files, directories, and glob patterns to Path objects."""+ resolved = []+ for path_str in paths:+ # Check if it has glob wildcards+ if any(char in path_str for char in ("*", "?", "[")):+ glob_matches = glob.glob(path_str, recursive=True)+ for match in glob_matches:+ p = Path(match)+ if p.is_file():+ resolved.append(p.resolve())+ else:+ p = Path(path_str)+ if p.is_dir():+ for root, _, files in os.walk(p):+ for file in files:+ file_path = Path(root) / file+ if file_path.suffix.lower() in SUPPORTED_EXTENSIONS:+ resolved.append(file_path.resolve())+ else:+ # Keep even if it doesn't exist so the error check can report it+ resolved.append(p.resolve())++ # Deduplicate while preserving order, then sort by file path for consistency+ seen = set()+ unique_paths = []+ for path in resolved:+ resolved_path = path.resolve() if path.exists() else path+ if resolved_path not in seen:+ seen.add(resolved_path)+ unique_paths.append(resolved_path)++ unique_paths.sort(key=lambda x: str(x).lower())+ return unique_paths+++def extract_single_file(input_path: Path, extraction_mode: str, install_mode: str) -> dict:+ """Extract text and metadata from a single file path."""+ input_str = str(input_path)++ if not input_path.exists():+ print(f"ERROR: File not found: {input_str}", file=sys.stderr)+ sys.exit(1)++ ext = input_path.suffix.lower()+ document_format = ext.lstrip(".")++ # Sniff magic bytes if suffix is not supported+ if ext not in SUPPORTED_EXTENSIONS:+ with open(input_str, "rb") as f:+ header = f.read(8)+ if header[:4] == b"%PDF":+ ext = ".pdf"+ document_format = "pdf"+ elif header[:2] == b"PK":+ try:+ with zipfile.ZipFile(input_str) as zf:+ names = set(zf.namelist())+ if "mimetype" in names and zf.read("mimetype").startswith(b"application/epub"):+ ext = ".epub"+ document_format = "epub"+ elif "word/document.xml" in names:+ ext = ".docx"+ document_format = "docx"+ else:+ print(+ f"ERROR: Unsupported ZIP-based format '{input_path.name}'. Supported: {supported_formats_message()}",+ file=sys.stderr,+ )+ sys.exit(1)+ except (zipfile.BadZipFile, KeyError, OSError):+ print(+ f"ERROR: Unsupported ZIP-based format '{input_path.name}'. Supported: {supported_formats_message()}",+ file=sys.stderr,+ )+ sys.exit(1)+ else:+ print(+ f"ERROR: Unsupported format '{ext or '<none>'}'. Supported: {supported_formats_message()}",+ file=sys.stderr,+ )+ sys.exit(1)++ prepare_dependencies(ext, extraction_mode, install_mode)++ if ext in CALIBRE_EBOOK_EXTENSIONS and not shutil.which("ebook-convert"):+ print(+ "MOBI/AZW/AZW3 extraction requires Calibre's ebook-convert command. "+ "Install Calibre and ensure ebook-convert is on PATH, then rerun this command.",+ file=sys.stderr,+ )+ sys.exit(1)++ text = ""+ method = ""+ pages = 0+ pages_label = "sections"++ if ext == ".epub":+ print(f"Extracting EPUB: {input_str}")+ text, method = extract_with_ebooklib(input_str)+ if not text or not text.strip():+ print("not available")+ print("Trying stdlib zipfile parser...", end=" ", flush=True)+ text = extract_with_zipfile(input_str)+ if text and text.strip():+ print("OK")+ method = "zipfile"+ else:+ print("FAILED")+ print(+ "\nERROR: Could not extract text from EPUB.\n"+ "Install ebooklib + beautifulsoup4 for best results:\n"+ " pip3 install ebooklib beautifulsoup4",+ file=sys.stderr,+ )+ sys.exit(1)+ else:+ method = "ebooklib"+ pages = count_epub_chapters(input_str)+ pages_label = "spine_items"+ elif ext == ".pdf":+ print(f"Extracting PDF: {input_str}")+ if extraction_mode == "technical":+ print("Mode: technical — using Docling (layout-aware)...", end=" ", flush=True)+ text = extract_with_docling(input_str)+ if text:+ method = "docling"+ print("OK")+ else:+ print("not available, falling back to pdftotext")+ extraction_mode = "text"++ if extraction_mode == "text" or not text:+ print("Mode: text — using pdftotext...")+ print("Trying pdftotext...", end=" ", flush=True)+ text = extract_with_pdftotext(input_str)++ if text:+ method = "pdftotext"+ print("OK")+ else:+ print("not available")+ print("Trying PyPDF2...", end=" ", flush=True)+ text = extract_with_pypdf2(input_str)+ if text:+ method = "PyPDF2"+ print("OK")+ else:+ print("not available")+ print("Trying pdfminer.six...", end=" ", flush=True)+ text = extract_with_pdfminer(input_str)+ if text:+ method = "pdfminer"+ print("OK")+ else:+ print("FAILED")+ print(+ "\nERROR: Could not extract text from PDF.\n"+ "Install one of: poppler-utils (pdftotext), PyPDF2, or pdfminer.six\n"+ " sudo apt install poppler-utils\n"+ " pip3 install PyPDF2\n"+ " pip3 install pdfminer.six",+ file=sys.stderr,+ )+ sys.exit(1)++ pages = count_pages(input_str)+ pages_label = "pages"+ elif ext in TEXT_EXTENSIONS:+ print(f"Extracting text document: {input_str}")+ text = read_text_file(input_str)+ if text is None or not text.strip():+ print("ERROR: Could not read text document", file=sys.stderr)+ sys.exit(1)+ method = "plain-text"+ pages = 0+ pages_label = "sections"+ elif ext in HTML_EXTENSIONS:+ print(f"Extracting HTML: {input_str}")+ text = extract_html_file(input_str)+ if text is None or not text.strip():+ print("ERROR: Could not extract text from HTML", file=sys.stderr)+ sys.exit(1)+ method = "html-parser"+ pages = 0+ pages_label = "sections"+ elif ext == ".docx":+ print(f"Extracting DOCX: {input_str}")+ text, method = extract_docx(input_str)+ pages = 0+ pages_label = "sections"+ elif ext == ".rtf":+ print(f"Extracting RTF: {input_str}")+ text, method = extract_rtf(input_str)+ pages = 0+ pages_label = "sections"+ elif ext in CALIBRE_EBOOK_EXTENSIONS:+ print(f"Extracting ebook with Calibre: {input_str}")+ text = extract_with_ebook_convert(input_str)+ if text is None or not text.strip():+ print(+ f"ERROR: Could not extract text from {ext}. Install Calibre and ensure ebook-convert is on PATH.",+ file=sys.stderr,+ )+ sys.exit(1)+ method = "ebook-convert"+ pages = 0+ pages_label = "sections"++ tokens = estimate_tokens(text)+ structure = detect_structure(text)+ file_size_mb = os.path.getsize(input_str) / (1024 * 1024)++ return {+ "source_file": str(input_path.resolve()),+ "filename": input_path.name,+ "format": document_format,+ "extraction_method": method,+ "file_size_mb": round(file_size_mb, 2),+ pages_label: pages,+ "pages_label": pages_label,+ "pages": pages,+ "chars": len(text),+ "words": len(text.split()),+ "estimated_tokens": tokens,+ "text": text,+ **structure,+ }+++def main():+ if len(sys.argv) < 2:+ print("Usage: extract.py <path-to-document-folder-or-glob>... [--mode technical|text] [--install-missing ask|yes|no]", file=sys.stderr)+ print(f"Supported formats: {supported_formats_message()}", file=sys.stderr)+ sys.exit(1)++ raw_input_paths, extraction_mode, install_mode = parse_arguments(sys.argv)++ if not raw_input_paths:+ print("ERROR: No input document, folder, or glob pattern specified.", file=sys.stderr)+ sys.exit(1)++ input_files = resolve_input_files(raw_input_paths)++ if not input_files:+ print(f"ERROR: No supported files found matching: {', '.join(raw_input_paths)}", file=sys.stderr)+ sys.exit(1)++ OUTPUT_DIR.mkdir(parents=True, exist_ok=True)++ extracted_sources = []+ combined_texts = []++ for file_path in input_files:+ res = extract_single_file(file_path, extraction_mode, install_mode)+ extracted_sources.append(res)++ # Format the text with a clear boundary+ separator = f"\n\n{'=' * 80}\nSOURCE: {res['filename']} (Path: {res['source_file']})\n{'=' * 80}\n\n"+ combined_texts.append(separator + res["text"])++ # Combine texts+ consolidated_text = "".join(combined_texts).strip()++ # Write combined text+ OUTPUT_TEXT.write_text(consolidated_text, encoding="utf-8")++ # Consolidate metadata+ total_file_size_mb = sum(src["file_size_mb"] for src in extracted_sources)+ total_pages = sum(src["pages"] for src in extracted_sources)+ total_chars = len(consolidated_text)+ total_words = len(consolidated_text.split())+ total_tokens = estimate_tokens(consolidated_text)++ # Detect structure on consolidated text+ consolidated_structure = detect_structure(consolidated_text)++ metadata = {+ "source_file": "Consolidated from multiple sources" if len(extracted_sources) > 1 else extracted_sources[0]["source_file"],+ "filename": "multi-source" if len(extracted_sources) > 1 else extracted_sources[0]["filename"],+ "format": "mixed" if len(extracted_sources) > 1 else extracted_sources[0]["format"],+ "extraction_method": "multi-method" if len(extracted_sources) > 1 else extracted_sources[0]["extraction_method"],+ "extraction_mode": extraction_mode,+ "file_size_mb": round(total_file_size_mb, 2),+ "pages": total_pages,+ "chars": total_chars,+ "words": total_words,+ "estimated_tokens": total_tokens,+ "estimated_tokens_human": f"~{total_tokens // 1000}K",+ "output_text": str(OUTPUT_TEXT),+ "total_sources": len(extracted_sources),+ "sources": [+ {+ "source_file": src["source_file"],+ "filename": src["filename"],+ "format": src["format"],+ "extraction_method": src["extraction_method"],+ "file_size_mb": src["file_size_mb"],+ "pages": src["pages"],+ "pages_label": src["pages_label"],+ "chars": src["chars"],+ "words": src["words"],+ "estimated_tokens": src["estimated_tokens"],+ "chapters_detected": src["chapters_detected"],+ "has_toc": src["has_toc"]+ }+ for src in extracted_sources+ ],+ **consolidated_structure,+ }++ OUTPUT_META.write_text(json.dumps(metadata, indent=2, ensure_ascii=False))++ page_line = f" Total Pages: {total_pages}"+ print("\nExtraction complete:")+ print(f" Sources : {len(extracted_sources)} processed")+ print(f" Size : {total_file_size_mb:.2f} MB")+ print(page_line)+ print(f" Words : {total_words:,}")+ print(f" Tokens : ~{total_tokens // 1000}K")+ print(f" Chapters: {consolidated_structure['chapters_detected']} detected overall")+ print(f" ToC : {'yes' if consolidated_structure['has_toc'] else 'not detected'}")+ if not consolidated_structure["has_toc"]:+ print(+ " WARN : No table of contents detected — chapter mapping in Step 3 "+ "will rely on heading scan only, which may miss or duplicate sections."+ )+ print(f"\n Text -> {OUTPUT_TEXT}")+ print(f" Meta -> {OUTPUT_META}")