| var cleanRegex = /^([^\/]+?\/)?[^\:]+?\:/; |
| |
| // SnippetTree constructor |
| function SnippetTree (obj) { |
| this.children = []; |
| this.data = obj; |
| |
| // Replace title |
| this.cleanTitle = function (title) { |
| return title.replace(cleanRegex, ""); |
| }; |
| |
| // Add new child to tree |
| this.addChild = function (childData) { |
| var c = new SnippetTree (childData); |
| this.children.push(c); |
| return c; |
| }; |
| |
| // Recursively parse children |
| this.parseChildren = function (children) { |
| for (var i in children) { |
| var c = children[i]; |
| if (c.nodeType === 1) { |
| if (c.getAttribute("title")) { |
| var title = this.cleanTitle(c.getAttribute("title")); |
| var childTree = this.addChild({ type : title }); |
| if (c.hasChildNodes()) |
| childTree.parseChildren(c.childNodes); |
| } |
| else if (c.hasChildNodes()) |
| this.parseChildren(c.childNodes); |
| } |
| else if (c.nodeType === 3) |
| if (c.nodeValue.match(/[-a-z0-9]/i)) { |
| this.addChild({ |
| type : "leaf", |
| word : c.nodeValue |
| }); |
| }; |
| }; |
| return this; |
| }; |
| }; |
| |
| // Make tree from snippet |
| function translateTree (snippet) { |
| var html = document.createElement("tree"); |
| html.innerHTML = snippet; |
| return new SnippetTree({ type : "ROOT" }).parseChildren(html.childNodes); |
| }; |
| |
| |