aboutsummaryrefslogtreecommitdiff
path: root/lib/diff.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/diff.js')
-rw-r--r--lib/diff.js75
1 files changed, 30 insertions, 45 deletions
diff --git a/lib/diff.js b/lib/diff.js
index 8d109751d..dfd0fc81a 100644
--- a/lib/diff.js
+++ b/lib/diff.js
@@ -7,37 +7,35 @@ function cleanAndGetIndexes(text) {
openTag: "{+",
replaceOpenTag: "",
closeTag: "+}",
- replaceCloseTag: "",
- }
+ replaceCloseTag: ""
+ };
var delRules = {
name: "del",
openTag: "[-",
replaceOpenTag: "",
closeTag: "-]",
- replaceCloseTag: "",
- }
+ replaceCloseTag: ""
+ };
var rules = [addRules, delRules];
- TagTypeEnum = {
- OPENING : 1,
- CLOSING : 2
- }
+ var TagTypeEnum = {
+ OPENING: 1,
+ CLOSING: 2
+ };
function tagLookup(rules, text, pos) {
var seen = false;
var type = null;
var rule = null;
- for (var i = 0; i<rules.length; i++) {
- var candidateTag = text.slice(pos, pos+rules[i].openTag.length);
- // console.log("Comparing "+candidateTag+" with "+rules[i].openTag);
+ for (var i = 0; i < rules.length; i++) {
+ var candidateTag = text.slice(pos, pos + rules[i].openTag.length);
if (rules[i].openTag == candidateTag) {
seen = true;
type = TagTypeEnum.OPENING;
rule = i;
break;
}
- var candidateTag = text.slice(pos, pos+rules[i].closeTag.length);
- // console.log("Comparing "+candidateTag+" with "+rules[i].closeTag);
+ candidateTag = text.slice(pos, pos + rules[i].closeTag.length);
if (rules[i].closeTag == candidateTag) {
seen = true;
type = TagTypeEnum.CLOSING;
@@ -46,21 +44,23 @@ function cleanAndGetIndexes(text) {
}
}
- return {seen: seen,
+ return {
+ seen: seen,
rule: rule,
- type: type}
+ type: type
+ };
}
var finalText = "";
var posInFinalText = 0; // character that is going to be treated
// The position in the original text:
var posInText = 0; // character that is going to be treated
- StateEnum = {
- OUTSIDE_TAG : 1,
- INSIDE_TAG : 2
- }
+ var StateEnum = {
+ OUTSIDE_TAG: 1,
+ INSIDE_TAG: 2
+ };
var state = StateEnum.OUTSIDE_TAG;
- var zones = [[],[]];
+ var zones = [[], []];
var currentTextBeginPos = 0;
var currentTextEndPos = null;
var currentTagBeginPos = null;
@@ -94,7 +94,7 @@ function cleanAndGetIndexes(text) {
function memorizeText() {
currentTextEndPos = posInText - 1;
if (currentTextEndPos >= currentTextBeginPos) {
- finalText = finalText.concat(text.slice(currentTextBeginPos,currentTextEndPos+1));
+ finalText = finalText.concat(text.slice(currentTextBeginPos, currentTextEndPos + 1));
}
}
@@ -108,7 +108,7 @@ function cleanAndGetIndexes(text) {
while (posInText < text.length) {
var tag = tagLookup(rules, text, posInText);
- if(tag.seen && tag.type == TagTypeEnum.OPENING) {
+ if (tag.seen && tag.type == TagTypeEnum.OPENING) {
if (state != StateEnum.OUTSIDE_TAG) {
log_error("Opening tag while not outside tag (tags cannot be nested)");
return null;
@@ -116,7 +116,7 @@ function cleanAndGetIndexes(text) {
currentTag = tag.rule;
seenOpeningTag();
state = StateEnum.INSIDE_TAG;
- } else if(tag.seen && tag.type == TagTypeEnum.CLOSING) {
+ } else if (tag.seen && tag.type == TagTypeEnum.CLOSING) {
if (state != StateEnum.INSIDE_TAG) {
log_error("Closing tag while not inside tag.");
return null;
@@ -133,17 +133,11 @@ function cleanAndGetIndexes(text) {
}
end();
- // console.log(JSON.stringify(finalText)+JSON.stringify(zones));
return {text: finalText, zones: zones};
}
function buildDiffHandler(config) {
- return function diffHandler(req, res, next) {
- // console.log("req: "+JSON.stringify(JSON.decycle(req)));
- // console.log("");
- // console.log("res: "+JSON.stringify(JSON.decycle(res)));
- // console.log("");
- // console.log("next: "+JSON.stringify(JSON.decycle(next)));
+ return function diffHandler(req, res) {
var before = req.body.before;
var after = req.body.after;
if (before === undefined) {
@@ -154,11 +148,6 @@ function buildDiffHandler(config) {
console.log("Warning : Bad request : wrong \"after\"");
//return next(new Error("Bad request : wrong \"after\""));
}
- //console.log("Before: ");
- //console.log(before);
- //console.log("After: ");
- //console.log(after);
-
// TODO : make async the two creation of temp files + call to wdiff ?
var wdiffExe = config.wdiffExe;
@@ -172,13 +161,13 @@ function buildDiffHandler(config) {
// TODO : get rid of this buffer or calculate it...
var maxSize = 100000;
var wdiffResult = child_process.spawnSync(
- wdiffExe,
- [tempBeforePath, tempAfterPath],
- {maxBuffer: 100000});
+ wdiffExe,
+ [tempBeforePath, tempAfterPath],
+ {maxBuffer: 100000});
res.set('Content-Type', 'application/json');
var cleaned = cleanAndGetIndexes(wdiffResult.stdout.toString());
- if (cleaned == null) {
+ if (cleaned === null) {
res.end(JSON.stringify({
computedDiff: "Failed to clean the diff",
zones: null
@@ -186,16 +175,12 @@ function buildDiffHandler(config) {
} else {
res.end(JSON.stringify({
computedDiff: cleaned.text,
- // for debug only:
- //computedDiff: cleaned.text+
- // "\n//// for reference: ////\n"+
- // wdiffResult.stdout.toString(),
zones: cleaned.zones
}));
}
- }
+ };
}
module.exports = {
- buildDiffHandler: buildDiffHandler,
+ buildDiffHandler: buildDiffHandler
};