forked from rtfpessoa/diff2html
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiff2html.js
171 lines (153 loc) · 5.57 KB
/
diff2html.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
*
* Diff to HTML (diff2html.js)
* Author: rtfpessoa
*
*/
global.diffStatementNewLines = 0;
global.coveredDiffStatementNewLines = 0;
global.uncoveredDiffStatementNewLines = 0;
global.nonExecutableDiffStatementNewLines = 0;
global.fileCoverageData = {};
global.files = [];
global.coverage = null;
global.coveredLines = 0;
global.uncoveredLines = 0;
global.overallStatementCoverageRate = 0;
global.fileDisplayMap = {};
global.displayMode = 'standard';
(function() {
var diffParser = require('./diff-parser.js').DiffParser;
var htmlPrinter = require('./html-printer.js').HtmlPrinter;
var utils = require('./utils.js').Utils;
function Diff2Html() {
}
var defaultConfig = {
inputFormat: 'diff',
outputFormat: 'line-by-line',
showFiles: false,
matching: 'none',
matchWordsThreshold: 0.25,
matchingMaxComparisons: 2500,
maxLineSizeInBlockForComparison: 200,
maxLineLengthHighlight: 10000,
templates: {},
rawTemplates: {},
renderNothingWhenEmpty: false
};
/*
* Generates json object from string diff input
*/
Diff2Html.prototype.getJsonFromDiff = function(diffInput, config) {
var cfg = utils.safeConfig(config, defaultConfig);
return diffParser.generateDiffJson(diffInput, cfg);
};
/*
* Generates the html diff. The config parameter configures the output/input formats and other options
*/
Diff2Html.prototype.getPrettyHtml = function(diffInput, config, coverage, coverageFilePatterns, workspace, customFileNameGeneratorFile, displayMode, strict, caseInsensitive) {
var cfg = utils.safeConfig(config, defaultConfig);
var diffJson = diffInput;
if (!cfg.inputFormat || cfg.inputFormat === 'diff') {
diffJson = diffParser.generateDiffJson(diffInput, cfg);
}
var diffOutput = '';
if (cfg.outputFormat === 'side-by-side') {
diffOutput = htmlPrinter.generateSideBySideJsonHtml(diffJson, cfg);
} else {
global.coverage = coverage;
global.displayMode = displayMode;
diffOutput = htmlPrinter.generateLineByLineJsonHtml(diffJson, cfg, coverage, coverageFilePatterns, workspace, customFileNameGeneratorFile, displayMode, strict, caseInsensitive);
}
var fileList = '';
if (cfg.showFiles === true) {
fileList = htmlPrinter.generateFileListSummary(diffJson, cfg);
}
return this.getMetadata(fileList, diffOutput);
};
Diff2Html.prototype.getMetadata = function(fileList, diffOutput) {
var effectiveChangedLines = global.coveredDiffStatementNewLines + global.uncoveredDiffStatementNewLines;
var diffStatementCoverageRate = effectiveChangedLines === 0 ? 100 : parseFloat((global.coveredDiffStatementNewLines / effectiveChangedLines * 100).toFixed(2));
for (var fileName in global.fileCoverageData) {
var covered = global.fileCoverageData[fileName].covered;
var uncovered = global.fileCoverageData[fileName].uncovered;
var total = covered + uncovered;
if (total === 0) {
global.fileCoverageData[fileName].coverageRate = null;
} else {
global.fileCoverageData[fileName].coverageRate = parseFloat((covered / total * 100).toFixed(2));
}
}
const fileCoverageData = [];
for (const fileName in global.fileCoverageData) {
const data = global.fileCoverageData[fileName];
fileCoverageData.push({
fileName,
covered: data.covered,
uncovered: data.uncovered,
coverageRate: data.coverageRate
});
}
fileCoverageData.sort((a, b) => {
return a.coverageRate - b.coverageRate;
});
var metadata = {
diffStatementNewLines: global.diffStatementNewLines,
coveredDiffStatementNewLines: global.coveredDiffStatementNewLines,
uncoveredDiffStatementNewLines: global.uncoveredDiffStatementNewLines,
nonExecutableDiffStatementNewLines: global.nonExecutableDiffStatementNewLines,
diffStatementCoverageRate,
overallStatementCoverageRate: global.overallStatementCoverageRate,
fileCoverageData,
files: global.files
};
return {
fileList,
htmlContents: diffOutput,
metadata
};
};
/*
* Deprecated methods - The following methods exist only to maintain compatibility with previous versions
*/
/*
* Generates pretty html from string diff input
*/
Diff2Html.prototype.getPrettyHtmlFromDiff = function(diffInput, config) {
var cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = 'diff';
cfg.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffInput, cfg);
};
/*
* Generates pretty html from a json object
*/
Diff2Html.prototype.getPrettyHtmlFromJson = function(diffJson, config) {
var cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = 'json';
cfg.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffJson, cfg);
};
/*
* Generates pretty side by side html from string diff input
*/
Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function(diffInput, config) {
var cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = 'diff';
cfg.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffInput, cfg);
};
/*
* Generates pretty side by side html from a json object
*/
Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function(diffJson, config) {
var cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = 'json';
cfg.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffJson, cfg);
};
var diffObject = new Diff2Html();
module.exports.Diff2Html = diffObject;
// Expose diff2html in the browser
global.Diff2Html = diffObject;
})();