From 5bcc6192a9bb23499e6d197df079aa290b980abd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20W=C3=B6lfer?= Date: Tue, 6 May 2025 11:03:33 +0200 Subject: [PATCH] Using mwn wikitext functions --- main.ts | 5 +++-- parse.ts | 30 +++++++++++++++--------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/main.ts b/main.ts index 46a3608..501ba48 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,4 @@ -import { Mwn } from 'npm:mwn' +import { Mwn, MwnWikitext } from 'npm:mwn' import process from "node:process"; import { parseSummary } from "./parse.ts"; @@ -52,7 +52,8 @@ async function main() { throw new Error("Latest revision has no content") } console.log(content) - parseSummary(content, relevantFiles[0]) + console.dir(bot.Wikitext.parseSections(content)[2]) + // parseSummary(content, relevantFiles[0]) } catch (error) { console.error('Error:', error); } finally { diff --git a/parse.ts b/parse.ts index 300e9dd..8b64787 100644 --- a/parse.ts +++ b/parse.ts @@ -1,25 +1,25 @@ +import { Mwn } from "mwn"; import { VideoDescription } from "./main.ts"; -export function parseTemplate(description: string): Record { +function parseTemplate(description: string): Record { - const templateRegex = /{{(.*DanceWorkshopDescription.*)}}/s; - - const match = description.match(templateRegex); - - if (!match) { - throw new Error("Could not find template") - } - - const propertyRegex = /\|\s*(?[^|]*?)\s*=(?[^|}]*)\s*$/gm - - const properties = match[1].matchAll(propertyRegex).map(v => { - if (!v.groups) { throw new Error("Faulty regex") }; - return ({ [v.groups['key'].trim()]: v.groups['value'].trim() }) + const r = new Mwn().Wikitext.parseTemplates(description, { + namePredicate: (name) => name === "DanceWorkshopDescription", }) - return Object.assign({}, ...Array.from(properties)) + if (r.length < 1) { + throw new Error("Could not find DanceWorkshopDescription") + } + 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 { + } export function parseSummary(description: string, name: string): VideoDescription { + const properties = parseTemplate(description) const b: Partial = {};