1
0
Fork 0

auto populate code reference

This commit is contained in:
Matteias Collet 2020-07-01 08:14:09 +02:00
parent f0574b22c9
commit 40a1b7794e
8 changed files with 758 additions and 160 deletions

View file

@ -1,13 +1,18 @@
const fs = require('fs');
const path = require('path');
const jsdom = require('jsdom');
const { JSDOM } = require('jsdom');
const jsonFilePath = path.join(__dirname, '../site/.vuepress/data/gameVersions.json');
const codeVersions = ['GMSE01', 'GMSJ01', 'GMSP01', 'GMSJ0A'];
const injectionTag = '<!-- injectionpoint -->';
// Converts the XML source to a JSON object
const parseXml = (xmlString) => {
const codeCollection = new jsdom.JSDOM(xmlString).window.document.getElementsByTagName('code');
const codeCollection = new JSDOM(xmlString, {
contentType: 'text/xml',
}).window.document.getElementsByTagName('code');
const parseTextNode = (node, identifier) => node.getElementsByTagName(identifier)[0].textContent;
const codes = [...codeCollection];
@ -21,37 +26,48 @@ const parseXml = (xmlString) => {
}));
};
const parseTextNode = (node, identifier) => node.getElementsByTagName(identifier)[0].textContent;
// Read the current code list
const codeJson = JSON.parse(fs.readFileSync(jsonFilePath));
// Populate all code fields in the codeJSON
for (let i = 0; i < codeVersions.length; i++) {
const xml = fs.readFileSync(path.join(__dirname, `../codes/${codeVersions[i]}.xml`));
codeJson.find((c) => c.identifier === codeVersions[i]).codes = parseXml(xml);
}
// Save the codeJSON with the updated codes
fs.writeFileSync(jsonFilePath, JSON.stringify(codeJson));
// Populate the code reference
for (let i = 0; i < codeVersions.length; i++) {
const reference = fs.readFileSync(
path.join(__dirname, `../site/code-references/${codeVersions[i].toLowerCase()}.md`),
const filePath = path.join(
__dirname,
`../site/code-reference/${codeVersions[i].toLowerCase()}.md`,
);
// Get the current reference
const reference = fs.readFileSync(filePath).toString();
if (!reference.includes(injectionTag)) {
throw new Error(`No injection tag found in ${codeVersions[i].toLowerCase()}.md`);
}
const fileContent = reference.split(injectionTag)[0] + injectionTag;
const codes = codeJson.find((c) => c.identifier === codeVersions[i]).codes;
// Everything afte rthe injection tag is deleted from the file
let fileContent = reference.split(injectionTag)[0] + injectionTag;
const codes = codeJson
.find((c) => c.identifier === codeVersions[i])
.codes.sort((a, b) => (a.title > b.title ? 1 : -1));
// Create a semi-markdown version for all codes
codes.forEach((code) => {
const title = `#${code.title}`;
const author = `*${code.author.includes(',') ? 'Authors' : 'Author'} ${code.author}`;
const version = `*${code.version} (${code.date})`;
const title = `### ${code.title}`;
const author = `*${code.author.includes(',') ? 'Authors:' : 'Author:'} ${code.author}*`;
const version = `*Version: ${code.version} (${code.date})*`;
const description = code.description;
fileContent += `\n\n${title}\n\n${version}\n\n${author}\n\n${description}`;
fileContent += `\n\n${title.trim()}\n\n${version.trim()} \n${author.trim()}\n\n${description.trim()}`;
});
fs.writeFileSync(`../site/code-references/${codeVersions[i].toLowerCase()}.md`, fileContent);
// Save the reference file
fs.writeFileSync(filePath, fileContent);
}