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")
|
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 => {
|
const properties = match[1].matchAll(propertyRegex).map(v => {
|
||||||
if (!v.groups) { throw new Error("Faulty regex") };
|
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))
|
return Object.assign({}, ...Array.from(properties))
|
||||||
}
|
}
|
||||||
@@ -38,29 +38,29 @@ export function parseSummary(description: string, name: string): VideoDescriptio
|
|||||||
|
|
||||||
b.event = properties['event'] || b.location
|
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')) {
|
if (!Object.hasOwn(properties, 'patterns')) {
|
||||||
console.warn(`Page ${name} has old template usage [no 'patterns' key]`)
|
console.warn(`Page ${name} has old template usage [no 'patterns' key]`)
|
||||||
const patternsRegex = /===\s*Shown Patterns\s*===\s*(?<patterns>(^\s*[:*]+.*$\s+)+)/m
|
b.patterns = parseBlock("Shown Patterns", description)
|
||||||
const match = description.match(patternsRegex)
|
} else {
|
||||||
if (!match) {
|
b.patterns = properties['patterns']
|
||||||
throw new Error("Could not find \"Shown Patterns\"")
|
|
||||||
}
|
|
||||||
if (!match.groups) {
|
|
||||||
throw new Error("Faulty regex")
|
|
||||||
}
|
|
||||||
b.patterns = match.groups['patterns']
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Object.hasOwn(properties, 'notes')) {
|
if (!Object.hasOwn(properties, 'notes')) {
|
||||||
console.warn(`Page ${name} has old template usage [no 'notes' key]`)
|
console.warn(`Page ${name} has old template usage [no 'notes' key]`)
|
||||||
const notesRegex = /===\s*Notes\s*===\s*(?<notes>(^\s*[:*]+.*$\s+)+)/m
|
b.notes = parseBlock("Notes", description)
|
||||||
const match = description.match(notesRegex)
|
} else {
|
||||||
if (!match) {
|
b.notes = properties['notes']
|
||||||
throw new Error("Could not find \"Notes\"")
|
|
||||||
}
|
|
||||||
if (!match.groups) {
|
|
||||||
throw new Error("Faulty regex")
|
|
||||||
}
|
|
||||||
b.notes = match.groups['notes']
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (b as VideoDescription)
|
return (b as VideoDescription)
|
||||||
|
|||||||
@@ -20,6 +20,5 @@ Deno.test(async function newTemplate() {
|
|||||||
|
|
||||||
assertEquals(desc.date, "2023-12-02")
|
assertEquals(desc.date, "2023-12-02")
|
||||||
assertArrayIncludes(desc.teachers, ["Sebastian Spindler"])
|
assertArrayIncludes(desc.teachers, ["Sebastian Spindler"])
|
||||||
console.dir(desc)
|
|
||||||
assertStringIncludes(desc.patterns, "Blocking Free Spin")
|
assertStringIncludes(desc.patterns, "Blocking Free Spin")
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user