@effortlessmotion/pdf-to-md 0.1.8 → 0.2.1
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/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/cli/index.js.map +1 -1
- package/dist/cjs/cli/render.js.map +1 -1
- package/dist/cjs/cli/types.js.map +1 -1
- package/dist/cjs/cli.js.map +1 -1
- package/dist/cjs/collect.js +91 -35
- package/dist/cjs/collect.js.map +1 -1
- package/dist/cjs/dict.js.map +1 -1
- package/dist/cjs/emit.js.map +1 -1
- package/dist/cjs/engines/tesseract.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/geometry.js.map +1 -1
- package/dist/cjs/index.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/pdf-open.js.map +1 -1
- package/dist/cjs/raster.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/resolve.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/cjs/validate.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/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/cli/index.js.map +1 -1
- package/dist/esm/cli/render.js.map +1 -1
- package/dist/esm/cli/types.js.map +1 -1
- package/dist/esm/cli.js.map +1 -1
- package/dist/esm/collect.js +64 -15
- package/dist/esm/collect.js.map +1 -1
- package/dist/esm/dict.js.map +1 -1
- package/dist/esm/emit.js.map +1 -1
- package/dist/esm/engines/tesseract.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/geometry.js.map +1 -1
- package/dist/esm/index.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/pdf-open.js.map +1 -1
- package/dist/esm/raster.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/resolve.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/dist/esm/validate.js.map +1 -1
- package/package.json +1 -1
- package/skills/pdf-to-md/SKILL.md +39 -66
package/dist/esm/analyze.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
// analyze() plus the OCR-path core (buildOcrAnalysisPages) that extractOcr
|
|
2
|
-
// shares with it
|
|
2
|
+
// shares with it. Text analysis uses text-analysis.ts so it captures the same
|
|
3
|
+
// blocks, source runs, and report input as extractText.
|
|
3
4
|
import { collect, pageCount } from './collect.js';
|
|
4
5
|
import { dictAvailable, dictUnavailableWarning } from './dict.js';
|
|
5
6
|
import { emitPage, renderEmitBlock } from './emit.js';
|
|
6
7
|
import { recognizePdf } from './engines/tesseract.js';
|
|
7
8
|
import { buildPageOCR, DEFAULT_TUNING, headingLevels } from './geometry.js';
|
|
8
|
-
import { toLines } from './lines.js';
|
|
9
|
-
import { buildMarkdown } from './markdown.js';
|
|
10
9
|
import { renderPagesToMarkdown } from './render-analysis.js';
|
|
11
|
-
import { buildOcrReport,
|
|
10
|
+
import { buildOcrReport, OUTPUT_VERSION } from './report.js';
|
|
11
|
+
import { buildTextAnalysis } from './text-analysis.js';
|
|
12
12
|
import { validateOcrAnalysisInput, validatePages, validateWordsInput } from './validate.js';
|
|
13
13
|
function toAnalysisWord(w) {
|
|
14
14
|
const box = {
|
|
@@ -81,6 +81,7 @@ function toAnalysisBlock(b, wordIndex) {
|
|
|
81
81
|
});
|
|
82
82
|
words = result.words;
|
|
83
83
|
}
|
|
84
|
+
words = words.filter((word)=>word.page >= first && word.page <= last);
|
|
84
85
|
(_opts_onWords = opts.onWords) === null || _opts_onWords === void 0 ? void 0 : _opts_onWords.call(opts, words);
|
|
85
86
|
// Stable per-page index, assigned in recognition/replay order — this IS
|
|
86
87
|
// the page's flat `words` list order; `wordIndex` recovers it later since geometry.ts never clones a Word (object identity survives row-building).
|
|
@@ -122,6 +123,7 @@ function toAnalysisBlock(b, wordIndex) {
|
|
|
122
123
|
}
|
|
123
124
|
pages.push({
|
|
124
125
|
page: pn,
|
|
126
|
+
path: 'ocr',
|
|
125
127
|
words: ((_byPage_get1 = byPage.get(pn)) !== null && _byPage_get1 !== void 0 ? _byPage_get1 : []).map(toAnalysisWord),
|
|
126
128
|
blocks
|
|
127
129
|
});
|
|
@@ -153,21 +155,9 @@ async function analyzeOcr(input, opts) {
|
|
|
153
155
|
pages
|
|
154
156
|
};
|
|
155
157
|
}
|
|
156
|
-
// TEXT path: deliberately NOT routed through the OCR shared-core pattern —
|
|
157
|
-
// buildMarkdown's reflow/hyphen-rejoin/list rules don't fit the 4-type Analysis block union, so this is an independent, intentionally coarser reconstruction (one word/block per visual LINE) — honest, not byte-identical to extractText (see AnalysisPage, src/types.ts).
|
|
158
|
-
// Converts a text-path Line's page-point geometry (src/lines.ts) into the
|
|
159
|
-
// same normalized box contract the OCR path uses (see AnalysisPage.words) — only the coordinate SYSTEM changes; `box.y` anchors on the line's baseline, `box.w` stays 0.
|
|
160
|
-
function textLineBox(l, dims) {
|
|
161
|
-
const { width, height } = dims;
|
|
162
|
-
return {
|
|
163
|
-
x: width > 0 ? l.x / width : 0,
|
|
164
|
-
y: height > 0 ? (height - l.y) / height : 0,
|
|
165
|
-
w: 0,
|
|
166
|
-
h: height > 0 ? l.h / height : 0
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
158
|
async function analyzeText(pdfPath, opts) {
|
|
170
159
|
var _opts_pages_last, _opts_pageMarkers;
|
|
160
|
+
var _opts_onWords;
|
|
171
161
|
const warn = (m)=>{
|
|
172
162
|
var _opts_onWarning;
|
|
173
163
|
return (_opts_onWarning = opts.onWarning) === null || _opts_onWarning === void 0 ? void 0 : _opts_onWarning.call(opts, m);
|
|
@@ -177,87 +167,26 @@ async function analyzeText(pdfPath, opts) {
|
|
|
177
167
|
first: opts.pages.first,
|
|
178
168
|
last: (_opts_pages_last = opts.pages.last) !== null && _opts_pages_last !== void 0 ? _opts_pages_last : opts.pages.first
|
|
179
169
|
} : {};
|
|
180
|
-
const
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
{
|
|
184
|
-
width: p.width,
|
|
185
|
-
height: p.height
|
|
186
|
-
}
|
|
187
|
-
]));
|
|
188
|
-
const pageLines = pages.map((p)=>({
|
|
189
|
-
page: p.page,
|
|
190
|
-
lines: toLines(p)
|
|
191
|
-
}));
|
|
192
|
-
const pageMarkers = (_opts_pageMarkers = opts.pageMarkers) !== null && _opts_pageMarkers !== void 0 ? _opts_pageMarkers : false;
|
|
193
|
-
const { md, stats, bodyH, headingSizes } = buildMarkdown(pageLines, heightChars, {
|
|
194
|
-
pageMarkers
|
|
195
|
-
});
|
|
196
|
-
const statsMd = pageMarkers ? md : buildMarkdown(pageLines, heightChars, {
|
|
197
|
-
pageMarkers: true
|
|
198
|
-
}).md;
|
|
199
|
-
const hybridPages = pages.filter((p)=>p.largeImage).map((p)=>p.page);
|
|
170
|
+
const collected = await collect(pdfPath, range);
|
|
171
|
+
const result = buildTextAnalysis(collected, (_opts_pageMarkers = opts.pageMarkers) !== null && _opts_pageMarkers !== void 0 ? _opts_pageMarkers : false);
|
|
172
|
+
const hybridPages = collected.pages.filter((page)=>page.largeImage).map((page)=>page.page);
|
|
200
173
|
for (const n of hybridPages){
|
|
201
174
|
warn(`p${n}: text layer used but page carries large image(s); if it has baked-in text this output won't have it — compare (don't blindly replace) against --pages ${n} --ocr, which can itself introduce errors`);
|
|
202
175
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
statsMd,
|
|
209
|
-
stats,
|
|
210
|
-
bodyH,
|
|
211
|
-
headingSizes,
|
|
212
|
-
hybridPages
|
|
213
|
-
});
|
|
214
|
-
// headingSizes is offset-independent — reused directly, then re-leveled
|
|
215
|
-
// here with offset 0 regardless of `opts.pageMarkers` (see AnalysisPage).
|
|
216
|
-
const levelOf = new Map(headingSizes.slice(0, 6).map((h, i)=>[
|
|
217
|
-
h,
|
|
218
|
-
Math.min(6, i + 1)
|
|
219
|
-
]));
|
|
220
|
-
const analysisPages = pageLines.map(({ page, lines })=>{
|
|
221
|
-
const dims = pageDims.get(page);
|
|
222
|
-
const words = lines.map((l)=>({
|
|
223
|
-
text: l.text,
|
|
224
|
-
box: textLineBox(l, dims)
|
|
225
|
-
}));
|
|
226
|
-
const blocks = lines.map((l, i)=>{
|
|
227
|
-
const level = levelOf.get(l.h);
|
|
228
|
-
return level !== undefined ? {
|
|
229
|
-
type: 'heading',
|
|
230
|
-
text: l.text,
|
|
231
|
-
level,
|
|
232
|
-
wordIndexes: [
|
|
233
|
-
i
|
|
234
|
-
]
|
|
235
|
-
} : {
|
|
236
|
-
type: 'paragraph',
|
|
237
|
-
text: l.text,
|
|
238
|
-
wordIndexes: [
|
|
239
|
-
i
|
|
240
|
-
]
|
|
241
|
-
};
|
|
242
|
-
});
|
|
243
|
-
return {
|
|
244
|
-
page,
|
|
245
|
-
words,
|
|
246
|
-
blocks
|
|
247
|
-
};
|
|
248
|
-
});
|
|
249
|
-
return {
|
|
250
|
-
outputVersion: OUTPUT_VERSION,
|
|
251
|
-
report,
|
|
252
|
-
pages: analysisPages
|
|
253
|
-
};
|
|
176
|
+
for (const page of collected.pages.filter((page)=>page.textChars === 0 && !page.hasImage)){
|
|
177
|
+
warn(`p${page.page}: no extractable text; the page may be blank or contain only vector content`);
|
|
178
|
+
}
|
|
179
|
+
(_opts_onWords = opts.onWords) === null || _opts_onWords === void 0 ? void 0 : _opts_onWords.call(opts, result.words);
|
|
180
|
+
return result.analysis;
|
|
254
181
|
}
|
|
255
182
|
/**
|
|
256
183
|
* Analyze a PDF (or replay an already-recognized OCR word dump) into
|
|
257
184
|
* `Analysis`: plain, JSON-serializable per-page `words`/`blocks` plus the same QA `report` the other entry points return. Render with toMarkdown()/toText(), or hand-edit/splice it (no merge API — see toMarkdown's doc comment).
|
|
258
185
|
*
|
|
259
186
|
* @param input a bare pdf path, or `{ words }` to replay a dump (always analyzed as OCR) — unlike extractOcr, never `{ pdfPath }`
|
|
260
|
-
* @param opts see {@link AnalyzeOptions}; for a
|
|
187
|
+
* @param opts see {@link AnalyzeOptions}; for a PDF path, `path` explicitly
|
|
188
|
+
* selects text (default) or OCR. Automatic per-page routing belongs to
|
|
189
|
+
* pdfToMarkdown and the CLI output formats.
|
|
261
190
|
*/ export async function analyze(input, opts = {}) {
|
|
262
191
|
validatePages(opts.pages);
|
|
263
192
|
if (typeof input !== 'string') return analyzeOcr({
|
package/dist/esm/analyze.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/formats/pdf-to-md/src/analyze.ts"],"sourcesContent":["// analyze() plus the OCR-path core (buildOcrAnalysisPages) that extractOcr\n// shares with it — same buildPageOCR -> emitPage -> renderPagesToMarkdown pipeline, so OCR-path markdown is byte-identical by construction, not two implementations kept in sync by hand. The TEXT path is NOT shared this way (see analyzeText).\nimport { collect, pageCount } from './collect.ts';\nimport { dictAvailable, dictUnavailableWarning } from './dict.ts';\nimport type { EmitBlock } from './emit.ts';\nimport { emitPage, renderEmitBlock } from './emit.ts';\nimport { recognizePdf } from './engines/tesseract.ts';\n// Type-only: erased at compile time, so this does NOT create a runtime\n// circular dependency even though src/extract.ts imports (value-level) from this module.\nimport type { ExtractOcrOptions, ExtractTextOptions, OcrWordInput } from './extract.ts';\nimport type { PageOCR, Tuning, Word } from './geometry.ts';\nimport { buildPageOCR, DEFAULT_TUNING, headingLevels } from './geometry.ts';\nimport { toLines } from './lines.ts';\nimport { buildMarkdown } from './markdown.ts';\nimport { renderPagesToMarkdown } from './render-analysis.ts';\nimport { buildOcrReport, buildReport, OUTPUT_VERSION } from './report.ts';\nimport type { Analysis, AnalysisBlock, AnalysisPage, AnalysisWord, Line, PageLines } from './types.ts';\nimport { validateOcrAnalysisInput, validatePages, validateWordsInput } from './validate.ts';\n\n/** analyze(pdf) with the default/'text' path — OCR-only fields don't exist\n * on this branch, so passing e.g. `tuning` without `path: 'ocr'` is a compile error. */\nexport type AnalyzeTextOptions = ExtractTextOptions & { path?: 'text' };\n\n/** analyze(pdf, {path:'ocr'}) — same fields as {@link ExtractOcrOptions},\n * unlocked only once `path: 'ocr'` is set. */\nexport type AnalyzeOcrOptions = ExtractOcrOptions & { path: 'ocr' };\n\n/** Options for {@link analyze}: which fields type-check depends on `path`\n * (ignored, forced 'ocr', when `input` is a `{ words }` replay). */\nexport type AnalyzeOptions = AnalyzeTextOptions | AnalyzeOcrOptions;\n\nfunction toAnalysisWord(w: OcrWordInput): AnalysisWord {\n const box = { x: w.x, y: w.y, w: w.w, h: w.h };\n return w.confidence === undefined ? { text: w.text, box } : { text: w.text, box, confidence: w.confidence };\n}\n\n// EmitBlock -> AnalysisBlock: collapses emit.ts's 'quote' kind (a pull-quote)\n// into Analysis's 'paragraph' type, `text` keeping its literal '> ' prefix — see AnalysisBlock's doc comment (src/types.ts) for why.\nfunction toAnalysisBlock(b: EmitBlock, wordIndex: Map<Word, number>): AnalysisBlock {\n const wordIndexes = b.words.map((w) => wordIndex.get(w) as number);\n if (b.kind === 'heading') return { type: 'heading', text: b.text, level: b.level, wordIndexes };\n if (b.kind === 'review') return { type: 'review', text: renderEmitBlock(b), wordIndexes };\n return { type: 'paragraph', text: renderEmitBlock(b), wordIndexes };\n}\n\n/** The OCR-path core shared by extractOcr and analyze(..., {path:'ocr'}):\n * recognizes (or replays) words and runs the unchanged buildPageOCR/emitPage pipeline. Word IDENTITY (object reference), not value, carries indices through into the emitted blocks — see `geometry.ts`'s OcrLine.words. */\nexport async function buildOcrAnalysisPages(input: { pdfPath: string } | { words: OcrWordInput[] }, opts: ExtractOcrOptions, warn: (m: string) => void): Promise<{ pages: AnalysisPage[]; words: OcrWordInput[] }> {\n const tuning: Tuning = { ...DEFAULT_TUNING, ...opts.tuning };\n const validated = validateOcrAnalysisInput(input);\n\n let words: OcrWordInput[];\n let first: number;\n let last: number;\n if ('words' in validated) {\n // Replay: page range from --pages if given, else every page up to the highest one the dump mentions.\n words = validated.words;\n first = opts.pages?.first ?? 1;\n last = opts.pages?.last ?? opts.pages?.first ?? Math.max(1, ...words.map((w) => w.page));\n } else {\n const totalPages = await pageCount(validated.pdfPath);\n const rawFirst = opts.pages?.first ?? 1;\n const rawLast = opts.pages?.last ?? (opts.pages ? rawFirst : totalPages);\n first = Math.min(Math.max(1, rawFirst), Math.max(1, totalPages));\n last = Math.min(Math.max(first, rawLast), totalPages);\n const result = await recognizePdf(validated.pdfPath, first, last, {\n dpi: opts.dpi,\n onProgress: opts.onProgress,\n onWarning: warn,\n });\n words = result.words;\n }\n\n opts.onWords?.(words);\n\n // Stable per-page index, assigned in recognition/replay order — this IS\n // the page's flat `words` list order; `wordIndex` recovers it later since geometry.ts never clones a Word (object identity survives row-building).\n const byPage = new Map<number, OcrWordInput[]>();\n const wordIndex = new Map<Word, number>();\n for (const w of words) {\n const list = byPage.get(w.page);\n if (list) {\n wordIndex.set(w, list.length);\n list.push(w);\n } else {\n wordIndex.set(w, 0);\n byPage.set(w.page, [w]);\n }\n }\n\n const pageOcrs = new Map<number, PageOCR>();\n for (let pn = first; pn <= last; pn++) pageOcrs.set(pn, buildPageOCR(byPage.get(pn) ?? [], tuning));\n const { levelOf } = headingLevels([...pageOcrs.values()], tuning);\n\n const pages: AnalysisPage[] = [];\n for (let pn = first; pn <= last; pn++) {\n const p = pageOcrs.get(pn) as PageOCR;\n // An in-range page with no recognized words stays visible (marker +\n // warning), never silently absent — same rule as extractOcr.\n if (!byPage.has(pn)) warn(`p${pn}: no recognized words, page emitted with no OCR text`);\n\n const blocks: AnalysisBlock[] = emitPage(p, levelOf, tuning).map((b) => toAnalysisBlock(b, wordIndex));\n // Decorative fragments always render as one visible \"> [floats]\" aside\n // per page (matching the one printed line) — see geometry.ts's PageOCR.floats.\n if (p.floats.length) {\n blocks.push({\n type: 'float',\n text: `> [floats] ${p.floats.map((f) => f.text).join(' · ')}`,\n wordIndexes: p.floats.flatMap((f) => f.words).map((w) => wordIndex.get(w) as number),\n });\n }\n\n pages.push({\n page: pn,\n words: (byPage.get(pn) ?? []).map(toAnalysisWord),\n blocks,\n });\n }\n\n return { pages, words };\n}\n\nasync function analyzeOcr(input: { pdfPath: string } | { words: OcrWordInput[] }, opts: ExtractOcrOptions): Promise<Analysis> {\n const warn = (m: string) => opts.onWarning?.(m);\n if (!dictAvailable()) warn(dictUnavailableWarning());\n const { pages, words } = await buildOcrAnalysisPages(input, opts, warn);\n const md = renderPagesToMarkdown(pages, { forcePageMarkers: true, pageMarkers: true });\n const report = buildOcrReport({ md, fallback: opts.fallback, words });\n return { outputVersion: OUTPUT_VERSION, report, pages };\n}\n\n// TEXT path: deliberately NOT routed through the OCR shared-core pattern —\n// buildMarkdown's reflow/hyphen-rejoin/list rules don't fit the 4-type Analysis block union, so this is an independent, intentionally coarser reconstruction (one word/block per visual LINE) — honest, not byte-identical to extractText (see AnalysisPage, src/types.ts).\n\n// Converts a text-path Line's page-point geometry (src/lines.ts) into the\n// same normalized box contract the OCR path uses (see AnalysisPage.words) — only the coordinate SYSTEM changes; `box.y` anchors on the line's baseline, `box.w` stays 0.\nfunction textLineBox(l: Line, dims: { width: number; height: number }): AnalysisWord['box'] {\n const { width, height } = dims;\n return {\n x: width > 0 ? l.x / width : 0,\n y: height > 0 ? (height - l.y) / height : 0,\n w: 0,\n h: height > 0 ? l.h / height : 0,\n };\n}\n\nasync function analyzeText(pdfPath: string, opts: ExtractTextOptions): Promise<Analysis> {\n const warn = (m: string) => opts.onWarning?.(m);\n if (!dictAvailable()) warn(dictUnavailableWarning());\n const range = opts.pages ? { first: opts.pages.first, last: opts.pages.last ?? opts.pages.first } : {};\n const { numPages, pages, heightChars } = await collect(pdfPath, range);\n const pageDims = new Map(pages.map((p) => [p.page, { width: p.width, height: p.height }]));\n const pageLines: PageLines[] = pages.map((p) => ({ page: p.page, lines: toLines(p) }));\n const pageMarkers = opts.pageMarkers ?? false;\n const { md, stats, bodyH, headingSizes } = buildMarkdown(pageLines, heightChars, { pageMarkers });\n const statsMd = pageMarkers ? md : buildMarkdown(pageLines, heightChars, { pageMarkers: true }).md;\n\n const hybridPages = pages.filter((p) => p.largeImage).map((p) => p.page);\n for (const n of hybridPages) {\n warn(`p${n}: text layer used but page carries large image(s); if it has baked-in text this output won't have it — compare (don't blindly replace) against --pages ${n} --ocr, which can itself introduce errors`);\n }\n\n // Report is measured from the REAL markdown above — accurate regardless of\n // the coarser block/word reconstruction below.\n const report = buildReport({ numPages, md, statsMd, stats, bodyH, headingSizes, hybridPages });\n\n // headingSizes is offset-independent — reused directly, then re-leveled\n // here with offset 0 regardless of `opts.pageMarkers` (see AnalysisPage).\n const levelOf = new Map(headingSizes.slice(0, 6).map((h, i) => [h, Math.min(6, i + 1)]));\n const analysisPages: AnalysisPage[] = pageLines.map(({ page, lines }) => {\n const dims = pageDims.get(page) as { width: number; height: number };\n const words: AnalysisWord[] = lines.map((l) => ({ text: l.text, box: textLineBox(l, dims) }));\n const blocks: AnalysisBlock[] = lines.map((l, i) => {\n const level = levelOf.get(l.h);\n return level !== undefined ? { type: 'heading' as const, text: l.text, level, wordIndexes: [i] } : { type: 'paragraph' as const, text: l.text, wordIndexes: [i] };\n });\n return { page, words, blocks };\n });\n\n return { outputVersion: OUTPUT_VERSION, report, pages: analysisPages };\n}\n\n/**\n * Analyze a PDF (or replay an already-recognized OCR word dump) into\n * `Analysis`: plain, JSON-serializable per-page `words`/`blocks` plus the same QA `report` the other entry points return. Render with toMarkdown()/toText(), or hand-edit/splice it (no merge API — see toMarkdown's doc comment).\n *\n * @param input a bare pdf path, or `{ words }` to replay a dump (always analyzed as OCR) — unlike extractOcr, never `{ pdfPath }`\n * @param opts see {@link AnalyzeOptions}; for a pdf path, `opts.path` picks 'text' (default) or 'ocr' — analyze() makes no auto-fallback decision (that's pdfToMarkdown's job)\n */\nexport async function analyze(input: string | { words: OcrWordInput[] }, opts: AnalyzeOptions = {}): Promise<Analysis> {\n validatePages(opts.pages);\n if (typeof input !== 'string') return analyzeOcr({ words: validateWordsInput(input) }, opts);\n if (opts.path === 'ocr') return analyzeOcr({ pdfPath: input }, opts);\n return analyzeText(input, opts);\n}\n"],"names":["collect","pageCount","dictAvailable","dictUnavailableWarning","emitPage","renderEmitBlock","recognizePdf","buildPageOCR","DEFAULT_TUNING","headingLevels","toLines","buildMarkdown","renderPagesToMarkdown","buildOcrReport","buildReport","OUTPUT_VERSION","validateOcrAnalysisInput","validatePages","validateWordsInput","toAnalysisWord","w","box","x","y","h","confidence","undefined","text","toAnalysisBlock","b","wordIndex","wordIndexes","words","map","get","kind","type","level","buildOcrAnalysisPages","input","opts","warn","byPage","tuning","validated","first","last","pages","Math","max","page","totalPages","pdfPath","rawFirst","rawLast","min","result","dpi","onProgress","onWarning","onWords","Map","list","set","length","push","pageOcrs","pn","levelOf","values","p","has","blocks","floats","f","join","flatMap","analyzeOcr","m","md","forcePageMarkers","pageMarkers","report","fallback","outputVersion","textLineBox","l","dims","width","height","analyzeText","range","numPages","heightChars","pageDims","pageLines","lines","stats","bodyH","headingSizes","statsMd","hybridPages","filter","largeImage","n","slice","i","analysisPages","analyze","path"],"mappings":"AAAA,2EAA2E;AAC3E,kPAAkP;AAClP,SAASA,OAAO,EAAEC,SAAS,QAAQ,eAAe;AAClD,SAASC,aA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,QAAQrE,KAAyC,EAAEC,OAAuB,CAAC,CAAC;IAChGvB,cAAcuB,KAAKO,KAAK;IACxB,IAAI,OAAOR,UAAU,UAAU,OAAOsC,WAAW;QAAE7C,OAAOd,mBAAmBqB;IAAO,GAAGC;IACvF,IAAIA,KAAKqE,IAAI,KAAK,OAAO,OAAOhC,WAAW;QAAEzB,SAASb;IAAM,GAAGC;IAC/D,OAAOkD,YAAYnD,OAAOC;AAC5B"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/open-source/formats/pdf-to-md/src/analyze.ts"],"sourcesContent":["// analyze() plus the OCR-path core (buildOcrAnalysisPages) that extractOcr\n// shares with it. Text analysis uses text-analysis.ts so it captures the same\n// blocks, source runs, and report input as extractText.\nimport { collect, pageCount } from './collect.ts';\nimport { dictAvailable, dictUnavailableWarning } from './dict.ts';\nimport type { EmitBlock } from './emit.ts';\nimport { emitPage, renderEmitBlock } from './emit.ts';\nimport { recognizePdf } from './engines/tesseract.ts';\n// Type-only: erased at compile time, so this does NOT create a runtime\n// circular dependency even though src/extract.ts imports (value-level) from this module.\nimport type { ExtractOcrOptions, ExtractTextOptions, OcrWordInput } from './extract.ts';\nimport type { PageOCR, Tuning, Word } from './geometry.ts';\nimport { buildPageOCR, DEFAULT_TUNING, headingLevels } from './geometry.ts';\nimport { renderPagesToMarkdown } from './render-analysis.ts';\nimport { buildOcrReport, OUTPUT_VERSION } from './report.ts';\nimport { buildTextAnalysis } from './text-analysis.ts';\nimport type { Analysis, AnalysisBlock, AnalysisPage, AnalysisWord } from './types.ts';\nimport { validateOcrAnalysisInput, validatePages, validateWordsInput } from './validate.ts';\n\n/** analyze(pdf) with the default/'text' path — OCR-only fields don't exist\n * on this branch, so passing e.g. `tuning` without `path: 'ocr'` is a compile error. */\nexport type AnalyzeTextOptions = ExtractTextOptions & { path?: 'text' };\n\n/** analyze(pdf, {path:'ocr'}) — same fields as {@link ExtractOcrOptions},\n * unlocked only once `path: 'ocr'` is set. */\nexport type AnalyzeOcrOptions = ExtractOcrOptions & { path: 'ocr' };\n\n/** Options for {@link analyze}: which fields type-check depends on `path`\n * (ignored, forced 'ocr', when `input` is a `{ words }` replay). */\nexport type AnalyzeOptions = AnalyzeTextOptions | AnalyzeOcrOptions;\n\nfunction toAnalysisWord(w: OcrWordInput): AnalysisWord {\n const box = { x: w.x, y: w.y, w: w.w, h: w.h };\n return w.confidence === undefined ? { text: w.text, box } : { text: w.text, box, confidence: w.confidence };\n}\n\n// EmitBlock -> AnalysisBlock: collapses emit.ts's 'quote' kind (a pull-quote)\n// into Analysis's 'paragraph' type, `text` keeping its literal '> ' prefix — see AnalysisBlock's doc comment (src/types.ts) for why.\nfunction toAnalysisBlock(b: EmitBlock, wordIndex: Map<Word, number>): AnalysisBlock {\n const wordIndexes = b.words.map((w) => wordIndex.get(w) as number);\n if (b.kind === 'heading') return { type: 'heading', text: b.text, level: b.level, wordIndexes };\n if (b.kind === 'review') return { type: 'review', text: renderEmitBlock(b), wordIndexes };\n return { type: 'paragraph', text: renderEmitBlock(b), wordIndexes };\n}\n\n/** The OCR-path core shared by extractOcr and analyze(..., {path:'ocr'}):\n * recognizes (or replays) words and runs the unchanged buildPageOCR/emitPage pipeline. Word IDENTITY (object reference), not value, carries indices through into the emitted blocks — see `geometry.ts`'s OcrLine.words. */\nexport async function buildOcrAnalysisPages(input: { pdfPath: string } | { words: OcrWordInput[] }, opts: ExtractOcrOptions, warn: (m: string) => void): Promise<{ pages: AnalysisPage[]; words: OcrWordInput[] }> {\n const tuning: Tuning = { ...DEFAULT_TUNING, ...opts.tuning };\n const validated = validateOcrAnalysisInput(input);\n\n let words: OcrWordInput[];\n let first: number;\n let last: number;\n if ('words' in validated) {\n // Replay: page range from --pages if given, else every page up to the highest one the dump mentions.\n words = validated.words;\n first = opts.pages?.first ?? 1;\n last = opts.pages?.last ?? opts.pages?.first ?? Math.max(1, ...words.map((w) => w.page));\n } else {\n const totalPages = await pageCount(validated.pdfPath);\n const rawFirst = opts.pages?.first ?? 1;\n const rawLast = opts.pages?.last ?? (opts.pages ? rawFirst : totalPages);\n first = Math.min(Math.max(1, rawFirst), Math.max(1, totalPages));\n last = Math.min(Math.max(first, rawLast), totalPages);\n const result = await recognizePdf(validated.pdfPath, first, last, {\n dpi: opts.dpi,\n onProgress: opts.onProgress,\n onWarning: warn,\n });\n words = result.words;\n }\n\n words = words.filter((word) => word.page >= first && word.page <= last);\n opts.onWords?.(words);\n\n // Stable per-page index, assigned in recognition/replay order — this IS\n // the page's flat `words` list order; `wordIndex` recovers it later since geometry.ts never clones a Word (object identity survives row-building).\n const byPage = new Map<number, OcrWordInput[]>();\n const wordIndex = new Map<Word, number>();\n for (const w of words) {\n const list = byPage.get(w.page);\n if (list) {\n wordIndex.set(w, list.length);\n list.push(w);\n } else {\n wordIndex.set(w, 0);\n byPage.set(w.page, [w]);\n }\n }\n\n const pageOcrs = new Map<number, PageOCR>();\n for (let pn = first; pn <= last; pn++) pageOcrs.set(pn, buildPageOCR(byPage.get(pn) ?? [], tuning));\n const { levelOf } = headingLevels([...pageOcrs.values()], tuning);\n\n const pages: AnalysisPage[] = [];\n for (let pn = first; pn <= last; pn++) {\n const p = pageOcrs.get(pn) as PageOCR;\n // An in-range page with no recognized words stays visible (marker +\n // warning), never silently absent — same rule as extractOcr.\n if (!byPage.has(pn)) warn(`p${pn}: no recognized words, page emitted with no OCR text`);\n\n const blocks: AnalysisBlock[] = emitPage(p, levelOf, tuning).map((b) => toAnalysisBlock(b, wordIndex));\n // Decorative fragments always render as one visible \"> [floats]\" aside\n // per page (matching the one printed line) — see geometry.ts's PageOCR.floats.\n if (p.floats.length) {\n blocks.push({\n type: 'float',\n text: `> [floats] ${p.floats.map((f) => f.text).join(' · ')}`,\n wordIndexes: p.floats.flatMap((f) => f.words).map((w) => wordIndex.get(w) as number),\n });\n }\n\n pages.push({\n page: pn,\n path: 'ocr',\n words: (byPage.get(pn) ?? []).map(toAnalysisWord),\n blocks,\n });\n }\n\n return { pages, words };\n}\n\nasync function analyzeOcr(input: { pdfPath: string } | { words: OcrWordInput[] }, opts: ExtractOcrOptions): Promise<Analysis> {\n const warn = (m: string) => opts.onWarning?.(m);\n if (!dictAvailable()) warn(dictUnavailableWarning());\n const { pages, words } = await buildOcrAnalysisPages(input, opts, warn);\n const md = renderPagesToMarkdown(pages, { forcePageMarkers: true, pageMarkers: true });\n const report = buildOcrReport({ md, fallback: opts.fallback, words });\n return { outputVersion: OUTPUT_VERSION, report, pages };\n}\n\nasync function analyzeText(pdfPath: string, opts: ExtractTextOptions): Promise<Analysis> {\n const warn = (m: string) => opts.onWarning?.(m);\n if (!dictAvailable()) warn(dictUnavailableWarning());\n const range = opts.pages ? { first: opts.pages.first, last: opts.pages.last ?? opts.pages.first } : {};\n const collected = await collect(pdfPath, range);\n const result = buildTextAnalysis(collected, opts.pageMarkers ?? false);\n const hybridPages = collected.pages.filter((page) => page.largeImage).map((page) => page.page);\n for (const n of hybridPages) {\n warn(`p${n}: text layer used but page carries large image(s); if it has baked-in text this output won't have it — compare (don't blindly replace) against --pages ${n} --ocr, which can itself introduce errors`);\n }\n for (const page of collected.pages.filter((page) => page.textChars === 0 && !page.hasImage)) {\n warn(`p${page.page}: no extractable text; the page may be blank or contain only vector content`);\n }\n opts.onWords?.(result.words);\n return result.analysis;\n}\n\n/**\n * Analyze a PDF (or replay an already-recognized OCR word dump) into\n * `Analysis`: plain, JSON-serializable per-page `words`/`blocks` plus the same QA `report` the other entry points return. Render with toMarkdown()/toText(), or hand-edit/splice it (no merge API — see toMarkdown's doc comment).\n *\n * @param input a bare pdf path, or `{ words }` to replay a dump (always analyzed as OCR) — unlike extractOcr, never `{ pdfPath }`\n * @param opts see {@link AnalyzeOptions}; for a PDF path, `path` explicitly\n * selects text (default) or OCR. Automatic per-page routing belongs to\n * pdfToMarkdown and the CLI output formats.\n */\nexport async function analyze(input: string | { words: OcrWordInput[] }, opts: AnalyzeOptions = {}): Promise<Analysis> {\n validatePages(opts.pages);\n if (typeof input !== 'string') return analyzeOcr({ words: validateWordsInput(input) }, opts);\n if (opts.path === 'ocr') return analyzeOcr({ pdfPath: input }, opts);\n return analyzeText(input, opts);\n}\n"],"names":["collect","pageCount","dictAvailable","dictUnavailableWarning","emitPage","renderEmitBlock","recognizePdf","buildPageOCR","DEFAULT_TUNING","headingLevels","renderPagesToMarkdown","buildOcrReport","OUTPUT_VERSION","buildTextAnalysis","validateOcrAnalysisInput","validatePages","validateWordsInput","toAnalysisWord","w","box","x","y","h","confidence","undefined","text","toAnalysisBlock","b","wordIndex","wordIndexes","words","map","get","kind","type","level","buildOcrAnalysisPages","input","opts","warn","byPage","tuning","validated","first","last","pages","Math","max","page","totalPages","pdfPath","rawFirst","rawLast","min","result","dpi","onProgress","onWarning","filter","word","onWords","Map","list","set","length","push","pageOcrs","pn","levelOf","values","p","has","blocks","floats","f","join","flatMap","path","analyzeOcr","m","md","forcePageMarkers","pageMarkers","report","fallback","outputVersion","analyzeText","range","collected","hybridPages","largeImage","n","textChars","hasImage","analysis","analyze"],"mappings":"AAAA,2EAA2E;AAC3E,8EAA8E;AAC9E,wDAAwD;AACxD,SAASA,OAAO,EAAEC,SAAS,QAAQ,eAAe;AAClD,SAASC,aAAa,EAAEC,sBAAsB,QAAQ,YAAY;AAElE,SAASC,QAAQ,EAAEC,eAAe,QAAQ,YAAY;AACtD,SAASC,YAAY,QAAQ,yBAAyB;AAKtD,SAASC,YAAY,EAAEC,cAAc,EAAEC,aAAa,QAAQ,gBAAgB;AAC5E,SAASC,qBAAqB,QAAQ,uBAAuB;AAC7D,SAASC,cAAc,EAAEC,cAAc,QAAQ,cAAc;AAC7D,SAASC,iBAAiB,QAAQ,qBAAqB;AAEvD,SAASC,wBAAwB,EAAEC,aAAa,EAAEC,kBAAkB,QAAQ,gBAAgB;AAc5F,SAASC,eAAeC,CAAe;IACrC,MAAMC,MAAM;QAAEC,GAAGF,EAAEE,CAAC;QAAEC,GAAGH,EAAEG,CAAC;QAAEH,GAAGA,EAAEA,CAAC;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K,CAAC,CAAC,EAAEoD,EAAE,uJAAuJ,EAAEA,EAAE,yCAAyC,CAAC;IAClN;IACA,KAAK,MAAM3C,QAAQwC,UAAU3C,KAAK,CAACa,MAAM,CAAC,CAACV,OAASA,KAAK4C,SAAS,KAAK,KAAK,CAAC5C,KAAK6C,QAAQ,EAAG;QAC3FtD,KAAK,CAAC,CAAC,EAAES,KAAKA,IAAI,CAAC,2EAA2E,CAAC;IACjG;KACAV,gBAAAA,KAAKsB,OAAO,cAAZtB,oCAAAA,mBAAAA,MAAegB,OAAOxB,KAAK;IAC3B,OAAOwB,OAAOwC,QAAQ;AACxB;AAEA;;;;;;;;CAQC,GACD,OAAO,eAAeC,QAAQ1D,KAAyC,EAAEC,OAAuB,CAAC,CAAC;IAChGvB,cAAcuB,KAAKO,KAAK;IACxB,IAAI,OAAOR,UAAU,UAAU,OAAOyC,WAAW;QAAEhD,OAAOd,mBAAmBqB;IAAO,GAAGC;IACvF,IAAIA,KAAKuC,IAAI,KAAK,OAAO,OAAOC,WAAW;QAAE5B,SAASb;IAAM,GAAGC;IAC/D,OAAOgD,YAAYjD,OAAOC;AAC5B"}
|
package/dist/esm/audit.d.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
export interface AuditResult {
|
|
3
3
|
/** JSON lines successfully parsed from the word dump */
|
|
4
4
|
parsed: number;
|
|
5
|
-
/** lines in the dump that were not parseable JSON
|
|
6
|
-
*
|
|
5
|
+
/** lines in the dump that were not parseable JSON; auditWords rejects
|
|
6
|
+
* evidence with any such line */
|
|
7
7
|
nonJsonSkipped: number;
|
|
8
8
|
/** distinct page numbers present in the dump */
|
|
9
9
|
pages: number;
|
|
@@ -13,7 +13,7 @@ export interface AuditResult {
|
|
|
13
13
|
* hyphen-join/split repair is applied; always `>= missing` */
|
|
14
14
|
strictDeficits: number;
|
|
15
15
|
/** words genuinely absent from the markdown after repair-checking; `0` is
|
|
16
|
-
* the only passing value (`pdf-to-md audit` exits
|
|
16
|
+
* the only passing value (`pdf-to-md audit` exits nonzero otherwise) */
|
|
17
17
|
missing: number;
|
|
18
18
|
/** per-page detail for every page with at least one missing word (capped
|
|
19
19
|
* sample of the missing tokens per page) */
|
|
@@ -29,8 +29,9 @@ export interface AuditResult {
|
|
|
29
29
|
lines: string[];
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
|
-
* Audit a --debug-words dump against a
|
|
33
|
-
* must land
|
|
32
|
+
* Audit a --debug-words dump against a Markdown file. Every recognized word
|
|
33
|
+
* must land on its page. Invalid evidence or absent page provenance throws a
|
|
34
|
+
* PdfToMdError with code `AUDIT_INPUT`.
|
|
34
35
|
*
|
|
35
36
|
* @param wordsPath path to a --debug-words JSON-lines dump (`{page, text, ...}` per line)
|
|
36
37
|
* @param mdPath path to the markdown file to check the dump against
|
package/dist/esm/audit.js
CHANGED
|
@@ -2,14 +2,40 @@
|
|
|
2
2
|
// shipped as part of the product, not just a path into the source tree.
|
|
3
3
|
//
|
|
4
4
|
// Every recognized word (a --debug-words dump, or an Analysis page's
|
|
5
|
-
// `words`) must appear in its page's
|
|
5
|
+
// `words`) must appear in its page's Markdown. Matching consumes evidence so
|
|
6
|
+
// a single output occurrence cannot satisfy repeated source occurrences.
|
|
6
7
|
//
|
|
7
8
|
// NOTE: no producer guarantees JSON key order in a --debug-words dump —
|
|
8
9
|
// parse, never grep.
|
|
9
10
|
import { readFileSync } from 'node:fs';
|
|
11
|
+
import { PdfToMdError } from './errors.js';
|
|
12
|
+
import { foldTypographic } from './lines.js';
|
|
10
13
|
import { bodyByPage } from './page-marker.js';
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
const WORD_RE = /[\p{L}\p{N}][\p{L}\p{N}\p{M}]*/gu;
|
|
15
|
+
const HYPHEN_RE = /[\p{Pd}\u2212]\s*$/u;
|
|
16
|
+
const LINE_HYPHEN_RE = /^[\p{Pd}\u2212][ \t]*\r?\n[ \t]*$/u;
|
|
17
|
+
const INLINE_HYPHEN_RE = /^[\p{Pd}\u2212][ \t]*$/u;
|
|
18
|
+
function tokens(text) {
|
|
19
|
+
const folded = foldTypographic(text);
|
|
20
|
+
WORD_RE.lastIndex = 0;
|
|
21
|
+
const matches = [
|
|
22
|
+
...folded.matchAll(WORD_RE)
|
|
23
|
+
];
|
|
24
|
+
return matches.map((match, index)=>{
|
|
25
|
+
var _ref;
|
|
26
|
+
const start = match.index;
|
|
27
|
+
const end = start + match[0].length;
|
|
28
|
+
const next = matches[index + 1];
|
|
29
|
+
return {
|
|
30
|
+
text: match[0].normalize('NFC').toLowerCase(),
|
|
31
|
+
start,
|
|
32
|
+
end,
|
|
33
|
+
gapAfter: folded.slice(end, (_ref = next === null || next === void 0 ? void 0 : next.index) !== null && _ref !== void 0 ? _ref : folded.length)
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function markdownText(md) {
|
|
38
|
+
return md.replace(/<!--[\s\S]*?-->/gu, ' ').replace(/!\[[^\]]*\]\([^)]*\)/gu, ' ').replace(/!\[[^\]]*\]\[[^\]]*\]/gu, ' ').replace(/<img\b[^>]*>/giu, ' ').replace(/<a\b[^>]*>/giu, ' ').replace(/<\/a>/giu, ' ').replace(/^\s*\[[^\]]+\]:[^\n]*$/gmu, ' ').replace(/^>\s*\[(?:review:[^\]]*|floats)\]\s*/gmu, '').replace(/\[([^\]]*)\]\([^)]*\)/gu, '$1').replace(/\[([^\]]*)\]\[[^\]]*\]/gu, '$1');
|
|
13
39
|
}
|
|
14
40
|
function counter(items) {
|
|
15
41
|
var _m_get;
|
|
@@ -18,8 +44,9 @@ function counter(items) {
|
|
|
18
44
|
return m;
|
|
19
45
|
}
|
|
20
46
|
/**
|
|
21
|
-
* Audit a --debug-words dump against a
|
|
22
|
-
* must land
|
|
47
|
+
* Audit a --debug-words dump against a Markdown file. Every recognized word
|
|
48
|
+
* must land on its page. Invalid evidence or absent page provenance throws a
|
|
49
|
+
* PdfToMdError with code `AUDIT_INPUT`.
|
|
23
50
|
*
|
|
24
51
|
* @param wordsPath path to a --debug-words JSON-lines dump (`{page, text, ...}` per line)
|
|
25
52
|
* @param mdPath path to the markdown file to check the dump against
|
|
@@ -27,15 +54,18 @@ function counter(items) {
|
|
|
27
54
|
const rec = new Map();
|
|
28
55
|
let parsed = 0;
|
|
29
56
|
let skipped = 0;
|
|
57
|
+
const errors = [];
|
|
58
|
+
const runsByPage = new Map();
|
|
30
59
|
const wordsRaw = readFileSync(wordsPath, 'utf8');
|
|
31
60
|
// A trailing "\n" at EOF must not yield a spurious final empty line; a
|
|
32
61
|
// genuine blank line mid-file does count, as skipped.
|
|
33
62
|
const rawLines = wordsRaw.split('\n');
|
|
34
63
|
if (rawLines.length > 0 && rawLines[rawLines.length - 1] === '') rawLines.pop();
|
|
35
|
-
for (
|
|
36
|
-
line =
|
|
64
|
+
for (const [lineIndex, rawLine] of rawLines.entries()){
|
|
65
|
+
const line = rawLine.trim();
|
|
37
66
|
if (!line.startsWith('{')) {
|
|
38
67
|
skipped++;
|
|
68
|
+
errors.push(`line ${lineIndex + 1} is not valid JSON`);
|
|
39
69
|
continue;
|
|
40
70
|
}
|
|
41
71
|
let o;
|
|
@@ -43,18 +73,87 @@ function counter(items) {
|
|
|
43
73
|
o = JSON.parse(line);
|
|
44
74
|
} catch {
|
|
45
75
|
skipped++;
|
|
76
|
+
errors.push(`line ${lineIndex + 1} is not valid JSON`);
|
|
46
77
|
continue;
|
|
47
78
|
}
|
|
48
79
|
parsed++;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
80
|
+
if (!isAuditRecord(o)) {
|
|
81
|
+
errors.push(`line ${lineIndex + 1} must contain a positive integer page and non-empty text`);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
let evidence = rec.get(o.page);
|
|
85
|
+
if (!evidence) {
|
|
86
|
+
evidence = {
|
|
87
|
+
tokens: [],
|
|
88
|
+
sourcePairs: []
|
|
89
|
+
};
|
|
90
|
+
rec.set(o.page, evidence);
|
|
91
|
+
}
|
|
92
|
+
const runTokenDetails = tokens(o.text);
|
|
93
|
+
const runTokens = runTokenDetails.map((token)=>token.text);
|
|
94
|
+
const start = evidence.tokens.length;
|
|
95
|
+
evidence.tokens.push(...runTokens);
|
|
96
|
+
for(let index = 0; index + 1 < runTokenDetails.length; index++){
|
|
97
|
+
if (INLINE_HYPHEN_RE.test(runTokenDetails[index].gapAfter)) {
|
|
98
|
+
evidence.sourcePairs.push({
|
|
99
|
+
first: start + index,
|
|
100
|
+
second: start + index + 1,
|
|
101
|
+
explicitHyphen: true
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const run = {
|
|
106
|
+
page: o.page,
|
|
107
|
+
tokens: runTokens.map((_, index)=>start + index),
|
|
108
|
+
terminalHyphen: HYPHEN_RE.test(o.text),
|
|
109
|
+
hyphenOnly: /^[\p{Pd}\u2212]\s*$/u.test(o.text),
|
|
110
|
+
leadingWhitespace: /^\s/u.test(o.text),
|
|
111
|
+
trailingWhitespace: /\s$/u.test(o.text),
|
|
112
|
+
box: recordBox(o)
|
|
113
|
+
};
|
|
114
|
+
const pageRuns = runsByPage.get(o.page);
|
|
115
|
+
if (pageRuns) pageRuns.push(run);
|
|
116
|
+
else runsByPage.set(o.page, [
|
|
117
|
+
run
|
|
118
|
+
]);
|
|
119
|
+
}
|
|
120
|
+
if (parsed === 0) errors.push('word dump contains no JSON records');
|
|
121
|
+
if (rec.size === 0) errors.push('word dump contains no valid records');
|
|
122
|
+
if ([
|
|
123
|
+
...rec.values()
|
|
124
|
+
].every((evidence)=>evidence.tokens.length === 0)) errors.push('word dump contains no lexical tokens');
|
|
125
|
+
for (const [page, runs] of runsByPage){
|
|
126
|
+
for(let index = 0; index + 1 < runs.length; index++){
|
|
127
|
+
const current = runs[index];
|
|
128
|
+
const next = runs[index + 1];
|
|
129
|
+
if (current.tokens.length > 0 && current.terminalHyphen && next.tokens.length > 0) {
|
|
130
|
+
const evidence = rec.get(page);
|
|
131
|
+
evidence.sourcePairs.push({
|
|
132
|
+
first: current.tokens[current.tokens.length - 1],
|
|
133
|
+
second: next.tokens[0],
|
|
134
|
+
explicitHyphen: true
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
if (current.tokens.length === 1 && next.tokens.length === 1 && !current.terminalHyphen && !next.hyphenOnly && !current.trailingWhitespace && !next.leadingWhitespace && touchingFragments(current.box, next.box)) {
|
|
138
|
+
const evidence = rec.get(page);
|
|
139
|
+
evidence.sourcePairs.push({
|
|
140
|
+
first: current.tokens[0],
|
|
141
|
+
second: next.tokens[0],
|
|
142
|
+
explicitHyphen: false
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
if (current.tokens.length === 0 || !next.hyphenOnly || index + 2 >= runs.length) continue;
|
|
146
|
+
const afterHyphen = runs[index + 2];
|
|
147
|
+
if (afterHyphen.tokens.length === 0) continue;
|
|
148
|
+
const evidence = rec.get(page);
|
|
149
|
+
evidence.sourcePairs.push({
|
|
150
|
+
first: current.tokens[current.tokens.length - 1],
|
|
151
|
+
second: afterHyphen.tokens[0],
|
|
152
|
+
explicitHyphen: true
|
|
153
|
+
});
|
|
56
154
|
}
|
|
57
155
|
}
|
|
156
|
+
if (errors.length > 0) throw auditInputError(errors);
|
|
58
157
|
const md = readFileSync(mdPath, 'utf8');
|
|
59
158
|
const out = bodyByPage(md);
|
|
60
159
|
const lines = [];
|
|
@@ -64,21 +163,20 @@ function counter(items) {
|
|
|
64
163
|
const pages = [
|
|
65
164
|
...rec.keys()
|
|
66
165
|
].sort((a, b)=>a - b);
|
|
166
|
+
const absent = pages.filter((page)=>!out.has(page));
|
|
167
|
+
if (absent.length > 0) {
|
|
168
|
+
const markers = absent.map((page)=>`p${page}`).join(', ');
|
|
169
|
+
throw auditInputError([
|
|
170
|
+
`Markdown has no provenance marker for ${markers}; add "### pN" or "<!-- pN -->" for every dumped page`
|
|
171
|
+
]);
|
|
172
|
+
}
|
|
67
173
|
for (const page of pages){
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
const missing =
|
|
74
|
-
for (const [tok, n] of counter((_rec_get = rec.get(page)) !== null && _rec_get !== void 0 ? _rec_get : [])){
|
|
75
|
-
var _ms_get;
|
|
76
|
-
const d = n - ((_ms_get = ms.get(tok)) !== null && _ms_get !== void 0 ? _ms_get : 0);
|
|
77
|
-
if (d <= 0) continue;
|
|
78
|
-
strict += d;
|
|
79
|
-
if (joined.includes(tok)) continue; // hyphen-join / split repair
|
|
80
|
-
for(let i = 0; i < d; i++)missing.push(tok);
|
|
81
|
-
}
|
|
174
|
+
const evidence = rec.get(page);
|
|
175
|
+
const output = tokens(markdownText(out.get(page)));
|
|
176
|
+
const outputTokens = output.map((token)=>token.text);
|
|
177
|
+
const strictMissing = strictDeficits(evidence.tokens, outputTokens);
|
|
178
|
+
strict += strictMissing;
|
|
179
|
+
const missing = missingTokens(evidence, output);
|
|
82
180
|
if (missing.length > 0) {
|
|
83
181
|
total += missing.length;
|
|
84
182
|
perPage.push({
|
|
@@ -91,7 +189,7 @@ function counter(items) {
|
|
|
91
189
|
const words = pages.reduce((sum, p)=>{
|
|
92
190
|
var _ref;
|
|
93
191
|
var _rec_get;
|
|
94
|
-
return sum + ((_ref = (_rec_get = rec.get(p)) === null || _rec_get === void 0 ? void 0 : _rec_get.length) !== null && _ref !== void 0 ? _ref : 0);
|
|
192
|
+
return sum + ((_ref = (_rec_get = rec.get(p)) === null || _rec_get === void 0 ? void 0 : _rec_get.tokens.length) !== null && _ref !== void 0 ? _ref : 0);
|
|
95
193
|
}, 0);
|
|
96
194
|
const summaryLine = `parsed=${parsed} non-json-skipped=${skipped} pages=${rec.size} words=${words} strict_deficits=${strict} MISSING=${total}`;
|
|
97
195
|
lines.push(summaryLine);
|
|
@@ -107,6 +205,115 @@ function counter(items) {
|
|
|
107
205
|
lines
|
|
108
206
|
};
|
|
109
207
|
}
|
|
208
|
+
function isAuditRecord(value) {
|
|
209
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) return false;
|
|
210
|
+
const record = value;
|
|
211
|
+
if (!(typeof record.page === 'number' && Number.isSafeInteger(record.page) && record.page > 0 && typeof record.text === 'string' && record.text.trim().length > 0)) return false;
|
|
212
|
+
const geometry = [
|
|
213
|
+
record.x,
|
|
214
|
+
record.y,
|
|
215
|
+
record.w,
|
|
216
|
+
record.h
|
|
217
|
+
];
|
|
218
|
+
return geometry.every((field)=>field === undefined) || geometry.every((field)=>typeof field === 'number' && Number.isFinite(field)) && record.w >= 0 && record.h >= 0;
|
|
219
|
+
}
|
|
220
|
+
function recordBox(record) {
|
|
221
|
+
if (record.x === undefined || record.y === undefined || record.w === undefined || record.h === undefined) return undefined;
|
|
222
|
+
return {
|
|
223
|
+
x: record.x,
|
|
224
|
+
y: record.y,
|
|
225
|
+
w: record.w,
|
|
226
|
+
h: record.h
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function touchingFragments(first, second) {
|
|
230
|
+
if (!first || !second) return false;
|
|
231
|
+
const height = Math.max(first.h, second.h);
|
|
232
|
+
if (height === 0) return false;
|
|
233
|
+
const gap = second.x - (first.x + first.w);
|
|
234
|
+
// Fragment repairs are restricted to nearly touching runs on one baseline.
|
|
235
|
+
return Math.abs(first.y - second.y) <= height * 0.25 && Math.abs(gap) <= height * 0.15;
|
|
236
|
+
}
|
|
237
|
+
function auditInputError(errors) {
|
|
238
|
+
return new PdfToMdError('AUDIT_INPUT', `audit evidence is invalid: ${errors.join('; ')}`);
|
|
239
|
+
}
|
|
240
|
+
function strictDeficits(source, output) {
|
|
241
|
+
var _outputCounts_get;
|
|
242
|
+
let deficits = 0;
|
|
243
|
+
const outputCounts = counter(output);
|
|
244
|
+
for (const [token, count] of counter(source))deficits += Math.max(0, count - ((_outputCounts_get = outputCounts.get(token)) !== null && _outputCounts_get !== void 0 ? _outputCounts_get : 0));
|
|
245
|
+
return deficits;
|
|
246
|
+
}
|
|
247
|
+
function missingTokens(evidence, output) {
|
|
248
|
+
const sourceAvailable = new Set(evidence.tokens.map((_, index)=>index));
|
|
249
|
+
const outputAvailable = new Set(output.map((_, index)=>index));
|
|
250
|
+
const linePairs = lineHyphenPairs(output);
|
|
251
|
+
const inlinePairs = inlineHyphenPairs(output);
|
|
252
|
+
// Reserve literal occurrences before attempting a repair. A repair may join
|
|
253
|
+
// several source tokens into one output token, so doing it first can steal
|
|
254
|
+
// an occurrence required by an unrelated literal source token.
|
|
255
|
+
for (const sourceIndex of [
|
|
256
|
+
...sourceAvailable
|
|
257
|
+
]){
|
|
258
|
+
const direct = output.findIndex((token, index)=>outputAvailable.has(index) && token.text === evidence.tokens[sourceIndex]);
|
|
259
|
+
if (direct < 0) continue;
|
|
260
|
+
sourceAvailable.delete(sourceIndex);
|
|
261
|
+
outputAvailable.delete(direct);
|
|
262
|
+
}
|
|
263
|
+
for (const pair of evidence.sourcePairs){
|
|
264
|
+
if (!sourceAvailable.has(pair.first) || !sourceAvailable.has(pair.second)) continue;
|
|
265
|
+
const joined = evidence.tokens[pair.first] + evidence.tokens[pair.second];
|
|
266
|
+
if (pair.explicitHyphen) {
|
|
267
|
+
var _inlinePairs_find;
|
|
268
|
+
const hyphenated = (_inlinePairs_find = inlinePairs.find(([first, second])=>outputAvailable.has(first) && outputAvailable.has(second) && output[first].text + output[second].text === joined)) !== null && _inlinePairs_find !== void 0 ? _inlinePairs_find : linePairs.find(([first, second])=>outputAvailable.has(first) && outputAvailable.has(second) && output[first].text + output[second].text === joined);
|
|
269
|
+
if (hyphenated) {
|
|
270
|
+
sourceAvailable.delete(pair.first);
|
|
271
|
+
sourceAvailable.delete(pair.second);
|
|
272
|
+
outputAvailable.delete(hyphenated[0]);
|
|
273
|
+
outputAvailable.delete(hyphenated[1]);
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const direct = output.findIndex((token, index)=>outputAvailable.has(index) && token.text === joined);
|
|
278
|
+
if (direct >= 0) {
|
|
279
|
+
sourceAvailable.delete(pair.first);
|
|
280
|
+
sourceAvailable.delete(pair.second);
|
|
281
|
+
outputAvailable.delete(direct);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
for (const sourceIndex of [
|
|
285
|
+
...sourceAvailable
|
|
286
|
+
]){
|
|
287
|
+
const split = linePairs.find(([first, second])=>outputAvailable.has(first) && outputAvailable.has(second) && output[first].text + output[second].text === evidence.tokens[sourceIndex]);
|
|
288
|
+
if (!split) continue;
|
|
289
|
+
sourceAvailable.delete(sourceIndex);
|
|
290
|
+
outputAvailable.delete(split[0]);
|
|
291
|
+
outputAvailable.delete(split[1]);
|
|
292
|
+
}
|
|
293
|
+
return [
|
|
294
|
+
...sourceAvailable
|
|
295
|
+
].map((index)=>evidence.tokens[index]);
|
|
296
|
+
}
|
|
297
|
+
function lineHyphenPairs(output) {
|
|
298
|
+
const pairs = [];
|
|
299
|
+
for(let index = 0; index + 1 < output.length; index++){
|
|
300
|
+
if (LINE_HYPHEN_RE.test(output[index].gapAfter)) pairs.push([
|
|
301
|
+
index,
|
|
302
|
+
index + 1
|
|
303
|
+
]);
|
|
304
|
+
}
|
|
305
|
+
return pairs;
|
|
306
|
+
}
|
|
307
|
+
function inlineHyphenPairs(output) {
|
|
308
|
+
const pairs = [];
|
|
309
|
+
for(let index = 0; index + 1 < output.length; index++){
|
|
310
|
+
if (INLINE_HYPHEN_RE.test(output[index].gapAfter)) pairs.push([
|
|
311
|
+
index,
|
|
312
|
+
index + 1
|
|
313
|
+
]);
|
|
314
|
+
}
|
|
315
|
+
return pairs;
|
|
316
|
+
}
|
|
110
317
|
// Renders a string list as ['a', 'b'] (single-quoted) to match frozen baseline comparisons byte-for-byte.
|
|
111
318
|
function pyRepr(items) {
|
|
112
319
|
return `[${items.map((s)=>`'${s.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`).join(', ')}]`;
|