import { Mwn } from "mwn"; import { VideoDescription } from "./main.ts"; function parseTemplate(description: string): { warnings: string[] } & { [k: string]: string } { const u = new (new Mwn().Wikitext)(description) const r = u.parseTemplates({ namePredicate: (name) => name === "DanceWorkshopDescription", }) const warnings = [] if (r.length < 1) { throw new Error("Could not find DanceWorkshopDescription") } if (r.length > 1) { warnings.push("More than one DanceWorkshopDescription template found") } const p = r[0] const properties = p.parameters.map(v => ({ [v.name]: v.value })) return Object.assign({}, { warnings }, ...properties) } 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'].trim() } export function parseSummary(description: string, name: string): VideoDescription { const properties = parseTemplate(description) const b: Partial = {}; b.nags = properties.warnings if (Object.hasOwn(properties, 'teachers')) { b.nags.push(`Contains 'teachers' key instead of 'leader'/'follower'`) 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')) { b.nags.push(`No 'patterns' key`) try { b.patterns = parseBlock("Shown Patterns", description) } catch (e) { b.nags.push("Shown Patterns: " + e) b.patterns = "" } } else { b.patterns = properties['patterns'] } if (!Object.hasOwn(properties, 'notes')) { b.nags.push(`No 'notes' key`) try { b.notes = parseBlock("Notes", description) } catch (e) { b.nags.push("Notes: " + e) b.notes = "" } } else { b.notes = properties['notes'] } b.path = name const titleMatch = name.match(/File:WCS \w+ (?.+)\.[^.]+/) b.title = titleMatch?.groups ? titleMatch.groups['title'] : name return (b as VideoDescription) }