基本完成26.05

This commit is contained in:
hydrogen committed 2026-05-16 22:32:25 +08:00
1 parent 71b70b3773
commit 8047347b56
262 files changed
+25821 -8528

No files matched your search

+72 -14
View File
@@ -250,20 +250,59 @@ export class IndexBuilder {
async build_index() {
const pdf_obj = await this.read_pdf();
const { titles, passages_index } = this.get_index_titles_from_file_name();
// Normalize titles (remove spaces/newlines) and require exact equality.
// Matching supports title text split across multiple PDF lines.
const normalizedTitles = titles.map((t) => ({
orig: t,
norm: t.replaceAll("\n", "").replaceAll(" ", ""),
}));
for (let i = 1; i <= this.total_pages; i++) {
let current_page_text = pdf_obj.getPageText(i).replaceAll("\n", "");
current_page_text = current_page_text.replaceAll(" ", "");
current_page_text = current_page_text.replaceAll("\r", "");
current_page_text = current_page_text.replaceAll("\t", "");
current_page_text = current_page_text.replaceAll("\f", "");
current_page_text = current_page_text.replaceAll("\v", "");
const title = titles.find((title) => current_page_text.includes(title));
if (title) {
this.indexed_map.set(title, {
page: i,
passage_index: passages_index.get(title)?.valueOf() ?? 0,
const pageTextRaw = pdf_obj.getPageText(i);
const lines = pageTextRaw.split(/\r?\n/).map((l) => l.trim()).filter(
(l) => l.length > 0,
);
const normalizedLines = lines.map((line) => line.replaceAll(" ", ""));
const found: Array<{ orig: string; index: number }> = [];
// Exact-title matching only, but ignore line breaks by allowing
// concatenation of consecutive lines until title length is reached.
for (const tt of normalizedTitles) {
if (this.indexed_map.has(tt.orig)) continue;
for (let start = 0; start < normalizedLines.length; start++) {
let merged = "";
for (let end = start; end < normalizedLines.length; end++) {
merged += normalizedLines[end];
if (merged.length > tt.norm.length) {
break;
}
if (merged === tt.norm) {
found.push({ orig: tt.orig, index: start });
start = normalizedLines.length; // break outer loop for this title
break;
}
}
}
}
if (found.length > 0) {
// Deduplicate and sort by occurrence index to preserve reading order.
const unique = new Map<string, number>();
found.forEach((f) => {
if (!unique.has(f.orig)) unique.set(f.orig, f.index);
});
continue;
const matches = [...unique.entries()].sort((a, b) => a[1] - b[1]);
for (const [title] of matches) {
if (this.indexed_map.has(title)) continue;
this.indexed_map.set(title, {
page: i,
passage_index: passages_index.get(title)?.valueOf() ?? 0,
});
}
}
}
}
@@ -440,21 +479,40 @@ export class OpenPGPSigner {
file_path: PathLike;
output_path: PathLike;
pri_key: Promise<openpgp.PrivateKey>;
pri_passphrase?: string;
constructor(file: PathLike, pri_key: PathLike, output_path: PathLike) {
constructor(
file: PathLike,
pri_key: PathLike,
output_path: PathLike,
pri_passphrase?: string,
) {
this.file_path = file;
this.output_path = output_path;
this.pri_passphrase = pri_passphrase;
this.pri_key = openpgp.readPrivateKey({
armoredKey: Deno.readTextFileSync(pri_key as string),
});
}
private async getDecryptedPrivateKey(): Promise<openpgp.PrivateKey> {
let privateKey = await this.pri_key;
if (this.pri_passphrase) {
privateKey = await openpgp.decryptKey({
privateKey,
passphrase: this.pri_passphrase,
});
}
return privateKey;
}
async sign() {
const file_bytes = await Deno.readFile(this.file_path as string);
const message = await openpgp.createMessage({ binary: file_bytes });
const signingKey = await this.getDecryptedPrivateKey();
const signature = await openpgp.sign({
message,
signingKeys: await this.pri_key,
signingKeys: signingKey,
format: "armored",
detached: true,
});