From e4481ea22b911d9818750e74ad30c7983d1a34dc Mon Sep 17 00:00:00 2001 From: Lorenzo Gil Date: Mon, 31 Oct 2011 11:18:44 +0100 Subject: [PATCH] Allow to get the HTML of the pad with the API --- node/db/API.js | 85 ++++++++++++++++++++++++++++++++++++++ node/handler/APIHandler.js | 1 + node/utils/ExportHtml.js | 2 + 3 files changed, 88 insertions(+) diff --git a/node/db/API.js b/node/db/API.js index 9912b098..2069ce68 100644 --- a/node/db/API.js +++ b/node/db/API.js @@ -25,6 +25,7 @@ var groupManager = require("./GroupManager"); var authorManager = require("./AuthorManager"); var sessionManager = require("./SessionManager"); var async = require("async"); +var exportHtml = require("../utils/ExportHtml"); /**********************/ /**GROUP FUNCTIONS*****/ @@ -169,6 +170,90 @@ exports.setText = function(padID, text, callback) }); } +/** +getHTML(padID, [rev]) returns the html of a pad + +Example returns: + +{code: 0, message:"ok", data: {text:"Welcome Text"}} +{code: 1, message:"padID does not exist", data: null} +*/ +exports.getHTML = function(padID, rev, callback) +{ + if(typeof rev == "function") + { + callback = rev; + rev = undefined; + } + + if (rev !== undefined && typeof rev != "number") + { + if (!isNaN(parseInt(rev))) + { + rev = parseInt(rev); + } + else + { + callback({stop: "rev is not a number"}); + return; + } + } + + if(rev !== undefined && rev < 0) + { + callback({stop: "rev is a negative number"}); + return; + } + + if(rev !== undefined && !is_int(rev)) + { + callback({stop: "rev is a float value"}); + return; + } + + getPadSafe(padID, true, function(err, pad) + { + if(err) + { + callback(err); + return; + } + + //the client asked for a special revision + if(rev !== undefined) + { + //check if this is a valid revision + if(rev > pad.getHeadRevisionNumber()) + { + callback({stop: "rev is higher than the head revision of the pad"}); + return; + } + + //get the html of this revision + exportHtml.getPadHTML(pad, rev, function(err, html) + { + if(!err) + { + data = {html: html}; + } + callback(err, data); + }); + } + //the client wants the latest text, lets return it to him + else + { + exportHtml.getPadHTML(pad, undefined, function (err, html) + { + if(!err) + { + data = {html: html}; + } + callback(err, data); + }); + } + }); +} + /*****************/ /**PAD FUNCTIONS */ /*****************/ diff --git a/node/handler/APIHandler.js b/node/handler/APIHandler.js index 34d63f18..57eeeb73 100644 --- a/node/handler/APIHandler.js +++ b/node/handler/APIHandler.js @@ -50,6 +50,7 @@ var functions = { "listSessionsOfAuthor" : ["authorID"], "getText" : ["padID", "rev"], "setText" : ["padID", "text"], + "getHTML" : ["padID", "rev"], "getRevisionsCount" : ["padID"], "deletePad" : ["padID"], "getReadOnlyID" : ["padID"], diff --git a/node/utils/ExportHtml.js b/node/utils/ExportHtml.js index dce156ec..354757e1 100644 --- a/node/utils/ExportHtml.js +++ b/node/utils/ExportHtml.js @@ -85,6 +85,8 @@ function getPadHTML(pad, revNum, callback) }); } +exports.getPadHTML = getPadHTML; + function getHTMLFromAtext(pad, atext) { var apool = pad.apool();