Fixed parsing
This commit is contained in:
40
parse.ts
40
parse.ts
@@ -10,11 +10,11 @@ export function parseTemplate(description: string): Record<string, string> {
|
||||
throw new Error("Could not find template")
|
||||
}
|
||||
|
||||
const propertyRegex = /\|\s*(?<key>[^|]*?)\s*=(?<value>[^|]*?)\s*$/gm
|
||||
const propertyRegex = /\|\s*(?<key>[^|]*?)\s*=(?<value>[^|}]*)\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*(?<content>(^\\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*(?<patterns>(^\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*(?<notes>(^\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)
|
||||
|
||||
@@ -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")
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user