var marked = require("marked");
var fs = require("fs-extra");
// bundle all content in a specific locale for use by the app
const defaultLocale = "en-GB";
var locale = defaultLocale;
var lpos = process.argv.indexOf('--locale');
if (lpos !== -1) { locale = process.argv[lpos+1]; }
// shim nodejs so that it knows what to do with jsx files: return empty objects.
var Module = require('module');
var originalRequire = Module.prototype.require;
Module.prototype.require = function() {
try {
return originalRequire.apply(this, arguments);
} catch (e) {
return {};
}
};
/**
*
*/
function chunkGraphicJSX(data, chunks, chunkMore) {
var p = 0,
next = chunkMore ? chunkMore[0] : false,
otherChunkers = chunkMore ? chunkMore.slice(1) : false,
gfxTag = ' block found: we're done here. Parse the remaining
// data for whatever else might be in there.
performChunking(data.substring(p), chunks, next, otherChunkers);
break;
}
// First parse the non-
data for whatever else might be in there.
performChunking(data.substring(p, gfx), chunks, next, otherChunkers);
// Then capture the
block and mark it as "don't convert"
let eol = data.indexOf(gfxEnd, gfx) + gfxEnd.length;
chunks.push({ convert: false, type: "gfx", s:gfx, e:eol, data: data.substring(gfx, eol) });
p = eol;
}
}
/**
*
*/
function chunkDivs(data, chunks, chunkMore) {
var p = 0,
next = chunkMore ? chunkMore[0] : false,
otherChunkers = chunkMore ? chunkMore.slice(1) : false,
divMatch = '\n
tags inside tags.
d = d.replace(/(\r?\n)*/g,'')
.replace(/<\/code>(\r?\n)*<\/pre>/g,'
');
// And then title extraction/rewriting
d = d.replace(/]+>([^<]+)<\/h1>/,function(_,t) {
processed.title = t;
return ` `;
});
return d;
}).join('');
processed.data = data;
} catch (e) {
// console.warn(e);
}
return processed;
}
/**
* Process a single locale, with `en-GB` fallback for missing files.
*/
function processLocale(locale) {
// Get the section map. This will try to load .jsx code, which will fail,
// but the above shim makes a failure simply return an empty object instead.
// This is good: we only care about the keys, not the content.
var index = require("./components/sections");
var sections = Object.keys(index);
var content = {};
sections.forEach((key, number) => {
// Grab locale file, or defaultLocale file if the chosen locale has
// has no translated content (yet)...
var localeCode = locale;
var loc = `./components/sections/${key}/content.${localeCode}.md`;
if (!fs.existsSync(loc)) {
localeCode = defaultLocale;
loc = `./components/sections/${key}/content.${localeCode}.md`;
}
// Read in the content.{lang}.md file
var processed = processLocation(loc, key, number);
content[key] = {
locale: localeCode,
title: processed.title,
getContent: "" + processed.data + " "
};
});
// We also need to localize the "LocaleSwitcher"
var localeCode = locale;
var loc = `./components/localized/LocaleSwitcher/content.${localeCode}.md`;
if (!fs.existsSync(loc)) {
localeCode = defaultLocale;
loc = `./components/localized/LocaleSwitcher/content.${localeCode}.md`;
}
var key = "locale-switcher";
var processed = processLocation(loc, key);
content[key] = {
locale: localeCode,
title: key,
getContent: "" + processed.data + " "
};
var bcode = JSON.stringify(content, false, 2);
bcode = bcode.replace(/"/g, "function(handler) { return ")
.replace(/this\.(\w)/g, "handler.$1")
.replace(/<\/section>"(,?)/g, " ; }$1\n")
.replace(/\\"/g,'"')
.replace(/\\n/g,'\n')
.replace(/>\n<')
.replace(/\\\\/g, '\\');
var bundle = `var React = require('react');\nvar Graphic = require("../../components/Graphic.jsx");\nvar SectionHeader = require("../../components/SectionHeader.jsx");\n\nmodule.exports = ${bcode};\n`;
var dir = `./locales/${locale}`;
fs.ensureDir(dir);
fs.writeFileSync(`${dir}/content.js`, bundle);
}
// find all locales used
var glob = require('glob');
glob("components/sections/**/content*md", (err, files) => {
var locales = [];
files.forEach(file => {
let locale = file.match(/content\.([^.]+)\.md/)[1];
if (locales.indexOf(locale) === -1) {
locales.push(locale);
}
});
locales.forEach(processLocale);
});