Using mwn wikitext functions

This commit is contained in:
Lukas Wölfer
2025-05-06 11:03:33 +02:00
parent 5cb3df617a
commit 5bcc6192a9
2 changed files with 18 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
import { Mwn } from 'npm:mwn' import { Mwn, MwnWikitext } from 'npm:mwn'
import process from "node:process"; import process from "node:process";
import { parseSummary } from "./parse.ts"; import { parseSummary } from "./parse.ts";
@@ -52,7 +52,8 @@ async function main() {
throw new Error("Latest revision has no content") throw new Error("Latest revision has no content")
} }
console.log(content) console.log(content)
parseSummary(content, relevantFiles[0]) console.dir(bot.Wikitext.parseSections(content)[2])
// parseSummary(content, relevantFiles[0])
} catch (error) { } catch (error) {
console.error('Error:', error); console.error('Error:', error);
} finally { } finally {

View File

@@ -1,25 +1,25 @@
import { Mwn } from "mwn";
import { VideoDescription } from "./main.ts"; import { VideoDescription } from "./main.ts";
export function parseTemplate(description: string): Record<string, string> { function parseTemplate(description: string): Record<string, string> {
const templateRegex = /{{(.*DanceWorkshopDescription.*)}}/s; const r = new Mwn().Wikitext.parseTemplates(description, {
namePredicate: (name) => name === "DanceWorkshopDescription",
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'].trim()]: v.groups['value'].trim() })
}) })
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 { export function parseSummary(description: string, name: string): VideoDescription {
const properties = parseTemplate(description) const properties = parseTemplate(description)
const b: Partial<VideoDescription> = {}; const b: Partial<VideoDescription> = {};