blob: 0ade20b09068d64adcd3cb6aba28bbf2afbecdcc [file] [log] [blame]
Nils Diewald1eba6572014-06-17 19:49:53 +00001var cleanRegex = /^([^\/]+?\/)?[^\:]+?\:/;
2
3// SnippetTree constructor
4function SnippetTree (obj) {
5 this.children = [];
6 this.data = obj;
7
8 // Replace title
9 this.cleanTitle = function (title) {
10 return title.replace(cleanRegex, "");
11 };
12
13 // Add new child to tree
14 this.addChild = function (childData) {
15 var c = new SnippetTree (childData);
16 this.children.push(c);
17 return c;
18 };
19
20 // Recursively parse children
21 this.parseChildren = function (children) {
22 for (var i in children) {
23 var c = children[i];
24 if (c.nodeType === 1) {
25 if (c.getAttribute("title")) {
26 var title = this.cleanTitle(c.getAttribute("title"));
27 var childTree = this.addChild({ type : title });
28 if (c.hasChildNodes())
29 childTree.parseChildren(c.childNodes);
30 }
31 else if (c.hasChildNodes())
32 this.parseChildren(c.childNodes);
33 }
34 else if (c.nodeType === 3)
35 if (c.nodeValue.match(/[-a-z0-9]/i)) {
36 this.addChild({
37 type : "leaf",
38 word : c.nodeValue
39 });
40 };
41 };
42 return this;
43 };
Nils Diewald44a72782014-06-20 16:03:21 +000044
45 // Recursively parse children
46 this.parseChildren2 = function (children) {
47 for (var i in children) {
48 var c = children[i];
49 if (c.nodeType === 1) {
50 if (c.getAttribute("title")) {
51 var title = this.cleanTitle(c.getAttribute("title"));
52 var childTree = this.addChild({ type : title });
53 if (c.hasChildNodes())
54 childTree.parseChildren2(c.childNodes);
55 }
56 else if (c.hasChildNodes())
57 this.parseChildren2(c.childNodes);
58 }
59 else if (c.nodeType === 3)
60 if (c.nodeValue.match(/[-a-z0-9]/i)) {
61 this.addChild({
62 type : "leaf",
63 word : c.nodeValue
64 });
65 };
66 };
67 return this;
68 };
69
70
71 this.toTable = function () {
72
73 };
74
75 var tree = document.createElement('tree');
76 html.innerHTML = snippet;
77 this.parseChildren2(html.childNodes);
Nils Diewald1eba6572014-06-17 19:49:53 +000078};
79
80// Make tree from snippet
81function translateTree (snippet) {
82 var html = document.createElement("tree");
83 html.innerHTML = snippet;
84 return new SnippetTree({ type : "ROOT" }).parseChildren(html.childNodes);
85};
86
87