Using mwn wikitext functions
This commit is contained in:
5
main.ts
5
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 {
|
||||
|
||||
30
parse.ts
30
parse.ts
@@ -1,25 +1,25 @@
|
||||
import { Mwn } from "mwn";
|
||||
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 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() })
|
||||
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<VideoDescription> = {};
|
||||
|
||||
Reference in New Issue
Block a user