Added JSON import

This commit is contained in:
Nelson Silva 2013-02-14 16:01:59 +00:00
parent 9e3f367446
commit 513485c6c9
2 changed files with 91 additions and 6 deletions

View file

@ -28,7 +28,8 @@ var ERR = require("async-stacktrace")
, settings = require('../utils/Settings') , settings = require('../utils/Settings')
, formidable = require('formidable') , formidable = require('formidable')
, os = require("os") , os = require("os")
, importHtml = require("../utils/ImportHtml"); , importHtml = require("../utils/ImportHtml")
, importJSON = require("../utils/ImportJSON");
//load abiword only if its enabled //load abiword only if its enabled
if(settings.abiword != null) if(settings.abiword != null)
@ -48,7 +49,9 @@ exports.doImport = function(req, res, padId)
var srcFile, destFile var srcFile, destFile
, pad , pad
, text; , text
, fileEnding;
async.series([ async.series([
//save the uploaded file to /tmp //save the uploaded file to /tmp
@ -72,11 +75,14 @@ exports.doImport = function(req, res, padId)
}); });
}, },
//ensure this is a file ending we know, else we change the file ending to .txt //ensure this is a file ending we know, else we change the file ending to .txt
//this allows us to accept source code files like .c or .java //this allows us to accept source code files like .c or .java
function(callback) { function(callback) {
var fileEnding = path.extname(srcFile).toLowerCase() fileEnding = path.extname(srcFile).toLowerCase();
, knownFileEndings = [".txt", ".doc", ".docx", ".pdf", ".odt", ".html", ".htm"]
var knownFileEndings = [".txt", ".doc", ".docx", ".pdf", ".odt", ".html", ".htm", ".json"]
, fileEndingKnown = (knownFileEndings.indexOf(fileEnding) > -1); , fileEndingKnown = (knownFileEndings.indexOf(fileEnding) > -1);
//if the file ending is known, continue as normal //if the file ending is known, continue as normal
@ -95,9 +101,12 @@ exports.doImport = function(req, res, padId)
//convert file to html //convert file to html
function(callback) { function(callback) {
var randNum = Math.floor(Math.random()*0xFFFFFFFF); var randNum = Math.floor(Math.random()*0xFFFFFFFF);
destFile = path.join(tmpDirectory, "eplite_import_" + randNum + ".htm"); destFile = path.join(tmpDirectory, "eplite_import_" + randNum + fileEnding);
if (abiword) { // check if this format is supported by AbiWord
var isAbiWordSupported = (['json'].indexOf(fileEnding) == -1);
if (abiword && isAbiWordSupported) {
abiword.convertFile(srcFile, destFile, "htm", function(err) { abiword.convertFile(srcFile, destFile, "htm", function(err) {
//catch convert errors //catch convert errors
if(err) { if(err) {
@ -143,6 +152,8 @@ exports.doImport = function(req, res, padId)
var fileEnding = path.extname(srcFile).toLowerCase(); var fileEnding = path.extname(srcFile).toLowerCase();
if (abiword || fileEnding == ".htm" || fileEnding == ".html") { if (abiword || fileEnding == ".htm" || fileEnding == ".html") {
importHtml.setPadHTML(pad, text); importHtml.setPadHTML(pad, text);
} else if (fileEnding == ".json") {
importJSON.setPadJSON(pad, JSON.parse(text));
} else { } else {
pad.setText(text); pad.setText(text);
} }

View file

@ -0,0 +1,74 @@
var log4js = require('log4js');
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
function keepText(builder, text, from, to) {
var chars = to - from;
var keepText = text.substring(from, to);
var keepLines = keepText.split("\n").length - 1;
builder.keep(chars, keepLines);
}
function keepTextWithAttributes(builder, text, from, to, attributes, apool) {
var keepText = text.substring(from, to);
var linePos = 0;
var length = keepText.length;
while (linePos < length) {
var lineEnd = keepText.indexOf('\n', linePos) + 1;
var lines;
if (lineEnd != 0) {
lines = 1;
} else {
lines = 0;
lineEnd = length;
}
var chars = lineEnd - linePos;
builder.keep(chars, lines, attributes, apool);
linePos += chars;
}
}
function setPadJSON(pad, json, callback) {
var apiLogger = log4js.getLogger("ImportHtml");
// Set the text
pad.setText(json.atext.text);
// Set the apoll
var nextNum = Math.max.apply(null, Object.keys(json.apool));
pad.apool().fromJsonable({numToAttrib:json.apool, nextNum:nextNum});
var padText = pad.text();
var padTextLength = padText.length;
// split the text into lines
json.atext.attributes.forEach(function(att) {
// use a builder
var builder = Changeset.builder(padTextLength);
var from = parseInt(att.from);
var to = parseInt(att.to);
// Keep everything before "from"
if (from > 0) {
keepText(builder, padText, 0, from);
}
// Keep but add the attribute
keepTextWithAttributes(builder, padText, from, to, "*" + att.id, pad.apool());
// Keep everything after "to"
if (to < padTextLength) {
keepText(builder, padText, to, padTextLength);
}
// apply the revision
pad.appendRevision(builder.toString());
})
}
exports.setPadJSON = setPadJSON;