Skip to content

Instantly share code, notes, and snippets.

@AntoineTurmel
Created July 1, 2012 00:31
Show Gist options
  • Save AntoineTurmel/3026206 to your computer and use it in GitHub Desktop.
Save AntoineTurmel/3026206 to your computer and use it in GitHub Desktop.
Entity Tool
/**
* Saebekassebil <Jakob Miland> 2012
* GeekShadow <Antoine Turmel> 2012
*
* Put this file in top of the project directory
* $ node search.js
*
**/
var fs = require('fs'),
url = require('url'),
path = require('path'),
child_process = require('child_process'),
exec = child_process.exec, child, dtds = {}, entitylist = {};
function processFile(path, callback) {
fs.readFile(path, 'utf8', function(error, data) {
if(error) {
return console.error('Error reading file: ' + error);
}
// Search for included DTDs
var matches = data.match(/SYSTEM\s+\"(.+?\.dtd)\"/g);
if(matches) {
matches.forEach(function(match) {
var dtdfile = match.match(/SYSTEM\s+\"(.+?\.dtd)\"/)[1];
if(!(dtdfile in dtds)) {
var dtd = processDTDFile(dtdfile);
if(dtd) {
dtds[dtdfile] = dtd;
console.log("Parsed DTD file: " + dtdfile);
}
}
});
}
// Search for entities
matches = data.match(/\&([a-zA-Z0-9\._]+)/g);
if(matches) {
matches.forEach(function(entity) {
entity = entity.substr(1); // cut off &
var adtd = hasEntity(entity);
if(entity in entitylist) {
entitylist[entity]++;
}
if(adtd) {
//console.log(adtd + ' has ' + entity);
} else {
//console.warn(entity + ' not found');
}
});
}
if(typeof callback === 'function') {
callback();
}
});
}
function hasEntity(entity) {
for(var i in dtds) {
if(dtds.hasOwnProperty(i)) {
if(entity in dtds[i]) {
return i;
}
}
}
return false;
}
function processDTDFile(file) {
// Translate chrome URL to absolute URL
var parsedURL = url.parse(file), filepath;
if(parsedURL.protocol !== 'chrome:') {
return null;
}
if(parsedURL.host === 'songbird') {
filepath = './locales/en-US/songbird/' + path.basename(parsedURL.pathname);
} else if(parsedURL.host === 'branding') {
filepath = './locales/branding/' + path.basename(parsedURL.pathname);
} else {
return null;
}
// Open and parse DTD file
var dtd = fs.readFileSync(filepath, 'utf8');
var lines = dtd.split('\n');
var regex = /<\!ENTITY\s+([a-zA-Z0-9\._]+)\s+\"(.+?)\"\s*?>/;
var entities = {};
for(var i = 0, length = lines.length; i < length; i++) {
var match = lines[i].match(regex);
if(match) {
if(match[1] in entities) {
console.warn('Overwritting entity ' + match[1]);
}
entities[match[1]] = match[2];
entitylist[match[1]] = 0;
}
}
return entities;
}
child = exec('find ./ | grep \'[.]xul\\|[.]xml\\|[.]xhtml\'', function(error, stdout, stderr) {
if(error) {
return console.error('Error finding files: ' + error);
}
var files = stdout.split('\n');
files = files.slice(0, files.length-1);
var active = -1;
function fileCallback() {
active++;
if(active <= files.length-1) {
processFile(files[active], fileCallback);
} else {
console.log('Unused entities:');
var count = 0;
for(var i in entitylist) {
if(entitylist[i] === 0) {
count++;
console.log(i);
}
}
console.log('A total of ' + count + ' unused entities');
}
}
fileCallback();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment