diff --git a/.gitignore b/.gitignore index 696b299..0807cd4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /scripts/mdx-scripts/node_modules /scripts/mdx-scripts/markdown-events /scripts/mdx-scripts/markdown-news +/scripts/mdx-scripts/markdown-talks diff --git a/scripts/mdx-scripts/talk-conversion.js b/scripts/mdx-scripts/talk-conversion.js new file mode 100644 index 0000000..789c06e --- /dev/null +++ b/scripts/mdx-scripts/talk-conversion.js @@ -0,0 +1,107 @@ +const fs = require("fs"); +const jsdom = require("jsdom"); +const { JSDOM } = jsdom; +const { window } = new JSDOM(""); +global.window = window; +var showdown = require("showdown"); +const converter = new showdown.Converter(); +const libxmljs = require("libxmljs"); + +const MIRROR_URL = "http://mirror.csclub.uwaterloo.ca/csclub/"; + +try { + fs.mkdirSync("./markdown-talks/"); +} catch (e) { + if (e.code !== "EEXIST") { + throw e; + } +} + +const file = fs + .readFileSync("../../media/index.xml", "utf8") + .replace(//, ""); + +const xml = libxmljs.parseHtml(file); +const talks = xml.find("//mediaitem"); + +talks.forEach((talk, idx) => { + const { filename, markdown } = xml2md(talk, idx); + + fs.promises.writeFile(`./markdown-talks/${filename}.md`, markdown); +}); + +/** + * + * @param {libxmljs.Element} talk + */ +function xml2md(talk, index) { + const title = talk.attr("title").value(); + + const abstract = talk + .get("abstract") + ?.childNodes() + .reduce((str, node) => str + node.toString(), "") + .trim(); + + const presentors = talk + .get("presentor") + .childNodes() + .toString() + .split(",") + .map((s) => s.trim()); + + const thumbSmall = MIRROR_URL + talk.get("thumbnail").attr("file").value(); + const thumbLarge = thumbSmall.replace("-thumb-small", "-thumb-large"); + + const links = talk + .find("mediafile") + .map((mf) => ({ + /** @type{string} */ + file: MIRROR_URL + mf.attr("file").value(), + + /** @type{string} */ + type: mf.attr("type").value(), + + /** @type{string | undefined} */ + size: mf.attr("size")?.value() ?? undefined, + })) + .map( + ({ file, type, size }) => + " " + + ` + - file: '${file.replaceAll("'", "''")}' + type: '${type.replaceAll("'", "''")}' + ${size == null ? "" : `size: '${size.replaceAll("'", "''")}'`} +`.trim(), + ); + + const markdown = + ` +--- +index: ${index} +title: '${title.replaceAll("'", "''")}' +presentors: + - '${presentors.join("\n - ").replaceAll("'", "''")}' +thumbnails: + small: '${thumbSmall}' + large: '${thumbLarge}' +links: +${links.join("\n")} +--- + +${converter.makeMarkdown(abstract ?? "")} +`.trim() + "\n"; + + return { + filename: title + .toLowerCase() + .replaceAll(" ", "-") + .replaceAll(":", "") + .replaceAll("=", "") + .replaceAll(",", "-") + .replaceAll("'", "-") + .replaceAll("+", "-plus-") + .replaceAll(/-+/g, "-"), + markdown, + }; +}