diff2html/scripts/release-website.js

73 lines
1.9 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
2016-08-23 17:20:54 +00:00
const fs = require("fs");
2016-08-23 17:20:54 +00:00
const hogan = require("hogan.js");
2016-08-23 17:20:54 +00:00
const root = "website/templates";
const pagesRoot = root + "/pages";
2016-08-23 17:20:54 +00:00
const websitePages = fs.readdirSync(root + "/pages");
2016-08-23 17:20:54 +00:00
const template = hogan.compile(readFile(root + "/template.mustache"));
const options = {
all: {
demoUrl: "demo.html?diff=https://github.com/rtfpessoa/diff2html/pull/106"
},
demo: {
extraClass: "template-index-min"
2016-10-15 21:18:16 +00:00
}
};
2016-10-09 15:56:58 +00:00
websitePages.map(function(page) {
const pagePartialTemplate = hogan.compile(readFile(pagesRoot + "/" + page + "/" + page + ".partial.mustache"));
const pageAssetsTemplate = hogan.compile(readFile(pagesRoot + "/" + page + "/" + page + "-assets.partial.mustache"));
const pageScriptsTemplate = hogan.compile(
readFile(pagesRoot + "/" + page + "/" + page + "-scripts.partial.mustache")
);
2016-10-15 21:18:16 +00:00
const templateOptions = {};
let key;
// Allow the pages to share common options
const genericOptions = options.all || {};
for (key in genericOptions) {
if (genericOptions.hasOwnProperty(key)) {
templateOptions[key] = genericOptions[key];
}
}
2016-10-15 21:18:16 +00:00
// Allow each page to have custom options
const pageOptions = options[page] || {};
for (key in pageOptions) {
2016-10-15 21:18:16 +00:00
if (pageOptions.hasOwnProperty(key)) {
templateOptions[key] = pageOptions[key];
}
}
const pagePartial = pagePartialTemplate.render(templateOptions);
const pageAssets = pageAssetsTemplate.render(templateOptions);
const pageScripts = pageScriptsTemplate.render(templateOptions);
templateOptions.assets = pageAssets;
templateOptions.scripts = pageScripts;
templateOptions.content = pagePartial;
const pageHtml = template.render(templateOptions);
writeFile("docs/" + page + ".html", pageHtml);
2016-10-09 15:56:58 +00:00
});
2016-08-23 17:20:54 +00:00
function readFile(filePath) {
2016-10-09 15:56:58 +00:00
try {
return fs.readFileSync(filePath, "utf8");
} catch (_ignore) {}
2016-10-09 15:56:58 +00:00
return "";
2016-08-23 17:20:54 +00:00
}
function writeFile(filePath, content) {
return fs.writeFileSync(filePath, content);
}