Skip to content

Instantly share code, notes, and snippets.

@sudodoki
Last active August 29, 2015 14:08
Show Gist options
  • Save sudodoki/79116be1465c4e1613f1 to your computer and use it in GitHub Desktop.
Save sudodoki/79116be1465c4e1613f1 to your computer and use it in GitHub Desktop.
Script used for nodeschool.github.io to add data-i18n attributes to index.html node
var fs = require('fs');
var cheerio = require('cheerio');
var find = require('cheerio-eq');
var selectors = require('./languages/selectors.json');
fs.readdir(__dirname, function (err, files) {
if (err) throw err;
files.filter(RegExp.prototype.test.bind(/\.html$/)).forEach(function (filename) {
processFile(filename);
});
});
function processFile(filename) {
fs.readFile(filename, function (err, data) {
if (err) return console.log('error is ', err);
var $ = cheerio.load(data);
// console.log($.root().html())
Object.keys(selectors).forEach(function (selector) {
var i18nKey = selectors[selector];
find($, selector).attr('data-i18n', i18nKey)
});
fs.writeFile('new-' + filename, $.root().html(), function (err) {
if (err) console.log('failed to capture the result for ' + filename)
console.log(filename + ' is done, thanks')
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment