-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (45 loc) · 1014 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Module dependencies.
*/
var request = require('superagent');
var jquery = require('cheerio').load;
/**
* Expose `parse`.
*/
module.exports = parse;
/**
* Parse wiki `url` and invoke `fn(err, pkgs)`.
*
* @param {String} url
* @param {Function} fn
* @api public
*/
function parse(url, fn) {
request
.get(url)
.end(function(err, res){
if (err) return fn(err);
if (res.error) return fn(res.error);
var $ = jquery(res.text);
var data = {};
var cat;
$('#wiki-body h2, #wiki-body h2 + ul li').each(function(){
var name = this[0].name;
// heading
if ('h' == name[0]) {
var text = $(this).text().trim();
cat = data[text] = [];
return;
}
// package
var tuple = $(this).text().split(':');
var name = tuple[0].trim();
var abbrs = tuple[1].split(',').map(function(abbr){return abbr.trim();});
cat.push({
name: name,
abbrs: abbrs
});
});
fn(null, data);
});
}