Skip to content

Instantly share code, notes, and snippets.

@glennblock
Created August 8, 2012 18:05
Show Gist options
  • Save glennblock/3297118 to your computer and use it in GitHub Desktop.
Save glennblock/3297118 to your computer and use it in GitHub Desktop.
Create counts for modules by category from nodejs.org module wiki
var request = require('request');
var sax = require('sax');
var parser = sax.parser(false);
var categories=[];
var category;
var FIND_MODULES_START = 0;
var FIND_CATEGORY = 1;
var FIND_CATEGORY_NAME = 2;
var FIND_MODULES = 3;
var DONE = 4;
var state = FIND_MODULES_START;
parser.onopentag = function(node) {
if (state === FIND_MODULES_START &&
node.name === "A" &&
node.attributes.HREF === "#wiki-modules") {
state = FIND_CATEGORY;
}
else if ((state === FIND_CATEGORY || state === FIND_MODULES) && node.name === "H2") {
state = FIND_CATEGORY_NAME;
}
else if (state === FIND_MODULES && node.name === "LI") {
category.modules++;
}
else if (state === FIND_MODULES && node.name === "DIV") {
if (node.attributes.ID === "gollum-footer") {
state = DONE;
}
}
}
parser.ontext = function(t) {
if (state === FIND_CATEGORY_NAME) {
category = {};
category.name = t;
category.modules = 0;
categories.push(category);
state = FIND_MODULES;
}
}
request('http://github.com/joyent/node/wiki/modules', function(err, res, body) {
parser.write(body).close();
var sorted = categories.sort(function(x,y) {
if (x.name === y.name) {
return 0;
}
else if (x.name < y.name) {
return -1;
}
else if (x.name > y.name) {
return 1;
}
});
sorted.forEach(function(item) {
console.log(item.name + ":" + item.modules);
});
});
@glennblock
Copy link
Author

Updated now to output sorted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment