From 5cb3df617a332d226ebd10052fcbe5c8ae50433a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20W=C3=B6lfer?= Date: Tue, 6 May 2025 10:41:47 +0200 Subject: [PATCH] Fixed parsing --- parse.ts | 40 ++++++++++++++++++++-------------------- parse_test.ts | 1 - 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/parse.ts b/parse.ts index a73ae62..300e9dd 100644 --- a/parse.ts +++ b/parse.ts @@ -10,11 +10,11 @@ export function parseTemplate(description: string): Record { throw new Error("Could not find template") } - const propertyRegex = /\|\s*(?[^|]*?)\s*=(?[^|]*?)\s*$/gm + const propertyRegex = /\|\s*(?[^|]*?)\s*=(?[^|}]*)\s*$/gm const properties = match[1].matchAll(propertyRegex).map(v => { if (!v.groups) { throw new Error("Faulty regex") }; - return ({ [v.groups['key']]: v.groups['value'] }) + return ({ [v.groups['key'].trim()]: v.groups['value'].trim() }) }) return Object.assign({}, ...Array.from(properties)) } @@ -38,29 +38,29 @@ export function parseSummary(description: string, name: string): VideoDescriptio b.event = properties['event'] || b.location + function parseBlock(header: string, text: string): string { + const regex = new RegExp(`===\\s*${header}\\s*===\\s*(?(^\\s*[:*]+.*$\\s+)+)`, "m") + const match = text.match(regex) + if (!match) { + throw new Error(`Could not find block ${header}`) + } + if (!match.groups) { + throw new Error("Faulty regex") + } + return match.groups['content'] + } if (!Object.hasOwn(properties, 'patterns')) { console.warn(`Page ${name} has old template usage [no 'patterns' key]`) - const patternsRegex = /===\s*Shown Patterns\s*===\s*(?(^\s*[:*]+.*$\s+)+)/m - const match = description.match(patternsRegex) - if (!match) { - throw new Error("Could not find \"Shown Patterns\"") - } - if (!match.groups) { - throw new Error("Faulty regex") - } - b.patterns = match.groups['patterns'] + b.patterns = parseBlock("Shown Patterns", description) + } else { + b.patterns = properties['patterns'] } + if (!Object.hasOwn(properties, 'notes')) { console.warn(`Page ${name} has old template usage [no 'notes' key]`) - const notesRegex = /===\s*Notes\s*===\s*(?(^\s*[:*]+.*$\s+)+)/m - const match = description.match(notesRegex) - if (!match) { - throw new Error("Could not find \"Notes\"") - } - if (!match.groups) { - throw new Error("Faulty regex") - } - b.notes = match.groups['notes'] + b.notes = parseBlock("Notes", description) + } else { + b.notes = properties['notes'] } return (b as VideoDescription) diff --git a/parse_test.ts b/parse_test.ts index 5d59388..7f1da38 100644 --- a/parse_test.ts +++ b/parse_test.ts @@ -20,6 +20,5 @@ Deno.test(async function newTemplate() { assertEquals(desc.date, "2023-12-02") assertArrayIncludes(desc.teachers, ["Sebastian Spindler"]) - console.dir(desc) assertStringIncludes(desc.patterns, "Blocking Free Spin") });