diff --git a/compile-pug.js b/compile-pug.js new file mode 100644 index 0000000000000000000000000000000000000000..0636f12d5db010f62cc449dea5b7dead64fe53ff --- /dev/null +++ b/compile-pug.js @@ -0,0 +1,63 @@ +const path = require('path') +const fs = require('fs').promises +const objectAssignDeep = require(`object-assign-deep`) +const yaml = require('js-yaml') +const pug = require('pug') +const chokidar = require('chokidar') + +const default_lang = 'en' + +const pugFile = './src/index.pug' +const dataDir = './translation' +const outDir = './www' + +async function compile() { + const langs = (await fs.readdir(dataDir)) + .filter(f => path.extname(f) === '.yml') + .map(f => path.basename(f, '.yml')) + + const langData = {} + + await Promise.all( + langs.map(lang => + fs + .readFile(`${dataDir}/${lang}.yml`, 'utf8') + .then(text => (langData[lang] = yaml.safeLoad(text))), + ), + ) + + for (const lang in langData) { + if (lang === default_lang) continue + langData[lang] = objectAssignDeep( + {}, + langData[default_lang], + langData[lang], + ) + } + + const options = JSON.parse(await fs.readFile('./src/config.json', 'utf-8')) + const pugFct = pug.compileFile(pugFile) + + await fs.mkdir(outDir, { recursive: true }) + + await Promise.all( + Object.entries(langData).map(([lang, data]) => { + options.lang=lang + options.data=data + + const html = pugFct(options) + const filename = `${outDir}/index-${lang}.html` + console.log(`write ${filename}`) + return fs.writeFile(filename, html) + }), + ) +} + +compile().then(() => { + if (!process.argv.includes('-w') && !process.argv.includes('--watch')) return + console.log('watching for changes…') + chokidar.watch([pugFile, dataDir]).on('change', editedFilePath => { + console.log(`\nchanged: ${editedFilePath}`) + compile() + }) +}) diff --git a/translation/en.yml b/translation/en.yml new file mode 100644 index 0000000000000000000000000000000000000000..9e8e911aaffe6050ff7252aef03c7d7a4b8a4a1e --- /dev/null +++ b/translation/en.yml @@ -0,0 +1,5 @@ +--- +title: My amazing website +welcome: Hi! welcome to our website +contact: click here to contact us +email: contact@example.com \ No newline at end of file diff --git a/translation/fr.yml b/translation/fr.yml new file mode 100644 index 0000000000000000000000000000000000000000..eb31599aa3f49416002c56eeed507438eb36ae20 --- /dev/null +++ b/translation/fr.yml @@ -0,0 +1,4 @@ +--- +title: Mon superbe site +welcome: Bonjour ! bienvenu sur notre site +contact: cliquez ici pour nous contacter \ No newline at end of file