-
Notifications
You must be signed in to change notification settings - Fork 3
/
treeBuild.js
99 lines (76 loc) · 2.02 KB
/
treeBuild.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
var async = require('async')
var request = require('request')
var tidyMarkdown = require('./tidyMarkdown')
var findTransclusions = require('./findTransclusions')
var regexps = require('./regexps')
module.exports = function( url, callback ) {
var parentTree = Tree({
label: null,
url: url,
parent: null,
depth: 0,
})
expandTree( parentTree, callback )
}
function Tree( attrs ) {
return {
label: attrs['label'],
url: attrs['url'],
parent: attrs['parent'],
depth: attrs['depth'],
content: null,
children: null,
}
}
function treeWithParent( treeNode ) {
return function ( attrs ) {
attrs.parent = treeNode
attrs.depth = treeNode.depth + 1
return Tree( attrs )
}
}
function expandTree( treeNode, callback ) {
if (isInfiniteLoop(treeNode)) return callback(null, treeNode)
var getUrl = prepareUrl(treeNode)
request.get( getUrl, function(err, response, body) {
if (err) {
console.error("there was an error getting: " + treeNode.url)
return callback(err)
}
treeNode.content = body
tidyMarkdown( treeNode )
treeNode.children = findTransclusions.md(treeNode.content).map( treeWithParent(treeNode) )
async.each(
treeNode.children,
expandTree,
function (err) {
callback(err, treeNode)
}
)
})
}
function isInfiniteLoop( tree, url ) {
if (tree.parent == null) return false
if (url == null) {
return isInfiniteLoop( tree.parent, tree.url )
}
if (tree.url === url) return true
return isInfiniteLoop ( tree.parent, url )
}
function prepareUrl( treeNode ) {
url = makeRaw(treeNode.url)
printGet( url, treeNode )
return url
}
function printGet( url, treeNode ) {
if (process.env.NODE_ENV != 'testing') {
console.log( Array(treeNode.depth*2).join(' ') + green('[Get] ') + url)
}
}
function green(string) { return ("\033[32m"+ string +"\033[0m") }
function makeRaw( url ) {
if (url.match( regexps.githublab )) {
url = url.replace(/\/blob\//, '/raw/')
}
return url
}