Refactored citra-games-wiki script to parse toml first, allowing for easier manipulation.

This commit is contained in:
chris062689 2017-06-07 00:48:39 -04:00
parent 416b3601e1
commit cf700917c4
2 changed files with 80 additions and 101 deletions

View File

@ -7,23 +7,19 @@ const logger = require('winston');
const sanitizeHtml = require('sanitize-html'); const sanitizeHtml = require('sanitize-html');
const toml = require('toml'); const toml = require('toml');
const tomlify = require('tomlify-j0.4');
const blackfriday = require('./blackfriday.js'); const blackfriday = require('./blackfriday.js');
const del = require('delete'); const del = require('delete');
const exec = require('sync-exec'); const exec = require('sync-exec');
const inputDirectoryGame = './citra-games-wiki'; const fsPathCode = './citra-games-wiki';
const inputDirectoryWiki = './citra-games-wiki.wiki'; const fsPathWiki = './citra-games-wiki.wiki';
const outputDirectoryMd = '../../site/content/game'; const fsPathHugoContent = '../../site/content/game';
const outputDirectoryBoxart = '../../site/static/images/game/boxart'; const fsPathHugoBoxart = '../../site/static/images/game/boxart';
const outputDirectoryIcons = '../../site/static/images/game/icons'; const fsPathHugoIcon = '../../site/static/images/game/icons';
const outputDirectoryScreenshots = '../../site/static/images/screenshots0'; const fsPathHugoScreenshots = '../../site/static/images/screenshots0';
const outputDirectorySavefiles = '../../site/static/savefiles/'; const fsPathHugoSavefiles = '../../site/static/savefiles/';
// The URL
function url(title) {
return '/wiki/' + title.replace(/\s+/g, '-').toLowerCase();
}
function gitPull(directory, repository) { function gitPull(directory, repository) {
if (fs.existsSync(directory)) { if (fs.existsSync(directory)) {
@ -43,135 +39,117 @@ function getDirectories (srcpath) {
} }
// Fetch game information stored in Github repository. // Fetch game information stored in Github repository.
gitPull(inputDirectoryGame, 'https://github.com/citra-emu/citra-games-wiki.git'); gitPull(fsPathCode, 'https://github.com/citra-emu/citra-games-wiki.git');
// Fetch game articles stored in Github wiki. // Fetch game articles stored in Github wiki.
gitPull(inputDirectoryWiki, 'https://github.com/citra-emu/citra-games-wiki.wiki.git'); gitPull(fsPathWiki, 'https://github.com/citra-emu/citra-games-wiki.wiki.git');
// Make sure the output directories in Hugo exist. // Make sure the output directories in Hugo exist.
if (fs.existsSync(outputDirectoryMd) == false) { [fsPathHugoContent, fsPathHugoBoxart, fsPathHugoIcon, fsPathHugoSavefiles, fsPathHugoScreenshots].forEach(function (path) {
logger.info(`Creating missing output directory: ${outputDirectoryMd}`); if (fs.existsSync(path) == false) {
fs.mkdirSync(outputDirectoryMd); logger.info(`Creating Hugo output directory: ${path}`);
} fs.mkdirSync(path);
}
if (fs.existsSync(outputDirectoryBoxart) == false) { });
logger.info(`Creating missing output directory: ${outputDirectoryBoxart}`);
fs.mkdirSync(outputDirectoryBoxart);
}
if (fs.existsSync(outputDirectoryIcons) == false) {
logger.info(`Creating missing output directory: ${outputDirectoryIcons}`);
fs.mkdirSync(outputDirectoryIcons);
}
if (fs.existsSync(outputDirectorySavefiles) == false) {
logger.info(`Creating missing output directory: ${outputDirectorySavefiles}`);
fs.mkdirSync(outputDirectorySavefiles);
}
if (fs.existsSync(outputDirectoryScreenshots) == false) {
logger.info(`Creating missing output directory: ${outputDirectoryScreenshots}`);
fs.mkdirSync(outputDirectoryScreenshots);
}
try { try {
// Loop through each game folder. // Loop through each game folder.
getDirectories(inputDirectoryGame).forEach(function(game) { getDirectories(fsPathCode).forEach(function(game) {
try { try {
if (game == '.git') { return; } if (game == '.git') { return; }
logger.info(`Creating Hugo files for ${game}`); logger.info(`Processing game: ${game}`);
// Copy the boxart for the game. // Copy the boxart for the game.
let boxartPath = `${inputDirectoryGame}/${game}/boxart.png`; fsextra.copySync(`${fsPathCode}/${game}/boxart.png`, `${fsPathHugoBoxart}/${game}.png`);
if (fs.existsSync(boxartPath)) {
fsextra.copySync(boxartPath, `${outputDirectoryBoxart}/${game}.png`);
}
// Copy the icon for the game. // Copy the icon for the game.
let iconPath = `${inputDirectoryGame}/${game}/icon.png`; fsextra.copySync(`${fsPathCode}/${game}/icon.png`, `${fsPathHugoIcon}/${game}.png`);
if (fs.existsSync(iconPath)) {
fsextra.copySync(iconPath, `${outputDirectoryIcons}/${game}.png`); var model = toml.parse(fs.readFileSync(`${fsPathCode}/${game}/game.dat`, 'utf8'));
let currentDate = new Date();
model.date = `${currentDate.toISOString()}`;
// SHORTCUTS BLOCK
// Parse testcase information out of the dat to reinject as shortcut values.
if (model.testcases == null || model.testcases.length == 0) {
model.compatibility = "99";
model.testcase_date = "2000-01-01";
} else {
let recent = model.testcases[0];
model.compatibility = recent.compatibility;
model.testcase_date = recent.date;
} }
// END SHORTCUTS BLOCK
// Copy the savefiles for the game. // SAVEFILE BLOCK
let inputDirectorySavefilesGame = `${inputDirectoryGame}/${game}/savefiles/`; var fsPathCodeSavefilesGame = `${fsPathCode}/${game}/savefiles/`;
let outputDirectorySavefilesGame = `${outputDirectorySavefiles}/${game}/`; var fsPathHugoSavefilesGame = `${fsPathHugoSavefiles}/${game}/`;
let savefileMetadataContents = []; if (fs.existsSync(fsPathCodeSavefilesGame)) {
// Create the savefile directory for the game.
if (fs.existsSync(inputDirectorySavefilesGame)) { if (fs.existsSync(fsPathHugoSavefilesGame) == false) {
// Create the savefile directory for each game. fs.mkdirSync(fsPathHugoSavefilesGame);
if (fs.existsSync(outputDirectorySavefilesGame) == false) {
fs.mkdirSync(outputDirectorySavefilesGame);
} }
// Copy all savefiles into the output folder and store their contents. // Copy all savefiles into the output folder, and read their data.
fs.readdirSync(inputDirectorySavefilesGame).forEach(file => { model.savefiles = [];
fs.readdirSync(fsPathCodeSavefilesGame).forEach(file => {
if (path.extname(file) == '.zip') { if (path.extname(file) == '.zip') {
fsextra.copySync(`${inputDirectorySavefilesGame}/${file}`, `${outputDirectorySavefilesGame}/${file.replace('.zip', '.zip')}`); fsextra.copySync(`${fsPathCodeSavefilesGame}/${file}`, `${fsPathHugoSavefilesGame}/${file}`);
} else if (path.extname(file) == '.dat') { } else if (path.extname(file) == '.dat') {
// Read the data file into an object.
let savefile = toml.parse(fs.readFileSync(`${fsPathCodeSavefilesGame}/${file}`, 'utf8'));
// Store the contents of the file in memory for adding it into the markdown later. // Store the contents of the file in memory for adding it into the markdown later.
savefileMetadataContents.push({ filename: file.replace('.dat', '.zip'), contents: fs.readFileSync(`${inputDirectorySavefilesGame}/${file}`, 'utf8') }); model.savefiles.push({
filename: file.replace('.dat', '.zip'),
title: savefile.title,
description: savefile.description,
author: savefile.author,
title_id: savefile.title_id
});
} }
}); });
// Finished copying all savefiles into the output folder, and reading their data.
} }
// END SAVEFILE BLOCK
// Copy the screenshots for the game. // Copy the screenshots for the game.
let inputDirectoryScreenshotsGame = `${inputDirectoryGame}/${game}/screenshots/`; let fsPathScreenshotInputGame = `${fsPathCode}/${game}/screenshots/`;
let outputDirectoryScreenshotsGame = `${outputDirectoryScreenshots}/${game}/`; let fsPathScreenshotOutputGame = `${fsPathHugoScreenshots}/${game}/`;
if (fs.existsSync(fsPathScreenshotInputGame)) {
if (fs.existsSync(inputDirectoryScreenshotsGame)) {
// Create the savefile directory for each game. // Create the savefile directory for each game.
if (fs.existsSync(outputDirectoryScreenshotsGame) == false) { if (fs.existsSync(fsPathScreenshotOutputGame) == false) {
fs.mkdirSync(outputDirectoryScreenshotsGame); fs.mkdirSync(fsPathScreenshotOutputGame);
} }
// Copy all screenshots into the output folder. // Copy all screenshots into the output folder.
fs.readdirSync(inputDirectoryScreenshotsGame).forEach(file => { fs.readdirSync(fsPathScreenshotInputGame).forEach(file => {
if (path.extname(file) == '.png') { if (path.extname(file) == '.png') {
fsextra.copySync(`${inputDirectoryScreenshotsGame}/${file}`, `${outputDirectoryScreenshotsGame}/${file}`); fsextra.copySync(`${fsPathScreenshotInputGame}/${file}`, `${fsPathScreenshotOutputGame}/${file}`);
} }
}); });
} }
// Create the markdown file to be displayed in Hugo. // WIKI BLOCK
let title = game.replace(/-/g, ' ').slice(0, -3); var wikiText = "";
var stats = fs.statSync(`${inputDirectoryGame}/${game}/game.dat`); let fsPathWikiGame = `${fsPathWiki}/${game}.md`;
let modified = new Date(util.inspect(stats.mtime)); if (fs.existsSync(fsPathWikiGame)) {
wikiText = fs.readFileSync(fsPathWikiGame, 'utf8');
let datContents = fs.readFileSync(`${inputDirectoryGame}/${game}/game.dat`, 'utf8'); // Fix Blackfriday markdown rendering differences.
datContents = `date = "${modified.toISOString()}"\r\n` + datContents; wikiText = blackfriday.fixLists(wikiText);
wikiText = blackfriday.fixLinks(wikiText);
// Parse testcase information out of the dat to reinject as shortcut values.
var dat = toml.parse(datContents);
if (dat.testcases == null || dat.testcases.length == 0) {
datContents = `compatibility = \"99\"\r\ntestcase_date = "2000-01-01"\r\n` + datContents;
} else { } else {
let recent = dat.testcases[0]; wikiText = "## No wiki exists yet for this game.";
datContents = `compatibility = \"${recent.compatibility}\"\r\ntestcase_date = "${recent.date}"\r\n` + datContents;
} }
// END WIKI BLOCK
var wikiContents = ""; let modelText = tomlify(model, null, 2);
let wikiPathGame = `${inputDirectoryWiki}/${game}.md`;
if (fs.existsSync(wikiPathGame)) {
wikiContents = fs.readFileSync(wikiPathGame, 'utf8');
} else {
logger.warn(`CompatDB: No wiki exists for game ${game}.`);
wikiContents = "## No wiki exists yet for this game.";
}
// Fix Blackfriday markdown rendering differences. let contentOutput = `+++\r\n${modelText}\r\n+++\r\n\r\n${wikiText}\r\n`;
wikiContents = blackfriday.fixLists(wikiContents); fs.writeFileSync(`${fsPathHugoContent}/${game}.md`, contentOutput);
wikiContents = blackfriday.fixLinks(wikiContents);
// Read all savefiles from array and copy them into the markdown.
savefileMetadataContents.forEach(function(savefile) {
let modified = new Date(util.inspect(stats.mtime));
datContents += `\r\n\r\n[[ savefiles ]]\r\n${savefile.contents}\r\nfilename = "${savefile.filename}"\r\ndate = "${modified.toISOString()}"\r\n`;
});
let output = `+++\r\n${datContents}\r\n+++\r\n\r\n${wikiContents}\r\n`;
fs.writeFileSync(`${outputDirectoryMd}/${game}.md`, output);
} catch (ex) { } catch (ex) {
logger.warn(`${game} failed to generate: ${ex}`); logger.warn(`${game} failed to generate: ${ex}`);
logger.error(ex); logger.error(ex);

View File

@ -12,6 +12,7 @@
"sanitize-html": "^1.14.1", "sanitize-html": "^1.14.1",
"sync-exec": "^0.6.2", "sync-exec": "^0.6.2",
"toml": "^2.3.2", "toml": "^2.3.2",
"tomlify-j0.4": "^2.0.0",
"winston": "^2.2.0" "winston": "^2.2.0"
}, },
"preferGlobal": false, "preferGlobal": false,