mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-05-15 03:26:53 -04:00
Added JSON export
This commit is contained in:
parent
a4e5adab3e
commit
9e3f367446
3 changed files with 53 additions and 1 deletions
|
@ -21,6 +21,7 @@
|
||||||
var ERR = require("async-stacktrace");
|
var ERR = require("async-stacktrace");
|
||||||
var exporthtml = require("../utils/ExportHtml");
|
var exporthtml = require("../utils/ExportHtml");
|
||||||
var exportdokuwiki = require("../utils/ExportDokuWiki");
|
var exportdokuwiki = require("../utils/ExportDokuWiki");
|
||||||
|
var exportjson = require("../utils/ExportJSON");
|
||||||
var padManager = require("../db/PadManager");
|
var padManager = require("../db/PadManager");
|
||||||
var async = require("async");
|
var async = require("async");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
@ -65,6 +66,16 @@ exports.doExport = function(req, res, padId, type)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
else if(type == "json")
|
||||||
|
{
|
||||||
|
padManager.getPad(padId, function(err, pad)
|
||||||
|
{
|
||||||
|
ERR(err);
|
||||||
|
exportjson.getPadJSON(pad, req.params.rev, function(err, json) {
|
||||||
|
res.send(json);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
else if(type == 'dokuwiki')
|
else if(type == 'dokuwiki')
|
||||||
{
|
{
|
||||||
var randNum;
|
var randNum;
|
||||||
|
|
|
@ -5,7 +5,7 @@ var importHandler = require('../../handler/ImportHandler');
|
||||||
|
|
||||||
exports.expressCreateServer = function (hook_name, args, cb) {
|
exports.expressCreateServer = function (hook_name, args, cb) {
|
||||||
args.app.get('/p/:pad/:rev?/export/:type', function(req, res, next) {
|
args.app.get('/p/:pad/:rev?/export/:type', function(req, res, next) {
|
||||||
var types = ["pdf", "doc", "txt", "html", "odt", "dokuwiki"];
|
var types = ["pdf", "doc", "txt", "html", "odt", "dokuwiki", "json"];
|
||||||
//send a 404 if we don't support this filetype
|
//send a 404 if we don't support this filetype
|
||||||
if (types.indexOf(req.params.type) == -1) {
|
if (types.indexOf(req.params.type) == -1) {
|
||||||
next();
|
next();
|
||||||
|
|
41
src/node/utils/ExportJSON.js
Normal file
41
src/node/utils/ExportJSON.js
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
||||||
|
var ERR = require("async-stacktrace");
|
||||||
|
|
||||||
|
function getJSONAttributes(attribs) {
|
||||||
|
var pos = 0;
|
||||||
|
var iter = Changeset.opIterator(attribs);
|
||||||
|
var attr = [];
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
var op = iter.next();
|
||||||
|
Changeset.eachAttribNumber(op.attribs, function (a){
|
||||||
|
attr.push({id: a, from:pos, to: pos + op.chars});
|
||||||
|
});
|
||||||
|
pos += op.chars;
|
||||||
|
}
|
||||||
|
return attr;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPadJSON(pad, rev, callback) {
|
||||||
|
|
||||||
|
//check parameters
|
||||||
|
if(!pad || !pad.id || !pad.atext || !pad.pool) {
|
||||||
|
throw new Error('Invalid pad');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!rev) {
|
||||||
|
rev = pad.getHeadRevisionNumber();
|
||||||
|
} else {
|
||||||
|
rev = parseInt(rev, 10);
|
||||||
|
if(rev < 0 || rev > pad.getHeadRevisionNumber()) {
|
||||||
|
throw new Error('Invalid start revision ' + fromRev);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pad.getInternalRevisionAText(rev, function (err, atext) {
|
||||||
|
if(ERR(err, callback)) return;
|
||||||
|
attributes = getJSONAttributes(atext.attribs);
|
||||||
|
callback(null, {apool: pad.apool().numToAttrib, atext: { attributes: attributes, text: atext.text}});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.getPadJSON = getPadJSON;
|
Loading…
Add table
Add a link
Reference in a new issue