Last active
August 29, 2015 14:21
-
-
Save danemacaulay/ab735b39790424305a24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var string = require('string'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var glob = require('glob'); | |
var cheerio = require('cheerio'); | |
glob('app/**/*.html', {}, function(er, files) { | |
files.forEach(function(file) { | |
var pathedFile = path.resolve(file); | |
var text = fs.readFileSync(pathedFile, 'utf-8'); | |
var $ = cheerio.load(text, {decodeEntities: false}); | |
$('*:contains("translate")') | |
.filter(function() { | |
var $this = $(this); | |
return $this.children().length === 0 && ($this.text().trim()).length > 0; | |
}) | |
.map(function(index, el) { | |
var text = $(el).text().trim(); | |
var translationID = string(text).between('{{', ' | translate}}').s | |
.replace(new RegExp('\"', 'g'), '') | |
.replace(new RegExp('\'', 'g'), ''); | |
$(el).attr('translate', translationID); | |
var templating = '{{' + string(text).between('{{', '}}').s + '}}'; | |
text = text.replace(templating, ''); | |
if (text.length > 0) { | |
$(el).text('<span>' + text + '</span>'); | |
}else { | |
$(el).text(text); | |
} | |
}); | |
var newHTML = $.html(); | |
fs.writeFileSync(pathedFile, newHTML, {encoding: 'utf-8'}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment