dubdiff/server/components/wdiff/index.js

323 lines
8.7 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
var _ = require('lodash'),
temp = require('temp'),
fs = require('fs'),
exec = require('child_process').exec,
Lexer = require('lex');
2016-05-24 16:56:13 +02:00
var diff = require('diff');
// Automatically track and cleanup files at exit
temp.track();
// Perform a comparison between a and b
2015-04-18 05:12:00 +02:00
// the callback should have parameters (err, result)
2016-05-24 16:56:13 +02:00
2016-11-04 00:16:54 +01:00
module.exports = jsdiffEngine
2016-05-24 16:56:13 +02:00
2016-11-04 00:16:54 +01:00
function jsdiffEngine (a, b, asMarkdown, callback) {
2016-05-24 16:56:13 +02:00
//a few strings have to be escaped: "[-", "-]", "{+", and "+}"
a = escapeString(a)
b = escapeString(b)
var diffRes = diff.diffWordsWithSpace(a,b, {ignoreWhitespace:true})
var diffStr = diffRes.map (part => {
if (part.added) return "{+"+part.value+"+}";
else if (part.removed) return "[-"+part.value+"-]";
else return part.value;
}).join("");
//if no difference was found by wdiff, err.code will be 0
var wdiffSame;
wdiffSame = false; //???
console.log(diffStr)
var resData = {wdiffNoMarkdown:unescapeString(diffStr), same: wdiffSame};
if (asMarkdown) {
//!!! this needs more sophisticated parsing
var markdown = unescapeString(rewriteWdiffMarkdown(diffStr))
resData.wdiff=markdown;
}
return callback(null, resData);
}
2016-11-04 00:16:54 +01:00
function wdiffEngine (a, b, asMarkdown, callback) {
2016-05-24 16:56:13 +02:00
//!!! this nested file-open is not a good pattern
// better would be to use promises and write the two files asynchronously
2016-05-11 06:02:42 +02:00
//a few strings have to be escaped: "[-", "-]", "{+", and "+}"
a = escapeString(a)
b = escapeString(b)
// open the first file
temp.open('wdiffa-', function(err, filea) {
//handle errors
2015-04-18 05:12:00 +02:00
if (err)
return callback(err);
//write the string to the file
fs.write(filea.fd, a);
//close the file
fs.close(filea.fd, function(err) {
if (err)
return callback(err);
//open the second file
temp.open('wdiffa-', function(err, fileb) {
2015-04-18 05:12:00 +02:00
if (err)
return callback(err);
2015-04-17 18:59:48 +02:00
//write the string to the file
fs.write(fileb.fd, b);
//close the file
fs.close(fileb.fd, function(err) {
if (err)
return callback(err);
2016-05-11 06:02:42 +02:00
var cmd = "wdiff " + filea.path + " " +fileb.path;
exec(cmd, function(err, stdout) {
2015-05-26 16:41:06 +02:00
2015-04-18 05:12:00 +02:00
if (err && err.code!=1 && err.code!=0) {
return callback(err);
}
//if no difference was found by wdiff, err.code will be 0
var wdiffSame;
wdiffSame = (err && err.code == 0) ? true:false;
2016-05-24 16:56:13 +02:00
console.log(stdout)
2016-05-11 06:02:42 +02:00
var resData = {wdiffNoMarkdown:unescapeString(stdout), same: wdiffSame};
if (asMarkdown) {
//!!! this needs more sophisticated parsing
2016-05-11 06:02:42 +02:00
var markdown = unescapeString(rewriteWdiffMarkdown(stdout))
2015-04-18 05:12:00 +02:00
resData.wdiff=markdown;
}
return callback(null, resData);
});
});
});
});
});
}
/* Rewrites the given wdiff output to correctly render as markdown,
assuming the source documents were also valid markdown. */
function rewriteWdiffMarkdown(source) {
//initialize a stack for the lexed input
2015-04-18 05:12:00 +02:00
//make it a lodash container, just for kicks
var tokens = _([]);
//define tokens
var LDEL = {type:"LDEL"}, RDEL = {type:"RDEL"}, LINS = {type:"LINS"}, RINS = {type:"RINS"};
//var STRING = {type: "STRING", value:""};
var RDEL_LINS = {type:"RDEL_LINS"};
var NEWLINE = {type:"\n"};
2015-04-18 05:12:00 +02:00
var isStringToken = function (token) { return token.type == "STRING";}
//create a lexer to process the wdiff string
var lexer = new Lexer(function (char) {
//the default rule creates a string on the stack for unmatched characters
2015-04-18 05:12:00 +02:00
//and just adds characters to it as they come in
if (tokens.size() == 0 || !isStringToken(tokens.last()))
tokens.push({type: "STRING", value:""});
tokens.last().value += char;
});
2015-04-18 05:12:00 +02:00
//rules for the newline character,
//as well as opening and closing (left and right) delete and insert tokens
lexer
.addRule(/\[-/, function () {
tokens.push(LDEL);
})
.addRule(/-\]/, function () {
tokens.push(RDEL);
})
.addRule(/{\+/, function () {
tokens.push(LINS);
})
.addRule(/\+}/, function () {
tokens.push(RINS);
})
//we have a special rule for joined delete and insert tokens
.addRule(/-\] {\+/, function() {
tokens.push(RDEL_LINS);
})
.addRule(/\n/, function () {
//tokens.push({type:"STRING", value:"\n"})
tokens.push(NEWLINE);
})
;
2015-04-18 05:12:00 +02:00
//do the lexing
lexer.setInput(source);
lexer.lex();
//# now we parse and transform the input
//create a stack for the transformed output
var transform = _([]);
//set the state variables for the parse
var SSTRING = "string", SINS = "ins", SDEL = "del", SDELINS = "delins";
var state = SSTRING;
2015-04-18 05:12:00 +02:00
//this is the index of the immediately previous delete string in the transform stack
var deleteStartIndex = -1
2015-04-18 05:12:00 +02:00
//iterate the input tokens to create the intermediate representation
tokens.forEach(function(token) {
//we add string tokens to the transformed stack
if (isStringToken(token)) {
//add the string with state information
var item = {
string: token.value,
state: state
};
//if this is the DELINS state, we will put the string in the transformed stack in a different order
// the INS string is spliced into place just after the first DEL string
// the point of this is so that the preceeding markdown formatting instructions
// on this line are applied equally to the del and ins strings
// an extra space is inserted between DEL and INS items, for readibility
if (state == SDELINS) {
state = SINS;
item.state = SINS;
var spaceItem = {string: ' ', state: SSTRING};
transform.splice(deleteStartIndex+1, 0, item);
transform.splice(deleteStartIndex+1, 0, spaceItem);
}
else {
transform.push(item);
}
}
//the various tokens control the transformation mode
if (token == LDEL) {
state = SDEL;
deleteStartIndex = transform.size();
}
if (token == LINS) {
state = SINS;
}
if (token == RDEL || token == RINS) {
state = SSTRING;
deleteStartIndex = -1;
}
if (token == RDEL_LINS) {
state = SDELINS;
}
if (token == NEWLINE) {
transform.push({string: '\n', state: state});
}
//ignore newlines (they get added to the output)
});
// * now emit the output string
var output = "";
var newline = true;
2015-04-18 05:12:00 +02:00
var newlineIndex = -1;
// prefixes are matched as follows:
// ^ - start of line
// ([ \t]*\>)* - blockquotes (possibly nested)
// (
2015-04-18 05:12:00 +02:00
// ([ \t]*#*) - headers
// |([ \t]+[\*\+-]) - unordered lists
// |([ \t]+[0-9]+\.) - numeric lists
// )?
2016-05-11 06:02:42 +02:00
// [ \t]* - trailing whitespace
2015-04-18 05:12:00 +02:00
//var PREFIX = /^([ \t]*\>)*(([ \t]*#*)|([ \t]*[\*\+-])|([ \t]*[\d]+\.))?[ \t]+/
var PREFIX = /^([ \t]*\>)*(([ \t]*#*)|([ \t]*[\*\+-])|([ \t]*[\d]+\.))?[ \t]*/
//var PREFIX = /^#*/
transform.forEach(function(item) {
//newlines are undecorated
if (item.string == '\n') {
output += '\n';
2015-04-18 05:12:00 +02:00
//flag the new line
newline = true;
2015-04-18 05:12:00 +02:00
//and record the offset in the output string
newlineIndex = output.length;
return
}
2015-04-18 05:12:00 +02:00
//wrap del strings with tags
if (item.state == SDEL) {
output += '<del>' + item.string + '</del>';
//del doesn't reset the newline state
}
2015-04-18 05:12:00 +02:00
//ins strings have to be handled a little differently:
//if this is an ins just after a newline, or after a del after a newline, we need to peel off any markdown formatting prefixes and insert them at the beginning of the line outside the del/ins tags
else if (item.state == SINS && newline) {
var prestring, poststring;
var match = item.string.match(PREFIX);
2015-04-18 05:12:00 +02:00
if (match == null)
prestring ="";
else
prestring = match[0];
poststring = item.string.substring(prestring.length);
2015-04-18 05:12:00 +02:00
output = output.substring(0, newlineIndex) + prestring + output.substring(newlineIndex);
output += '<ins>' + poststring + '</ins>';
newline = false;
newlineIndex = -1;
}
2015-04-18 05:12:00 +02:00
else if (item.state == SINS) {
output += '<ins>' + item.string + '</ins>';
}
//and just output other strings
2015-04-18 05:12:00 +02:00
else {
output += item.string;
//this resets the newline state
newline = false;
newlineIndex = -1;
}
});
2015-04-18 05:12:00 +02:00
return output;
2015-04-17 18:59:48 +02:00
}
2016-05-11 06:02:42 +02:00
function escapeString(str) {
str = str.replace(/\[\-/gm, "&#91;-")
str = str.replace(/\-\]/gm, "-&#93;")
return str
}
function unescapeString(str) {
str = str.replace(/\&\#91\;-/gm, "[-")
str = str.replace(/-\&\#93\;/gm, "-]")
return str
}