-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparse-examples.js
165 lines (150 loc) · 4.13 KB
/
parse-examples.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var fs = require('fs');
var path = require('path');
var async = require('async');
var Parser = require('htmlparser2').Parser;
var exampleDir = path.join(__dirname, '..', 'examples');
/**
* List all .html files in the example directory (excluding index.html).
* @param {function(Error, Array.<string>)} callback Called with any error or
* the list of paths to examples.
*/
function listExamples(callback) {
fs.readdir(exampleDir, function(err, items) {
if (err) {
return callback(err);
}
var examplePaths = items.filter(function(item) {
return /\.html$/i.test(item) && item !== 'index.html';
}).map(function(item) {
return path.join(exampleDir, item);
});
callback(null, examplePaths);
});
}
/**
* Parse info from examples.
* @param {Array.<string>} examplePaths Paths to examples.
* @param {function(Error, Array.<Object>)} callback Called with any error or
* the list of example info objects.
*/
function parseExamples(examplePaths, callback) {
async.map(examplePaths, function(examplePath, next) {
fs.readFile(examplePath, function(err, data) {
if (err) {
return next(err);
}
var name = path.basename(examplePath);
var info = {
link: name,
example: name,
title: '',
shortdesc: '',
tags: ''
};
var key;
var openTag;
var parser = new Parser({
onopentag: function(tag, attrs) {
if (attrs.id in info) {
key = attrs.id;
openTag = tag;
}
if (tag in info) {
key = tag;
openTag = tag;
}
},
ontext: function(text) {
if (key) {
info[key] += text.replace(/\n/g, '').trim() + ' ';
}
},
onclosetag: function(tag) {
if (tag === openTag) {
info[key] = info[key].trim();
key = undefined;
openTag = undefined;
}
},
onerror: function(err2) {
var message = 'Trouble parsing ' + examplePath + '\n' + err2.message;
next(new Error(message));
}
});
parser.write(data.toString('utf8'));
parser.end();
next(null, info);
});
}, callback);
}
/**
* Create an inverted index of keywords from examples. Property names are
* lowercased words. Property values are objects mapping example index to word
* count.
* @param {Array.<Object>} exampleInfos Array of example info objects.
* @return {Object} Word index.
*/
function createWordIndex(exampleInfos) {
var index = {};
var keys = ['shortdesc', 'title', 'tags'];
exampleInfos.forEach(function(info, i) {
keys.forEach(function(key) {
var text = info[key];
var words = text ? text.split(/\W+/) : [];
words.forEach(function(word) {
if (word) {
word = word.toLowerCase();
var counts = index[word];
if (counts) {
if (index in counts) {
counts[i] += 1;
} else {
counts[i] = 1;
}
} else {
counts = {};
counts[i] = 1;
index[word] = counts;
}
}
});
});
});
return index;
}
/**
* Write the example-list.js file with example info and word index.
* @param {Array.<Object>} exampleInfos Array of example info objects.
* @param {function(Error)} callback Called with any error.
*/
function writeExampleList(exampleInfos, callback) {
var info = {
examples: exampleInfos,
index: createWordIndex(exampleInfos)
};
var indexPath = path.join(exampleDir, 'example-list.js');
var str = 'var info = ' + JSON.stringify(info);
fs.writeFile(indexPath, str, callback);
}
/**
* List examples, parse them, and write example list.
* @param {function(Error)} callback Called with any error.
*/
function main(callback) {
async.waterfall([
listExamples,
parseExamples,
writeExampleList
], callback);
}
if (require.main === module) {
main(function(err) {
if (err) {
process.stderr.write(err.message + '\n');
process.exit(1);
} else {
process.exit(0);
}
});
}
module.exports = main;