Made first version work

This commit is contained in:
Lukas Wölfer
2025-05-06 18:53:30 +02:00
parent 5bcc6192a9
commit 9e00e029c0
4 changed files with 91 additions and 32 deletions

View File

@@ -3,19 +3,31 @@ import { VideoDescription } from "./main.ts";
function parseTemplate(description: string): Record<string, string> {
const r = new Mwn().Wikitext.parseTemplates(description, {
const u = new (new Mwn().Wikitext)(description)
const r = u.parseTemplates({
namePredicate: (name) => name === "DanceWorkshopDescription",
})
if (r.length < 1) {
throw new Error("Could not find DanceWorkshopDescription")
}
if (r.length > 1) {
console.warn("More than one DanceWorkshopDescription template found")
}
const p = r[0]
const properties = p.parameters.map(v => ({ [v.name]: v.value }))
return Object.assign({}, ...properties)
}
function parseSection(description:string, header: string) : string {
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'].trim()
}
export function parseSummary(description: string, name: string): VideoDescription {
@@ -38,17 +50,7 @@ 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]`)
b.patterns = parseBlock("Shown Patterns", description)
@@ -62,6 +64,8 @@ export function parseSummary(description: string, name: string): VideoDescriptio
} else {
b.notes = properties['notes']
}
b.path = name
const titleMatch = name.match(/File:WCS \w+ (?<title>.+)\.[^.]+/)
b.title = titleMatch?.groups ? titleMatch.groups['title'] : name
return (b as VideoDescription)
}