|
72 | 72 | /*
|
73 | 73 | * Generates json object from string diff input
|
74 | 74 | */
|
75 |
| - Diff2Html.prototype.getJsonFromDiff = function(diffInput) { |
76 |
| - return diffParser.generateDiffJson(diffInput); |
| 75 | + Diff2Html.prototype.getJsonFromDiff = function(diffInput, config) { |
| 76 | + var configOrEmpty = config || {}; |
| 77 | + return diffParser.generateDiffJson(diffInput, configOrEmpty); |
77 | 78 | };
|
78 | 79 |
|
79 | 80 | /*
|
|
84 | 85 |
|
85 | 86 | var diffJson = diffInput;
|
86 | 87 | if (!configOrEmpty.inputFormat || configOrEmpty.inputFormat === 'diff') {
|
87 |
| - diffJson = diffParser.generateDiffJson(diffInput); |
| 88 | + diffJson = diffParser.generateDiffJson(diffInput, configOrEmpty); |
88 | 89 | }
|
89 | 90 |
|
90 | 91 | var fileList = '';
|
|
186 | 187 |
|
187 | 188 | DiffParser.prototype.LINE_TYPE = LINE_TYPE;
|
188 | 189 |
|
189 |
| - DiffParser.prototype.generateDiffJson = function(diffInput) { |
| 190 | + DiffParser.prototype.generateDiffJson = function(diffInput, config) { |
190 | 191 | var files = [];
|
191 | 192 | var currentFile = null;
|
192 | 193 | var currentBlock = null;
|
|
296 | 297 | var deletedFileMode = /^deleted file mode (\d{6})/;
|
297 | 298 | var newFileMode = /^new file mode (\d{6})/;
|
298 | 299 |
|
299 |
| - var copyFrom = /^copy from (.+)/; |
300 |
| - var copyTo = /^copy to (.+)/; |
| 300 | + var copyFrom = /^copy from "?(.+?)"?/; |
| 301 | + var copyTo = /^copy to "?(.+?)"?/; |
301 | 302 |
|
302 |
| - var renameFrom = /^rename from (.+)/; |
303 |
| - var renameTo = /^rename to (.+)/; |
| 303 | + var renameFrom = /^rename from "?(.+?)"?/; |
| 304 | + var renameTo = /^rename to "?(.+?)"?/; |
304 | 305 |
|
305 | 306 | var similarityIndex = /^similarity index (\d+)%/;
|
306 | 307 | var dissimilarityIndex = /^dissimilarity index (\d+)%/;
|
|
323 | 324 | var values = [];
|
324 | 325 | if (utils.startsWith(line, 'diff')) {
|
325 | 326 | startFile();
|
326 |
| - } else if (currentFile && !currentFile.oldName && (values = /^--- [aiwco]\/(.+)$/.exec(line))) { |
| 327 | + } else if (currentFile && !currentFile.oldName && (values = getSrcFilename(line, config))) { |
327 | 328 | currentFile.oldName = values[1];
|
328 | 329 | currentFile.language = getExtension(currentFile.oldName, currentFile.language);
|
329 |
| - } else if (currentFile && !currentFile.newName && (values = /^\+\+\+ [biwco]?\/(.+)$/.exec(line))) { |
| 330 | + } else if (currentFile && !currentFile.newName && (values = getDstFilename(line, config))) { |
330 | 331 | currentFile.newName = values[1];
|
331 | 332 | currentFile.language = getExtension(currentFile.newName, currentFile.language);
|
332 | 333 | } else if (currentFile && utils.startsWith(line, '@@')) {
|
|
389 | 390 | return language;
|
390 | 391 | }
|
391 | 392 |
|
| 393 | + function getSrcFilename(line, cfg) { |
| 394 | + var prefixes = ["a\\/", "i\\/", "w\\/", "c\\/", "o\\/"]; |
| 395 | + |
| 396 | + if (cfg.srcPrefix) prefixes.push(cfg.srcPrefix); |
| 397 | + |
| 398 | + return _getFilename('---', line, prefixes); |
| 399 | + } |
| 400 | + |
| 401 | + function getDstFilename(line, cfg) { |
| 402 | + var prefixes = ["b\\/", "i\\/", "w\\/", "c\\/", "o\\/"]; |
| 403 | + |
| 404 | + if (cfg.dstPrefix) prefixes.push(cfg.dstPrefix); |
| 405 | + |
| 406 | + return _getFilename('\\+\\+\\+', line, prefixes); |
| 407 | + } |
| 408 | + |
| 409 | + function _getFilename(linePrefix, line, prefixes) { |
| 410 | + var prefixesStr = prefixes.join("|"); |
| 411 | + return new RegExp('^' + linePrefix + ' "?(?:' + prefixesStr + ')(.+?)"?$').exec(line); |
| 412 | + } |
| 413 | + |
392 | 414 | module.exports.DiffParser = new DiffParser();
|
393 | 415 |
|
394 | 416 | })();
|
|
0 commit comments