@effortlessmotion/pdf-to-md 0.1.4 → 0.1.6

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.
@@ -1,5 +1,6 @@
1
1
  // audit command — the severity-1 audit instrument (README's "Design
2
2
  // contract") as a first-class subcommand. Thin wrapper over src/audit.ts's auditWords (the library returns data, only the CLI prints and exits).
3
+ import { auditWords } from '../audit.js';
3
4
  const USAGE = 'usage: pdf-to-md audit <words.jsonl> <file.md>';
4
5
  const audit = async (ctx)=>{
5
6
  if (ctx.rest.includes('-h') || ctx.rest.includes('--help')) {
@@ -8,9 +9,6 @@ const audit = async (ctx)=>{
8
9
  }
9
10
  const [wordsPath, mdPath] = ctx.rest;
10
11
  if (!wordsPath || !mdPath) ctx.usageError(USAGE);
11
- // Imported here, not at module top, same lazy-load rule as every other
12
- // command — `pdf-to-md audit` should never pay for what the other commands need.
13
- const { auditWords } = await import('../audit.js');
14
12
  const result = auditWords(wordsPath, mdPath);
15
13
  for (const line of result.lines)console.log(line);
16
14
  // MISSING>0 is the severity-1 fail signal: a recognized word this markdown
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/cli/audit.ts"],"sourcesContent":["// audit command — the severity-1 audit instrument (README's \"Design\n// contract\") as a first-class subcommand. Thin wrapper over src/audit.ts's auditWords (the library returns data, only the CLI prints and exits).\nimport type { Command } from './types.ts';\n\nconst USAGE = 'usage: pdf-to-md audit <words.jsonl> <file.md>';\n\nconst audit: Command = async (ctx) => {\n if (ctx.rest.includes('-h') || ctx.rest.includes('--help')) {\n console.log(USAGE);\n return;\n }\n const [wordsPath, mdPath] = ctx.rest;\n if (!wordsPath || !mdPath) ctx.usageError(USAGE);\n\n // Imported here, not at module top, same lazy-load rule as every other\n // command — `pdf-to-md audit` should never pay for what the other commands need.\n const { auditWords } = await import('../audit.ts');\n const result = auditWords(wordsPath, mdPath);\n for (const line of result.lines) console.log(line);\n\n // MISSING>0 is the severity-1 fail signal: a recognized word this markdown\n // lost. Exit 1 so the subcommand is usable as a CI gate; strict_deficits from hyphen-join repairs are reported but not failures.\n if (result.missing > 0) process.exitCode = 1;\n};\n\nexport default audit;\n"],"names":["USAGE","audit","ctx","rest","includes","console","log","wordsPath","mdPath","usageError","auditWords","result","line","lines","missing","process","exitCode"],"mappings":"AAAA,oEAAoE;AACpE,iJAAiJ;AAGjJ,MAAMA,QAAQ;AAEd,MAAMC,QAAiB,OAAOC;IAC5B,IAAIA,IAAIC,IAAI,CAACC,QAAQ,CAAC,SAASF,IAAIC,IAAI,CAACC,QAAQ,CAAC,WAAW;QAC1DC,QAAQC,GAAG,CAACN;QACZ;IACF;IACA,MAAM,CAACO,WAAWC,OAAO,GAAGN,IAAIC,IAAI;IACpC,IAAI,CAACI,aAAa,CAACC,QAAQN,IAAIO,UAAU,CAACT;IAE1C,uEAAuE;IACvE,iFAAiF;IACjF,MAAM,EAAEU,UAAU,EAAE,GAAG,MAAM,MAAM,CAAC;IACpC,MAAMC,SAASD,WAAWH,WAAWC;IACrC,KAAK,MAAMI,QAAQD,OAAOE,KAAK,CAAER,QAAQC,GAAG,CAACM;IAE7C,2EAA2E;IAC3E,iIAAiI;IACjI,IAAID,OAAOG,OAAO,GAAG,GAAGC,QAAQC,QAAQ,GAAG;AAC7C;AAEA,eAAef,MAAM"}
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/cli/audit.ts"],"sourcesContent":["// audit command — the severity-1 audit instrument (README's \"Design\n// contract\") as a first-class subcommand. Thin wrapper over src/audit.ts's auditWords (the library returns data, only the CLI prints and exits).\nimport { auditWords } from '../audit.ts';\nimport type { Command } from './types.ts';\n\nconst USAGE = 'usage: pdf-to-md audit <words.jsonl> <file.md>';\n\nconst audit: Command = async (ctx) => {\n if (ctx.rest.includes('-h') || ctx.rest.includes('--help')) {\n console.log(USAGE);\n return;\n }\n const [wordsPath, mdPath] = ctx.rest;\n if (!wordsPath || !mdPath) ctx.usageError(USAGE);\n\n const result = auditWords(wordsPath, mdPath);\n for (const line of result.lines) console.log(line);\n\n // MISSING>0 is the severity-1 fail signal: a recognized word this markdown\n // lost. Exit 1 so the subcommand is usable as a CI gate; strict_deficits from hyphen-join repairs are reported but not failures.\n if (result.missing > 0) process.exitCode = 1;\n};\n\nexport default audit;\n"],"names":["auditWords","USAGE","audit","ctx","rest","includes","console","log","wordsPath","mdPath","usageError","result","line","lines","missing","process","exitCode"],"mappings":"AAAA,oEAAoE;AACpE,iJAAiJ;AACjJ,SAASA,UAAU,QAAQ,cAAc;AAGzC,MAAMC,QAAQ;AAEd,MAAMC,QAAiB,OAAOC;IAC5B,IAAIA,IAAIC,IAAI,CAACC,QAAQ,CAAC,SAASF,IAAIC,IAAI,CAACC,QAAQ,CAAC,WAAW;QAC1DC,QAAQC,GAAG,CAACN;QACZ;IACF;IACA,MAAM,CAACO,WAAWC,OAAO,GAAGN,IAAIC,IAAI;IACpC,IAAI,CAACI,aAAa,CAACC,QAAQN,IAAIO,UAAU,CAACT;IAE1C,MAAMU,SAASX,WAAWQ,WAAWC;IACrC,KAAK,MAAMG,QAAQD,OAAOE,KAAK,CAAEP,QAAQC,GAAG,CAACK;IAE7C,2EAA2E;IAC3E,iIAAiI;IACjI,IAAID,OAAOG,OAAO,GAAG,GAAGC,QAAQC,QAAQ,GAAG;AAC7C;AAEA,eAAed,MAAM"}
@@ -3,6 +3,9 @@
3
3
  import { readFileSync, writeFileSync } from 'node:fs';
4
4
  import path from 'node:path';
5
5
  import { parseArgs } from 'node:util';
6
+ import { analyze } from '../analyze.js';
7
+ import { extractOcr, needsOcrFallback, ocrFallbackNotice, pdfToMarkdown } from '../extract.js';
8
+ import { toText } from '../render-analysis.js';
6
9
  const USAGE = 'usage: pdf-to-md [extract] <file.pdf> [out.md] [--json] [--page-markers] [--ocr] [--no-ocr] ' + '[--pages N[-M]] [--stdout] [--words-json=FILE] [--debug-words=FILE] ' + '[--format md|txt|raw (default md; raw = Analysis JSON incl. the report; txt = reading-order plain text)] ' + '[--dpi=N (OCR render resolution, default 288; raise for tiny print or chart/label pages, ' + '>~400 measured worse on ordinary prose pages)] ' + '[--float-max-words=N] [--float-max-width=F] [--float-margin=F] [--para-gap=F] [--heading-scale=F] ' + '[--col-max-width=F] [--col-split=F] [--line-y-tol=F] [--line-height-ratio=F]';
7
10
  const FORMATS = [
8
11
  'md',
@@ -220,9 +223,6 @@ const extract = async (ctx)=>{
220
223
  let content;
221
224
  let report;
222
225
  if (format === 'md') {
223
- // Unchanged path: extractOcr/pdfToMarkdown directly, exactly as before
224
- // --format existed.
225
- const { extractOcr, pdfToMarkdown } = await import('../extract.js');
226
226
  let markdown;
227
227
  if (words !== undefined) {
228
228
  ({ markdown, report } = await extractOcr({
@@ -240,9 +240,6 @@ const extract = async (ctx)=>{
240
240
  } else {
241
241
  // raw/txt: both render from an Analysis — raw IS the Analysis as JSON;
242
242
  // txt is toText(analysis). Mirrors pdfToMarkdown's own auto-OCR fallback decision so a different --format doesn't silently pick a different path.
243
- const { analyze } = await import('../analyze.js');
244
- const { toText } = await import('../render-analysis.js');
245
- const { needsOcrFallback, ocrFallbackNotice } = await import('../extract.js');
246
243
  let analysis;
247
244
  if (words !== undefined) {
248
245
  analysis = await analyze({
@@ -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 type { OcrWordInput } from '../extract.ts';\nimport type { Tuning } from '../geometry.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 // Unchanged path: extractOcr/pdfToMarkdown directly, exactly as before\n // --format existed.\n const { extractOcr, pdfToMarkdown } = await import('../extract.ts');\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 const { analyze } = await import('../analyze.ts');\n const { toText } = await import('../render-analysis.ts');\n const { needsOcrFallback, ocrFallbackNotice } = await import('../extract.ts');\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":["readFileSync","writeFileSync","path","parseArgs","USAGE","FORMATS","TUNING_FLAGS","name","key","allowZero","isInt","extract","ctx","rest","includes","usageError","parsed","args","options","json","type","default","ocr","stdout","help","short","pages","dpi","format","Object","fromEntries","map","f","allowPositionals","err","message","values","positionals","console","log","src","outArg","wordsPath","formatRaw","join","JSON","stringify","undefined","m","exec","first","Number","NaN","last","tuning","spec","raw","value","ok","isFinite","Math","trunc","source","defaultExt","dest","replace","onWarning","error","showProgress","process","stderr","isTTY","onProgress","e","page","words","ms","debugWordsPath","trim","onWords","lines","w","entry","text","x","y","h","confidence","length","common","ocrCommon","skipped","parseWordLines","content","report","extractOcr","pdfToMarkdown","markdown","noOcr","pageMarkers","analyze","toText","needsOcrFallback","ocrFallbackNotice","analysis","charsPerPage","fallback","write","out","basename","padEnd","headings","junkHeadings","paragraphs","callouts","floats","danglingLong","bodyFontHeight","listItems","joinedPairs","sparse","line","split","push","parse"],"mappings":"AAAA,2EAA2E;AAC3E,qHAA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OAAOmB,UAAU,CAAC,UAAU,EAAEnB,OAAOoB,QAAQ,CAAC,QAAQ,GAAEpB,iBAAAA,OAAOqB,MAAM,cAAbrB,4BAAAA,iBAAiB,EAAE,UAAU,EAAEA,OAAOsB,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAEtB,OAAOW,YAAY,CAAC,WAAW,CAAC;IAC1R,OAAO;QACLnE,QAAQ6B,KAAK,CACX,GAAGjE,KAAK2G,QAAQ,CAAC7C,MAAM8C,MAAM,CAAC,IAAI,MAAM,EAAEhB,OAAOuB,cAAc,CAAC,SAAS,EAAEvB,OAAOiB,QAAQ,CAAC,MAAM,EAAEjB,OAAOkB,YAAY,CAAC,EAAE,CAAC,GACxH,CAAC,MAAM,EAAElB,OAAOwB,SAAS,CAAC,OAAO,EAAExB,OAAOmB,UAAU,CAAC,UAAU,EAAEnB,OAAOoB,QAAQ,CAAC,OAAO,EAAEpB,OAAOyB,WAAW,CAAC,CAAC,CAAC,GAC/G,CAAC,CAAC,EAAEzB,OAAOW,YAAY,CAAC,WAAW,CAAC,GACnCX,CAAAA,OAAO0B,MAAM,GAAG,0EAA0E,EAAC;IAElG;AACF;AAEA,8EAA8E;AAC9E,wGAAwG;AACxG,SAAS5B,eAAepC,GAAW;IACjC,MAAMmB,QAAwB,EAAE;IAChC,IAAIgB,UAAU;IACd,KAAK,MAAM8B,QAAQjE,IAAIkE,KAAK,CAAC,MAAO;QAClC,IAAI,CAACD,KAAK3C,IAAI,IAAI;QAClB,IAAI;YACFH,MAAMgD,IAAI,CAAC9E,KAAK+E,KAAK,CAACH;QACxB,EAAE,OAAM;YACN9B;QACF;IACF;IACA,OAAO;QAAEhB;QAAOgB;IAAQ;AAC1B;AAEA,eAAehF,QAAQ"}
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":["readFileSync","writeFileSync","path","parseArgs","analyze","extractOcr","needsOcrFallback","ocrFallbackNotice","pdfToMarkdown","toText","USAGE","FORMATS","TUNING_FLAGS","name","key","allowZero","isInt","extract","ctx","rest","includes","usageError","parsed","args","options","json","type","default","ocr","stdout","help","short","pages","dpi","format","Object","fromEntries","map","f","allowPositionals","err","message","values","positionals","console","log","src","outArg","wordsPath","formatRaw","join","JSON","stringify","undefined","m","exec","first","Number","NaN","last","tuning","spec","raw","value","ok","isFinite","Math","trunc","source","defaultExt","dest","replace","onWarning","error","showProgress","process","stderr","isTTY","onProgress","e","page","words","ms","debugWordsPath","trim","onWords","lines","w","entry","text","x","y","h","confidence","length","common","ocrCommon","skipped","parseWordLines","content","report","markdown","noOcr","pageMarkers","analysis","charsPerPage","fallback","write","out","basename","padEnd","headings","junkHeadings","paragraphs","callouts","floats","danglingLong","bodyFontHeight","listItems","joinedPairs","sparse","line","split","push","parse"],"mappings":"AAAA,2EAA2E;AAC3E,qHAAqH;AACrH,SAASA,YAAY,EAAEC,aAAa,QAAQ,UAAU;AACtD,OAAOC,UAAU,YAAY;AAC7B,SAASC,SAAS,QAAQ,YAAY;AACtC,SAASC,OAAO,QAAQ,gBAAgB;AAExC,SAASC,UAAU,EAAEC,gBAAgB,EAAEC,iBAAiB,EAAEC,aAAa,QAAQ,gBAAgB;AAE/F,SAASC,MAAM,QAAQ,wBAAwB;AAI/C,MAAMC,QACJ,iGACA,yEACA,8GACA,8FACA,oDACA,uGACA;AAEF,MAAMC,UAAU;IAAC;IAAM;IAAO;CAAM;AAGpC,mHAAmH;AACnH,MAAMC,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,MAAMG,UAAmB,OAAOC;IAC9B,uEAAuE;IACvE,kGAAkG;IAClG,IAAIA,IAAIC,IAAI,CAACC,QAAQ,CAAC,kBAAkB;QACtCF,IAAIG,UAAU,CAAC,GAAGX,MAAM,oDAAoD,CAAC;IAC/E;IAEA,MAAMY,SAAS,AAAC,CAAA;QACd,IAAI;YACF,OAAOnB,UAAU;gBACfoB,MAAML,IAAIC,IAAI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node:util';
6
6
  import { PdfToMdError } from '../errors.js';
7
+ import { awaitPdfOpen, openPdfForRender } from '../pdf-open.js';
8
+ import { renderLoadedPage } from '../raster.js';
7
9
  const USAGE = 'usage: pdf-to-md render <file.pdf> <page>[-<page>] [--dpi|-d N (default 288)] [--out|-o DIR (default .)]';
8
10
  const render = async (ctx)=>{
9
11
  var _values_out;
@@ -53,9 +55,6 @@ const render = async (ctx)=>{
53
55
  recursive: true
54
56
  });
55
57
  const stem = path.basename(pdfPath, path.extname(pdfPath)).replaceAll(' ', '-');
56
- // Imported here, not at module top: keeps pdfjs/canvas out of every other command's startup.
57
- const { awaitPdfOpen, openPdfForRender } = await import('../pdf-open.js');
58
- const { renderLoadedPage } = await import('../raster.js');
59
58
  const loadingTask = openPdfForRender(pdfPath);
60
59
  try {
61
60
  const doc = await awaitPdfOpen(loadingTask, pdfPath);
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/cli/render.ts"],"sourcesContent":["// render command — render PDF pages to PNG for human/vision-model\n// verification (extraction output is a claim; the page itself is the truth). Thin wrapper: parse flags, call src/raster.ts, write files.\nimport { mkdirSync, writeFileSync } from 'node:fs';\nimport path from 'node:path';\nimport { parseArgs } from 'node:util';\nimport { PdfToMdError } from '../errors.ts';\nimport type { Command } from './types.ts';\n\nconst USAGE = 'usage: pdf-to-md render <file.pdf> <page>[-<page>] [--dpi|-d N (default 288)] [--out|-o DIR (default .)]';\n\nconst render: Command = async (ctx) => {\n const parsed = (() => {\n try {\n return parseArgs({\n args: ctx.rest,\n options: {\n dpi: { type: 'string', default: '288', short: 'd' },\n out: { type: 'string', short: 'o' },\n help: { type: 'boolean', default: false, short: 'h' },\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, positionals } = parsed;\n\n if (values.help) {\n console.log(USAGE);\n return;\n }\n\n const [pdfPath, spec] = positionals;\n if (!pdfPath || !spec) ctx.usageError(USAGE);\n\n const m = /^(\\d+)(?:-(\\d+))?$/.exec(spec);\n if (m === null) throw ctx.usageError(`${USAGE}\\ncould not parse page spec: ${spec}`);\n const firstPage = Number(m[1]);\n const lastPage = m[2] !== undefined ? Number(m[2]) : firstPage;\n if (!(firstPage >= 1) || !(lastPage >= firstPage)) throw ctx.usageError(`${USAGE}\\ninvalid page spec: ${spec}`);\n\n const dpi = Number.parseFloat(values.dpi as string);\n if (!(dpi > 0)) ctx.usageError(`${USAGE}\\n--dpi must be positive`);\n\n const outDir = values.out ?? process.cwd();\n mkdirSync(outDir, { recursive: true });\n const stem = path.basename(pdfPath, path.extname(pdfPath)).replaceAll(' ', '-');\n\n // Imported here, not at module top: keeps pdfjs/canvas out of every other command's startup.\n const { awaitPdfOpen, openPdfForRender } = await import('../pdf-open.ts');\n const { renderLoadedPage } = await import('../raster.ts');\n const loadingTask = openPdfForRender(pdfPath);\n try {\n const doc = await awaitPdfOpen(loadingTask, pdfPath);\n\n // Whole-range check before any file is written, so a partly-valid range\n // fails atomically; renderLoadedPage re-validates per page.\n if (lastPage > doc.numPages) {\n throw new PdfToMdError('PAGE_RANGE', `page range ${firstPage}-${lastPage} is outside this document (1-${doc.numPages})`);\n }\n for (let n = firstPage; n <= lastPage; n++) {\n const { png } = await renderLoadedPage(doc, n, dpi);\n const outPath = path.join(outDir, `${stem}-p${n}.png`);\n writeFileSync(outPath, png);\n console.log(outPath);\n }\n } finally {\n await loadingTask.destroy();\n }\n};\n\nexport default render;\n"],"names":["mkdirSync","writeFileSync","path","parseArgs","PdfToMdError","USAGE","render","ctx","values","parsed","args","rest","options","dpi","type","default","short","out","help","allowPositionals","err","usageError","message","positionals","console","log","pdfPath","spec","m","exec","firstPage","Number","lastPage","undefined","parseFloat","outDir","process","cwd","recursive","stem","basename","extname","replaceAll","awaitPdfOpen","openPdfForRender","renderLoadedPage","loadingTask","doc","numPages","n","png","outPath","join","destroy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OAAO"}
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/cli/render.ts"],"sourcesContent":["// render command — render PDF pages to PNG for human/vision-model\n// verification (extraction output is a claim; the page itself is the truth). Thin wrapper: parse flags, call src/raster.ts, write files.\nimport { mkdirSync, writeFileSync } from 'node:fs';\nimport path from 'node:path';\nimport { parseArgs } from 'node:util';\nimport { PdfToMdError } from '../errors.ts';\nimport { awaitPdfOpen, openPdfForRender } from '../pdf-open.ts';\nimport { renderLoadedPage } from '../raster.ts';\nimport type { Command } from './types.ts';\n\nconst USAGE = 'usage: pdf-to-md render <file.pdf> <page>[-<page>] [--dpi|-d N (default 288)] [--out|-o DIR (default .)]';\n\nconst render: Command = async (ctx) => {\n const parsed = (() => {\n try {\n return parseArgs({\n args: ctx.rest,\n options: {\n dpi: { type: 'string', default: '288', short: 'd' },\n out: { type: 'string', short: 'o' },\n help: { type: 'boolean', default: false, short: 'h' },\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, positionals } = parsed;\n\n if (values.help) {\n console.log(USAGE);\n return;\n }\n\n const [pdfPath, spec] = positionals;\n if (!pdfPath || !spec) ctx.usageError(USAGE);\n\n const m = /^(\\d+)(?:-(\\d+))?$/.exec(spec);\n if (m === null) throw ctx.usageError(`${USAGE}\\ncould not parse page spec: ${spec}`);\n const firstPage = Number(m[1]);\n const lastPage = m[2] !== undefined ? Number(m[2]) : firstPage;\n if (!(firstPage >= 1) || !(lastPage >= firstPage)) throw ctx.usageError(`${USAGE}\\ninvalid page spec: ${spec}`);\n\n const dpi = Number.parseFloat(values.dpi as string);\n if (!(dpi > 0)) ctx.usageError(`${USAGE}\\n--dpi must be positive`);\n\n const outDir = values.out ?? process.cwd();\n mkdirSync(outDir, { recursive: true });\n const stem = path.basename(pdfPath, path.extname(pdfPath)).replaceAll(' ', '-');\n\n const loadingTask = openPdfForRender(pdfPath);\n try {\n const doc = await awaitPdfOpen(loadingTask, pdfPath);\n\n // Whole-range check before any file is written, so a partly-valid range\n // fails atomically; renderLoadedPage re-validates per page.\n if (lastPage > doc.numPages) {\n throw new PdfToMdError('PAGE_RANGE', `page range ${firstPage}-${lastPage} is outside this document (1-${doc.numPages})`);\n }\n for (let n = firstPage; n <= lastPage; n++) {\n const { png } = await renderLoadedPage(doc, n, dpi);\n const outPath = path.join(outDir, `${stem}-p${n}.png`);\n writeFileSync(outPath, png);\n console.log(outPath);\n }\n } finally {\n await loadingTask.destroy();\n }\n};\n\nexport default render;\n"],"names":["mkdirSync","writeFileSync","path","parseArgs","PdfToMdError","awaitPdfOpen","openPdfForRender","renderLoadedPage","USAGE","render","ctx","values","parsed","args","rest","options","dpi","type","default","short","out","help","allowPositionals","err","usageError","message","positionals","console","log","pdfPath","spec","m","exec","firstPage","Number","lastPage","undefined","parseFloat","outDir","process","cwd","recursive","stem","basename","extname","replaceAll","loadingTask","doc","numPages","n","png","outPath","join","destroy"],"mappings":"AAAA,kEAAkE;AAClE,yIAAyI;AACzI,SAASA,SAAS,EAAEC,aAAa,QAAQ,UAAU;AACnD,OAAOC,UAAU,YAAY;AAC7B,SAASC,SAAS,QAAQ,YAAY;AACtC,SAASC,YAAY,QAAQ,eAAe;AAC5C,SAASC,YAAY,EAAEC,gBAAgB,QAAQ,iBAAiB;AAChE,SAASC,gBAAgB,QAAQ,eAAe;AAGhD,MAAMC,QAAQ;AAEd,MAAMC,SAAkB,OAAOC;QAoCdC;IAnCf,MAAMC,SAAS,AAAC,CAAA;QACd,IAAI;YACF,OAAOT,UAAU;gBACfU,MAAMH,IAAII,IAAI;gBACdC,SAAS;oBACPC,KAAK;wBAAEC,MAAM;wBAAUC,SAAS;wBAAOC,OAAO;oBAAI;oBAClDC,KAAK;wBAAEH,MAAM;wBAAUE,OAAO;oBAAI;oBAClCE,MAAM;wBAAEJ,MAAM;wBAAWC,SAAS;wBAAOC,OAAO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F,MAAMkB,MAAM,MAAM1C,aAAayC,aAAajB;QAE5C,wEAAwE;QACxE,4DAA4D;QAC5D,IAAIM,WAAWY,IAAIC,QAAQ,EAAE;YAC3B,MAAM,IAAI5C,aAAa,cAAc,CAAC,WAAW,EAAE6B,UAAU,CAAC,EAAEE,SAAS,6BAA6B,EAAEY,IAAIC,QAAQ,CAAC,CAAC,CAAC;QACzH;QACA,IAAK,IAAIC,IAAIhB,WAAWgB,KAAKd,UAAUc,IAAK;YAC1C,MAAM,EAAEC,GAAG,EAAE,GAAG,MAAM3C,iBAAiBwC,KAAKE,GAAGjC;YAC/C,MAAMmC,UAAUjD,KAAKkD,IAAI,CAACd,QAAQ,GAAGI,KAAK,EAAE,EAAEO,EAAE,IAAI,CAAC;YACrDhD,cAAckD,SAASD;YACvBvB,QAAQC,GAAG,CAACuB;QACd;IACF,SAAU;QACR,MAAML,YAAYO,OAAO;IAC3B;AACF;AAEA,eAAe5C,OAAO"}
package/dist/esm/cli.d.ts CHANGED
@@ -1,2 +1 @@
1
- export declare function isBelowNodeFloor(version: string, floor: string): boolean | null;
2
1
  export default function cli(argv: string[], name: string): Promise<void>;
package/dist/esm/cli.js CHANGED
@@ -17,38 +17,11 @@ function packageVersion() {
17
17
  return 'unknown';
18
18
  }
19
19
  }
20
- // Exported so the comparison is unit-testable without spawning a subprocess
21
- // with a faked Node version. null means unparseable input, not "not below".
22
- export function isBelowNodeFloor(version, floor) {
23
- const parts = (v)=>v.replace(/^v/, '').replace(/^>=/, '').split('.').map(Number);
24
- const v = parts(version);
25
- const f = parts(floor);
26
- if (v.length !== 3 || f.length !== 3 || v.some(Number.isNaN) || f.some(Number.isNaN)) return null;
27
- const [rMaj, rMin, rPat] = v;
28
- const [fMaj, fMin, fPat] = f;
29
- return rMaj !== fMaj ? rMaj < fMaj : rMin !== fMin ? rMin < fMin : rPat < fPat;
30
- }
31
- // Reads engines.node from package.json (kept as the single source of truth,
32
- // not hardcoded here) and returns it iff process.version is below it.
33
- function belowNodeFloor(version = process.version) {
34
- try {
35
- var _pkg_engines;
36
- const pkgPath = path.resolve(__dirname, '..', '..', 'package.json');
37
- const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
38
- const floor = (_pkg_engines = pkg.engines) === null || _pkg_engines === void 0 ? void 0 : _pkg_engines.node;
39
- if (!floor) return null;
40
- return isBelowNodeFloor(version, floor) ? floor.replace(/^>=/, '') : null;
41
- } catch {
42
- return null;
43
- }
44
- }
45
20
  function usage(name) {
46
21
  return `usage: ${name} <file.pdf> [out.md] [flags...] convert a PDF to markdown\n` + ` ${name} extract <file.pdf> [out.md] [flags...] (same, explicit)\n` + ` ${name} render <file.pdf> <page>[-<page>] [--dpi|-d N (default 288)] [--out|-o DIR (default .)]\n` + ` ${name} audit <words.jsonl> <file.md> severity-1 word-loss check\n` + ` ${name} --version | --help\n` + '\n' + 'extract flags:\n' + ' --ocr force the OCR path (image-only/scanned pages)\n' + ' --no-ocr never fall back to OCR; keep sparse text output\n' + ' --pages N[-M] convert only these 1-based source pages\n' + ' --stdout write markdown to stdout instead of a file\n' + ' --json print the full QA report as JSON\n' + ` --page-markers emit a "### pN" heading per page\n` + ' --format md|txt|raw output format (default md; raw = Analysis JSON incl. the\n' + ' report; txt = reading-order plain text)\n' + ' --words-json=FILE OCR path from a recognized-word dump (offline replay)\n' + ' --debug-words=FILE dump every recognized word as JSON lines to FILE\n' + ' --dpi=N OCR render resolution (default 288; raise for tiny print or\n' + ' chart/label pages, >~400 measured worse on ordinary prose)\n' + '\n' + `extract tuning (measured defaults; see README's tuning table):\n` + ' --float-max-words=N --float-max-width=F --float-margin=F --para-gap=F\n' + ' --heading-scale=F --col-max-width=F --col-split=F --line-y-tol=F\n' + ' --line-height-ratio=F\n' + '\n' + 'examples:\n' + ` ${name} book.pdf # text layer -> book.md (auto-OCR if scanned)\n` + ` ${name} scan.pdf out.md --ocr --json # force OCR, print the QA report\n` + ` ${name} book.pdf --pages 27-31 --stdout # re-extract a page range to stdout\n` + ` ${name} render book.pdf 27 # render p27 to PNG for eyeballing (288 dpi)\n` + ` ${name} audit words.jsonl book.md # check no recognized word was lost`;
47
22
  }
48
23
  // Thrown errors -> exit 1 with the message verbatim; usage errors exit 2.
49
24
  export default async function cli(argv, name) {
50
- const floor = belowNodeFloor();
51
- if (floor) console.error(`${name}: Node ${process.version} is below the required >=${floor} — the OCR path can silently find no words instead of erroring; upgrade Node.`);
52
25
  if (argv[0] === '--version' || argv[0] === '-v') {
53
26
  console.log(packageVersion());
54
27
  return;
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/cli.ts"],"sourcesContent":["// Parsing and dispatch only. Commands live in src/cli/, one file each,\n// lazy-loaded — nothing tree- or dependency-heavy may be imported at the top of this file (`render` must never load the OCR engine, `--version` must load almost nothing).\nimport { readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { COMMANDS } from './cli/index.ts';\nimport type { Ctx } from './cli/types.ts';\n\n// __dirname (CJS) or its ESM equivalent, then two fixed hops up to the\n// package root. NOT `import.meta.resolve('pdf-to-md/package.json')` — Node's self-reference lookup throws ERR_MODULE_NOT_FOUND from a file this deep; a location-relative path is stable across both build targets.\nconst __dirname = path.dirname(typeof __filename !== 'undefined' ? __filename : fileURLToPath(import.meta.url));\n\nfunction packageVersion(): string {\n try {\n const pkgPath = path.resolve(__dirname, '..', '..', 'package.json');\n const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) as { version?: string };\n return pkg.version ?? 'unknown';\n } catch {\n return 'unknown';\n }\n}\n\n// Exported so the comparison is unit-testable without spawning a subprocess\n// with a faked Node version. null means unparseable input, not \"not below\".\nexport function isBelowNodeFloor(version: string, floor: string): boolean | null {\n const parts = (v: string) => v.replace(/^v/, '').replace(/^>=/, '').split('.').map(Number);\n const v = parts(version);\n const f = parts(floor);\n if (v.length !== 3 || f.length !== 3 || v.some(Number.isNaN) || f.some(Number.isNaN)) return null;\n const [rMaj, rMin, rPat] = v;\n const [fMaj, fMin, fPat] = f;\n return rMaj !== fMaj ? rMaj < fMaj : rMin !== fMin ? rMin < fMin : rPat < fPat;\n}\n\n// Reads engines.node from package.json (kept as the single source of truth,\n// not hardcoded here) and returns it iff process.version is below it.\nfunction belowNodeFloor(version = process.version): string | null {\n try {\n const pkgPath = path.resolve(__dirname, '..', '..', 'package.json');\n const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) as { engines?: { node?: string } };\n const floor = pkg.engines?.node;\n if (!floor) return null;\n return isBelowNodeFloor(version, floor) ? floor.replace(/^>=/, '') : null;\n } catch {\n return null;\n }\n}\n\nfunction usage(name: string): string {\n return (\n `usage: ${name} <file.pdf> [out.md] [flags...] convert a PDF to markdown\\n` +\n ` ${name} extract <file.pdf> [out.md] [flags...] (same, explicit)\\n` +\n ` ${name} render <file.pdf> <page>[-<page>] [--dpi|-d N (default 288)] [--out|-o DIR (default .)]\\n` +\n ` ${name} audit <words.jsonl> <file.md> severity-1 word-loss check\\n` +\n ` ${name} --version | --help\\n` +\n '\\n' +\n 'extract flags:\\n' +\n ' --ocr force the OCR path (image-only/scanned pages)\\n' +\n ' --no-ocr never fall back to OCR; keep sparse text output\\n' +\n ' --pages N[-M] convert only these 1-based source pages\\n' +\n ' --stdout write markdown to stdout instead of a file\\n' +\n ' --json print the full QA report as JSON\\n' +\n ` --page-markers emit a \"### pN\" heading per page\\n` +\n ' --format md|txt|raw output format (default md; raw = Analysis JSON incl. the\\n' +\n ' report; txt = reading-order plain text)\\n' +\n ' --words-json=FILE OCR path from a recognized-word dump (offline replay)\\n' +\n ' --debug-words=FILE dump every recognized word as JSON lines to FILE\\n' +\n ' --dpi=N OCR render resolution (default 288; raise for tiny print or\\n' +\n ' chart/label pages, >~400 measured worse on ordinary prose)\\n' +\n '\\n' +\n `extract tuning (measured defaults; see README's tuning table):\\n` +\n ' --float-max-words=N --float-max-width=F --float-margin=F --para-gap=F\\n' +\n ' --heading-scale=F --col-max-width=F --col-split=F --line-y-tol=F\\n' +\n ' --line-height-ratio=F\\n' +\n '\\n' +\n 'examples:\\n' +\n ` ${name} book.pdf # text layer -> book.md (auto-OCR if scanned)\\n` +\n ` ${name} scan.pdf out.md --ocr --json # force OCR, print the QA report\\n` +\n ` ${name} book.pdf --pages 27-31 --stdout # re-extract a page range to stdout\\n` +\n ` ${name} render book.pdf 27 # render p27 to PNG for eyeballing (288 dpi)\\n` +\n ` ${name} audit words.jsonl book.md # check no recognized word was lost`\n );\n}\n\n// Thrown errors -> exit 1 with the message verbatim; usage errors exit 2.\nexport default async function cli(argv: string[], name: string): Promise<void> {\n const floor = belowNodeFloor();\n if (floor) console.error(`${name}: Node ${process.version} is below the required >=${floor} — the OCR path can silently find no words instead of erroring; upgrade Node.`);\n\n if (argv[0] === '--version' || argv[0] === '-v') {\n console.log(packageVersion());\n return;\n }\n if (argv.length === 0 || argv[0] === '--help' || argv[0] === '-h') {\n // No args is a usage ERROR (bare `pdf-to-md` can't do anything); asking\n // for --help is not.\n if (argv.length === 0) {\n console.error(usage(name));\n process.exit(2);\n }\n console.log(`${name} v${packageVersion()}`);\n console.log('');\n console.log(usage(name));\n return;\n }\n\n const ctx: Ctx = {\n name,\n rest: argv.slice(1),\n usageError(message) {\n console.error(message);\n process.exit(2);\n },\n };\n\n try {\n const load = COMMANDS[argv[0]];\n if (load) {\n await (await load()).default(ctx);\n } else {\n // Compat shim: `pdf-to-md file.pdf [...]` — any first word that isn't\n // a reserved command is extract's own first argument.\n ctx.rest = argv;\n await (await COMMANDS.extract()).default(ctx);\n }\n } catch (err) {\n console.error((err as Error).message);\n process.exit(1);\n }\n}\n"],"names":["readFileSync","path","fileURLToPath","COMMANDS","__dirname","dirname","__filename","url","packageVersion","pkg","pkgPath","resolve","JSON","parse","version","isBelowNodeFloor","floor","parts","v","replace","split","map","Number","f","length","some","isNaN","rMaj","rMin","rPat","fMaj","fMin","fPat","belowNodeFloor","process","engines","node","usage","name","cli","argv","console","error","log","exit","ctx","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AeA,IAAI,CAAC,EAAE,KAAK,MAAM;QAC/CC,QAAQE,GAAG,CAACnC;QACZ;IACF;IACA,IAAIgC,KAAKhB,MAAM,KAAK,KAAKgB,IAAI,CAAC,EAAE,KAAK,YAAYA,IAAI,CAAC,EAAE,KAAK,MAAM;QACjE,wEAAwE;QACxE,qBAAqB;QACrB,IAAIA,KAAKhB,MAAM,KAAK,GAAG;YACrBiB,QAAQC,KAAK,CAACL,MAAMC;YACpBJ,QAAQU,IAAI,CAAC;QACf;QACAH,QAAQE,GAAG,CAAC,GAAGL,KAAK,EAAE,EAAE9B,kBAAkB;QAC1CiC,QAAQE,GAAG,CAAC;QACZF,QAAQE,GAAG,CAACN,MAAMC;QAClB;IACF;IAEA,MAAMO,MAAW;QACfP;QACAQ,MAAMN,KAAKO,KAAK,CAAC;QACjBC,YAAWC,OAAO;YAChBR,QAAQC,KAAK,CAACO;YACdf,QAAQU,IAAI,CAAC;QACf;IACF;IAEA,IAAI;QACF,MAAMM,OAAO/C,QAAQ,CAACqC,IAAI,CAAC,EAAE,CAAC;QAC9B,IAAIU,MAAM;YACR,MAAM,AAAC,CAAA,MAAMA,MAAK,EAAGC,OAAO,CAACN;QAC/B,OAAO;YACL,sEAAsE;YACtE,sDAAsD;YACtDA,IAAIC,IAAI,GAAGN;YACX,MAAM,AAAC,CAAA,MAAMrC,SAASiD,OAAO,EAAC,EAAGD,OAAO,CAACN;QAC3C;IACF,EAAE,OAAOQ,KAAK;QACZZ,QAAQC,KAAK,CAAC,AAACW,IAAcJ,OAAO;QACpCf,QAAQU,IAAI,CAAC;IACf;AACF"}
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/cli.ts"],"sourcesContent":["// Parsing and dispatch only. Commands live in src/cli/, one file each,\n// lazy-loaded — nothing tree- or dependency-heavy may be imported at the top of this file (`render` must never load the OCR engine, `--version` must load almost nothing).\nimport { readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { COMMANDS } from './cli/index.ts';\nimport type { Ctx } from './cli/types.ts';\n\n// __dirname (CJS) or its ESM equivalent, then two fixed hops up to the\n// package root. NOT `import.meta.resolve('pdf-to-md/package.json')` — Node's self-reference lookup throws ERR_MODULE_NOT_FOUND from a file this deep; a location-relative path is stable across both build targets.\nconst __dirname = path.dirname(typeof __filename !== 'undefined' ? __filename : fileURLToPath(import.meta.url));\n\nfunction packageVersion(): string {\n try {\n const pkgPath = path.resolve(__dirname, '..', '..', 'package.json');\n const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) as { version?: string };\n return pkg.version ?? 'unknown';\n } catch {\n return 'unknown';\n }\n}\n\nfunction usage(name: string): string {\n return (\n `usage: ${name} <file.pdf> [out.md] [flags...] convert a PDF to markdown\\n` +\n ` ${name} extract <file.pdf> [out.md] [flags...] (same, explicit)\\n` +\n ` ${name} render <file.pdf> <page>[-<page>] [--dpi|-d N (default 288)] [--out|-o DIR (default .)]\\n` +\n ` ${name} audit <words.jsonl> <file.md> severity-1 word-loss check\\n` +\n ` ${name} --version | --help\\n` +\n '\\n' +\n 'extract flags:\\n' +\n ' --ocr force the OCR path (image-only/scanned pages)\\n' +\n ' --no-ocr never fall back to OCR; keep sparse text output\\n' +\n ' --pages N[-M] convert only these 1-based source pages\\n' +\n ' --stdout write markdown to stdout instead of a file\\n' +\n ' --json print the full QA report as JSON\\n' +\n ` --page-markers emit a \"### pN\" heading per page\\n` +\n ' --format md|txt|raw output format (default md; raw = Analysis JSON incl. the\\n' +\n ' report; txt = reading-order plain text)\\n' +\n ' --words-json=FILE OCR path from a recognized-word dump (offline replay)\\n' +\n ' --debug-words=FILE dump every recognized word as JSON lines to FILE\\n' +\n ' --dpi=N OCR render resolution (default 288; raise for tiny print or\\n' +\n ' chart/label pages, >~400 measured worse on ordinary prose)\\n' +\n '\\n' +\n `extract tuning (measured defaults; see README's tuning table):\\n` +\n ' --float-max-words=N --float-max-width=F --float-margin=F --para-gap=F\\n' +\n ' --heading-scale=F --col-max-width=F --col-split=F --line-y-tol=F\\n' +\n ' --line-height-ratio=F\\n' +\n '\\n' +\n 'examples:\\n' +\n ` ${name} book.pdf # text layer -> book.md (auto-OCR if scanned)\\n` +\n ` ${name} scan.pdf out.md --ocr --json # force OCR, print the QA report\\n` +\n ` ${name} book.pdf --pages 27-31 --stdout # re-extract a page range to stdout\\n` +\n ` ${name} render book.pdf 27 # render p27 to PNG for eyeballing (288 dpi)\\n` +\n ` ${name} audit words.jsonl book.md # check no recognized word was lost`\n );\n}\n\n// Thrown errors -> exit 1 with the message verbatim; usage errors exit 2.\nexport default async function cli(argv: string[], name: string): Promise<void> {\n if (argv[0] === '--version' || argv[0] === '-v') {\n console.log(packageVersion());\n return;\n }\n if (argv.length === 0 || argv[0] === '--help' || argv[0] === '-h') {\n // No args is a usage ERROR (bare `pdf-to-md` can't do anything); asking\n // for --help is not.\n if (argv.length === 0) {\n console.error(usage(name));\n process.exit(2);\n }\n console.log(`${name} v${packageVersion()}`);\n console.log('');\n console.log(usage(name));\n return;\n }\n\n const ctx: Ctx = {\n name,\n rest: argv.slice(1),\n usageError(message) {\n console.error(message);\n process.exit(2);\n },\n };\n\n try {\n const load = COMMANDS[argv[0]];\n if (load) {\n await (await load()).default(ctx);\n } else {\n // Compat shim: `pdf-to-md file.pdf [...]` — any first word that isn't\n // a reserved command is extract's own first argument.\n ctx.rest = argv;\n await (await COMMANDS.extract()).default(ctx);\n }\n } catch (err) {\n console.error((err as Error).message);\n process.exit(1);\n }\n}\n"],"names":["readFileSync","path","fileURLToPath","COMMANDS","__dirname","dirname","__filename","url","packageVersion","pkg","pkgPath","resolve","JSON","parse","version","usage","name","cli","argv","console","log","length","error","process","exit","ctx","rest","slice","usageError","message","load","default","extract","err"],"mappings":"AAAA,uEAAuE;AACvE,2KAA2K;AAC3K,SAASA,YAAY,QAAQ,UAAU;AACvC,OAAOC,UAAU,YAAY;AAC7B,SAASC,aAAa,QAAQ,WAAW;AACzC,SAASC,QAAQ,Q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AAM,KAAK,KAAKH,IAAI,CAAC,EAAE,KAAK,YAAYA,IAAI,CAAC,EAAE,KAAK,MAAM;QACjE,wEAAwE;QACxE,qBAAqB;QACrB,IAAIA,KAAKG,MAAM,KAAK,GAAG;YACrBF,QAAQG,KAAK,CAACP,MAAMC;YACpBO,QAAQC,IAAI,CAAC;QACf;QACAL,QAAQC,GAAG,CAAC,GAAGJ,KAAK,EAAE,EAAER,kBAAkB;QAC1CW,QAAQC,GAAG,CAAC;QACZD,QAAQC,GAAG,CAACL,MAAMC;QAClB;IACF;IAEA,MAAMS,MAAW;QACfT;QACAU,MAAMR,KAAKS,KAAK,CAAC;QACjBC,YAAWC,OAAO;YAChBV,QAAQG,KAAK,CAACO;YACdN,QAAQC,IAAI,CAAC;QACf;IACF;IAEA,IAAI;QACF,MAAMM,OAAO3B,QAAQ,CAACe,IAAI,CAAC,EAAE,CAAC;QAC9B,IAAIY,MAAM;YACR,MAAM,AAAC,CAAA,MAAMA,MAAK,EAAGC,OAAO,CAACN;QAC/B,OAAO;YACL,sEAAsE;YACtE,sDAAsD;YACtDA,IAAIC,IAAI,GAAGR;YACX,MAAM,AAAC,CAAA,MAAMf,SAAS6B,OAAO,EAAC,EAAGD,OAAO,CAACN;QAC3C;IACF,EAAE,OAAOQ,KAAK;QACZd,QAAQG,KAAK,CAAC,AAACW,IAAcJ,OAAO;QACpCN,QAAQC,IAAI,CAAC;IACf;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effortlessmotion/pdf-to-md",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Extract PDFs to structured markdown: text layer via pdf.js, OCR fallback for image-only pages, pure npm and cross-platform",
5
5
  "keywords": [
6
6
  "pdf",