@blackbelt-technology/pi-dashboard-document-converter 0.5.4
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/.pi/skills/document-converter/SKILL.md +79 -0
- package/README.md +106 -0
- package/engine/.dockerignore +4 -0
- package/engine/Dockerfile +75 -0
- package/engine/IMAGE_VERSION +1 -0
- package/engine/README.md +65 -0
- package/engine/VENDOR.md +40 -0
- package/engine/build-image.sh +29 -0
- package/engine/document_converter/LICENSE +21 -0
- package/engine/document_converter/__init__.py +365 -0
- package/engine/document_converter/__main__.py +5 -0
- package/engine/document_converter/asciidoc_converter.py +534 -0
- package/engine/document_converter/cli.py +751 -0
- package/engine/document_converter/code_renderer.py +356 -0
- package/engine/document_converter/converter.py +928 -0
- package/engine/document_converter/cover_page.py +448 -0
- package/engine/document_converter/diagram_resizer.py +311 -0
- package/engine/document_converter/diagram_sizing.py +357 -0
- package/engine/document_converter/docx_reader.py +627 -0
- package/engine/document_converter/field_updater.py +277 -0
- package/engine/document_converter/font_manager.py +850 -0
- package/engine/document_converter/frontmatter_parser.py +644 -0
- package/engine/document_converter/graphviz_renderer.py +354 -0
- package/engine/document_converter/image_extractor.py +281 -0
- package/engine/document_converter/image_style.py +237 -0
- package/engine/document_converter/logo_manager.py +1021 -0
- package/engine/document_converter/manifest_manager.py +288 -0
- package/engine/document_converter/mermaid_renderer.py +371 -0
- package/engine/document_converter/meta_mapper.py +398 -0
- package/engine/document_converter/pandoc_styles.py +222 -0
- package/engine/document_converter/pdf_converter.py +735 -0
- package/engine/document_converter/pdf_server.py +283 -0
- package/engine/document_converter/plantuml_renderer.py +460 -0
- package/engine/document_converter/plantuml_server.py +273 -0
- package/engine/document_converter/pyproject.toml +80 -0
- package/engine/document_converter/round_trip.py +312 -0
- package/engine/document_converter/spacing_applier.py +332 -0
- package/engine/document_converter/spacing_config.py +323 -0
- package/engine/document_converter/style_extractor.py +420 -0
- package/engine/document_converter/svg_embedder.py +294 -0
- package/engine/document_converter/system_variables.py +158 -0
- package/engine/document_converter/table_formatter.py +450 -0
- package/engine/document_converter/template_extractor.py +1334 -0
- package/engine/document_converter/template_manager.py +427 -0
- package/engine/document_converter/textbox_extractor.py +551 -0
- package/engine/document_converter/toc_generator.py +559 -0
- package/engine/document_converter/toc_style.py +290 -0
- package/engine/document_converter/tree_renderer.py +328 -0
- package/engine/document_converter/utils.py +134 -0
- package/engine/document_converter/variable_manager.py +394 -0
- package/engine/engine_cli.py +371 -0
- package/engine/frontmatter_filler/doc-meta.sample.yaml +106 -0
- package/engine/frontmatter_filler/fill.py +517 -0
- package/engine/markdown_table_profiler/profile.py +442 -0
- package/engine/nano-banana-styles.yaml +59 -0
- package/engine/tests/test_styled_diagrams.py +109 -0
- package/package.json +32 -0
- package/src/__tests__/engine.test.ts +72 -0
- package/src/__tests__/facade.test.ts +120 -0
- package/src/__tests__/integration.test.ts +101 -0
- package/src/__tests__/ocr.test.ts +34 -0
- package/src/__tests__/provenance.test.ts +89 -0
- package/src/__tests__/routing.test.ts +40 -0
- package/src/engine.ts +187 -0
- package/src/errors.ts +43 -0
- package/src/index.ts +179 -0
- package/src/ocr.ts +43 -0
- package/src/provenance.ts +79 -0
- package/src/routing.ts +49 -0
- package/src/schema.json +69 -0
- package/src/schema.ts +107 -0
- package/src/types.ts +90 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* document-converter facade — the ONLY call surface.
|
|
3
|
+
*
|
|
4
|
+
* Callers never touch Python, docling, pandoc, or the nano-banana CLI. Every
|
|
5
|
+
* method orchestrates the Dockerized engine and returns a typed result or
|
|
6
|
+
* rejects with a `DocConverterError`.
|
|
7
|
+
*/
|
|
8
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
9
|
+
import { basename, join } from "node:path";
|
|
10
|
+
import { runEngine, type EngineConfig } from "./engine.js";
|
|
11
|
+
import { mapOcrCodes } from "./ocr.js";
|
|
12
|
+
import { ingestDocType } from "./routing.js";
|
|
13
|
+
import {
|
|
14
|
+
buildProvenance,
|
|
15
|
+
sha256File,
|
|
16
|
+
stampProvenance,
|
|
17
|
+
} from "./provenance.js";
|
|
18
|
+
import type {
|
|
19
|
+
ConvertToMarkdownOptions,
|
|
20
|
+
ConvertToMarkdownResult,
|
|
21
|
+
ExtractResult,
|
|
22
|
+
FillFrontmatterOptions,
|
|
23
|
+
MergeBackOptions,
|
|
24
|
+
ProduceResult,
|
|
25
|
+
ProfileTablesOptions,
|
|
26
|
+
RenderDocxOptions,
|
|
27
|
+
RenderPdfOptions,
|
|
28
|
+
} from "./types.js";
|
|
29
|
+
|
|
30
|
+
export interface DocumentConverterConfig {
|
|
31
|
+
/** Image tag, e.g. `pi-doc-engine:0.1.0`. */
|
|
32
|
+
image: string;
|
|
33
|
+
/** Staging dir where ingest writes `.md` for kb. Created on demand. */
|
|
34
|
+
stagingDir: string;
|
|
35
|
+
/** Extra host dirs to bind-mount path-identically. */
|
|
36
|
+
mounts?: string[];
|
|
37
|
+
/** Test hook: inject the engine config (e.g. a fake runner). */
|
|
38
|
+
engine?: Partial<EngineConfig>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Build a facade bound to one image + staging dir. */
|
|
42
|
+
export function createDocumentConverter(config: DocumentConverterConfig) {
|
|
43
|
+
const engineCfg = (extraMounts: string[] = []): EngineConfig => ({
|
|
44
|
+
image: config.image,
|
|
45
|
+
mounts: [config.stagingDir, ...(config.mounts ?? []), ...extraMounts],
|
|
46
|
+
...config.engine,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
/** Ingest any supported format -> provenance-stamped Markdown for kb. */
|
|
51
|
+
async convertToMarkdown(
|
|
52
|
+
input: string,
|
|
53
|
+
opts: ConvertToMarkdownOptions = {},
|
|
54
|
+
): Promise<ConvertToMarkdownResult> {
|
|
55
|
+
const docType = ingestDocType(input);
|
|
56
|
+
const ocr = opts.ocr ?? {};
|
|
57
|
+
const engine = ocr.engine ?? "easyocr";
|
|
58
|
+
const codes = ocr.lang ? mapOcrCodes(ocr.lang, engine) : [];
|
|
59
|
+
|
|
60
|
+
const { markdown } = await runEngine<{ markdown: string }>(engineCfg(), {
|
|
61
|
+
command: "convertToMarkdown",
|
|
62
|
+
input,
|
|
63
|
+
ocr: { mode: ocr.mode ?? "auto", engine, codes },
|
|
64
|
+
tables: opts.tables ?? "on",
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Provenance is written on the TS side (task 3.3) so kb can trace chunks.
|
|
68
|
+
const sha256 = await sha256File(input);
|
|
69
|
+
const provenance = buildProvenance({ sourcePath: input, sha256, docType });
|
|
70
|
+
const body = stampProvenance(markdown, provenance);
|
|
71
|
+
|
|
72
|
+
await mkdir(config.stagingDir, { recursive: true });
|
|
73
|
+
const output =
|
|
74
|
+
opts.output ?? join(config.stagingDir, replaceExt(basename(input), ".md"));
|
|
75
|
+
await writeFile(output, body, "utf-8");
|
|
76
|
+
return { output, provenance };
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
/** Markdown -> templated DOCX (TOC, cover, diagrams from frontmatter). */
|
|
80
|
+
async renderDocx(input: string, opts: RenderDocxOptions): Promise<ProduceResult> {
|
|
81
|
+
return runEngine<ProduceResult>(engineCfg(), {
|
|
82
|
+
command: "renderDocx",
|
|
83
|
+
input,
|
|
84
|
+
output: opts.output,
|
|
85
|
+
template: opts.template,
|
|
86
|
+
templatesDir: opts.templatesDir,
|
|
87
|
+
language: opts.language,
|
|
88
|
+
nano_banana: opts.nanoBanana,
|
|
89
|
+
cacheDir: opts.cacheDir,
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
/** Markdown|DOCX -> PDF (pandoc/Gotenberg path). */
|
|
94
|
+
async renderPdf(input: string, opts: RenderPdfOptions): Promise<ProduceResult> {
|
|
95
|
+
return runEngine<ProduceResult>(engineCfg(), {
|
|
96
|
+
command: "renderPdf",
|
|
97
|
+
input,
|
|
98
|
+
output: opts.output,
|
|
99
|
+
pageSize: opts.pageSize,
|
|
100
|
+
template: opts.template,
|
|
101
|
+
});
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
/** DOCX -> editable Markdown + `document_meta.xml` for round-trip. */
|
|
105
|
+
async extractForEdit(input: string, output: string): Promise<ExtractResult> {
|
|
106
|
+
return runEngine<ExtractResult>(engineCfg(), {
|
|
107
|
+
command: "extractForEdit",
|
|
108
|
+
input,
|
|
109
|
+
output,
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
/** Merge edited Markdown back into a DOCX, preserving formatting. */
|
|
114
|
+
async mergeBack(
|
|
115
|
+
original: string,
|
|
116
|
+
edited: string,
|
|
117
|
+
opts: MergeBackOptions,
|
|
118
|
+
): Promise<ProduceResult> {
|
|
119
|
+
return runEngine<ProduceResult>(engineCfg(), {
|
|
120
|
+
command: "mergeBack",
|
|
121
|
+
original,
|
|
122
|
+
edited,
|
|
123
|
+
output: opts.output,
|
|
124
|
+
meta: opts.meta,
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
/** Fill/refresh YAML frontmatter across Markdown files. */
|
|
129
|
+
async fillFrontmatter(
|
|
130
|
+
paths: string[],
|
|
131
|
+
opts: FillFrontmatterOptions = {},
|
|
132
|
+
): Promise<void> {
|
|
133
|
+
await runEngine(engineCfg(), {
|
|
134
|
+
command: "fillFrontmatter",
|
|
135
|
+
paths,
|
|
136
|
+
mode: opts.mode,
|
|
137
|
+
config: opts.config,
|
|
138
|
+
language: opts.language,
|
|
139
|
+
set: opts.set,
|
|
140
|
+
apply: opts.apply ?? true,
|
|
141
|
+
});
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
/** Inject `table_profiles:` width ratios into frontmatter. */
|
|
145
|
+
async profileTables(
|
|
146
|
+
paths: string[],
|
|
147
|
+
opts: ProfileTablesOptions = {},
|
|
148
|
+
): Promise<void> {
|
|
149
|
+
await runEngine(engineCfg(), {
|
|
150
|
+
command: "profileTables",
|
|
151
|
+
paths,
|
|
152
|
+
apply: opts.apply ?? true,
|
|
153
|
+
percentile: opts.percentile,
|
|
154
|
+
smoothing: opts.smoothing,
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The facade instance type. */
|
|
161
|
+
export type DocumentConverter = ReturnType<typeof createDocumentConverter>;
|
|
162
|
+
|
|
163
|
+
function replaceExt(name: string, ext: string): string {
|
|
164
|
+
return name.replace(/\.[^.]+$/, "") + ext;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export { DocConverterError } from "./errors.js";
|
|
168
|
+
export type { DocConverterErrorCode } from "./errors.js";
|
|
169
|
+
export * from "./types.js";
|
|
170
|
+
export type {
|
|
171
|
+
DocumentFrontmatter,
|
|
172
|
+
Provenance,
|
|
173
|
+
NanoBananaConfig,
|
|
174
|
+
TableProfiles,
|
|
175
|
+
TableProfile,
|
|
176
|
+
} from "./schema.js";
|
|
177
|
+
export { mapOcrCodes, SUPPORTED_LANGUAGES } from "./ocr.js";
|
|
178
|
+
export { docTypeOf, isIngestable, ingestDocType } from "./routing.js";
|
|
179
|
+
export { buildProvenance, stampProvenance, provenanceFrontmatter, sha256File } from "./provenance.js";
|
package/src/ocr.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical language name -> per-engine OCR code.
|
|
3
|
+
*
|
|
4
|
+
* The facade takes canonical names (`"hungarian"`) and maps to the code each
|
|
5
|
+
* engine expects (EasyOCR `hu` vs Tesseract `hun`). An unsupported name/engine
|
|
6
|
+
* combo raises a typed error rather than silently producing empty OCR.
|
|
7
|
+
*/
|
|
8
|
+
import { DocConverterError } from "./errors.js";
|
|
9
|
+
import type { OcrEngine } from "./types.js";
|
|
10
|
+
|
|
11
|
+
/** canonical name -> { engine -> code }. Extend as languages are needed. */
|
|
12
|
+
const LANG_CODES: Record<string, Partial<Record<OcrEngine, string>>> = {
|
|
13
|
+
english: { easyocr: "en", tesseract: "eng", ocrmac: "en-US" },
|
|
14
|
+
hungarian: { easyocr: "hu", tesseract: "hun", ocrmac: "hu-HU" },
|
|
15
|
+
german: { easyocr: "de", tesseract: "deu", ocrmac: "de-DE" },
|
|
16
|
+
french: { easyocr: "fr", tesseract: "fra", ocrmac: "fr-FR" },
|
|
17
|
+
spanish: { easyocr: "es", tesseract: "spa", ocrmac: "es-ES" },
|
|
18
|
+
italian: { easyocr: "it", tesseract: "ita", ocrmac: "it-IT" },
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Map canonical language names to per-engine codes for the given engine.
|
|
23
|
+
* RapidOCR ignores language codes (returns `[]`). Throws `OCR_LANG_UNSUPPORTED`
|
|
24
|
+
* for any name the engine cannot serve.
|
|
25
|
+
*/
|
|
26
|
+
export function mapOcrCodes(langs: string[], engine: OcrEngine): string[] {
|
|
27
|
+
if (engine === "rapidocr") return [];
|
|
28
|
+
return langs.map((raw) => {
|
|
29
|
+
const name = raw.trim().toLowerCase();
|
|
30
|
+
const entry = LANG_CODES[name];
|
|
31
|
+
const code = entry?.[engine];
|
|
32
|
+
if (!code) {
|
|
33
|
+
throw new DocConverterError({
|
|
34
|
+
code: "OCR_LANG_UNSUPPORTED",
|
|
35
|
+
message: `language "${raw}" is not supported by OCR engine "${engine}"`,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return code;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Canonical names known to the mapper (for validation / docs). */
|
|
43
|
+
export const SUPPORTED_LANGUAGES = Object.keys(LANG_CODES);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provenance frontmatter writer.
|
|
3
|
+
*
|
|
4
|
+
* Stamps each ingested `.md` with origin metadata so kb chunks trace back to the
|
|
5
|
+
* source file. Re-ingesting an unchanged file yields an identical `sha256`, so
|
|
6
|
+
* the staging output is byte-stable (idempotent by hash).
|
|
7
|
+
*/
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { readFile } from "node:fs/promises";
|
|
10
|
+
import type { DocType, Provenance } from "./schema.js";
|
|
11
|
+
|
|
12
|
+
/** Compute the sha256 of a file's bytes. */
|
|
13
|
+
export async function sha256File(path: string): Promise<string> {
|
|
14
|
+
const buf = await readFile(path);
|
|
15
|
+
return createHash("sha256").update(buf).digest("hex");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface BuildProvenanceInput {
|
|
19
|
+
sourcePath: string;
|
|
20
|
+
sha256: string;
|
|
21
|
+
docType: DocType;
|
|
22
|
+
/** Defaults to now (ISO-8601). Pass a fixed value for deterministic tests. */
|
|
23
|
+
convertedAt?: string;
|
|
24
|
+
page?: number;
|
|
25
|
+
slide?: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Assemble a `Provenance` record. */
|
|
29
|
+
export function buildProvenance(input: BuildProvenanceInput): Provenance {
|
|
30
|
+
const prov: Provenance = {
|
|
31
|
+
source_path: input.sourcePath,
|
|
32
|
+
sha256: input.sha256,
|
|
33
|
+
doc_type: input.docType,
|
|
34
|
+
converted_at: input.convertedAt ?? new Date().toISOString(),
|
|
35
|
+
};
|
|
36
|
+
if (input.page !== undefined) prov.page = input.page;
|
|
37
|
+
if (input.slide !== undefined) prov.slide = input.slide;
|
|
38
|
+
return prov;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The `provenance:` YAML key block (no fences), deterministic key order. */
|
|
42
|
+
function provenanceKeys(prov: Provenance): string {
|
|
43
|
+
const lines = ["provenance:"];
|
|
44
|
+
lines.push(` source_path: ${yamlScalar(prov.source_path)}`);
|
|
45
|
+
lines.push(` sha256: ${prov.sha256}`);
|
|
46
|
+
lines.push(` doc_type: ${prov.doc_type}`);
|
|
47
|
+
lines.push(` converted_at: ${yamlScalar(prov.converted_at)}`);
|
|
48
|
+
if (prov.page !== undefined) lines.push(` page: ${prov.page}`);
|
|
49
|
+
if (prov.slide !== undefined) lines.push(` slide: ${prov.slide}`);
|
|
50
|
+
return lines.join("\n");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Serialize a standalone `provenance:` YAML frontmatter block. */
|
|
54
|
+
export function provenanceFrontmatter(prov: Provenance): string {
|
|
55
|
+
return `---\n${provenanceKeys(prov)}\n---\n`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Prepend a provenance frontmatter block to a Markdown body. If the body already
|
|
60
|
+
* opens with a `---` frontmatter block, the provenance keys are injected just
|
|
61
|
+
* after the opening fence so existing keys are preserved.
|
|
62
|
+
*/
|
|
63
|
+
export function stampProvenance(markdown: string, prov: Provenance): string {
|
|
64
|
+
if (!markdown.startsWith("---\n")) {
|
|
65
|
+
return `${provenanceFrontmatter(prov)}${markdown}`;
|
|
66
|
+
}
|
|
67
|
+
// Inject provenance keys right after the opening `---\n`.
|
|
68
|
+
return `---\n${provenanceKeys(prov)}\n${markdown.slice(4)}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Quote a YAML scalar when it could be misparsed; otherwise pass through.
|
|
73
|
+
* Safe plain set excludes spaces (a path with spaces is quoted); colons are
|
|
74
|
+
* allowed so ISO timestamps stay unquoted.
|
|
75
|
+
*/
|
|
76
|
+
function yamlScalar(value: string): string {
|
|
77
|
+
if (/^[\w./@:-]+$/.test(value)) return value;
|
|
78
|
+
return JSON.stringify(value);
|
|
79
|
+
}
|
package/src/routing.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Extension routing: file extension -> DocType, and ingest support check. */
|
|
2
|
+
import { extname } from "node:path";
|
|
3
|
+
import { DocConverterError } from "./errors.js";
|
|
4
|
+
import type { DocType } from "./schema.js";
|
|
5
|
+
|
|
6
|
+
const EXT_TO_DOCTYPE: Record<string, DocType> = {
|
|
7
|
+
".pdf": "pdf",
|
|
8
|
+
".docx": "docx",
|
|
9
|
+
".pptx": "pptx",
|
|
10
|
+
".xlsx": "xlsx",
|
|
11
|
+
".md": "md",
|
|
12
|
+
".markdown": "md",
|
|
13
|
+
".mdx": "md",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** Formats the ingest direction (-> Markdown) accepts. */
|
|
17
|
+
const INGESTABLE: ReadonlySet<DocType> = new Set(["pdf", "docx", "pptx", "xlsx"]);
|
|
18
|
+
|
|
19
|
+
/** Resolve a file's DocType from its extension, or throw `UNSUPPORTED_FORMAT`. */
|
|
20
|
+
export function docTypeOf(path: string): DocType {
|
|
21
|
+
const ext = extname(path).toLowerCase();
|
|
22
|
+
const t = EXT_TO_DOCTYPE[ext];
|
|
23
|
+
if (!t) {
|
|
24
|
+
throw new DocConverterError({
|
|
25
|
+
code: "UNSUPPORTED_FORMAT",
|
|
26
|
+
message: `unsupported file extension: ${ext || "(none)"}`,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return t;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** True when the file can be ingested to Markdown. */
|
|
33
|
+
export function isIngestable(path: string): boolean {
|
|
34
|
+
const ext = extname(path).toLowerCase();
|
|
35
|
+
const t = EXT_TO_DOCTYPE[ext];
|
|
36
|
+
return t !== undefined && INGESTABLE.has(t);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Resolve DocType for ingest; throw if the format is not ingestable. */
|
|
40
|
+
export function ingestDocType(path: string): DocType {
|
|
41
|
+
const t = docTypeOf(path);
|
|
42
|
+
if (!INGESTABLE.has(t)) {
|
|
43
|
+
throw new DocConverterError({
|
|
44
|
+
code: "UNSUPPORTED_FORMAT",
|
|
45
|
+
message: `cannot ingest ${t} to Markdown`,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return t;
|
|
49
|
+
}
|
package/src/schema.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"$id": "https://blackbelt.technology/pi-dashboard/document-frontmatter.schema.json",
|
|
4
|
+
"title": "Unified document frontmatter",
|
|
5
|
+
"description": "Shared bus contract read/written by frontmatter-filler, markdown-table-profiler, document_converter, ingest, and kb. Mirrors src/schema.ts; keep both in sync.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"additionalProperties": true,
|
|
8
|
+
"properties": {
|
|
9
|
+
"template": { "type": "string" },
|
|
10
|
+
"document_id": { "type": "string" },
|
|
11
|
+
"document_type": { "type": "string" },
|
|
12
|
+
"document_title": { "type": "string" },
|
|
13
|
+
"document_name": { "type": "string" },
|
|
14
|
+
"project_name": { "type": "string" },
|
|
15
|
+
"project_description": { "type": "string" },
|
|
16
|
+
"author": { "type": "string" },
|
|
17
|
+
"client_name": { "type": "string" },
|
|
18
|
+
"company_info": { "type": "string" },
|
|
19
|
+
"contact_info": { "type": "string" },
|
|
20
|
+
"version": { "type": "string" },
|
|
21
|
+
"language": { "type": "string", "description": "Canonical language code (en, hu, de, ...)." },
|
|
22
|
+
"toc_heading": { "type": "string" },
|
|
23
|
+
"enable_cover_page": { "type": "boolean" },
|
|
24
|
+
"enable_toc": { "type": "boolean" },
|
|
25
|
+
"logos": {
|
|
26
|
+
"type": "object",
|
|
27
|
+
"additionalProperties": { "type": "string" }
|
|
28
|
+
},
|
|
29
|
+
"diagram_format": { "type": "string", "enum": ["png", "svg"] },
|
|
30
|
+
"diagram_width": { "type": "number" },
|
|
31
|
+
"diagram_scale": { "type": "number" },
|
|
32
|
+
"nano_banana": {
|
|
33
|
+
"type": "object",
|
|
34
|
+
"required": ["enabled"],
|
|
35
|
+
"properties": {
|
|
36
|
+
"enabled": { "type": "boolean" },
|
|
37
|
+
"style": { "type": "string", "description": "Named style from nano-banana-styles.yaml." }
|
|
38
|
+
},
|
|
39
|
+
"additionalProperties": false
|
|
40
|
+
},
|
|
41
|
+
"table_profiles": {
|
|
42
|
+
"type": "object",
|
|
43
|
+
"description": "profile-id (tbl_<hash>_<lang>) -> { columns, widths }.",
|
|
44
|
+
"additionalProperties": {
|
|
45
|
+
"type": "object",
|
|
46
|
+
"required": ["columns", "widths"],
|
|
47
|
+
"properties": {
|
|
48
|
+
"columns": { "type": "array", "items": { "type": "string" } },
|
|
49
|
+
"widths": { "type": "array", "items": { "type": "number" } }
|
|
50
|
+
},
|
|
51
|
+
"additionalProperties": false
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"provenance": {
|
|
55
|
+
"type": "object",
|
|
56
|
+
"description": "Ingest-written origin record; kb traces chunks back via this.",
|
|
57
|
+
"required": ["source_path", "sha256", "doc_type", "converted_at"],
|
|
58
|
+
"properties": {
|
|
59
|
+
"source_path": { "type": "string" },
|
|
60
|
+
"sha256": { "type": "string" },
|
|
61
|
+
"doc_type": { "type": "string", "enum": ["pdf", "docx", "pptx", "xlsx", "md"] },
|
|
62
|
+
"converted_at": { "type": "string", "format": "date-time" },
|
|
63
|
+
"page": { "type": "number" },
|
|
64
|
+
"slide": { "type": "number" }
|
|
65
|
+
},
|
|
66
|
+
"additionalProperties": false
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified document-frontmatter schema — the shared bus.
|
|
3
|
+
*
|
|
4
|
+
* ONE YAML frontmatter contract read/written by every stage:
|
|
5
|
+
* - frontmatter-filler WRITES template vars (project_name, author, toc_heading, language, logos, document_id, ...)
|
|
6
|
+
* - markdown-table-profiler WRITES `table_profiles`
|
|
7
|
+
* - document_converter (produce) READS vars + `nano_banana` + `table_profiles` + toc/cover flags
|
|
8
|
+
* - ingest WRITES `provenance` (source_path, sha256, doc_type, converted_at, page/slide)
|
|
9
|
+
* - kb READS `provenance` to trace chunks back to origin
|
|
10
|
+
*
|
|
11
|
+
* Defined first because it is expensive to change later. The JSON Schema mirror
|
|
12
|
+
* lives at `src/schema.json`; keep both in sync.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** Diagram raster format the produce engine emits for rendered diagrams. */
|
|
16
|
+
export type DiagramFormat = "png" | "svg";
|
|
17
|
+
|
|
18
|
+
/** Logo placeholders resolved by the produce engine, keyed by slot name. */
|
|
19
|
+
export type LogoMap = Record<string, string>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Opt-in styled-diagram rendering. When `enabled`, each Mermaid block is
|
|
23
|
+
* replaced by a cached `.mermaid-cache/<md5>.png` generated via the nano-banana
|
|
24
|
+
* CLI using the named `style`. Any failure falls back to mmdc.
|
|
25
|
+
*/
|
|
26
|
+
export interface NanoBananaConfig {
|
|
27
|
+
enabled: boolean;
|
|
28
|
+
/** Named style from `nano-banana-styles.yaml`. */
|
|
29
|
+
style?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* One profiled table: deterministic id -> column headers + relative width
|
|
34
|
+
* multipliers (mean ~ 1.0). Written by markdown-table-profiler.
|
|
35
|
+
*/
|
|
36
|
+
export interface TableProfile {
|
|
37
|
+
columns: string[];
|
|
38
|
+
widths: number[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** `table_profiles` block: profile-id -> profile. Id form: `tbl_<hash>_<lang>`. */
|
|
42
|
+
export type TableProfiles = Record<string, TableProfile>;
|
|
43
|
+
|
|
44
|
+
/** Document type for provenance — drives ingest routing + kb tracing. */
|
|
45
|
+
export type DocType = "pdf" | "docx" | "pptx" | "xlsx" | "md";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Ingest-written provenance. Lets kb chunks trace back to the originating file.
|
|
49
|
+
* `page`/`slide` present only where the source format has them.
|
|
50
|
+
*/
|
|
51
|
+
export interface Provenance {
|
|
52
|
+
source_path: string;
|
|
53
|
+
sha256: string;
|
|
54
|
+
doc_type: DocType;
|
|
55
|
+
/** ISO-8601 timestamp of the conversion run. */
|
|
56
|
+
converted_at: string;
|
|
57
|
+
page?: number;
|
|
58
|
+
slide?: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Template/branding variables consumed by the produce engine. Written by
|
|
63
|
+
* frontmatter-filler (defaults -> language pack -> glob override -> CLI).
|
|
64
|
+
* Open set: the engine tolerates extra string vars, so this is non-exhaustive.
|
|
65
|
+
*/
|
|
66
|
+
export interface TemplateVars {
|
|
67
|
+
template?: string;
|
|
68
|
+
document_id?: string;
|
|
69
|
+
document_type?: string;
|
|
70
|
+
document_title?: string;
|
|
71
|
+
document_name?: string;
|
|
72
|
+
project_name?: string;
|
|
73
|
+
project_description?: string;
|
|
74
|
+
author?: string;
|
|
75
|
+
client_name?: string;
|
|
76
|
+
company_info?: string;
|
|
77
|
+
contact_info?: string;
|
|
78
|
+
version?: string;
|
|
79
|
+
/** Canonical language code (`en`, `hu`, `de`, ...). Drives language pack. */
|
|
80
|
+
language?: string;
|
|
81
|
+
toc_heading?: string;
|
|
82
|
+
enable_cover_page?: boolean;
|
|
83
|
+
enable_toc?: boolean;
|
|
84
|
+
logos?: LogoMap;
|
|
85
|
+
diagram_format?: DiagramFormat;
|
|
86
|
+
diagram_width?: number;
|
|
87
|
+
diagram_scale?: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The unified frontmatter document. Every field is optional because each stage
|
|
92
|
+
* fills only its slice; downstream stages read what they need.
|
|
93
|
+
*/
|
|
94
|
+
export interface DocumentFrontmatter extends TemplateVars {
|
|
95
|
+
nano_banana?: NanoBananaConfig;
|
|
96
|
+
table_profiles?: TableProfiles;
|
|
97
|
+
provenance?: Provenance;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Frontmatter keys the ingest stage owns. */
|
|
101
|
+
export const PROVENANCE_KEY = "provenance" as const;
|
|
102
|
+
|
|
103
|
+
/** Frontmatter keys the table profiler owns. */
|
|
104
|
+
export const TABLE_PROFILES_KEY = "table_profiles" as const;
|
|
105
|
+
|
|
106
|
+
/** Frontmatter key the styled-diagram path reads. */
|
|
107
|
+
export const NANO_BANANA_KEY = "nano_banana" as const;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/** Facade option + result types. */
|
|
2
|
+
import type { DocType, NanoBananaConfig, Provenance } from "./schema.js";
|
|
3
|
+
|
|
4
|
+
/** OCR engines bundled in the image. */
|
|
5
|
+
export type OcrEngine = "easyocr" | "tesseract" | "rapidocr" | "ocrmac";
|
|
6
|
+
|
|
7
|
+
/** OCR run mode. `auto` = native-first; `force` = full-page OCR; `off` = none. */
|
|
8
|
+
export type OcrMode = "auto" | "force" | "off";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Per-convert OCR config. `lang` carries CANONICAL language names
|
|
12
|
+
* (e.g. `"hungarian"`); the facade maps them to per-engine codes before the
|
|
13
|
+
* engine sees them, so a wrong code can never silently produce empty output.
|
|
14
|
+
*/
|
|
15
|
+
export interface OcrOptions {
|
|
16
|
+
mode?: OcrMode;
|
|
17
|
+
lang?: string[];
|
|
18
|
+
engine?: OcrEngine;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Options for ingest (any format -> Markdown). */
|
|
22
|
+
export interface ConvertToMarkdownOptions {
|
|
23
|
+
/** Where the staging `.md` is written. Defaults under the staging dir. */
|
|
24
|
+
output?: string;
|
|
25
|
+
ocr?: OcrOptions;
|
|
26
|
+
/** `off` disables TableFormer (huge-PDF hang escape hatch). Default `on`. */
|
|
27
|
+
tables?: "on" | "off";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Result of an ingest run. */
|
|
31
|
+
export interface ConvertToMarkdownResult {
|
|
32
|
+
/** Path to the staging `.md` with provenance frontmatter. */
|
|
33
|
+
output: string;
|
|
34
|
+
provenance: Provenance;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Options for Markdown -> DOCX. */
|
|
38
|
+
export interface RenderDocxOptions {
|
|
39
|
+
output: string;
|
|
40
|
+
template?: string;
|
|
41
|
+
templatesDir?: string;
|
|
42
|
+
language?: string;
|
|
43
|
+
nanoBanana?: NanoBananaConfig;
|
|
44
|
+
/** Diagram cache dir (`.mermaid-cache`); defaults beside the input. */
|
|
45
|
+
cacheDir?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Options for Markdown|DOCX -> PDF. */
|
|
49
|
+
export interface RenderPdfOptions {
|
|
50
|
+
output: string;
|
|
51
|
+
pageSize?: "a4" | "letter" | "legal" | "a3" | "a5";
|
|
52
|
+
template?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Result carrying the produced artifact path. */
|
|
56
|
+
export interface ProduceResult {
|
|
57
|
+
output: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Result of extracting a DOCX for editing. */
|
|
61
|
+
export interface ExtractResult {
|
|
62
|
+
output: string;
|
|
63
|
+
/** Path to `document_meta.xml` used to merge edits back. */
|
|
64
|
+
meta: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Options for merging edited Markdown back into a DOCX. */
|
|
68
|
+
export interface MergeBackOptions {
|
|
69
|
+
output: string;
|
|
70
|
+
/** `document_meta.xml` from the prior extract; preserves formatting. */
|
|
71
|
+
meta?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Frontmatter-fill options (subset of the engine fill.py surface). */
|
|
75
|
+
export interface FillFrontmatterOptions {
|
|
76
|
+
mode?: "fill" | "update" | "replace";
|
|
77
|
+
config?: string;
|
|
78
|
+
language?: string;
|
|
79
|
+
set?: string[];
|
|
80
|
+
apply?: boolean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Table-profile options. */
|
|
84
|
+
export interface ProfileTablesOptions {
|
|
85
|
+
apply?: boolean;
|
|
86
|
+
percentile?: number;
|
|
87
|
+
smoothing?: "linear" | "sqrt" | "log";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type { DocType };
|