2021-06-14 14:31:36 -04:00
|
|
|
const fs = require("fs");
|
|
|
|
const jsdom = require("jsdom");
|
|
|
|
const { JSDOM } = jsdom;
|
|
|
|
const { window } = new JSDOM("");
|
|
|
|
global.window = window;
|
|
|
|
var showdown = require("showdown");
|
|
|
|
const converterShowdown = new showdown.Converter();
|
|
|
|
const libxmljs = require("libxmljs");
|
2021-06-19 20:56:29 -04:00
|
|
|
const getTerm = require("./getTerm.js");
|
2021-06-14 14:31:36 -04:00
|
|
|
|
2021-06-19 20:56:29 -04:00
|
|
|
fs.readFile("../../news.xml", "utf8", (err, data) => {
|
2021-06-14 14:31:36 -04:00
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-19 20:56:29 -04:00
|
|
|
data = data.replace(/<!DOCTYPE.*>/, "");
|
2021-06-14 14:31:36 -04:00
|
|
|
parseXML(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.mkdir("./markdown-news/", { recursive: true }, (err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
});
|
|
|
|
|
|
|
|
const nodeChildrenToString = (nodeChildren) => {
|
|
|
|
let string = "";
|
|
|
|
for (let i = 0; i < nodeChildren.length; ++i) {
|
|
|
|
if (nodeChildren[i].type() === "text") {
|
|
|
|
string += nodeChildren[i];
|
|
|
|
} else {
|
|
|
|
string += nodeChildren[i].toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const parseXML = (XML) => {
|
|
|
|
const xmlDoc = libxmljs.parseHtml(XML);
|
|
|
|
let newsdefsChildren = xmlDoc.get("//newsdefs").childNodes();
|
|
|
|
let currentTerm = "";
|
2021-06-19 20:56:29 -04:00
|
|
|
newsdefsChildren.forEach((newsItem) => {
|
|
|
|
if (newsItem.type() === "element") {
|
|
|
|
const author = newsItem.attr("author").value();
|
|
|
|
const date = newsItem.attr("date").value();
|
2021-06-14 14:31:36 -04:00
|
|
|
currentTerm = getTerm(date);
|
2021-06-19 20:56:29 -04:00
|
|
|
const contentNodes = newsItem.childNodes();
|
2021-06-14 14:31:36 -04:00
|
|
|
const content = converterShowdown.makeMarkdown(
|
|
|
|
nodeChildrenToString(contentNodes)
|
|
|
|
);
|
|
|
|
|
2021-07-12 20:55:38 -04:00
|
|
|
const md = `---
|
2021-08-17 16:42:37 -04:00
|
|
|
author: '${author.replace(/'/g, "''")}'
|
|
|
|
date: '${date.replace(/'/g, "''")}'
|
2021-07-12 20:55:38 -04:00
|
|
|
---
|
2021-06-14 14:31:36 -04:00
|
|
|
|
|
|
|
${content}`;
|
2021-07-12 20:55:38 -04:00
|
|
|
const mdTitle = `${date}-${author}`;
|
2021-07-26 20:32:13 -04:00
|
|
|
fs.mkdirSync(
|
|
|
|
`./markdown-news/${currentTerm.year}/${currentTerm.term}`,
|
|
|
|
{ recursive: true },
|
|
|
|
(err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
fs.writeFileSync(
|
2021-08-17 16:42:37 -04:00
|
|
|
`./markdown-news/${currentTerm.year}/${currentTerm.term}/${mdTitle}.md`,
|
2021-07-12 20:55:38 -04:00
|
|
|
md,
|
2021-06-14 17:12:09 -04:00
|
|
|
(err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
}
|
|
|
|
);
|
2021-06-14 14:31:36 -04:00
|
|
|
}
|
2021-06-19 20:56:29 -04:00
|
|
|
});
|
2021-06-14 14:31:36 -04:00
|
|
|
};
|