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/rtf.py
View on GitHubimport html
import re
import sys
from book_to_skill.parsers.text import read_text_file
from book_to_skill.exceptions import ExtractionError
# RTF unicode escape: \uN (signed decimal) followed by its fallback char(s).
# Decode the code point and drop the standard single fallback — a \'XX hex byte
# or a literal "?". Assumes the default \uc1 (one fallback char); \ucN directives
# and multi-char/group fallbacks are not parsed (best-effort fallback only).
_RTF_UNICODE = re.compile(r"\\u(-?\d+)[ ]?(?:\\'[0-9a-fA-F]{2}|\?)?")
def _rtf_unicode_repl(match: re.Match) -> str:
cp = int(match.group(1)) % 0x10000 # RTF uses signed 16-bit; wrap negatives
if cp == 0 or 0xD800 <= cp <= 0xDFFF: # NUL and lone surrogates: unwanted in text
return ""
return chr(cp)
# RTF groups whose contents are metadata or formatting tables rather than
# document text. Stripping only the control words inside them (what the cleanup
# below does) leaves the residue behind: font and style *names*, the generator
# string, and the \info title/author all end up in the extracted book text.
_SKIP_DESTINATIONS = frozenset({
"fonttbl", # {\fonttbl{\f0\fnil Calibri;}} -> "Calibri;"
"colortbl", # {\colortbl;\red255...;} -> ";;;"
"stylesheet", # {\stylesheet{\s0 Normal;}} -> "Normal;"
"info", # {\info{\title X}{\author Y}} -> "XY"
"listtable", "listoverridetable", "revtbl", "rsidtbl",
"latentstyles", "datastore", "themedata", "colorschememapping",
"filetbl", "xmlnstbl", "pgptbl", "protusertbl", "userprops",
"docvar",
"pict", "objdata", # binary image / OLE payloads as hex text
"bkmkstart", "bkmkend",
})
# The first control word of a group, allowing the "\*" ignorable-destination
# prefix: "{\fonttbl", "{\*\generator", "{\*\bkmkstart".
_GROUP_DESTINATION = re.compile(r"\\\*?\\?([a-zA-Z]+)")
def _strip_destination_groups(raw: str) -> str:
"""Remove RTF groups that hold no document text.
Tracks brace depth so a whole group is dropped, not just its control words.
Per the RTF spec a reader that does not understand a ``\\*`` destination must
skip the entire group, which also handles ``\\*\\generator`` and any vendor
extension without naming it. Escaped ``\\{`` / ``\\}`` / ``\\\\`` are not
treated as delimiters.
A useful side effect: for a field, ``{\\field{\\*\\fldinst HYPERLINK ...}
{\\fldrslt visible text}}`` keeps the result and drops the instruction.
"""
out: list[str] = []
index = 0
depth = 0
skip_at_depth = 0 # non-zero while inside a skipped group
length = len(raw)
while index < length:
char = raw[index]
# Escaped literal: "\{", "\}", "\\" are text, never group delimiters.
if char == "\\" and index + 1 < length and raw[index + 1] in "{}\\":
if not skip_at_depth:
out.append(raw[index:index + 2])
index += 2
continue
if char == "{":
depth += 1
if not skip_at_depth:
match = _GROUP_DESTINATION.match(raw, index + 1)
ignorable = raw.startswith("{\\*", index)
if ignorable or (match and match.group(1) in _SKIP_DESTINATIONS):
skip_at_depth = depth
else:
out.append(char)
index += 1
continue
if char == "}":
if skip_at_depth and depth == skip_at_depth:
skip_at_depth = 0
elif not skip_at_depth:
out.append(char)
depth -= 1
index += 1
continue
if not skip_at_depth:
out.append(char)
index += 1
if skip_at_depth:
# Unterminated destination group: the file is malformed and everything
# after the unclosed brace was just dropped, which could be the whole
# book. Leaking some metadata residue is the lesser evil, so fall back
# to the unscanned text rather than returning a truncated document.
return raw
return "".join(out)
def strip_rtf_fallback(raw: str) -> str:
# Drop metadata/table groups wholesale first, so their contents never reach
# the control-word cleanup that would otherwise strip the markup and leave
# the names behind as if they were prose.
raw = _strip_destination_groups(raw)
raw = _RTF_UNICODE.sub(_rtf_unicode_repl, raw) # decode \uN escapes first
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)
# Park the three escaped literals ("\\", "\{", "\}") on placeholders before
# the sweeps below, which would otherwise strip the backslash as a control
# symbol and then delete the brace along with the real group delimiters —
# leaving a stray "\" where the book said "{a, b}". Longest escape first.
raw = (
raw.replace("\\\\", "\x01")
.replace("\\{", "\x02")
.replace("\\}", "\x03")
)
raw = re.sub(r"\\[a-zA-Z]+-?\d* ?", "", raw)
raw = raw.replace("{", "").replace("}", "")
raw = raw.replace("\x01", "\\").replace("\x02", "{").replace("\x03", "}")
return html.unescape(raw)
def extract_rtf(rtf_path: str) -> tuple[str, str]:
raw = read_text_file(rtf_path)
if raw is None:
raise ExtractionError(f"Could not read RTF file: {rtf_path}")
try:
from striprtf.striprtf import rtf_to_text
text = rtf_to_text(raw)
if text.strip():
return text, "striprtf"
except ImportError:
pass
except Exception as e:
print(f" [warn] extract_rtf/striprtf failed: {type(e).__name__}: {e}", file=sys.stderr)
return strip_rtf_fallback(raw), "rtf-regex"
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 |