-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadTree
More file actions
39 lines (37 loc) · 774 Bytes
/
readTree
File metadata and controls
39 lines (37 loc) · 774 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
35
36
37
38
39
<!-- Read tree from indented file -->
<script>
var data = {}
var x = new XMLHttpRequest()
x.open('get', 'classicmodels.tree')
x.overrideMimeType('text/plain')
x.onload = function(){
var stack = [-1];
var ids = [ null ];
var id = 0;
for(var line of x.response.split(/\r?\n/g)){
var p = line.search(/\S/)
if(p==stack[stack.length-1]){
stack.pop()
ids.pop()
}
if(p>=stack[stack.length-1]){
stack.push(p)
ids.push(id++)
}
while(p<stack[stack.length-1]){
stack.pop()
ids.pop()
}
if(p>=0){
var parent = ids[ids.length-1]
if(parent){
data[id] = { parent: parent, child: [], text: line.trim() }
data[parent].child.push(id)
}else
data[id] = { child: [], text: line.trim() }
}
}
console.log(data)
}
x.send()
</script>