var fs = require("fs"), path = require("path"); function htmlspecialchars(str) { //Hum, hum... Could do better if (typeof str !== "string") return ""; return str.replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } function renderPath(el, pathstring) { return ''; } var Tools = { "Text": function (el) { return '' + htmlspecialchars(el.txt || "") + ''; }, "Pencil": function (el) { if (!el._children) return ""; switch (el._children.length) { case 0: return ""; case 1: var pathstring = "M" + el._children[0].x + " " + el._children[0].y + "L" + el._children[0].x + " " + el._children[0].y; break; default: var pathstring = "M" + el._children[0].x + " " + el._children[0].y + "L"; for (var i = 1; i < el._children.length; i++) { pathstring += (+el._children[i].x) + " " + (+el._children[i].y) + " "; } } return renderPath(el, pathstring); }, "Rectangle": function (el) { var pathstring = "M" + el.x + " " + el.y + "L" + el.x + " " + el.y2 + "L" + el.x2 + " " + el.y2 + "L" + el.x2 + " " + el.y + "L" + el.x + " " + el.y; return renderPath(el, pathstring); }, "Straight line": function (el) { var pathstring = "M" + el.x + " " + el.y + "L" + el.x2 + " " + el.y2; return renderPath(el, pathstring); } }; function toSVG(obj) { var margin = 500, maxelems = 1e4; var elements = "", i = 0, w = 500, h = 500; var t = Date.now(); var elems = Object.values(obj); while (elems.length > 0) { if (++i > maxelems) break; var elem = elems.pop(); elems = elems.concat(elem._children || []); if (elem.x && elem.x + margin > w) w = elem.x + margin; if (elem.y && elem.y + margin > h) h = elem.y + margin; var renderFun = Tools[elem.tool]; if (renderFun) elements += renderFun(elem); } console.error(i + " elements treated in " + (Date.now() - t) + "ms."); var svg = '' + '' + elements + ''; return svg; } function renderBoard(file, callback) { var t = Date.now(); fs.readFile(file, function (err, data) { if (err) return callback(err); var board = JSON.parse(data); console.warn("JSON parsed in " + (Date.now() - t) + "ms."); var svg = toSVG(board); console.warn("Board rendered in " + (Date.now() - t) + "ms."); callback(null, svg); }); } if (require.main === module) { var HISTORY_FILE = process.argv[2] || path.join(__dirname, "../server-data/board-anonymous.json"); renderBoard(HISTORY_FILE, function (err, rendered) { console.log(rendered); }); } else { module.exports = { 'renderBoard': renderBoard }; }