Nils Diewald | 33e1555 | 2014-06-13 19:38:37 +0000 | [diff] [blame^] | 1 | var cleanRegex = /^([^\/]+?\/)?[^\:]+?\:/; |
| 2 | var splitRegex = /^(.+?):([^:]+?)$/; |
| 3 | |
| 4 | // SnippetTree constructor |
| 5 | function SnippetTree (obj) { |
| 6 | this.children = []; |
| 7 | this.data = obj; |
| 8 | |
| 9 | // Replace title |
| 10 | this.cleanTitle = function (title) { |
| 11 | return title.replace(cleanRegex, ""); |
| 12 | }; |
| 13 | |
| 14 | // Add new child to tree |
| 15 | this.addChild = function (childData) { |
| 16 | var c = new SnippetTree (childData); |
| 17 | this.children.push(c); |
| 18 | return c; |
| 19 | }; |
| 20 | |
| 21 | // Recursively parse children |
| 22 | this.parseChildren = function (children) { |
| 23 | for (var i in children) { |
| 24 | var c = children[i]; |
| 25 | if (c.nodeType === 1) { |
| 26 | if (c.getAttribute("title")) { |
| 27 | var title = this.cleanTitle(c.getAttribute("title")); |
| 28 | var childTree = this.addChild({ type : title }); |
| 29 | if (c.hasChildNodes()) |
| 30 | childTree.parseChildren(c.childNodes); |
| 31 | } |
| 32 | else if (c.hasChildNodes()) |
| 33 | this.parseChildren(c.childNodes); |
| 34 | } |
| 35 | else if (c.nodeType === 3) |
| 36 | if (c.nodeValue.match(/[-a-z0-9]/i)) { |
| 37 | this.addChild({ |
| 38 | type : "leaf", |
| 39 | word : c.nodeValue |
| 40 | }); |
| 41 | }; |
| 42 | }; |
| 43 | return this; |
| 44 | }; |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | // SnippetTable constructor |
| 49 | function SnippetTable (obj) { |
| 50 | this.info = []; |
| 51 | this.overall = {}; |
| 52 | this.pos = 0; |
| 53 | this.load = function (children) { |
| 54 | for (var i in children) { |
| 55 | var c = children[i]; |
| 56 | |
| 57 | // element with title |
| 58 | if (c.nodeType === 1) { |
| 59 | if (c.getAttribute("title")) { |
| 60 | if (splitRegex.exec(c.getAttribute("title"))) { |
| 61 | |
| 62 | // Create object on position unless it exists |
| 63 | if (!this.info[this.pos]) { |
| 64 | this.info[this.pos] = {}; |
| 65 | }; |
| 66 | |
| 67 | // Fill position with info |
| 68 | this.info[this.pos][RegExp.$1] = RegExp.$2; |
| 69 | this.overall[RegExp.$1] = 1; |
| 70 | }; |
| 71 | }; |
| 72 | |
| 73 | // depth search |
| 74 | if (c.hasChildNodes()) |
| 75 | this.load(c.childNodes); |
| 76 | } |
| 77 | |
| 78 | // Leaf node - store string on position and go to next string |
| 79 | else if (c.nodeType === 3) |
| 80 | if (c.nodeValue.match(/[-a-z0-9]/i)) |
| 81 | this.info[this.pos++]["-s"] = c.nodeValue; |
| 82 | }; |
| 83 | return this; |
| 84 | }; |
| 85 | }; |
| 86 | |
| 87 | |
| 88 | // Make tree from snippet |
| 89 | function translateTree (snippet) { |
| 90 | var html = document.createElement("tree"); |
| 91 | html.innerHTML = snippet; |
| 92 | return new SnippetTree({ type : "ROOT" }).parseChildren(html.childNodes); |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | // Make table from snippet |
| 97 | function translateTable (snippet) { |
| 98 | // Create wrapper element |
| 99 | var html = document.createElement("table"); |
| 100 | html.innerHTML = snippet; |
| 101 | |
| 102 | // Create table object and load data from HTML |
| 103 | var info = new SnippetTable(); |
| 104 | info.load(html.childNodes); |
| 105 | |
| 106 | // Sort keys |
| 107 | var overallArray = []; |
| 108 | for (i in info.overall) { |
| 109 | overallArray.push(i); |
| 110 | }; |
| 111 | overallArray.sort(); |
| 112 | |
| 113 | // Create HTML based on info |
| 114 | var d = document; |
| 115 | var table = d.createElement('table'); |
| 116 | var tr = d.createElement('tr'); |
| 117 | table.appendChild(tr); |
| 118 | var th = d.createElement('th'); |
| 119 | tr.appendChild(th); |
| 120 | |
| 121 | // Header line with surface strings |
| 122 | for (i in info.info) { |
| 123 | th = d.createElement('th'); |
| 124 | tr.appendChild(th); |
| 125 | th.appendChild(d.createTextNode(info.info[i]["-s"])); |
| 126 | }; |
| 127 | |
| 128 | // Annotations |
| 129 | for (i in overallArray) { |
| 130 | i = overallArray[i]; |
| 131 | |
| 132 | tr = d.createElement('tr'); |
| 133 | table.appendChild(tr); |
| 134 | th = d.createElement('th'); |
| 135 | tr.appendChild(th); |
| 136 | th.appendChild(d.createTextNode(i)); |
| 137 | for (t in info.info) { |
| 138 | var td = d.createElement('td'); |
| 139 | tr.appendChild(td); |
| 140 | if (info.info[t][i] !== undefined) |
| 141 | td.appendChild(d.createTextNode(info.info[t][i])); |
| 142 | }; |
| 143 | }; |
| 144 | |
| 145 | // return HTML object |
| 146 | return table; |
| 147 | }; |