agentleFS
abduallahJamalabduallahJamal/qalam-skills/skills/extract-docs/SKILL.md

extract-docs

Skill1 starsChanged 15 days agoMITView on GitHub

Convert PDF, DOCX, EPUB, HTML, and scanned pages into clean Markdown that survives translation — heading structure, tables, code, figures, footnotes, and Arabic extraction pitfalls. Use before translating a book or manual, or when asked to "get the text out of this PDF".

  • Installs packages

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 2 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Ask before installing anything that downloads models (pandoc-incompatible pip installs, Marker, MinerU).

    line 41
  2. Distinctive:

    Always verify against the page image for at least: the first page, one page with numbers, one page with Latin text, one page with a table.

    line 115
  3. Very specific:

    Keep images: extract them to media/, keep relative paths, and add alt text placeholders so a translator can translate them.

    line 147
  4. Distinctive:

    Keep the original filename and record the source in a small frontmatter block:

    line 149

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Extract documents

Translation quality is capped by extraction quality. A PDF flattened into one paragraph per page produces a translation that is faithful and unreadable; code turned into prose produces a translation that is wrong.

Governing rule: extraction is a verification task, not a conversion task. Anyone can run pandoc; the value is in checking what came out.

When to use this

  • translate-docs receives a PDF, DOCX, EPUB, or HTML source.
  • The user asks to extract text, convert a book/document to Markdown, or "clean up" an OCR dump.
  • Arabic PDFs: bidi extraction produces reversed or disconnected text and needs the specific checks below.

Choose the tool by source

SourceToolWhy
DOCX, ODT, EPUB, HTMLpandoc -t gfmPreserves structure; fast; no models
DOCX with complex tablespandoc then manual repairTable merges rarely survive
Text-layer PDFpdftotext -layout or markitdownLayout mode keeps columns apart
Text-layer PDF with figures/mathMinerU or MarkerLayout-aware; heavy install, ask first
Scanned PDFOCR (tesseract ara+eng, or a layout model)Needs Arabic language data installed
Legacy .doc/.pptLibreOffice convert → then pandocsoffice --headless --convert-to docx
Web pagemarkitdown, or paste into a Markdown converterKeep the canonical URL in frontmatter

Ask before installing anything that downloads models (pandoc-incompatible pip installs, Marker, MinerU). Give the user the command and the download size.

Procedure

1. Establish the reference

Extraction bugs are invisible unless you know what the source contains. Before converting, record from the original:

  • total pages or chapter count,
  • the table of contents with page numbers,
  • number of figures, tables, and code listings,
  • anything unusual: footnotes, marginalia, multi-column layout, RTL body text, mixed-script paragraphs.

You will check against these numbers at the end. Without them you cannot tell a clean extraction from a lossy one.

2. Convert

# DOCX / EPUB / HTML
pandoc input.docx -t gfm --wrap=none --extract-media=./media -o extracted.md

# Text-layer PDF, layout preserved
pdftotext -layout -enc UTF-8 input.pdf - > extracted.txt
# then wrap the text into Markdown, or:
markitdown input.pdf -o extracted.md

# Layout-aware PDF (only if installed and the user agreed)
marker_single input.pdf --output_dir out

For Arabic text-layer PDFs, test both pdftotext and markitdown on one page and compare: one of them will render logical order correctly and the other will not.

3. Repair structure

Extraction output needs three kinds of repair. Do them in this order.

Headings. Decide the level scheme and apply it. A PDF gives you bold lines, not headings; a DOCX usually gives real heading styles that pandoc converts correctly.

# Book title
## Part
### Chapter
#### Section

Paragraphs. OCR and pdftotext break lines mid-sentence. Join lines that end without terminal punctuation and continue in the same paragraph; keep hard breaks only where the source has them (poetry, code, addresses).

Tables. A table that lost its header row is worse than no table: the translator cannot tell which cell is the label. Rebuild from the source screenshot or page image when the extracted text is ambiguous.

4. Arabic-specific extraction problems

These are the ones that silently produce garbage:

SymptomCauseFix
Letters disconnected: "م ر ح ب ا"Presentation forms or missing shapingCheck for U+FB50–U+FEFF; convert with NFKC; if the PDF has no text layer, OCR with ara data
Text in visual order (reversed)PDF stores glyph positions, not logical orderRe-extract with a tool that reorders bidi (pdftotext -layout usually does; some OCR does not)
Numbers reversed (123 → 321)Bidi run splitIsolate numeric runs; verify against the page image
Latin words inside Arabic reversedSameSame
Broken words at line endsHyphenation or kashidaRejoin; never hyphens in Arabic
Diacritics detachedPresentation-form extractionNFKC normalisation, then spot-check
Footnote markers movedText-layer orderingMove markers back to their anchors

Always verify against the page image for at least: the first page, one page with numbers, one page with Latin text, one page with a table.

5. Verify the extraction

python -m qalam lang extracted.md          # script distribution, direction
python -m qalam chunk extracted.md --out-dir /tmp/check --target-chars 6000

Compare with the reference you recorded:

  • Chapter count matches the table of contents
  • Heading count and levels are plausible
  • Figure count matches; every image has an alt text placeholder
  • Table count matches
  • No page numbers left inline as stray digits
  • No running headers/footers repeated per page
  • No hyphenation artifacts (- at line end followed by lowercase)
  • First and last paragraph of each chapter are complete sentences
  • Arabic: letters joined, logical order, numbers correct
  • Code blocks still look like code

6. Clean up

  • Strip running headers, footers, and page numbers (they become noise in a translation and inflate the word count).
  • Normalise whitespace: one space after punctuation, no trailing spaces, single blank line between paragraphs.
  • Convert smart quotes consistently (“ ” ‘ ’ → " ', or keep typographic quotes throughout — pick one).
  • Replace ligatures and presentation forms (NFKC).
  • Keep images: extract them to media/, keep relative paths, and add alt text placeholders so a translator can translate them.
  • Keep the original filename and record the source in a small frontmatter block:
---
source_file: book.pdf
source_pages: 412
extracted_by: pdftotext -layout
extracted_on: 2026-11-05
known_issues: "figures 12 and 19 lost their captions in the source"
---

7. Report

Give: tool used, page/chapter counts before and after, structures repaired, anything lost, and the exact list of pages a human should eyeball. Then hand off to translate-docs.

What "clean" means for translation

A translation-ready Markdown file has:

  • headings that reflect the real hierarchy,
  • paragraphs that are single logical paragraphs,
  • tables with headers,
  • code in fences, untouched,
  • no page furniture,
  • no OCR noise,
  • figures with alt text,
  • a stable chunk size that does not cut a table or a code block.

Failure modes

SymptomCauseFix
Translation is fluent but repeats itselfExtracted text contains duplicated columnsRe-extract without -layout, or with column detection
Whole sections missingPDF pages are imagesOCR those pages
Arabic reads backwards in the editorEditor lacks RTL/bidi supportVerify in a bidi-aware viewer before "fixing" the text
Code translated into proseExtraction dropped fencesRe-fence every code block before translating
Table cells merged across rowsDOCX merged cellsRebuild that table manually
Chapter headings became bold paragraphsNo heading styles in sourceApply the heading scheme manually; do not translate first

References

  • references/extraction-tools.md — commands, flags, and trade-offs.
  • references/pdf-arabic.md — bidi and shaping repair in detail.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.