68 lines
2.3 KiB
TypeScript
68 lines
2.3 KiB
TypeScript
import { VideoDescription } from "./main.ts";
|
|
|
|
export function parseTemplate(description: string): Record<string, string> {
|
|
|
|
const templateRegex = /{{(.*DanceWorkshopDescription.*)}}/s;
|
|
|
|
const match = description.match(templateRegex);
|
|
|
|
if (!match) {
|
|
throw new Error("Could not find template")
|
|
}
|
|
|
|
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 Object.assign({}, ...Array.from(properties))
|
|
}
|
|
|
|
export function parseSummary(description: string, name: string): VideoDescription {
|
|
const properties = parseTemplate(description)
|
|
|
|
const b: Partial<VideoDescription> = {};
|
|
|
|
if (Object.hasOwn(properties, 'teachers')) {
|
|
console.warn(`Page ${name} has old template usage [contains 'teachers' key]`)
|
|
const t = properties['teachers'].split('&').map(item => item.trim());
|
|
b.teachers = t
|
|
} else {
|
|
b.teachers = [properties['leader'], properties['follower']].filter(v => v !== undefined)
|
|
}
|
|
|
|
b.location = properties['location']
|
|
b.date = properties['date']
|
|
b.level = properties['level']
|
|
|
|
b.event = properties['event'] || b.location
|
|
|
|
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']
|
|
}
|
|
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']
|
|
}
|
|
|
|
return (b as VideoDescription)
|
|
}
|