|
8 | 8 | (function() { |
9 | 9 | var diffParser = require('./diff-parser.js').DiffParser; |
10 | 10 | var htmlPrinter = require('./html-printer.js').HtmlPrinter; |
| 11 | + var utils = require('./utils.js').Utils; |
11 | 12 |
|
12 | 13 | function Diff2Html() { |
13 | 14 | } |
14 | 15 |
|
15 | | - /* |
16 | | - * Line diff type configuration |
17 | | - var config = { |
18 | | - 'wordByWord': true, // (default) |
19 | | - // OR |
20 | | - 'charByChar': true |
21 | | - }; |
22 | | - */ |
| 16 | + var defaultConfig = { |
| 17 | + wordByWord: true, |
| 18 | + outputFormat: 'line-by-line', |
| 19 | + matching: 'none', |
| 20 | + matchWordsThreshold: 0.25, |
| 21 | + matchingMaxComparisons: 2500, |
| 22 | + maxLineLengthHighlight: 10000 |
| 23 | + }; |
23 | 24 |
|
24 | 25 | /* |
25 | 26 | * Generates json object from string diff input |
26 | 27 | */ |
27 | 28 | Diff2Html.prototype.getJsonFromDiff = function(diffInput, config) { |
28 | | - var configOrEmpty = config || {}; |
29 | | - return diffParser.generateDiffJson(diffInput, configOrEmpty); |
| 29 | + var cfg = utils.safeConfig(config, defaultConfig); |
| 30 | + return diffParser.generateDiffJson(diffInput, cfg); |
30 | 31 | }; |
31 | 32 |
|
32 | 33 | /* |
33 | 34 | * Generates the html diff. The config parameter configures the output/input formats and other options |
34 | 35 | */ |
35 | 36 | Diff2Html.prototype.getPrettyHtml = function(diffInput, config) { |
36 | | - var configOrEmpty = config || {}; |
| 37 | + var cfg = utils.safeConfig(config, defaultConfig); |
37 | 38 |
|
38 | 39 | var diffJson = diffInput; |
39 | | - if (!configOrEmpty.inputFormat || configOrEmpty.inputFormat === 'diff') { |
40 | | - diffJson = diffParser.generateDiffJson(diffInput, configOrEmpty); |
| 40 | + if (!cfg.inputFormat || cfg.inputFormat === 'diff') { |
| 41 | + diffJson = diffParser.generateDiffJson(diffInput, cfg); |
41 | 42 | } |
42 | 43 |
|
43 | 44 | var fileList = ''; |
44 | | - if (configOrEmpty.showFiles === true) { |
45 | | - fileList = htmlPrinter.generateFileListSummary(diffJson, configOrEmpty); |
| 45 | + if (cfg.showFiles === true) { |
| 46 | + fileList = htmlPrinter.generateFileListSummary(diffJson, cfg); |
46 | 47 | } |
47 | 48 |
|
48 | 49 | var diffOutput = ''; |
49 | | - if (configOrEmpty.outputFormat === 'side-by-side') { |
50 | | - diffOutput = htmlPrinter.generateSideBySideJsonHtml(diffJson, configOrEmpty); |
| 50 | + if (cfg.outputFormat === 'side-by-side') { |
| 51 | + diffOutput = htmlPrinter.generateSideBySideJsonHtml(diffJson, cfg); |
51 | 52 | } else { |
52 | | - diffOutput = htmlPrinter.generateLineByLineJsonHtml(diffJson, configOrEmpty); |
| 53 | + diffOutput = htmlPrinter.generateLineByLineJsonHtml(diffJson, cfg); |
53 | 54 | } |
54 | 55 |
|
55 | 56 | return fileList + diffOutput; |
|
63 | 64 | * Generates pretty html from string diff input |
64 | 65 | */ |
65 | 66 | Diff2Html.prototype.getPrettyHtmlFromDiff = function(diffInput, config) { |
66 | | - var configOrEmpty = config || {}; |
67 | | - configOrEmpty.inputFormat = 'diff'; |
68 | | - configOrEmpty.outputFormat = 'line-by-line'; |
69 | | - return this.getPrettyHtml(diffInput, configOrEmpty); |
| 67 | + var cfg = utils.safeConfig(config, defaultConfig); |
| 68 | + cfg.inputFormat = 'diff'; |
| 69 | + cfg.outputFormat = 'line-by-line'; |
| 70 | + return this.getPrettyHtml(diffInput, cfg); |
70 | 71 | }; |
71 | 72 |
|
72 | 73 | /* |
73 | 74 | * Generates pretty html from a json object |
74 | 75 | */ |
75 | 76 | Diff2Html.prototype.getPrettyHtmlFromJson = function(diffJson, config) { |
76 | | - var configOrEmpty = config || {}; |
77 | | - configOrEmpty.inputFormat = 'json'; |
78 | | - configOrEmpty.outputFormat = 'line-by-line'; |
79 | | - return this.getPrettyHtml(diffJson, configOrEmpty); |
| 77 | + var cfg = utils.safeConfig(config, defaultConfig); |
| 78 | + cfg.inputFormat = 'json'; |
| 79 | + cfg.outputFormat = 'line-by-line'; |
| 80 | + return this.getPrettyHtml(diffJson, cfg); |
80 | 81 | }; |
81 | 82 |
|
82 | 83 | /* |
83 | 84 | * Generates pretty side by side html from string diff input |
84 | 85 | */ |
85 | 86 | Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function(diffInput, config) { |
86 | | - var configOrEmpty = config || {}; |
87 | | - configOrEmpty.inputFormat = 'diff'; |
88 | | - configOrEmpty.outputFormat = 'side-by-side'; |
89 | | - return this.getPrettyHtml(diffInput, configOrEmpty); |
| 87 | + var cfg = utils.safeConfig(config, defaultConfig); |
| 88 | + cfg.inputFormat = 'diff'; |
| 89 | + cfg.outputFormat = 'side-by-side'; |
| 90 | + return this.getPrettyHtml(diffInput, cfg); |
90 | 91 | }; |
91 | 92 |
|
92 | 93 | /* |
93 | 94 | * Generates pretty side by side html from a json object |
94 | 95 | */ |
95 | 96 | Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function(diffJson, config) { |
96 | | - var configOrEmpty = config || {}; |
97 | | - configOrEmpty.inputFormat = 'json'; |
98 | | - configOrEmpty.outputFormat = 'side-by-side'; |
99 | | - return this.getPrettyHtml(diffJson, configOrEmpty); |
| 97 | + var cfg = utils.safeConfig(config, defaultConfig); |
| 98 | + cfg.inputFormat = 'json'; |
| 99 | + cfg.outputFormat = 'side-by-side'; |
| 100 | + return this.getPrettyHtml(diffJson, cfg); |
100 | 101 | }; |
101 | 102 |
|
102 | 103 | var diffObject = new Diff2Html(); |
|
0 commit comments