@effortlessmotion/pdf-to-md 0.1.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/cjs/analyze.d.cts +3 -1
- package/dist/cjs/analyze.d.ts +3 -1
- package/dist/cjs/analyze.js +38 -100
- package/dist/cjs/analyze.js.map +1 -1
- package/dist/cjs/audit.d.cts +6 -5
- package/dist/cjs/audit.d.ts +6 -5
- package/dist/cjs/audit.js +368 -60
- package/dist/cjs/audit.js.map +1 -1
- package/dist/cjs/cli/extract.js +13 -48
- package/dist/cjs/cli/extract.js.map +1 -1
- package/dist/cjs/collect.js +91 -35
- package/dist/cjs/collect.js.map +1 -1
- package/dist/cjs/errors.d.cts +2 -0
- package/dist/cjs/errors.d.ts +2 -0
- package/dist/cjs/errors.js.map +1 -1
- package/dist/cjs/extract.d.cts +20 -9
- package/dist/cjs/extract.d.ts +20 -9
- package/dist/cjs/extract.js +333 -106
- package/dist/cjs/extract.js.map +1 -1
- package/dist/cjs/lines.js +21 -10
- package/dist/cjs/lines.js.map +1 -1
- package/dist/cjs/markdown.js +140 -54
- package/dist/cjs/markdown.js.map +1 -1
- package/dist/cjs/page-marker.d.cts +1 -1
- package/dist/cjs/page-marker.d.ts +1 -1
- package/dist/cjs/page-marker.js +39 -8
- package/dist/cjs/page-marker.js.map +1 -1
- package/dist/cjs/render-analysis.d.cts +4 -2
- package/dist/cjs/render-analysis.d.ts +4 -2
- package/dist/cjs/render-analysis.js +5 -3
- package/dist/cjs/render-analysis.js.map +1 -1
- package/dist/cjs/report.d.cts +2 -2
- package/dist/cjs/report.d.ts +2 -2
- package/dist/cjs/report.js +27 -8
- package/dist/cjs/report.js.map +1 -1
- package/dist/cjs/text-analysis.d.cts +12 -0
- package/dist/cjs/text-analysis.d.ts +12 -0
- package/dist/cjs/text-analysis.js +175 -0
- package/dist/cjs/text-analysis.js.map +1 -0
- package/dist/cjs/types.d.cts +40 -3
- package/dist/cjs/types.d.ts +40 -3
- package/dist/cjs/types.js +2 -3
- package/dist/cjs/types.js.map +1 -1
- package/dist/esm/analyze.d.ts +3 -1
- package/dist/esm/analyze.js +18 -89
- package/dist/esm/analyze.js.map +1 -1
- package/dist/esm/audit.d.ts +6 -5
- package/dist/esm/audit.js +236 -29
- package/dist/esm/audit.js.map +1 -1
- package/dist/esm/cli/extract.js +10 -23
- package/dist/esm/cli/extract.js.map +1 -1
- package/dist/esm/collect.js +64 -15
- package/dist/esm/collect.js.map +1 -1
- package/dist/esm/errors.d.ts +2 -0
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/extract.d.ts +20 -9
- package/dist/esm/extract.js +167 -76
- package/dist/esm/extract.js.map +1 -1
- package/dist/esm/lines.js +14 -10
- package/dist/esm/lines.js.map +1 -1
- package/dist/esm/markdown.js +110 -27
- package/dist/esm/markdown.js.map +1 -1
- package/dist/esm/page-marker.d.ts +1 -1
- package/dist/esm/page-marker.js +15 -9
- package/dist/esm/page-marker.js.map +1 -1
- package/dist/esm/render-analysis.d.ts +4 -2
- package/dist/esm/render-analysis.js +9 -5
- package/dist/esm/render-analysis.js.map +1 -1
- package/dist/esm/report.d.ts +2 -2
- package/dist/esm/report.js +12 -7
- package/dist/esm/report.js.map +1 -1
- package/dist/esm/text-analysis.d.ts +12 -0
- package/dist/esm/text-analysis.js +81 -0
- package/dist/esm/text-analysis.js.map +1 -0
- package/dist/esm/types.d.ts +40 -3
- package/dist/esm/types.js +4 -4
- package/dist/esm/types.js.map +1 -1
- package/package.json +1 -1
- package/skills/pdf-to-md/SKILL.md +39 -66
package/dist/cjs/audit.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/audit.ts"],"sourcesContent":["// audit.ts — the severity-1 audit instrument (README's \"Design contract\"),\n// shipped as part of the product, not just a path into the source tree.\n//\n// Every recognized word (a --debug-words dump, or an Analysis page's\n// `words`) must appear in its page's markdown — normalized, compared as a per-page multiset, with substring repair for hyphen joins.\n//\n// NOTE: no producer guarantees JSON key order in a --debug-words dump —\n// parse, never grep.\nimport { readFileSync } from 'node:fs';\nimport { bodyByPage } from './page-marker.ts';\n\nfunction norm(s: string): string {\n return s\n .normalize('NFKD')\n .replace(/\\p{M}/gu, '')\n .toLowerCase()\n .replace(/[^a-z0-9]/g, '');\n}\n\nfunction counter(items: Iterable<string>): Map<string, number> {\n const m = new Map<string, number>();\n for (const item of items) m.set(item, (m.get(item) ?? 0) + 1);\n return m;\n}\n\n/** auditWords()'s result — the severity-1 no-silent-loss check. */\nexport interface AuditResult {\n /** JSON lines successfully parsed from the word dump */\n parsed: number;\n /** lines in the dump that were not parseable JSON, skipped rather than\n * failing the whole audit (same policy `--words-json` itself uses) */\n nonJsonSkipped: number;\n /** distinct page numbers present in the dump */\n pages: number;\n /** total recognized words across all pages in the dump */\n words: number;\n /** words present in the dump but not found verbatim on their page, BEFORE\n * hyphen-join/split repair is applied; always `>= missing` */\n strictDeficits: number;\n /** words genuinely absent from the markdown after repair-checking; `0` is\n * the only passing value (`pdf-to-md audit` exits 1 otherwise) */\n missing: number;\n /** per-page detail for every page with at least one missing word (capped\n * sample of the missing tokens per page) */\n perPage: { page: number; missing: string[] }[];\n /** the one-line `parsed=… pages=… words=… strict_deficits=… MISSING=…`\n * summary, also the last entry of `lines` */\n summaryLine: string;\n /** every line `pdf-to-md audit` prints, in order: one per page with\n * missing words, then `summaryLine` */\n lines: string[];\n}\n\n/**\n * Audit a --debug-words dump against a markdown file: every recognized word\n * must land somewhere in its page's markdown. `result.missing === 0` is the pass/fail signal.\n *\n * @param wordsPath path to a --debug-words JSON-lines dump (`{page, text, ...}` per line)\n * @param mdPath path to the markdown file to check the dump against\n */\nexport function auditWords(wordsPath: string, mdPath: string): AuditResult {\n const rec = new Map<number, string[]>();\n let parsed = 0;\n let skipped = 0;\n\n const wordsRaw = readFileSync(wordsPath, 'utf8');\n // A trailing \"\\n\" at EOF must not yield a spurious final empty line; a\n // genuine blank line mid-file does count, as skipped.\n const rawLines = wordsRaw.split('\\n');\n if (rawLines.length > 0 && rawLines[rawLines.length - 1] === '') rawLines.pop();\n for (let line of rawLines) {\n line = line.trim();\n if (!line.startsWith('{')) {\n skipped++;\n continue;\n }\n let o: { page: number; text: string };\n try {\n o = JSON.parse(line);\n } catch {\n skipped++;\n continue;\n }\n parsed++;\n const t = norm(o.text);\n if (t) {\n const arr = rec.get(o.page);\n if (arr) arr.push(t);\n else rec.set(o.page, [t]);\n }\n }\n\n const md = readFileSync(mdPath, 'utf8');\n const out = bodyByPage(md);\n\n const lines: string[] = [];\n let total = 0;\n let strict = 0;\n const perPage: { page: number; missing: string[] }[] = [];\n const pages = [...rec.keys()].sort((a, b) => a - b);\n\n for (const page of pages) {\n const toks = (out.get(page)?.match(/\\S+/g) ?? []).map(norm).filter((t) => t.length > 0);\n const ms = counter(toks);\n const joined = toks.join('');\n const missing: string[] = [];\n for (const [tok, n] of counter(rec.get(page) ?? [])) {\n const d = n - (ms.get(tok) ?? 0);\n if (d <= 0) continue;\n strict += d;\n if (joined.includes(tok)) continue; // hyphen-join / split repair\n for (let i = 0; i < d; i++) missing.push(tok);\n }\n if (missing.length > 0) {\n total += missing.length;\n perPage.push({ page, missing });\n lines.push(`p${page}: MISSING ${missing.length}: ${pyRepr(missing.slice(0, 12))}`);\n }\n }\n\n const words = pages.reduce((sum, p) => sum + (rec.get(p)?.length ?? 0), 0);\n const summaryLine = `parsed=${parsed} non-json-skipped=${skipped} pages=${rec.size} words=${words} strict_deficits=${strict} MISSING=${total}`;\n lines.push(summaryLine);\n\n return {\n parsed,\n nonJsonSkipped: skipped,\n pages: rec.size,\n words,\n strictDeficits: strict,\n missing: total,\n perPage,\n summaryLine,\n lines,\n };\n}\n\n// Renders a string list as ['a', 'b'] (single-quoted) to match frozen baseline comparisons byte-for-byte.\nfunction pyRepr(items: string[]): string {\n return `[${items.map((s) => `'${s.replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\")}'`).join(', ')}]`;\n}\n"],"names":["auditWords","norm","s","normalize","replace","toLowerCase","counter","items","m","Map","item","set","get","wordsPath","mdPath","rec","parsed","skipped","wordsRaw","readFileSync","rawLines","split","length","pop","line","trim","startsWith","o","JSON","parse","t","text","arr","page","push","md","out","bodyByPage","lines","total","strict","perPage","pages","keys","sort","a","b","toks","match","map","filter","ms","joined","join","missing","tok","n","d","includes","i","pyRepr","slice","words","reduce","sum","p","summaryLine","size","nonJsonSkipped","strictDeficits"],"mappings":"AAAA,2EAA2E;AAC3E,wEAAwE;AACxE,EAAE;AACF,qEAAqE;AACrE,qIAAqI;AACrI,EAAE;AACF,wEAAwE;AACxE,qBAAqB;;;;;+BAqDLA;;;eAAAA;;;sBApDa;4BACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3B,SAASC,KAAKC,CAAS;IACrB,OAAOA,EACJC,SAAS,CAAC,QACVC,OAAO,CAAC,27JAAW,IACnBC,WAAW,GACXD,OAAO,CAAC,cAAc;AAC3B;AAEA,SAASE,QAAQC,KAAuB;QAECC;IADvC,IAAMA,IAAI,IAAIC;QACT,kCAAA,2BAAA;;QAAL,QAAK,YAAcF,0BAAd,SAAA,6BAAA,QAAA,yBAAA;YAAA,IAAMG,OAAN;YAAqBF,EAAEG,GAAG,CAACD,MAAM,EAACF,SAAAA,EAAEI,GAAG,CAACF,mBAANF,oBAAAA,SAAe,KAAK;;;QAAtD;QAAA;;;iBAAA,6BAAA;gBAAA;;;gBAAA;sBAAA;;;;IACL,OAAOA;AACT;AAqCO,SAASR,WAAWa,SAAiB,EAAEC,MAAc;IAC1D,IAAMC,MAAM,IAAIN;IAChB,IAAIO,SAAS;IACb,IAAIC,UAAU;IAEd,IAAMC,WAAWC,IAAAA,oBAAY,EAACN,WAAW;IACzC,uEAAuE;IACvE,sDAAsD;IACtD,IAAMO,WAAWF,SAASG,KAAK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AAO,sBAAqCD,OAAjBE,SAAQ,WAA2B6C,OAAlB/C,IAAIoD,IAAI,EAAC,WAAkC3B,OAAzBsB,OAAM,qBAAqCvB,OAAlBC,QAAO,aAAiB,OAAND;IACvID,MAAMJ,IAAI,CAACgC;IAEX,OAAO;QACLlD,QAAAA;QACAoD,gBAAgBnD;QAChByB,OAAO3B,IAAIoD,IAAI;QACfL,OAAAA;QACAO,gBAAgB7B;QAChBc,SAASf;QACTE,SAAAA;QACAyB,aAAAA;QACA5B,OAAAA;IACF;AACF;AAEA,0GAA0G;AAC1G,SAASsB,OAAOrD,KAAe;IAC7B,OAAO,AAAC,IAAqF,OAAlFA,MAAM0C,GAAG,CAAC,SAAC/C;eAAM,AAAC,IAAiD,OAA9CA,EAAEE,OAAO,CAAC,OAAO,QAAQA,OAAO,CAAC,MAAM,QAAO;OAAIiD,IAAI,CAAC,OAAM;AAC/F"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/audit.ts"],"sourcesContent":["// audit.ts — the severity-1 audit instrument (README's \"Design contract\"),\n// shipped as part of the product, not just a path into the source tree.\n//\n// Every recognized word (a --debug-words dump, or an Analysis page's\n// `words`) must appear in its page's Markdown. Matching consumes evidence so\n// a single output occurrence cannot satisfy repeated source occurrences.\n//\n// NOTE: no producer guarantees JSON key order in a --debug-words dump —\n// parse, never grep.\nimport { readFileSync } from 'node:fs';\nimport { PdfToMdError } from './errors.ts';\nimport { foldTypographic } from './lines.ts';\nimport { bodyByPage } from './page-marker.ts';\n\nconst WORD_RE = /[\\p{L}\\p{N}][\\p{L}\\p{N}\\p{M}]*/gu;\nconst HYPHEN_RE = /[\\p{Pd}\\u2212]\\s*$/u;\nconst LINE_HYPHEN_RE = /^[\\p{Pd}\\u2212][ \\t]*\\r?\\n[ \\t]*$/u;\nconst INLINE_HYPHEN_RE = /^[\\p{Pd}\\u2212][ \\t]*$/u;\n\ninterface Token {\n text: string;\n start: number;\n end: number;\n gapAfter: string;\n}\n\ninterface SourceRun {\n page: number;\n tokens: number[];\n terminalHyphen: boolean;\n hyphenOnly: boolean;\n leadingWhitespace: boolean;\n trailingWhitespace: boolean;\n box?: Box;\n}\n\ninterface PageEvidence {\n tokens: string[];\n sourcePairs: SourcePair[];\n}\n\ninterface SourcePair {\n first: number;\n second: number;\n explicitHyphen: boolean;\n}\n\ninterface Box {\n x: number;\n y: number;\n w: number;\n h: number;\n}\n\ninterface AuditRecord {\n page: number;\n text: string;\n x?: number;\n y?: number;\n w?: number;\n h?: number;\n}\n\nfunction tokens(text: string): Token[] {\n const folded = foldTypographic(text);\n WORD_RE.lastIndex = 0;\n const matches = [...folded.matchAll(WORD_RE)];\n return matches.map((match, index) => {\n const start = match.index as number;\n const end = start + match[0].length;\n const next = matches[index + 1];\n return {\n text: match[0].normalize('NFC').toLowerCase(),\n start,\n end,\n gapAfter: folded.slice(end, next?.index ?? folded.length),\n };\n });\n}\n\nfunction markdownText(md: string): string {\n return md\n .replace(/<!--[\\s\\S]*?-->/gu, ' ')\n .replace(/!\\[[^\\]]*\\]\\([^)]*\\)/gu, ' ')\n .replace(/!\\[[^\\]]*\\]\\[[^\\]]*\\]/gu, ' ')\n .replace(/<img\\b[^>]*>/giu, ' ')\n .replace(/<a\\b[^>]*>/giu, ' ')\n .replace(/<\\/a>/giu, ' ')\n .replace(/^\\s*\\[[^\\]]+\\]:[^\\n]*$/gmu, ' ')\n .replace(/^>\\s*\\[(?:review:[^\\]]*|floats)\\]\\s*/gmu, '')\n .replace(/\\[([^\\]]*)\\]\\([^)]*\\)/gu, '$1')\n .replace(/\\[([^\\]]*)\\]\\[[^\\]]*\\]/gu, '$1');\n}\n\nfunction counter(items: Iterable<string>): Map<string, number> {\n const m = new Map<string, number>();\n for (const item of items) m.set(item, (m.get(item) ?? 0) + 1);\n return m;\n}\n\n/** auditWords()'s result — the severity-1 no-silent-loss check. */\nexport interface AuditResult {\n /** JSON lines successfully parsed from the word dump */\n parsed: number;\n /** lines in the dump that were not parseable JSON; auditWords rejects\n * evidence with any such line */\n nonJsonSkipped: number;\n /** distinct page numbers present in the dump */\n pages: number;\n /** total recognized words across all pages in the dump */\n words: number;\n /** words present in the dump but not found verbatim on their page, BEFORE\n * hyphen-join/split repair is applied; always `>= missing` */\n strictDeficits: number;\n /** words genuinely absent from the markdown after repair-checking; `0` is\n * the only passing value (`pdf-to-md audit` exits nonzero otherwise) */\n missing: number;\n /** per-page detail for every page with at least one missing word (capped\n * sample of the missing tokens per page) */\n perPage: { page: number; missing: string[] }[];\n /** the one-line `parsed=… pages=… words=… strict_deficits=… MISSING=…`\n * summary, also the last entry of `lines` */\n summaryLine: string;\n /** every line `pdf-to-md audit` prints, in order: one per page with\n * missing words, then `summaryLine` */\n lines: string[];\n}\n\n/**\n * Audit a --debug-words dump against a Markdown file. Every recognized word\n * must land on its page. Invalid evidence or absent page provenance throws a\n * PdfToMdError with code `AUDIT_INPUT`.\n *\n * @param wordsPath path to a --debug-words JSON-lines dump (`{page, text, ...}` per line)\n * @param mdPath path to the markdown file to check the dump against\n */\nexport function auditWords(wordsPath: string, mdPath: string): AuditResult {\n const rec = new Map<number, PageEvidence>();\n let parsed = 0;\n let skipped = 0;\n const errors: string[] = [];\n const runsByPage = new Map<number, SourceRun[]>();\n\n const wordsRaw = readFileSync(wordsPath, 'utf8');\n // A trailing \"\\n\" at EOF must not yield a spurious final empty line; a\n // genuine blank line mid-file does count, as skipped.\n const rawLines = wordsRaw.split('\\n');\n if (rawLines.length > 0 && rawLines[rawLines.length - 1] === '') rawLines.pop();\n for (const [lineIndex, rawLine] of rawLines.entries()) {\n const line = rawLine.trim();\n if (!line.startsWith('{')) {\n skipped++;\n errors.push(`line ${lineIndex + 1} is not valid JSON`);\n continue;\n }\n let o: unknown;\n try {\n o = JSON.parse(line);\n } catch {\n skipped++;\n errors.push(`line ${lineIndex + 1} is not valid JSON`);\n continue;\n }\n parsed++;\n if (!isAuditRecord(o)) {\n errors.push(`line ${lineIndex + 1} must contain a positive integer page and non-empty text`);\n continue;\n }\n let evidence = rec.get(o.page);\n if (!evidence) {\n evidence = { tokens: [], sourcePairs: [] };\n rec.set(o.page, evidence);\n }\n const runTokenDetails = tokens(o.text);\n const runTokens = runTokenDetails.map((token) => token.text);\n const start = evidence.tokens.length;\n evidence.tokens.push(...runTokens);\n for (let index = 0; index + 1 < runTokenDetails.length; index++) {\n if (INLINE_HYPHEN_RE.test(runTokenDetails[index].gapAfter)) {\n evidence.sourcePairs.push({ first: start + index, second: start + index + 1, explicitHyphen: true });\n }\n }\n const run = {\n page: o.page,\n tokens: runTokens.map((_, index) => start + index),\n terminalHyphen: HYPHEN_RE.test(o.text),\n hyphenOnly: /^[\\p{Pd}\\u2212]\\s*$/u.test(o.text),\n leadingWhitespace: /^\\s/u.test(o.text),\n trailingWhitespace: /\\s$/u.test(o.text),\n box: recordBox(o),\n };\n const pageRuns = runsByPage.get(o.page);\n if (pageRuns) pageRuns.push(run);\n else runsByPage.set(o.page, [run]);\n }\n\n if (parsed === 0) errors.push('word dump contains no JSON records');\n if (rec.size === 0) errors.push('word dump contains no valid records');\n if ([...rec.values()].every((evidence) => evidence.tokens.length === 0)) errors.push('word dump contains no lexical tokens');\n for (const [page, runs] of runsByPage) {\n for (let index = 0; index + 1 < runs.length; index++) {\n const current = runs[index];\n const next = runs[index + 1];\n if (current.tokens.length > 0 && current.terminalHyphen && next.tokens.length > 0) {\n const evidence = rec.get(page) as PageEvidence;\n evidence.sourcePairs.push({ first: current.tokens[current.tokens.length - 1], second: next.tokens[0], explicitHyphen: true });\n }\n if (current.tokens.length === 1 && next.tokens.length === 1 && !current.terminalHyphen && !next.hyphenOnly && !current.trailingWhitespace && !next.leadingWhitespace && touchingFragments(current.box, next.box)) {\n const evidence = rec.get(page) as PageEvidence;\n evidence.sourcePairs.push({ first: current.tokens[0], second: next.tokens[0], explicitHyphen: false });\n }\n if (current.tokens.length === 0 || !next.hyphenOnly || index + 2 >= runs.length) continue;\n const afterHyphen = runs[index + 2];\n if (afterHyphen.tokens.length === 0) continue;\n const evidence = rec.get(page) as PageEvidence;\n evidence.sourcePairs.push({ first: current.tokens[current.tokens.length - 1], second: afterHyphen.tokens[0], explicitHyphen: true });\n }\n }\n if (errors.length > 0) throw auditInputError(errors);\n\n const md = readFileSync(mdPath, 'utf8');\n const out = bodyByPage(md);\n\n const lines: string[] = [];\n let total = 0;\n let strict = 0;\n const perPage: { page: number; missing: string[] }[] = [];\n const pages = [...rec.keys()].sort((a, b) => a - b);\n\n const absent = pages.filter((page) => !out.has(page));\n if (absent.length > 0) {\n const markers = absent.map((page) => `p${page}`).join(', ');\n throw auditInputError([`Markdown has no provenance marker for ${markers}; add \"### pN\" or \"<!-- pN -->\" for every dumped page`]);\n }\n\n for (const page of pages) {\n const evidence = rec.get(page) as PageEvidence;\n const output = tokens(markdownText(out.get(page) as string));\n const outputTokens = output.map((token) => token.text);\n const strictMissing = strictDeficits(evidence.tokens, outputTokens);\n strict += strictMissing;\n const missing = missingTokens(evidence, output);\n if (missing.length > 0) {\n total += missing.length;\n perPage.push({ page, missing });\n lines.push(`p${page}: MISSING ${missing.length}: ${pyRepr(missing.slice(0, 12))}`);\n }\n }\n\n const words = pages.reduce((sum, p) => sum + (rec.get(p)?.tokens.length ?? 0), 0);\n const summaryLine = `parsed=${parsed} non-json-skipped=${skipped} pages=${rec.size} words=${words} strict_deficits=${strict} MISSING=${total}`;\n lines.push(summaryLine);\n\n return {\n parsed,\n nonJsonSkipped: skipped,\n pages: rec.size,\n words,\n strictDeficits: strict,\n missing: total,\n perPage,\n summaryLine,\n lines,\n };\n}\n\nfunction isAuditRecord(value: unknown): value is AuditRecord {\n if (typeof value !== 'object' || value === null || Array.isArray(value)) return false;\n const record = value as Partial<AuditRecord>;\n if (!(typeof record.page === 'number' && Number.isSafeInteger(record.page) && record.page > 0 && typeof record.text === 'string' && record.text.trim().length > 0)) return false;\n const geometry = [record.x, record.y, record.w, record.h];\n return geometry.every((field) => field === undefined) || (geometry.every((field) => typeof field === 'number' && Number.isFinite(field)) && (record.w as number) >= 0 && (record.h as number) >= 0);\n}\n\nfunction recordBox(record: AuditRecord): Box | undefined {\n if (record.x === undefined || record.y === undefined || record.w === undefined || record.h === undefined) return undefined;\n return { x: record.x, y: record.y, w: record.w, h: record.h };\n}\n\nfunction touchingFragments(first: Box | undefined, second: Box | undefined): boolean {\n if (!first || !second) return false;\n const height = Math.max(first.h, second.h);\n if (height === 0) return false;\n const gap = second.x - (first.x + first.w);\n // Fragment repairs are restricted to nearly touching runs on one baseline.\n return Math.abs(first.y - second.y) <= height * 0.25 && Math.abs(gap) <= height * 0.15;\n}\n\nfunction auditInputError(errors: string[]): PdfToMdError {\n return new PdfToMdError('AUDIT_INPUT', `audit evidence is invalid: ${errors.join('; ')}`);\n}\n\nfunction strictDeficits(source: string[], output: string[]): number {\n let deficits = 0;\n const outputCounts = counter(output);\n for (const [token, count] of counter(source)) deficits += Math.max(0, count - (outputCounts.get(token) ?? 0));\n return deficits;\n}\n\nfunction missingTokens(evidence: PageEvidence, output: Token[]): string[] {\n const sourceAvailable = new Set(evidence.tokens.map((_, index) => index));\n const outputAvailable = new Set(output.map((_, index) => index));\n const linePairs = lineHyphenPairs(output);\n const inlinePairs = inlineHyphenPairs(output);\n\n // Reserve literal occurrences before attempting a repair. A repair may join\n // several source tokens into one output token, so doing it first can steal\n // an occurrence required by an unrelated literal source token.\n for (const sourceIndex of [...sourceAvailable]) {\n const direct = output.findIndex((token, index) => outputAvailable.has(index) && token.text === evidence.tokens[sourceIndex]);\n if (direct < 0) continue;\n sourceAvailable.delete(sourceIndex);\n outputAvailable.delete(direct);\n }\n\n for (const pair of evidence.sourcePairs) {\n if (!sourceAvailable.has(pair.first) || !sourceAvailable.has(pair.second)) continue;\n const joined = evidence.tokens[pair.first] + evidence.tokens[pair.second];\n if (pair.explicitHyphen) {\n const hyphenated =\n inlinePairs.find(([first, second]) => outputAvailable.has(first) && outputAvailable.has(second) && output[first].text + output[second].text === joined) ?? linePairs.find(([first, second]) => outputAvailable.has(first) && outputAvailable.has(second) && output[first].text + output[second].text === joined);\n if (hyphenated) {\n sourceAvailable.delete(pair.first);\n sourceAvailable.delete(pair.second);\n outputAvailable.delete(hyphenated[0]);\n outputAvailable.delete(hyphenated[1]);\n continue;\n }\n }\n const direct = output.findIndex((token, index) => outputAvailable.has(index) && token.text === joined);\n if (direct >= 0) {\n sourceAvailable.delete(pair.first);\n sourceAvailable.delete(pair.second);\n outputAvailable.delete(direct);\n }\n }\n\n for (const sourceIndex of [...sourceAvailable]) {\n const split = linePairs.find(([first, second]) => outputAvailable.has(first) && outputAvailable.has(second) && output[first].text + output[second].text === evidence.tokens[sourceIndex]);\n if (!split) continue;\n sourceAvailable.delete(sourceIndex);\n outputAvailable.delete(split[0]);\n outputAvailable.delete(split[1]);\n }\n\n return [...sourceAvailable].map((index) => evidence.tokens[index]);\n}\n\nfunction lineHyphenPairs(output: Token[]): [number, number][] {\n const pairs: [number, number][] = [];\n for (let index = 0; index + 1 < output.length; index++) {\n if (LINE_HYPHEN_RE.test(output[index].gapAfter)) pairs.push([index, index + 1]);\n }\n return pairs;\n}\n\nfunction inlineHyphenPairs(output: Token[]): [number, number][] {\n const pairs: [number, number][] = [];\n for (let index = 0; index + 1 < output.length; index++) {\n if (INLINE_HYPHEN_RE.test(output[index].gapAfter)) pairs.push([index, index + 1]);\n }\n return pairs;\n}\n\n// Renders a string list as ['a', 'b'] (single-quoted) to match frozen baseline comparisons byte-for-byte.\nfunction pyRepr(items: string[]): string {\n return `[${items.map((s) => `'${s.replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\")}'`).join(', ')}]`;\n}\n"],"names":["auditWords","WORD_RE","HYPHEN_RE","LINE_HYPHEN_RE","INLINE_HYPHEN_RE","tokens","text","folded","foldTypographic","lastIndex","matches","matchAll","map","match","index","start","end","length","next","normalize","toLowerCase","gapAfter","slice","markdownText","md","replace","counter","items","m","Map","item","set","get","wordsPath","mdPath","rec","parsed","skipped","errors","runsByPage","wordsRaw","readFileSync","rawLines","split","pop","lineIndex","rawLine","evidence","line","trim","startsWith","push","o","JSON","parse","isAuditRecord","page","sourcePairs","runTokenDetails","runTokens","token","test","first","second","explicitHyphen","run","_","terminalHyphen","hyphenOnly","leadingWhitespace","trailingWhitespace","box","recordBox","pageRuns","entries","size","values","every","runs","current","touchingFragments","afterHyphen","auditInputError","out","bodyByPage","lines","total","strict","perPage","pages","keys","sort","a","b","absent","filter","has","markers","join","output","outputTokens","strictMissing","strictDeficits","missing","missingTokens","pyRepr","words","reduce","sum","p","summaryLine","nonJsonSkipped","value","Array","isArray","record","Number","isSafeInteger","geometry","x","y","w","h","field","undefined","isFinite","height","Math","max","gap","abs","PdfToMdError","source","outputCounts","deficits","count","sourceAvailable","Set","outputAvailable","linePairs","lineHyphenPairs","inlinePairs","inlineHyphenPairs","sourceIndex","direct","findIndex","delete","pair","joined","hyphenated","find","pairs","s"],"mappings":"AAAA,2EAA2E;AAC3E,wEAAwE;AACxE,EAAE;AACF,qEAAqE;AACrE,6EAA6E;AAC7E,yEAAyE;AACzE,EAAE;AACF,wEAAwE;AACxE,qBAAqB;;;;;+BAgILA;;;eAAAA;;;sBA/Ha;wBACA;uBACG;4BACL;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3B,IAAMC,UAAU;AAChB,IAAMC,YAAY;AAClB,IAAMC,iBAAiB;AACvB,IAAMC,mBAAmB;AA8CzB,SAASC,OAAOC,IAAY;IAC1B,IAAMC,SAASC,IAAAA,wBAAe,EAACF;IAC/BL,QAAQQ,SAAS,GAAG;IACpB,IAAMC,UAAW,qBAAGH,OAAOI,QAAQ,CAACV;IACpC,OAAOS,QAAQE,GAAG,CAAC,SAACC,OAAOC;;QACzB,IAAMC,QAAQF,MAAMC,KAAK;QACzB,IAAME,MAAMD,QAAQF,KAAK,CAAC,EAAE,CAACI,MAAM;QACnC,IAAMC,OAAOR,OAAO,CAAC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;SAAE;IAClF;IACA,OAAOyI;AACT;AAEA,0GAA0G;AAC1G,SAAS9C,OAAO9E,KAAe;IAC7B,OAAO,AAAC,IAAqF,OAAlFA,MAAMf,GAAG,CAAC,SAAC4I;eAAM,AAAC,IAAiD,OAA9CA,EAAE/H,OAAO,CAAC,OAAO,QAAQA,OAAO,CAAC,MAAM,QAAO;OAAIyE,IAAI,CAAC,OAAM;AAC/F"}
|
package/dist/cjs/cli/extract.js
CHANGED
|
@@ -290,7 +290,7 @@ var TUNING_FLAGS = [
|
|
|
290
290
|
];
|
|
291
291
|
var extract = function extract(ctx) {
|
|
292
292
|
return _async_to_generator(function() {
|
|
293
|
-
var parsed, values, positionals, _positionals, src, outArg, wordsPath, formatRaw, format, pages, m, first, last, tuning, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, spec, raw, value, ok, dpi, raw1, value1, source, defaultExt, dest, onWarning, showProgress, onProgress, debugWordsPath, onWords, common, ocrCommon, words, skipped, ref, content, report, markdown, ref1, ref2, analysis,
|
|
293
|
+
var parsed, values, positionals, _positionals, src, outArg, wordsPath, formatRaw, format, pages, m, first, last, tuning, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, spec, raw, value, ok, dpi, raw1, value1, source, defaultExt, dest, onWarning, showProgress, onProgress, debugWordsPath, onWords, common, ocrCommon, words, skipped, ref, content, report, markdown, ref1, ref2, analysis, _report_floats;
|
|
294
294
|
return _ts_generator(this, function(_state) {
|
|
295
295
|
switch(_state.label){
|
|
296
296
|
case 0:
|
|
@@ -461,7 +461,8 @@ var extract = function extract(ctx) {
|
|
|
461
461
|
} : undefined;
|
|
462
462
|
common = {
|
|
463
463
|
pages: pages,
|
|
464
|
-
onWarning: onWarning
|
|
464
|
+
onWarning: onWarning,
|
|
465
|
+
onWords: onWords
|
|
465
466
|
};
|
|
466
467
|
ocrCommon = _object_spread_props(_object_spread({}, common), {
|
|
467
468
|
tuning: tuning,
|
|
@@ -511,7 +512,7 @@ var extract = function extract(ctx) {
|
|
|
511
512
|
content = markdown;
|
|
512
513
|
return [
|
|
513
514
|
3,
|
|
514
|
-
|
|
515
|
+
10
|
|
515
516
|
];
|
|
516
517
|
case 5:
|
|
517
518
|
if (!(words !== undefined)) return [
|
|
@@ -529,62 +530,26 @@ var extract = function extract(ctx) {
|
|
|
529
530
|
case 6:
|
|
530
531
|
analysis = _state.sent();
|
|
531
532
|
return [
|
|
532
|
-
3,
|
|
533
|
-
13
|
|
534
|
-
];
|
|
535
|
-
case 7:
|
|
536
|
-
if (!values.ocr) return [
|
|
537
533
|
3,
|
|
538
534
|
9
|
|
539
535
|
];
|
|
536
|
+
case 7:
|
|
540
537
|
return [
|
|
541
538
|
4,
|
|
542
|
-
(0,
|
|
543
|
-
|
|
539
|
+
(0, _extractts.analyzePdfAuto)(src, _object_spread_props(_object_spread({}, ocrCommon), {
|
|
540
|
+
ocr: values.ocr,
|
|
541
|
+
noOcr: values['no-ocr'],
|
|
542
|
+
pageMarkers: values['page-markers']
|
|
544
543
|
}))
|
|
545
544
|
];
|
|
546
545
|
case 8:
|
|
547
|
-
analysis = _state.sent();
|
|
548
|
-
|
|
549
|
-
3,
|
|
550
|
-
13
|
|
551
|
-
];
|
|
546
|
+
analysis = _state.sent().analysis;
|
|
547
|
+
_state.label = 9;
|
|
552
548
|
case 9:
|
|
553
|
-
return [
|
|
554
|
-
4,
|
|
555
|
-
(0, _analyzets.analyze)(src, _object_spread_props(_object_spread({}, common), {
|
|
556
|
-
path: 'text',
|
|
557
|
-
pageMarkers: values['page-markers']
|
|
558
|
-
}))
|
|
559
|
-
];
|
|
560
|
-
case 10:
|
|
561
|
-
text = _state.sent();
|
|
562
|
-
if (!((0, _extractts.needsOcrFallback)(text.report.charsPerPage) && !values['no-ocr'])) return [
|
|
563
|
-
3,
|
|
564
|
-
12
|
|
565
|
-
];
|
|
566
|
-
onWarning((0, _extractts.ocrFallbackNotice)(text.report.charsPerPage));
|
|
567
|
-
return [
|
|
568
|
-
4,
|
|
569
|
-
(0, _analyzets.analyze)(src, _object_spread_props(_object_spread({}, ocrCommon), {
|
|
570
|
-
path: 'ocr',
|
|
571
|
-
fallback: true
|
|
572
|
-
}))
|
|
573
|
-
];
|
|
574
|
-
case 11:
|
|
575
|
-
analysis = _state.sent();
|
|
576
|
-
return [
|
|
577
|
-
3,
|
|
578
|
-
13
|
|
579
|
-
];
|
|
580
|
-
case 12:
|
|
581
|
-
analysis = text;
|
|
582
|
-
_state.label = 13;
|
|
583
|
-
case 13:
|
|
584
549
|
report = analysis.report;
|
|
585
550
|
content = format === 'raw' ? JSON.stringify(analysis, null, 1) : (0, _renderanalysists.toText)(analysis);
|
|
586
|
-
_state.label =
|
|
587
|
-
case
|
|
551
|
+
_state.label = 10;
|
|
552
|
+
case 10:
|
|
588
553
|
if (values.stdout) process.stdout.write(content);
|
|
589
554
|
else (0, _nodefs.writeFileSync)(dest, content);
|
|
590
555
|
// The library's Report stays clean of source/out; --json consumers keep
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/cli/extract.ts"],"sourcesContent":["// extract command — parse this command's flags, call src/extract.ts, print\n// and write the result. No business logic here — the library returns data and throws; only the CLI prints and exits.\nimport { readFileSync, writeFileSync } from 'node:fs';\nimport path from 'node:path';\nimport { parseArgs } from 'node:util';\nimport { analyze } from '../analyze.ts';\nimport type { OcrWordInput } from '../extract.ts';\nimport { extractOcr, needsOcrFallback, ocrFallbackNotice, pdfToMarkdown } from '../extract.ts';\nimport type { Tuning } from '../geometry.ts';\nimport { toText } from '../render-analysis.ts';\nimport type { Analysis, Report } from '../types.ts';\nimport type { Command } from './types.ts';\n\nconst USAGE =\n 'usage: pdf-to-md [extract] <file.pdf> [out.md] [--json] [--page-markers] [--ocr] [--no-ocr] ' +\n '[--pages N[-M]] [--stdout] [--words-json=FILE] [--debug-words=FILE] ' +\n '[--format md|txt|raw (default md; raw = Analysis JSON incl. the report; txt = reading-order plain text)] ' +\n '[--dpi=N (OCR render resolution, default 288; raise for tiny print or chart/label pages, ' +\n '>~400 measured worse on ordinary prose pages)] ' +\n '[--float-max-words=N] [--float-max-width=F] [--float-margin=F] [--para-gap=F] [--heading-scale=F] ' +\n '[--col-max-width=F] [--col-split=F] [--line-y-tol=F] [--line-height-ratio=F]';\n\nconst FORMATS = ['md', 'txt', 'raw'] as const;\ntype Format = (typeof FORMATS)[number];\n\n// A bad tuning-flag value (non-numeric, or <= 0 unless the flag allows zero) is a usage error, not a silent no-op.\nconst TUNING_FLAGS: {\n name: string;\n key: keyof Tuning;\n allowZero?: boolean;\n isInt?: boolean;\n}[] = [\n {\n name: 'float-max-words',\n key: 'floatMaxWords',\n allowZero: true,\n isInt: true,\n },\n { name: 'float-max-width', key: 'floatMaxWidth' },\n { name: 'float-margin', key: 'floatMargin' },\n { name: 'para-gap', key: 'paraGap' },\n { name: 'heading-scale', key: 'headingScale' },\n { name: 'col-max-width', key: 'colMaxWidth' },\n { name: 'col-split', key: 'colSplit' },\n { name: 'line-y-tol', key: 'lineYTol' },\n { name: 'line-height-ratio', key: 'lineHeightRatio' },\n];\n\nconst extract: Command = async (ctx) => {\n // --debug-words takes a required FILE value; a bare --debug-words is a\n // usage error, checked before parseArgs so it can't silently swallow the next token as its value.\n if (ctx.rest.includes('--debug-words')) {\n ctx.usageError(`${USAGE}\\n--debug-words requires a value: --debug-words=FILE`);\n }\n\n const parsed = (() => {\n try {\n return parseArgs({\n args: ctx.rest,\n options: {\n json: { type: 'boolean', default: false },\n 'page-markers': { type: 'boolean', default: false },\n ocr: { type: 'boolean', default: false },\n 'no-ocr': { type: 'boolean', default: false },\n stdout: { type: 'boolean', default: false },\n 'debug-words': { type: 'string' },\n help: { type: 'boolean', default: false, short: 'h' },\n pages: { type: 'string' },\n dpi: { type: 'string' },\n format: { type: 'string', default: 'md' },\n 'words-json': { type: 'string' },\n ...Object.fromEntries(TUNING_FLAGS.map((f) => [f.name, { type: 'string' as const }])),\n },\n allowPositionals: true,\n });\n } catch (err) {\n // usageError exits; the throw is dead code that tells the compiler so.\n throw ctx.usageError(`${(err as Error).message}\\n${USAGE}`);\n }\n })();\n const values = parsed.values as Record<string, string | boolean | undefined>;\n const positionals = parsed.positionals;\n\n if (values.help) {\n console.log(USAGE);\n return;\n }\n\n const [src, outArg] = positionals;\n const wordsPath = values['words-json'] as string | undefined;\n if (!src && !wordsPath) ctx.usageError(USAGE);\n\n const formatRaw = values.format as string;\n if (!(FORMATS as readonly string[]).includes(formatRaw)) {\n ctx.usageError(`${USAGE}\\n--format must be one of ${FORMATS.join('|')} (got ${JSON.stringify(formatRaw)})`);\n }\n const format = formatRaw as Format;\n\n // --pages: a malformed spec is a usage error; out-of-range page NUMBERS\n // clamp instead (collect.ts / the engine apply the same clamping policy).\n let pages: { first: number; last: number } | undefined;\n if (values.pages !== undefined) {\n const m = /^(\\d+)(?:-(\\d+))?$/.exec(values.pages as string);\n const first = m ? Number(m[1]) : Number.NaN;\n const last = m ? (m[2] !== undefined ? Number(m[2]) : first) : Number.NaN;\n if (!m || first < 1 || last < first) {\n ctx.usageError(`${USAGE}\\n--pages requires N or N-M, N >= 1, M >= N (got ${JSON.stringify(values.pages)})`);\n }\n pages = { first, last };\n }\n\n const tuning: Partial<Tuning> = {};\n for (const spec of TUNING_FLAGS) {\n const raw = values[spec.name] as string | undefined;\n if (raw === undefined) continue;\n const value = Number(raw);\n const ok = raw !== '' && Number.isFinite(value) && (spec.allowZero ? value >= 0 : value > 0);\n if (!ok) {\n ctx.usageError(`${USAGE}\\n--${spec.name} requires a ${spec.allowZero ? 'non-negative' : 'positive'} number (got ${JSON.stringify(raw)})`);\n }\n (tuning[spec.key] as number) = spec.isInt ? Math.trunc(value) : value;\n }\n\n // --dpi: page-render resolution, validated like the tuning flags. Default\n // 288 is the measured operating point; >~400 measured WORSE on ordinary pages — not a \"bigger is safer\" knob.\n let dpi: number | undefined;\n if (values.dpi !== undefined) {\n const raw = values.dpi as string;\n const value = Number(raw);\n if (raw === '' || !Number.isFinite(value) || value <= 0) {\n ctx.usageError(`${USAGE}\\n--dpi requires a positive number (got ${JSON.stringify(raw)})`);\n }\n dpi = value;\n }\n\n // In --words-json mode there may be no PDF positional; the words file\n // stands in for naming, extension stripped so the default dest can never overwrite the input. Default extension follows --format.\n const source = src ?? (wordsPath as string);\n const defaultExt = format === 'raw' ? 'json' : format === 'txt' ? 'txt' : 'md';\n const dest = outArg || `${source.replace(/\\.(pdf|jsonl)$/i, '')}.${defaultExt}`;\n\n const onWarning = (m: string) => console.error(m);\n const showProgress = process.stderr.isTTY;\n // onProgress carries per-page timing/count only; human-readable\n // diagnostics (e.g. the sparse-retry notice) arrive via onWarning instead, so they print regardless of TTY-ness.\n const onProgress = (e: { type: 'page'; page: number; words: number; ms: number }) => {\n if (showProgress) console.error(`p${e.page}: ${e.words} words, ${e.ms}ms`);\n };\n const debugWordsPath = values['debug-words'] as string | undefined;\n if (debugWordsPath !== undefined && debugWordsPath.trim() === '') {\n ctx.usageError(`${USAGE}\\n--debug-words requires a value: --debug-words=FILE`);\n }\n // The severity-1 audit instrument: every recognized word as clean JSON\n // lines to its own file. stderr stays a human channel (mixed prose), never a dump target — this file is guaranteed byte-for-byte parseable.\n const onWords = debugWordsPath\n ? (words: OcrWordInput[]) => {\n const lines = words.map((w) => {\n const entry: OcrWordInput = { page: w.page, text: w.text, x: w.x, y: w.y, w: w.w, h: w.h };\n if (w.confidence !== undefined) entry.confidence = w.confidence;\n return JSON.stringify(entry);\n });\n writeFileSync(debugWordsPath, lines.length ? `${lines.join('\\n')}\\n` : '');\n }\n : undefined;\n\n const common = { pages, onWarning };\n const ocrCommon = {\n ...common,\n tuning,\n dpi,\n onProgress,\n onWords,\n };\n\n let words: OcrWordInput[] | undefined;\n let skipped = 0;\n if (wordsPath !== undefined) {\n ({ words, skipped } = parseWordLines(readFileSync(wordsPath, 'utf8')));\n if (skipped > 0) console.error(`--words-json: skipped ${skipped} non-JSON line(s)`);\n }\n\n let content: string;\n let report: Report;\n if (format === 'md') {\n let markdown: string;\n if (words !== undefined) {\n ({ markdown, report } = await extractOcr({ words }, ocrCommon));\n } else {\n ({ markdown, report } = await pdfToMarkdown(src, {\n ...ocrCommon,\n ocr: values.ocr as boolean,\n noOcr: values['no-ocr'] as boolean,\n pageMarkers: values['page-markers'] as boolean,\n }));\n }\n content = markdown;\n } else {\n // raw/txt: both render from an Analysis — raw IS the Analysis as JSON;\n // txt is toText(analysis). Mirrors pdfToMarkdown's own auto-OCR fallback decision so a different --format doesn't silently pick a different path.\n let analysis: Analysis;\n if (words !== undefined) {\n analysis = await analyze({ words }, { ...ocrCommon, path: 'ocr' });\n } else if (values.ocr) {\n analysis = await analyze(src, { ...ocrCommon, path: 'ocr' });\n } else {\n const text = await analyze(src, { ...common, path: 'text', pageMarkers: values['page-markers'] as boolean });\n if (needsOcrFallback(text.report.charsPerPage) && !values['no-ocr']) {\n onWarning(ocrFallbackNotice(text.report.charsPerPage));\n analysis = await analyze(src, { ...ocrCommon, path: 'ocr', fallback: true });\n } else {\n analysis = text;\n }\n }\n report = analysis.report;\n content = format === 'raw' ? JSON.stringify(analysis, null, 1) : toText(analysis);\n }\n\n if (values.stdout) process.stdout.write(content);\n else writeFileSync(dest, content);\n\n // The library's Report stays clean of source/out; --json consumers keep\n // report.json files around and need to know which input produced each one, so the CLI (only) decorates the printed JSON with source/out here.\n if (values.json) console.log(JSON.stringify({ source, out: dest, ...report }, null, 1));\n else if (report.path === 'ocr') {\n console.error(`${path.basename(dest).padEnd(32)} OCR path heads=${report.headings}(junk ${report.junkHeadings}) ` + `paras=${report.paragraphs} callouts=${report.callouts} floats=${report.floats ?? 0} dangling=${report.danglingLong} (long) ` + `~${report.charsPerPage} chars/page`);\n } else {\n console.error(\n `${path.basename(dest).padEnd(32)} body=${report.bodyFontHeight}pt heads=${report.headings}(junk ${report.junkHeadings}) ` +\n `lists=${report.listItems} paras=${report.paragraphs} callouts=${report.callouts} pairs=${report.joinedPairs} ` +\n `~${report.charsPerPage} chars/page` +\n (report.sparse ? ' ⚠️ implausibly sparse — pages are likely images; re-run with --ocr' : '')\n );\n }\n};\n\n// Parse one word-dump JSON line per word — key order is not guaranteed, parse\n// never grep. Non-JSON lines are skipped with a count, not fatal (the auditor applies the same policy).\nfunction parseWordLines(raw: string): { words: OcrWordInput[]; skipped: number } {\n const words: OcrWordInput[] = [];\n let skipped = 0;\n for (const line of raw.split('\\n')) {\n if (!line.trim()) continue;\n try {\n words.push(JSON.parse(line));\n } catch {\n skipped++;\n }\n }\n return { words, skipped };\n}\n\nexport default extract;\n"],"names":["USAGE","FORMATS","TUNING_FLAGS","name","key","allowZero","isInt","extract","ctx","parsed","values","positionals","src","outArg","wordsPath","formatRaw","format","pages","m","first","last","tuning","spec","raw","value","ok","dpi","source","defaultExt","dest","onWarning","showProgress","onProgress","debugWordsPath","onWords","common","ocrCommon","words","skipped","content","report","markdown","analysis","text","rest","includes","usageError","parseArgs","args","options","json","type","default","ocr","stdout","help","short","Object","fromEntries","map","f","allowPositionals","err","message","console","log","JSON","join","stringify","undefined","exec","Number","NaN","isFinite","Math","trunc","replace","error","process","stderr","isTTY","e","page","ms","trim","lines","w","entry","x","y","h","confidence","writeFileSync","length","parseWordLines","readFileSync","extractOcr","pdfToMarkdown","noOcr","pageMarkers","analyze","path","needsOcrFallback","charsPerPage","ocrFallbackNotice","fallback","toText","write","out","basename","padEnd","headings","junkHeadings","paragraphs","callouts","floats","danglingLong","bodyFontHeight","listItems","joinedPairs","sparse","split","line","push","parse"],"mappings":"AAAA,2EAA2E;AAC3E,qHAAqH;;;;;+BA0PrH;;;eAAA;;;sBAzP4C;+DAC3B;wBACS;yBACF;yBAEuD;gCAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,KAAK,CAACF;YACxB,EAAE,eAAM;gBACNpF;YACF;QACF;;QAPK;QAAA;;;iBAAA,6BAAA;gBAAA;;;gBAAA;sBAAA;;;;IAQL,OAAO;QAAED,OAAAA;QAAOC,SAAAA;IAAQ;AAC1B;IAEA,WAAe/B"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/cli/extract.ts"],"sourcesContent":["// extract command — parse this command's flags, call src/extract.ts, print\n// and write the result. No business logic here — the library returns data and throws; only the CLI prints and exits.\nimport { readFileSync, writeFileSync } from 'node:fs';\nimport path from 'node:path';\nimport { parseArgs } from 'node:util';\nimport { analyze } from '../analyze.ts';\nimport type { OcrWordInput } from '../extract.ts';\nimport { analyzePdfAuto, extractOcr, pdfToMarkdown } from '../extract.ts';\nimport type { Tuning } from '../geometry.ts';\nimport { toText } from '../render-analysis.ts';\nimport type { Analysis, Report } from '../types.ts';\nimport type { Command } from './types.ts';\n\nconst USAGE =\n 'usage: pdf-to-md [extract] <file.pdf> [out.md] [--json] [--page-markers] [--ocr] [--no-ocr] ' +\n '[--pages N[-M]] [--stdout] [--words-json=FILE] [--debug-words=FILE] ' +\n '[--format md|txt|raw (default md; raw = Analysis JSON incl. the report; txt = reading-order plain text)] ' +\n '[--dpi=N (OCR render resolution, default 288; raise for tiny print or chart/label pages, ' +\n '>~400 measured worse on ordinary prose pages)] ' +\n '[--float-max-words=N] [--float-max-width=F] [--float-margin=F] [--para-gap=F] [--heading-scale=F] ' +\n '[--col-max-width=F] [--col-split=F] [--line-y-tol=F] [--line-height-ratio=F]';\n\nconst FORMATS = ['md', 'txt', 'raw'] as const;\ntype Format = (typeof FORMATS)[number];\n\n// A bad tuning-flag value (non-numeric, or <= 0 unless the flag allows zero) is a usage error, not a silent no-op.\nconst TUNING_FLAGS: {\n name: string;\n key: keyof Tuning;\n allowZero?: boolean;\n isInt?: boolean;\n}[] = [\n {\n name: 'float-max-words',\n key: 'floatMaxWords',\n allowZero: true,\n isInt: true,\n },\n { name: 'float-max-width', key: 'floatMaxWidth' },\n { name: 'float-margin', key: 'floatMargin' },\n { name: 'para-gap', key: 'paraGap' },\n { name: 'heading-scale', key: 'headingScale' },\n { name: 'col-max-width', key: 'colMaxWidth' },\n { name: 'col-split', key: 'colSplit' },\n { name: 'line-y-tol', key: 'lineYTol' },\n { name: 'line-height-ratio', key: 'lineHeightRatio' },\n];\n\nconst extract: Command = async (ctx) => {\n // --debug-words takes a required FILE value; a bare --debug-words is a\n // usage error, checked before parseArgs so it can't silently swallow the next token as its value.\n if (ctx.rest.includes('--debug-words')) {\n ctx.usageError(`${USAGE}\\n--debug-words requires a value: --debug-words=FILE`);\n }\n\n const parsed = (() => {\n try {\n return parseArgs({\n args: ctx.rest,\n options: {\n json: { type: 'boolean', default: false },\n 'page-markers': { type: 'boolean', default: false },\n ocr: { type: 'boolean', default: false },\n 'no-ocr': { type: 'boolean', default: false },\n stdout: { type: 'boolean', default: false },\n 'debug-words': { type: 'string' },\n help: { type: 'boolean', default: false, short: 'h' },\n pages: { type: 'string' },\n dpi: { type: 'string' },\n format: { type: 'string', default: 'md' },\n 'words-json': { type: 'string' },\n ...Object.fromEntries(TUNING_FLAGS.map((f) => [f.name, { type: 'string' as const }])),\n },\n allowPositionals: true,\n });\n } catch (err) {\n // usageError exits; the throw is dead code that tells the compiler so.\n throw ctx.usageError(`${(err as Error).message}\\n${USAGE}`);\n }\n })();\n const values = parsed.values as Record<string, string | boolean | undefined>;\n const positionals = parsed.positionals;\n\n if (values.help) {\n console.log(USAGE);\n return;\n }\n\n const [src, outArg] = positionals;\n const wordsPath = values['words-json'] as string | undefined;\n if (!src && !wordsPath) ctx.usageError(USAGE);\n\n const formatRaw = values.format as string;\n if (!(FORMATS as readonly string[]).includes(formatRaw)) {\n ctx.usageError(`${USAGE}\\n--format must be one of ${FORMATS.join('|')} (got ${JSON.stringify(formatRaw)})`);\n }\n const format = formatRaw as Format;\n\n // --pages: a malformed spec is a usage error; out-of-range page NUMBERS\n // clamp instead (collect.ts / the engine apply the same clamping policy).\n let pages: { first: number; last: number } | undefined;\n if (values.pages !== undefined) {\n const m = /^(\\d+)(?:-(\\d+))?$/.exec(values.pages as string);\n const first = m ? Number(m[1]) : Number.NaN;\n const last = m ? (m[2] !== undefined ? Number(m[2]) : first) : Number.NaN;\n if (!m || first < 1 || last < first) {\n ctx.usageError(`${USAGE}\\n--pages requires N or N-M, N >= 1, M >= N (got ${JSON.stringify(values.pages)})`);\n }\n pages = { first, last };\n }\n\n const tuning: Partial<Tuning> = {};\n for (const spec of TUNING_FLAGS) {\n const raw = values[spec.name] as string | undefined;\n if (raw === undefined) continue;\n const value = Number(raw);\n const ok = raw !== '' && Number.isFinite(value) && (spec.allowZero ? value >= 0 : value > 0);\n if (!ok) {\n ctx.usageError(`${USAGE}\\n--${spec.name} requires a ${spec.allowZero ? 'non-negative' : 'positive'} number (got ${JSON.stringify(raw)})`);\n }\n (tuning[spec.key] as number) = spec.isInt ? Math.trunc(value) : value;\n }\n\n // --dpi: page-render resolution, validated like the tuning flags. Default\n // 288 is the measured operating point; >~400 measured WORSE on ordinary pages — not a \"bigger is safer\" knob.\n let dpi: number | undefined;\n if (values.dpi !== undefined) {\n const raw = values.dpi as string;\n const value = Number(raw);\n if (raw === '' || !Number.isFinite(value) || value <= 0) {\n ctx.usageError(`${USAGE}\\n--dpi requires a positive number (got ${JSON.stringify(raw)})`);\n }\n dpi = value;\n }\n\n // In --words-json mode there may be no PDF positional; the words file\n // stands in for naming, extension stripped so the default dest can never overwrite the input. Default extension follows --format.\n const source = src ?? (wordsPath as string);\n const defaultExt = format === 'raw' ? 'json' : format === 'txt' ? 'txt' : 'md';\n const dest = outArg || `${source.replace(/\\.(pdf|jsonl)$/i, '')}.${defaultExt}`;\n\n const onWarning = (m: string) => console.error(m);\n const showProgress = process.stderr.isTTY;\n // onProgress carries per-page timing/count only; human-readable\n // diagnostics (e.g. the sparse-retry notice) arrive via onWarning instead, so they print regardless of TTY-ness.\n const onProgress = (e: { type: 'page'; page: number; words: number; ms: number }) => {\n if (showProgress) console.error(`p${e.page}: ${e.words} words, ${e.ms}ms`);\n };\n const debugWordsPath = values['debug-words'] as string | undefined;\n if (debugWordsPath !== undefined && debugWordsPath.trim() === '') {\n ctx.usageError(`${USAGE}\\n--debug-words requires a value: --debug-words=FILE`);\n }\n // The severity-1 audit instrument: every recognized word as clean JSON\n // lines to its own file. stderr stays a human channel (mixed prose), never a dump target — this file is guaranteed byte-for-byte parseable.\n const onWords = debugWordsPath\n ? (words: OcrWordInput[]) => {\n const lines = words.map((w) => {\n const entry: OcrWordInput = { page: w.page, text: w.text, x: w.x, y: w.y, w: w.w, h: w.h };\n if (w.confidence !== undefined) entry.confidence = w.confidence;\n return JSON.stringify(entry);\n });\n writeFileSync(debugWordsPath, lines.length ? `${lines.join('\\n')}\\n` : '');\n }\n : undefined;\n\n const common = { pages, onWarning, onWords };\n const ocrCommon = {\n ...common,\n tuning,\n dpi,\n onProgress,\n onWords,\n };\n\n let words: OcrWordInput[] | undefined;\n let skipped = 0;\n if (wordsPath !== undefined) {\n ({ words, skipped } = parseWordLines(readFileSync(wordsPath, 'utf8')));\n if (skipped > 0) console.error(`--words-json: skipped ${skipped} non-JSON line(s)`);\n }\n\n let content: string;\n let report: Report;\n if (format === 'md') {\n let markdown: string;\n if (words !== undefined) {\n ({ markdown, report } = await extractOcr({ words }, ocrCommon));\n } else {\n ({ markdown, report } = await pdfToMarkdown(src, {\n ...ocrCommon,\n ocr: values.ocr as boolean,\n noOcr: values['no-ocr'] as boolean,\n pageMarkers: values['page-markers'] as boolean,\n }));\n }\n content = markdown;\n } else {\n // raw/txt render the same routed Analysis as the Markdown API, so a\n // format switch cannot discard a sparse image page or change audit input.\n let analysis: Analysis;\n if (words !== undefined) {\n analysis = await analyze({ words }, { ...ocrCommon, path: 'ocr' });\n } else {\n analysis = (\n await analyzePdfAuto(src, {\n ...ocrCommon,\n ocr: values.ocr as boolean,\n noOcr: values['no-ocr'] as boolean,\n pageMarkers: values['page-markers'] as boolean,\n })\n ).analysis;\n }\n report = analysis.report;\n content = format === 'raw' ? JSON.stringify(analysis, null, 1) : toText(analysis);\n }\n\n if (values.stdout) process.stdout.write(content);\n else writeFileSync(dest, content);\n\n // The library's Report stays clean of source/out; --json consumers keep\n // report.json files around and need to know which input produced each one, so the CLI (only) decorates the printed JSON with source/out here.\n if (values.json) console.log(JSON.stringify({ source, out: dest, ...report }, null, 1));\n else if (report.path === 'ocr') {\n console.error(`${path.basename(dest).padEnd(32)} OCR path heads=${report.headings}(junk ${report.junkHeadings}) ` + `paras=${report.paragraphs} callouts=${report.callouts} floats=${report.floats ?? 0} dangling=${report.danglingLong} (long) ` + `~${report.charsPerPage} chars/page`);\n } else {\n console.error(\n `${path.basename(dest).padEnd(32)} body=${report.bodyFontHeight}pt heads=${report.headings}(junk ${report.junkHeadings}) ` +\n `lists=${report.listItems} paras=${report.paragraphs} callouts=${report.callouts} pairs=${report.joinedPairs} ` +\n `~${report.charsPerPage} chars/page` +\n (report.sparse ? ' ⚠️ implausibly sparse — pages are likely images; re-run with --ocr' : '')\n );\n }\n};\n\n// Parse one word-dump JSON line per word — key order is not guaranteed, parse\n// never grep. Non-JSON lines are skipped with a count, not fatal (the auditor applies the same policy).\nfunction parseWordLines(raw: string): { words: OcrWordInput[]; skipped: number } {\n const words: OcrWordInput[] = [];\n let skipped = 0;\n for (const line of raw.split('\\n')) {\n if (!line.trim()) continue;\n try {\n words.push(JSON.parse(line));\n } catch {\n skipped++;\n }\n }\n return { words, skipped };\n}\n\nexport default extract;\n"],"names":["USAGE","FORMATS","TUNING_FLAGS","name","key","allowZero","isInt","extract","ctx","parsed","values","positionals","src","outArg","wordsPath","formatRaw","format","pages","m","first","last","tuning","spec","raw","value","ok","dpi","source","defaultExt","dest","onWarning","showProgress","onProgress","debugWordsPath","onWords","common","ocrCommon","words","skipped","content","report","markdown","analysis","rest","includes","usageError","parseArgs","args","options","json","type","default","ocr","stdout","help","short","Object","fromEntries","map","f","allowPositionals","err","message","console","log","JSON","join","stringify","undefined","exec","Number","NaN","isFinite","Math","trunc","replace","error","process","stderr","isTTY","e","page","ms","trim","lines","w","entry","text","x","y","h","confidence","writeFileSync","length","parseWordLines","readFileSync","extractOcr","pdfToMarkdown","noOcr","pageMarkers","analyze","path","analyzePdfAuto","toText","write","out","basename","padEnd","headings","junkHeadings","paragraphs","callouts","floats","danglingLong","charsPerPage","bodyFontHeight","listItems","joinedPairs","sparse","split","line","push","parse"],"mappings":"AAAA,2EAA2E;AAC3E,qHAAqH;;;;;+BAyPrH;;;eAAA;;;sBAxP4C;+DAC3B;wBACS;yBACF;yBAEkC;gCAEnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIvB,IAAMA,QACJ,iGACA,yEACA,8GACA,8FACA,oDACA,uGACA;AAEF,IAAMC,UAAU;IAAC;IAAM;IAAO;CAAM;AAGpC,mHAAmH;AACnH,IAAMC,eAKA;IACJ;QACEC,MAAM;QACNC,KAAK;QACLC,WAAW;QACXC,OAAO;IACT;IACA;QAAEH,MAAM;QAAmBC,KAAK;IAAgB;IAChD;QAAED,MAAM;QAAgBC,KAAK;IAAc;IAC3C;QAAED,MAAM;QAAYC,KAAK;IAAU;IACnC;QAAED,MAAM;QAAiBC,KAAK;IAAe;IAC7C;QAAED,MAAM;QAAiBC,KAAK;IAAc;IAC5C;QAAED,MAAM;QAAaC,KAAK;IAAW;IACrC;QAAED,MAAM;QAAcC,KAAK;IAAW;IACtC;QAAED,MAAM;QAAqBC,KAAK;IAAkB;CACrD;AAED,IAAMG,UAAmB,iBAAOC;;YAO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IAAIC,UAAU;QACT,kCAAA,2BAAA;;QAAL,QAAK,YAAcf,IAAIgG,KAAK,CAAC,0BAAxB,SAAA,6BAAA,QAAA,yBAAA,iCAA+B;YAA/B,IAAMC,OAAN;YACH,IAAI,CAACA,KAAKrC,IAAI,IAAI;YAClB,IAAI;gBACF9C,MAAMoF,IAAI,CAACxD,KAAKyD,KAAK,CAACF;YACxB,EAAE,eAAM;gBACNlF;YACF;QACF;;QAPK;QAAA;;;iBAAA,6BAAA;gBAAA;;;gBAAA;sBAAA;;;;IAQL,OAAO;QAAED,OAAAA;QAAOC,SAAAA;IAAQ;AAC1B;IAEA,WAAe/B"}
|
package/dist/cjs/collect.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// Pass 1: load
|
|
2
|
-
//
|
|
1
|
+
// Pass 1: load each PDF text run into viewport points, plus the document-wide
|
|
2
|
+
// char-weighted font-height histogram. Height rounds to 0.1pt for stable histogram keys.
|
|
3
3
|
"use strict";
|
|
4
4
|
Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
value: true
|
|
@@ -28,6 +28,9 @@ function _array_like_to_array(arr, len) {
|
|
|
28
28
|
function _array_with_holes(arr) {
|
|
29
29
|
if (Array.isArray(arr)) return arr;
|
|
30
30
|
}
|
|
31
|
+
function _array_without_holes(arr) {
|
|
32
|
+
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
33
|
+
}
|
|
31
34
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
32
35
|
try {
|
|
33
36
|
var info = gen[key](arg);
|
|
@@ -54,6 +57,11 @@ function _async_to_generator(fn) {
|
|
|
54
57
|
});
|
|
55
58
|
};
|
|
56
59
|
}
|
|
60
|
+
function _iterable_to_array(iter) {
|
|
61
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) {
|
|
62
|
+
return Array.from(iter);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
57
65
|
function _iterable_to_array_limit(arr, i) {
|
|
58
66
|
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
59
67
|
if (_i == null) return;
|
|
@@ -81,9 +89,15 @@ function _iterable_to_array_limit(arr, i) {
|
|
|
81
89
|
function _non_iterable_rest() {
|
|
82
90
|
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
83
91
|
}
|
|
92
|
+
function _non_iterable_spread() {
|
|
93
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
94
|
+
}
|
|
84
95
|
function _sliced_to_array(arr, i) {
|
|
85
96
|
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
86
97
|
}
|
|
98
|
+
function _to_consumable_array(arr) {
|
|
99
|
+
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
100
|
+
}
|
|
87
101
|
function _ts_generator(thisArg, body) {
|
|
88
102
|
var f, y, t, _ = {
|
|
89
103
|
label: 0,
|
|
@@ -194,12 +208,22 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
194
208
|
// HYBRID-PAGE DETECTION (README's "Design contract"): a page can carry a
|
|
195
209
|
// real text layer AND a separately-painted large image with its OWN baked-in text that `page.getTextContent()` never sees.
|
|
196
210
|
//
|
|
197
|
-
//
|
|
198
|
-
//
|
|
211
|
+
// Image presence uses the paint operators for XObjects, inline images, and masks.
|
|
212
|
+
// The large-image threshold uses `paintImageXObject` dimensions when available.
|
|
199
213
|
var LARGE_IMAGE_MIN_PX = 200;
|
|
200
|
-
|
|
214
|
+
var IMAGE_PAINT_OPS = new Set([
|
|
215
|
+
_pdf.OPS.paintImageMaskXObject,
|
|
216
|
+
_pdf.OPS.paintImageMaskXObjectGroup,
|
|
217
|
+
_pdf.OPS.paintImageMaskXObjectRepeat,
|
|
218
|
+
_pdf.OPS.paintImageXObject,
|
|
219
|
+
_pdf.OPS.paintImageXObjectRepeat,
|
|
220
|
+
_pdf.OPS.paintInlineImageXObject,
|
|
221
|
+
_pdf.OPS.paintInlineImageXObjectGroup,
|
|
222
|
+
_pdf.OPS.paintSolidColorImageMask
|
|
223
|
+
]);
|
|
224
|
+
function getPageImageSignals(page, textChars) {
|
|
201
225
|
return _async_to_generator(function() {
|
|
202
|
-
var _ref, fnArray, argsArray, i, _argsArray_i, w, h;
|
|
226
|
+
var _ref, fnArray, argsArray, hasImage, largeImage, i, _argsArray_i, w, h;
|
|
203
227
|
return _ts_generator(this, function(_state) {
|
|
204
228
|
switch(_state.label){
|
|
205
229
|
case 0:
|
|
@@ -209,22 +233,22 @@ function hasLargeImage(page) {
|
|
|
209
233
|
];
|
|
210
234
|
case 1:
|
|
211
235
|
_ref = _state.sent(), fnArray = _ref.fnArray, argsArray = _ref.argsArray;
|
|
236
|
+
hasImage = false;
|
|
237
|
+
largeImage = false;
|
|
212
238
|
for(i = 0; i < fnArray.length; i++){
|
|
239
|
+
if (IMAGE_PAINT_OPS.has(fnArray[i])) hasImage = true;
|
|
213
240
|
if (fnArray[i] !== _pdf.OPS.paintImageXObject) continue;
|
|
214
241
|
_argsArray_i = _sliced_to_array(argsArray[i], 3), w = _argsArray_i[1], h = _argsArray_i[2];
|
|
215
242
|
if (typeof w === 'number' && typeof h === 'number') {
|
|
216
|
-
if (w >= LARGE_IMAGE_MIN_PX && h >= LARGE_IMAGE_MIN_PX)
|
|
217
|
-
|
|
218
|
-
true
|
|
219
|
-
];
|
|
220
|
-
} else return [
|
|
221
|
-
2,
|
|
222
|
-
true
|
|
223
|
-
]; // dims unavailable: coarser "op present" signal
|
|
243
|
+
if (w >= LARGE_IMAGE_MIN_PX && h >= LARGE_IMAGE_MIN_PX) largeImage = true;
|
|
244
|
+
} else largeImage = true; // dims unavailable: coarser "op present" signal
|
|
224
245
|
}
|
|
225
246
|
return [
|
|
226
247
|
2,
|
|
227
|
-
|
|
248
|
+
{
|
|
249
|
+
hasImage: hasImage,
|
|
250
|
+
largeImage: textChars > 80 && largeImage
|
|
251
|
+
}
|
|
228
252
|
];
|
|
229
253
|
}
|
|
230
254
|
});
|
|
@@ -252,7 +276,7 @@ function pageCount(src) {
|
|
|
252
276
|
}
|
|
253
277
|
function collect(_0) {
|
|
254
278
|
return _async_to_generator(function(src) {
|
|
255
|
-
var _ref, first, last, doc, from, to, pages, heightChars, p, page, items, vp, glyphs, textChars, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, it, h,
|
|
279
|
+
var _ref, first, last, doc, from, to, pages, heightChars, p, page, items, vp, viewportScale, glyphs, textChars, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, it, _Math, _Math1, _Math2, _Math3, _Math4, _Math5, transform, baselineLength, w, h, baselineX, baselineY, corners, boundsX, boundsY, _ref1, hasImage, largeImage;
|
|
256
280
|
var _arguments = arguments;
|
|
257
281
|
return _ts_generator(this, function(_state) {
|
|
258
282
|
switch(_state.label){
|
|
@@ -273,7 +297,7 @@ function collect(_0) {
|
|
|
273
297
|
case 2:
|
|
274
298
|
if (!(p <= to)) return [
|
|
275
299
|
3,
|
|
276
|
-
|
|
300
|
+
7
|
|
277
301
|
];
|
|
278
302
|
return [
|
|
279
303
|
4,
|
|
@@ -290,22 +314,61 @@ function collect(_0) {
|
|
|
290
314
|
vp = page.getViewport({
|
|
291
315
|
scale: 1
|
|
292
316
|
});
|
|
317
|
+
viewportScale = Math.hypot(vp.transform[0], vp.transform[1]);
|
|
293
318
|
glyphs = [];
|
|
294
319
|
textChars = 0;
|
|
295
320
|
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
296
321
|
try {
|
|
297
322
|
for(_iterator = items[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
298
323
|
it = _step.value;
|
|
324
|
+
;
|
|
299
325
|
if (!('str' in it) || typeof it.str !== 'string') continue;
|
|
300
326
|
textChars += it.str.length;
|
|
301
327
|
if (!it.str.trim()) continue;
|
|
302
|
-
|
|
328
|
+
transform = _pdf.Util.transform(vp.transform, it.transform);
|
|
329
|
+
baselineLength = Math.hypot(transform[0], transform[1]);
|
|
330
|
+
w = Math.abs(it.width) * viewportScale;
|
|
331
|
+
h = Math.round(Math.hypot(transform[2], transform[3]) * 10) / 10;
|
|
332
|
+
baselineX = baselineLength > 0 ? transform[0] / baselineLength * w : w;
|
|
333
|
+
baselineY = baselineLength > 0 ? transform[1] / baselineLength * w : 0;
|
|
334
|
+
corners = [
|
|
335
|
+
[
|
|
336
|
+
transform[4],
|
|
337
|
+
transform[5]
|
|
338
|
+
],
|
|
339
|
+
[
|
|
340
|
+
transform[4] + baselineX,
|
|
341
|
+
transform[5] + baselineY
|
|
342
|
+
],
|
|
343
|
+
[
|
|
344
|
+
transform[4] + baselineX + transform[2],
|
|
345
|
+
transform[5] + baselineY + transform[3]
|
|
346
|
+
],
|
|
347
|
+
[
|
|
348
|
+
transform[4] + transform[2],
|
|
349
|
+
transform[5] + transform[3]
|
|
350
|
+
]
|
|
351
|
+
];
|
|
352
|
+
boundsX = corners.map(function(param) {
|
|
353
|
+
var _param = _sliced_to_array(param, 1), x = _param[0];
|
|
354
|
+
return x;
|
|
355
|
+
});
|
|
356
|
+
boundsY = corners.map(function(param) {
|
|
357
|
+
var _param = _sliced_to_array(param, 2), y = _param[1];
|
|
358
|
+
return y;
|
|
359
|
+
});
|
|
303
360
|
glyphs.push({
|
|
304
361
|
s: it.str,
|
|
305
|
-
x:
|
|
306
|
-
y:
|
|
307
|
-
w:
|
|
308
|
-
h: h
|
|
362
|
+
x: transform[4],
|
|
363
|
+
y: transform[5],
|
|
364
|
+
w: w,
|
|
365
|
+
h: h,
|
|
366
|
+
bounds: {
|
|
367
|
+
x: (_Math = Math).min.apply(_Math, _to_consumable_array(boundsX)),
|
|
368
|
+
y: (_Math1 = Math).min.apply(_Math1, _to_consumable_array(boundsY)),
|
|
369
|
+
w: (_Math2 = Math).max.apply(_Math2, _to_consumable_array(boundsX)) - (_Math3 = Math).min.apply(_Math3, _to_consumable_array(boundsX)),
|
|
370
|
+
h: (_Math4 = Math).max.apply(_Math4, _to_consumable_array(boundsY)) - (_Math5 = Math).min.apply(_Math5, _to_consumable_array(boundsY))
|
|
371
|
+
}
|
|
309
372
|
});
|
|
310
373
|
heightChars.set(h, (heightChars.get(h) || 0) + it.str.length);
|
|
311
374
|
}
|
|
@@ -323,36 +386,29 @@ function collect(_0) {
|
|
|
323
386
|
}
|
|
324
387
|
}
|
|
325
388
|
}
|
|
326
|
-
_tmp = textChars > 80;
|
|
327
|
-
if (!_tmp) return [
|
|
328
|
-
3,
|
|
329
|
-
6
|
|
330
|
-
];
|
|
331
389
|
return [
|
|
332
390
|
4,
|
|
333
|
-
|
|
391
|
+
getPageImageSignals(page, textChars)
|
|
334
392
|
];
|
|
335
393
|
case 5:
|
|
336
|
-
|
|
337
|
-
_state.label = 6;
|
|
338
|
-
case 6:
|
|
339
|
-
largeImage = _tmp;
|
|
394
|
+
_ref1 = _state.sent(), hasImage = _ref1.hasImage, largeImage = _ref1.largeImage;
|
|
340
395
|
pages.push({
|
|
341
396
|
page: p,
|
|
342
397
|
glyphs: glyphs,
|
|
343
398
|
width: vp.width,
|
|
344
399
|
height: vp.height,
|
|
345
400
|
textChars: textChars,
|
|
401
|
+
hasImage: hasImage,
|
|
346
402
|
largeImage: largeImage
|
|
347
403
|
});
|
|
348
|
-
_state.label =
|
|
349
|
-
case
|
|
404
|
+
_state.label = 6;
|
|
405
|
+
case 6:
|
|
350
406
|
p++;
|
|
351
407
|
return [
|
|
352
408
|
3,
|
|
353
409
|
2
|
|
354
410
|
];
|
|
355
|
-
case
|
|
411
|
+
case 7:
|
|
356
412
|
// `numPages` is the SELECTED count, not doc.numPages: the auto-OCR
|
|
357
413
|
// chars/page floor must judge a `--pages` subset against its own page count, or a genuinely image-only page reads as merely "sparse".
|
|
358
414
|
return [
|
package/dist/cjs/collect.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/collect.ts"],"sourcesContent":["// Pass 1: load the PDF and collect positioned glyphs per page, plus the\n// document-wide char-weighted font-height histogram. Heights round to 0.1pt for stable histogram keys; y is flipped so sorting ascending reads top-down.\nimport type { PDFPageProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { OPS } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { openDocument } from './pdf-open.ts';\nimport type { CollectedPage, CollectResult, Glyph } from './types.ts';\n\n// HYBRID-PAGE DETECTION (README's \"Design contract\"): a page can carry a\n// real text layer AND a separately-painted large image with its OWN baked-in text that `page.getTextContent()` never sees.\n//\n// Signal: scan the operator list for `OPS.paintImageXObject`, whose args are\n// `[objId, width, height]` — available synchronously, `large` is >=200x200px. Masks/nested Form XObjects aren't walked (accepted gap); missing dims fall back to \"op present\" as the signal.\nconst LARGE_IMAGE_MIN_PX = 200;\nasync function hasLargeImage(page: PDFPageProxy): Promise<boolean> {\n const { fnArray, argsArray } = await page.getOperatorList();\n for (let i = 0; i < fnArray.length; i++) {\n if (fnArray[i] !== OPS.paintImageXObject) continue;\n const [, w, h] = argsArray[i];\n if (typeof w === 'number' && typeof h === 'number') {\n if (w >= LARGE_IMAGE_MIN_PX && h >= LARGE_IMAGE_MIN_PX) return true;\n } else return true; // dims unavailable: coarser \"op present\" signal\n }\n return false;\n}\n\n// { first, last }: 1-based, inclusive page range (the CLI's --pages).\n// Out-of-range values CLAMP to the document's bounds; `page` on each returned entry is always the SOURCE page number, so markers/splicing stay correct.\n//\n// CAVEAT: `heightChars` is built ONLY from the pages actually collected, so\n// a `--pages` subset's heading levels can differ from a full run's — accepted since re-extracted pages get spliced back in for their prose, not diffed.\n\n// Cheap page-count-only load (no per-page text extraction) — the OCR path\n// needs it to resolve its own [first, last] clamping and print a \"### pN\" marker for every page in range, including ones with no recognized words.\nexport async function pageCount(src: string): Promise<number> {\n const doc = await openDocument(src);\n return doc.numPages;\n}\n\nexport async function collect(src: string, { first, last }: { first?: number; last?: number } = {}): Promise<CollectResult> {\n const doc = await openDocument(src);\n const from = Math.min(Math.max(1, first ?? 1), Math.max(1, doc.numPages));\n const to = Math.min(Math.max(from, last ?? doc.numPages), doc.numPages);\n const pages: CollectedPage[] = [];\n const heightChars = new Map<number, number>();\n for (let p = from; p <= to; p++) {\n const page = await doc.getPage(p);\n const { items } = await page.getTextContent();\n const vp = page.getViewport({ scale: 1 });\n const glyphs: Glyph[] = [];\n let textChars = 0;\n for (const it of items) {\n if (!('str' in it) || typeof it.str !== 'string') continue;\n textChars += it.str.length;\n if (!it.str.trim()) continue;\n const h = Math.round(Math.abs(it.transform[3]) * 10) / 10;\n glyphs.push({\n s: it.str,\n x: it.transform[4],\n y: vp.height - it.transform[5],\n w: it.width,\n h,\n });\n heightChars.set(h, (heightChars.get(h) || 0) + it.str.length);\n }\n // \"text layer used\" for hybrid-page purposes (page text > 80 chars) — a\n // page with only a couple of stray scraps isn't \"using\" the text layer even if it also carries a large image.\n const largeImage = textChars > 80 && (await hasLargeImage(page));\n pages.push({\n page: p,\n glyphs,\n width: vp.width,\n height: vp.height,\n textChars,\n largeImage,\n });\n }\n // `numPages` is the SELECTED count, not doc.numPages: the auto-OCR\n // chars/page floor must judge a `--pages` subset against its own page count, or a genuinely image-only page reads as merely \"sparse\".\n return { numPages: pages.length, pages, heightChars };\n}\n"],"names":["collect","pageCount","LARGE_IMAGE_MIN_PX","hasLargeImage","page","fnArray","argsArray","i","w","h","getOperatorList","length","OPS","paintImageXObject","src","doc","openDocument","numPages","first","last","from","to","pages","heightChars","p","items","vp","glyphs","textChars","it","largeImage","Math","min","max","Map","getPage","getTextContent","getViewport","scale","str","trim","round","abs","transform","push","s","x","y","height","width","set","get"],"mappings":"AAAA,wEAAwE;AACxE,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AY2B,GAAG,CAACzC,GAAG,AAACc,CAAAA,YAAY4B,GAAG,CAAC1C,MAAM,CAAA,IAAKoB,GAAGU,GAAG,CAAC5B,MAAM;wBAC9D;;wBAbK;wBAAA;;;iCAAA,6BAAA;gCAAA;;;gCAAA;sCAAA;;;;2BAgBciB,YAAY;+BAAZA;;;;oBAAmB;;wBAAMzB,cAAcC;;;2BAApB;;;oBAAhC0B;oBACNR,MAAMsB,IAAI,CAAC;wBACTxC,MAAMoB;wBACNG,QAAAA;wBACAsB,OAAOvB,GAAGuB,KAAK;wBACfD,QAAQtB,GAAGsB,MAAM;wBACjBpB,WAAAA;wBACAE,YAAAA;oBACF;;;oBA9B0BN;;;;;;oBAgC5B,mEAAmE;oBACnE,sIAAsI;oBACtI;;wBAAO;4BAAEP,UAAUK,MAAMX,MAAM;4BAAEW,OAAAA;4BAAOC,aAAAA;wBAAY;;;;IACtD"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/collect.ts"],"sourcesContent":["// Pass 1: load each PDF text run into viewport points, plus the document-wide\n// char-weighted font-height histogram. Height rounds to 0.1pt for stable histogram keys.\nimport type { PDFPageProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { OPS, Util } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { openDocument } from './pdf-open.ts';\nimport type { CollectedPage, CollectResult, Glyph } from './types.ts';\n\n// HYBRID-PAGE DETECTION (README's \"Design contract\"): a page can carry a\n// real text layer AND a separately-painted large image with its OWN baked-in text that `page.getTextContent()` never sees.\n//\n// Image presence uses the paint operators for XObjects, inline images, and masks.\n// The large-image threshold uses `paintImageXObject` dimensions when available.\nconst LARGE_IMAGE_MIN_PX = 200;\ninterface PageImageSignals {\n hasImage: boolean;\n largeImage: boolean;\n}\n\nconst IMAGE_PAINT_OPS = new Set([OPS.paintImageMaskXObject, OPS.paintImageMaskXObjectGroup, OPS.paintImageMaskXObjectRepeat, OPS.paintImageXObject, OPS.paintImageXObjectRepeat, OPS.paintInlineImageXObject, OPS.paintInlineImageXObjectGroup, OPS.paintSolidColorImageMask]);\n\nasync function getPageImageSignals(page: PDFPageProxy, textChars: number): Promise<PageImageSignals> {\n const { fnArray, argsArray } = await page.getOperatorList();\n let hasImage = false;\n let largeImage = false;\n for (let i = 0; i < fnArray.length; i++) {\n if (IMAGE_PAINT_OPS.has(fnArray[i])) hasImage = true;\n if (fnArray[i] !== OPS.paintImageXObject) continue;\n const [, w, h] = argsArray[i];\n if (typeof w === 'number' && typeof h === 'number') {\n if (w >= LARGE_IMAGE_MIN_PX && h >= LARGE_IMAGE_MIN_PX) largeImage = true;\n } else largeImage = true; // dims unavailable: coarser \"op present\" signal\n }\n return { hasImage, largeImage: textChars > 80 && largeImage };\n}\n\n// { first, last }: 1-based, inclusive page range (the CLI's --pages).\n// Out-of-range values CLAMP to the document's bounds; `page` on each returned entry is always the SOURCE page number, so markers/splicing stay correct.\n//\n// CAVEAT: `heightChars` is built ONLY from the pages actually collected, so\n// a `--pages` subset's heading levels can differ from a full run's — accepted since re-extracted pages get spliced back in for their prose, not diffed.\n\n// Cheap page-count-only load (no per-page text extraction) — the OCR path\n// needs it to resolve its own [first, last] clamping and print a \"### pN\" marker for every page in range, including ones with no recognized words.\nexport async function pageCount(src: string): Promise<number> {\n const doc = await openDocument(src);\n return doc.numPages;\n}\n\nexport async function collect(src: string, { first, last }: { first?: number; last?: number } = {}): Promise<CollectResult> {\n const doc = await openDocument(src);\n const from = Math.min(Math.max(1, first ?? 1), Math.max(1, doc.numPages));\n const to = Math.min(Math.max(from, last ?? doc.numPages), doc.numPages);\n const pages: CollectedPage[] = [];\n const heightChars = new Map<number, number>();\n for (let p = from; p <= to; p++) {\n const page = await doc.getPage(p);\n const { items } = await page.getTextContent();\n const vp = page.getViewport({ scale: 1 });\n const viewportScale = Math.hypot(vp.transform[0], vp.transform[1]);\n const glyphs: Glyph[] = [];\n let textChars = 0;\n for (const it of items) {\n if (!('str' in it) || typeof it.str !== 'string') continue;\n textChars += it.str.length;\n if (!it.str.trim()) continue;\n const transform = Util.transform(vp.transform, it.transform);\n const baselineLength = Math.hypot(transform[0], transform[1]);\n const w = Math.abs(it.width) * viewportScale;\n const h = Math.round(Math.hypot(transform[2], transform[3]) * 10) / 10;\n const baselineX = baselineLength > 0 ? (transform[0] / baselineLength) * w : w;\n const baselineY = baselineLength > 0 ? (transform[1] / baselineLength) * w : 0;\n const corners = [\n [transform[4], transform[5]],\n [transform[4] + baselineX, transform[5] + baselineY],\n [transform[4] + baselineX + transform[2], transform[5] + baselineY + transform[3]],\n [transform[4] + transform[2], transform[5] + transform[3]],\n ];\n const boundsX = corners.map(([x]) => x);\n const boundsY = corners.map(([, y]) => y);\n glyphs.push({\n s: it.str,\n x: transform[4],\n y: transform[5],\n w,\n h,\n bounds: {\n x: Math.min(...boundsX),\n y: Math.min(...boundsY),\n w: Math.max(...boundsX) - Math.min(...boundsX),\n h: Math.max(...boundsY) - Math.min(...boundsY),\n },\n });\n heightChars.set(h, (heightChars.get(h) || 0) + it.str.length);\n }\n // \"text layer used\" for hybrid-page purposes (page text > 80 chars) — a\n // page with only a couple of stray scraps isn't \"using\" the text layer even if it also carries a large image.\n const { hasImage, largeImage } = await getPageImageSignals(page, textChars);\n pages.push({\n page: p,\n glyphs,\n width: vp.width,\n height: vp.height,\n textChars,\n hasImage,\n largeImage,\n });\n }\n // `numPages` is the SELECTED count, not doc.numPages: the auto-OCR\n // chars/page floor must judge a `--pages` subset against its own page count, or a genuinely image-only page reads as merely \"sparse\".\n return { numPages: pages.length, pages, heightChars };\n}\n"],"names":["collect","pageCount","LARGE_IMAGE_MIN_PX","IMAGE_PAINT_OPS","Set","OPS","paintImageMaskXObject","paintImageMaskXObjectGroup","paintImageMaskXObjectRepeat","paintImageXObject","paintImageXObjectRepeat","paintInlineImageXObject","paintInlineImageXObjectGroup","paintSolidColorImageMask","getPageImageSignals","page","textChars","fnArray","argsArray","hasImage","largeImage","i","w","h","getOperatorList","length","has","src","doc","openDocument","numPages","first","last","from","to","pages","heightChars","p","items","vp","viewportScale","glyphs","it","Math","transform","baselineLength","baselineX","baselineY","corners","boundsX","boundsY","min","max","Map","getPage","getTextContent","getViewport","scale","hypot","str","trim","Util","abs","width","round","map","x","y","push","s","bounds","set","get","height"],"mappings":"AAAA,8EAA8E;AAC9E,yFAAyF;;;;;;;;;;;;QA+CnEA;eAAAA;;QALAC;eAAAA;;;mBAxCI;yBACG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAG7B,yEAAyE;AACzE,2HAA2H;AAC3H,EAAE;AACF,kFAAkF;AAClF,gFAAgF;AAChF,IAAMC,qBAAqB;AAM3B,IAAMC,kBAAkB,IAAIC,IAAI;IAACC,QAAG,CAACC,qBAAqB;IAAED,QAAG,CAACE,0BAA0B;IAAEF,QAAG,CAACG,2BAA2B;IAAEH,QAAG,CAACI,iBAAiB;IAAEJ,QAAG,CAACK,uBAAuB;IAAEL,QAAG,CAACM,uBAAuB;IAAEN,QAAG,CAACO,4BAA4B;IAAEP,QAAG,CAACQ,wBAAwB;CAAC;AAE7Q,SAAeC,oBAAoBC,IAAkB,EAAEC,SAAiB;;YACvC,MAAvBC,SAASC,WACbC,UACAC,YACKC,GAGUH,cAARI,GAAGC;;;;oBANiB;;wBAAMR,KAAKS,eAAe;;;oBAA1B,OAAA,eAAvBP,UAAuB,KAAvBA,SAASC,YAAc,KAAdA;oBACbC,WAAW;oBACXC,aAAa;oBACjB,IAASC,IAAI,GAAGA,IAAIJ,QAAQQ,MAAM,EAAEJ,IAAK;wBACvC,IAAIlB,gBAAgBuB,GAAG,CAACT,OAAO,CAACI,EAAE,GAAGF,WAAW;wBAChD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NI,QAAAA;wBACAsB,OAAOxB,GAAGwB,KAAK;wBACfU,QAAQlC,GAAGkC,MAAM;wBACjBzD,WAAAA;wBACAG,UAAAA;wBACAC,YAAAA;oBACF;;;oBAnD0BiB;;;;;;oBAqD5B,mEAAmE;oBACnE,sIAAsI;oBACtI;;wBAAO;4BAAEP,UAAUK,MAAMV,MAAM;4BAAEU,OAAAA;4BAAOC,aAAAA;wBAAY;;;;IACtD"}
|
package/dist/cjs/errors.d.cts
CHANGED
|
@@ -11,6 +11,8 @@ export type PdfToMdErrorCode =
|
|
|
11
11
|
| 'OCR_PAGE_FAILED'
|
|
12
12
|
/** malformed word-dump input (--words-json / extractOcr words) */
|
|
13
13
|
| 'WORDS_INPUT'
|
|
14
|
+
/** malformed or insufficient evidence passed to auditWords */
|
|
15
|
+
| 'AUDIT_INPUT'
|
|
14
16
|
/** malformed Analysis handed to toMarkdown/toText (e.g. an out-of-range
|
|
15
17
|
* wordIndexes entry) — validated fail-fast, whole object checked before any output. */
|
|
16
18
|
| 'ANALYSIS_INPUT';
|
package/dist/cjs/errors.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export type PdfToMdErrorCode =
|
|
|
11
11
|
| 'OCR_PAGE_FAILED'
|
|
12
12
|
/** malformed word-dump input (--words-json / extractOcr words) */
|
|
13
13
|
| 'WORDS_INPUT'
|
|
14
|
+
/** malformed or insufficient evidence passed to auditWords */
|
|
15
|
+
| 'AUDIT_INPUT'
|
|
14
16
|
/** malformed Analysis handed to toMarkdown/toText (e.g. an out-of-range
|
|
15
17
|
* wordIndexes entry) — validated fail-fast, whole object checked before any output. */
|
|
16
18
|
| 'ANALYSIS_INPUT';
|
package/dist/cjs/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/errors.ts"],"sourcesContent":["// Library code throws; only src/cli.ts prints and exits.\n\n/** The closed set of error codes every `PdfToMdError` carries — switch on\n * `code` instead of parsing `message`. */\nexport type PdfToMdErrorCode =\n /** input missing/unreadable/not a PDF */\n | 'PDF_OPEN'\n /** malformed page selection (validated before any work) */\n | 'PAGE_RANGE'\n /** rendering an opened, in-range page to PNG failed */\n | 'PAGE_RENDER'\n /** the OCR engine crashed twice on one page (message names it) */\n | 'OCR_PAGE_FAILED'\n /** malformed word-dump input (--words-json / extractOcr words) */\n | 'WORDS_INPUT'\n /** malformed Analysis handed to toMarkdown/toText (e.g. an out-of-range\n * wordIndexes entry) — validated fail-fast, whole object checked before any output. */\n | 'ANALYSIS_INPUT';\n\n/** The error every entry point throws — never a bare `Error`. */\nexport class PdfToMdError extends Error {\n code: PdfToMdErrorCode;\n\n constructor(code: PdfToMdErrorCode, message: string) {\n super(message);\n this.name = 'PdfToMdError';\n this.code = code;\n }\n}\n"],"names":["PdfToMdError","code","message","name","Error"],"mappings":"AAAA,yDAAyD;AAEzD;wCACwC;;;;+
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/errors.ts"],"sourcesContent":["// Library code throws; only src/cli.ts prints and exits.\n\n/** The closed set of error codes every `PdfToMdError` carries — switch on\n * `code` instead of parsing `message`. */\nexport type PdfToMdErrorCode =\n /** input missing/unreadable/not a PDF */\n | 'PDF_OPEN'\n /** malformed page selection (validated before any work) */\n | 'PAGE_RANGE'\n /** rendering an opened, in-range page to PNG failed */\n | 'PAGE_RENDER'\n /** the OCR engine crashed twice on one page (message names it) */\n | 'OCR_PAGE_FAILED'\n /** malformed word-dump input (--words-json / extractOcr words) */\n | 'WORDS_INPUT'\n /** malformed or insufficient evidence passed to auditWords */\n | 'AUDIT_INPUT'\n /** malformed Analysis handed to toMarkdown/toText (e.g. an out-of-range\n * wordIndexes entry) — validated fail-fast, whole object checked before any output. */\n | 'ANALYSIS_INPUT';\n\n/** The error every entry point throws — never a bare `Error`. */\nexport class PdfToMdError extends Error {\n code: PdfToMdErrorCode;\n\n constructor(code: PdfToMdErrorCode, message: string) {\n super(message);\n this.name = 'PdfToMdError';\n this.code = code;\n }\n}\n"],"names":["PdfToMdError","code","message","name","Error"],"mappings":"AAAA,yDAAyD;AAEzD;wCACwC;;;;+BAmB3BA;;;eAAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAN,IAAA,AAAMA,6BAAN;;cAAMA;aAAAA,aAGCC,IAAsB,EAAEC,OAAe;gCAHxCF;;gBAIT,kBAJSA;YAIHE;;QACN,MAAKC,IAAI,GAAG;QACZ,MAAKF,IAAI,GAAGA;;;WANHD;qBAAqBI"}
|