Created
August 8, 2012 18:05
-
-
Save glennblock/3297118 to your computer and use it in GitHub Desktop.
Create counts for modules by category from nodejs.org module wiki
This file contains hidden or 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
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); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated now to output sorted.