diff --git a/lib/hypertopic.js b/lib/hypertopic.js index d79f8d9..fff0644 100644 --- a/lib/hypertopic.js +++ b/lib/hypertopic.js @@ -24,6 +24,8 @@ exports.Viewpoint = function(o) { } } + var util = require("lib/util"); + this.addRow = function(key, value) { var subject = key[key.length-1]; var predicate = Object.keys(value)[0]; @@ -79,6 +81,33 @@ exports.Viewpoint = function(o) { send(""); send(""); } + + this.sendFreemindNode = function(id) { + var node = this.data.topics[id]; + send(''); + for each (var child in node.narrower) { + this.sendFreemindNode(child.id); + } + send(''); + } + + this.sendFreemind = function() { + start({headers:{ + "Content-Type": "application/x-freemind", + "Content-Disposition": "attachment; filename=" + + this.data.viewpoint_name.replace(/[ ,]/g, "_") + ".mm;" + }}); + send(''); + send(''); + send(''); + for each (var upper in this.data.upper) { + this.sendFreemindNode(upper.id); + } + send(''); + send(''); + } } exports.Item = function() { diff --git a/lib/utf8.js b/lib/utf8.js deleted file mode 100644 index 9eb23e0..0000000 --- a/lib/utf8.js +++ /dev/null @@ -1,25 +0,0 @@ -//Ported from json2.js -exports.utf8Encode = function (string) { - var escapable = /[\\\"\x00-\x1f\x7f-\uffff]/g, - meta = { // table of character substitutions - '\b': '\\b', - '\t': '\\t', - '\n': '\\n', - '\f': '\\f', - '\r': '\\r', - '"' : '\\"', - '\\': '\\\\' - }; - escapable.lastIndex = 0; - return escapable.test(string) - ? string.replace(escapable, - function (a) { - var c = meta[a]; - return typeof c === 'string' ? c : - //TODO escape &? - '[[[__AND___]]]#x' + - ('0000' + a.charCodeAt(0).toString(16)).slice(-4) - + ';'; - }) - : string ; -} \ No newline at end of file diff --git a/lib/util.js b/lib/util.js index da5ff72..403ed12 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,30 +1,3 @@ -exports.normalize = function(obj) -{ - if(!obj.rows) return obj; - var rows = obj.rows; - var result = {}; - for(var i=0; i < rows.length; i++) - { - var r = rows[i]; - var keys = r.key; - var current = result; - for(var k=0; k < keys.length; k++) - { - if(!current[keys[k]]) - current[keys[k]] = {}; - current = current[keys[k]]; - } - var value = r.value; - for(var attribute in value) - { - if(!current[attribute]) - current[attribute] = []; - current[attribute].push(value[attribute]); - } - } - return result; -} - exports.sendView = function() { send('{"rows":['); var row = getRow(); @@ -35,3 +8,14 @@ exports.sendView = function() { } send(']}'); } + +exports.xmlencode = function(data) { + switch (typeof data) { + case "object": + data = data[0]; + case "string": + data = data.replace(/\&/g,'&'+'amp;').replace(/'); - xml.@version = "0.9.0"; - var doc = {"rows": []}; - while (r = getRow()) - doc.rows.push(r); - var viewpoint = require("lib/util").normalize(doc); - var viewpointID; - for(viewpointID in viewpoint) - break; - viewpoint = viewpoint[viewpointID]; - var root = ; - xml.appendChild(root); - - delete viewpoint.name; - delete viewpoint.user; - var uppers = []; - for(var i=0, upper; upper = viewpoint.upper[i]; i++){ - uppers.push(upper.id); - root.appendChild(); - } - - send(xml); - }); - provides("html", function() { - send("

Export viewpoint to Freemind

"); - send("

You can dump a viewpoint to a freemind.

"); - send("

" + JSON.stringify(head) + "

"); - }); - provides("json", function() { - var doc = {"rows": []}; - while (r = getRow()) - doc.rows.push(r); - var docFormated = require("lib/util").normalize(doc); - send(JSON.stringify(docFormated)); - }); -}; \ No newline at end of file diff --git a/lists/topicV1.js b/lists/topicV1.js index 8b114c0..9c17adf 100644 --- a/lists/topicV1.js +++ b/lists/topicV1.js @@ -1,4 +1,6 @@ function(head, req) { + var util = require("lib/util"); + start({ "headers": { "Content-Type": "text/xml" @@ -13,10 +15,6 @@ function(head, req) { return result; } - function xmlencode(string) { - return string.replace(/\&/g,'&'+'amp;').replace(/'); - send(xmlencode(h.text)); + send(util.xmlencode(h.text)); send('\n'); } send('\n'); diff --git a/lists/viewpoint.js b/lists/viewpoint.js index b607f0d..5169f27 100644 --- a/lists/viewpoint.js +++ b/lists/viewpoint.js @@ -8,6 +8,18 @@ function (head, req) { while (row = getRow()) { viewpoint.addRow(row.key, row.value); } - viewpoint.sendHTML(); + if (req.query.download) { + viewpoint.sendFreemind(); + } else { + viewpoint.sendHTML(); + } + }); + + registerType("freemind", "application/x-freemind"); + provides("freemind", function() { + while (row = getRow()) { + viewpoint.addRow(row.key, row.value); + } + viewpoint.sendFreemind(); }); } diff --git a/shows/itemV1.js b/shows/itemV1.js index 9b44aa9..4590afa 100644 --- a/shows/itemV1.js +++ b/shows/itemV1.js @@ -1,8 +1,5 @@ function(o, req) { - - function xmlencode(string) { - return string.replace(/\&/g,'&'+'amp;').replace(/\n'); for (var key in o) { @@ -20,7 +17,7 @@ function(o, req) { break; case 'resource': send('\n'); if (o.resource.indexOf('picture')>=0) { send(''); - xmlDoc.@version = "0.9.0"; - xmlDoc.appendChild(); - var root - = ; - root.appendChild(); - if(doc.users) - for(var i=0, user; user = doc.users[i]; i++) - root.appendChild( - ); - xmlDoc.appendChild(root); - - var uppers = []; - for(var id in doc.topics){ - if(!doc.topics[id].broader || doc.topics[id].broader.length == 0) - uppers.push(id); - if(doc.topics[id].broader) - for(var i=0, parentId; parentId = doc.topics[id].broader[i]; i++) - (doc.topics[parentId].narrower) - ? doc.topics[parentId].narrower.push(id) - : doc.topics[parentId].narrower = [id]; - if(!doc.topics[id].narrower) - doc.topics[id].narrower = []; - - doc.topics[id].xmlNode - = ; - } - - //log(uppers); - function appendSubTopics(id){ - for(var i=0, tid; tid = doc.topics[id].narrower[i]; i++) - { - if(doc.topics[tid].xmlNode) - { - appendSubTopics(tid); - doc.topics[id].xmlNode.appendChild(doc.topics[tid].xmlNode); - delete doc.topics[tid].xmlNode; - } - else - doc.topics[id].xmlNode.appendChild(); - } - } - - for(var i=0, id; id = uppers[i]; i++) - { - appendSubTopics(id); - root.appendChild(doc.topics[id].xmlNode); - } - var xmlStr = dec(xmlDoc.toXMLString()); - if(req.query.download) - { - filename = encodeURIComponent(doc.viewpoint_name) + ".mm"; - return { - body : xmlStr, - headers : { - "Content-Type" : "application/xml", - "Content-Disposition" : "attachment; filename=" + filename + ";" - } - } - } - else - return { - body : xmlStr, - headers : { "Content-Type" : "application/xml" } - } -}; \ No newline at end of file