-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoriginal.js
More file actions
34 lines (30 loc) · 896 Bytes
/
Copy pathoriginal.js
File metadata and controls
34 lines (30 loc) · 896 Bytes
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
var cli = process.argv.slice(2)
var path = require('path')
var fs = require('fs')
var cheerio = require('cheerio')
var $ = cheerio.load(fs.readFileSync(path.resolve(__dirname, cli[0])))
function getCategories($a) {
var $node = $a.closest('DL').prev();
var title = $node.text();
if ($node.length > 0 && title.length > 0) {
return title = [title].concat(getCategories($node));
} else {
return [];
}
}
var jsonbmArray = []
$('a').each(function (index, a) {
let $a = $(a)
let categories = getCategories($a)
// add level information
let new_categories = categories.reverse().map(function (currentValue, index) {
return currentValue['level'] = index + 1, currentValue
})
let jsonbm = {
'title': $a.text(),
'url': $a.attr('href'),
'categories': categories,
}
jsonbmArray.push(jsonbm)
})
fs.writeFileSync(path.resolve(__dirname, cli[1]), JSON.stringify(jsonbmArray, null, 4))