-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
61 lines (55 loc) · 2.02 KB
/
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
58
59
60
61
/*!
* cheerio-tableparser
* https://github.com/misterparser/cheerio-tableparser
* https://www.npmjs.com/package/cheerio-tableparser
*
* Copyright (c) 2011 Francis Chong
* Copyright (c) 2016 Mister Parser
* Licensed under the MIT licenses.
*
*/
module.exports = function($) {
$.prototype.parsetable = function(dupCols, dupRows, textMode) {
if (dupCols === undefined) dupCols = false;
if (dupRows === undefined) dupRows = false;
if (textMode === undefined) textMode = false;
var columns = [],
curr_x = 0,
curr_y = 0;
$("tr", this).each(function(row_idx, row) {
curr_y = 0;
$("td, th", row).each(function(col_idx, col) {
var rowspan = $(col).attr('rowspan') || 1;
var colspan = $(col).attr('colspan') || 1;
if (textMode === true) {
var content = $(col).text().trim() || "";
} else {
var content = $(col).html() || "";
}
var x = 0,
y = 0;
for (x = 0; x < rowspan; x++) {
for (y = 0; y < colspan; y++) {
if (columns[curr_y + y] === undefined) {
columns[curr_y + y] = []
}
while (columns[curr_y + y][curr_x + x] !== undefined) {
curr_y += 1
if (columns[curr_y + y] === undefined) {
columns[curr_y + y] = []
}
}
if ((x === 0 || dupRows) && (y === 0 || dupCols)) {
columns[curr_y + y][curr_x + x] = content
} else {
columns[curr_y + y][curr_x + x] = ""
}
}
}
curr_y += 1;
});
curr_x += 1;
});
return columns;
};
}