Skip to content

Commit 0e5c6cd

Browse files
committed
Merge pull request #81 from rtfpessoa/improve-file-summary
Improve file summary
2 parents 15a2794 + fa9bdeb commit 0e5c6cd

32 files changed

+894
-464
lines changed

src/file-list-printer.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,34 @@
99

1010
var printerUtils = require('./printer-utils.js').PrinterUtils;
1111

12+
var hoganUtils = require('./hoganjs-utils.js').HoganJsUtils;
13+
var baseTemplatesPath = 'file-summary';
14+
var iconsBaseTemplatesPath = 'icon';
15+
1216
function FileListPrinter() {
1317
}
1418

1519
FileListPrinter.prototype.generateFileList = function(diffFiles) {
16-
return '<div class="d2h-file-list-wrapper">\n' +
17-
' <div class="d2h-file-list-header">\n' +
18-
' <span class="d2h-file-list-title">Files changed (' + diffFiles.length + ')&nbsp&nbsp</span>\n' +
19-
' <a class="d2h-file-switch d2h-hide">hide</a>\n' +
20-
' <a class="d2h-file-switch d2h-show">show</a>\n' +
21-
' </div>\n' +
22-
' <table class="d2h-file-list">\n' +
23-
24-
diffFiles.map(function(file) {
25-
return ' <tr class="d2h-file-list-line">\n' +
26-
' <td class="d2h-lines-added">\n' +
27-
' <span>+' + file.addedLines + '</span>\n' +
28-
' </td>\n' +
29-
' <td class="d2h-lines-deleted">\n' +
30-
' <span>-' + file.deletedLines + '</span>\n' +
31-
' </td>\n' +
32-
' <td class="d2h-file-name-wrapper">\n' +
33-
' <a href="#' + printerUtils.getHtmlId(file) + '" class="d2h-file-name">' +
34-
'&nbsp;' + printerUtils.getDiffName(file) +
35-
' </a>\n' +
36-
' </td>\n' +
37-
' </tr>\n';
38-
}).join('\n') +
39-
'</table></div>\n';
20+
var lineTemplate = hoganUtils.template(baseTemplatesPath, 'line');
21+
22+
var files = diffFiles.map(function(file) {
23+
var fileTypeName = printerUtils.getFileTypeIcon(file);
24+
var iconTemplate = hoganUtils.template(iconsBaseTemplatesPath, fileTypeName);
25+
26+
return lineTemplate.render({
27+
fileHtmlId: printerUtils.getHtmlId(file),
28+
fileName: printerUtils.getDiffName(file),
29+
deletedLines: '-' + file.deletedLines,
30+
addedLines: '+' + file.addedLines
31+
}, {
32+
fileIcon: iconTemplate
33+
});
34+
}).join('\n');
35+
36+
return hoganUtils.render(baseTemplatesPath, 'wrapper', {
37+
filesNumber: diffFiles.length,
38+
files: files
39+
});
4040
};
4141

4242
module.exports.FileListPrinter = new FileListPrinter();

src/hoganjs-utils.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@
2020
}
2121

2222
HoganJsUtils.prototype.render = function(namespace, view, params, configuration) {
23-
var config = configuration || {};
24-
var templateKey = this._templateKey(namespace, view);
25-
26-
var template = this._getTemplate(templateKey, config);
23+
var template = this.template(namespace, view, configuration);
2724
if (template) {
2825
return template.render(params);
2926
}
3027

3128
return null;
3229
};
3330

31+
HoganJsUtils.prototype.template = function(namespace, view, configuration) {
32+
var config = configuration || {};
33+
var templateKey = this._templateKey(namespace, view);
34+
35+
return this._getTemplate(templateKey, config);
36+
};
37+
3438
HoganJsUtils.prototype._getTemplate = function(templateKey, config) {
3539
var template;
3640

src/line-by-line-printer.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,36 @@
1313
var Rematch = require('./rematch.js').Rematch;
1414

1515
var hoganUtils = require('./hoganjs-utils.js').HoganJsUtils;
16+
var genericTemplatesPath = 'generic';
1617
var baseTemplatesPath = 'line-by-line';
18+
var iconsBaseTemplatesPath = 'icon';
19+
var tagsBaseTemplatesPath = 'tag';
1720

1821
function LineByLinePrinter(config) {
1922
this.config = config;
2023
}
2124

2225
LineByLinePrinter.prototype.makeFileDiffHtml = function(file, diffs) {
23-
return hoganUtils.render(baseTemplatesPath, 'file-diff', {
26+
var fileDiffTemplate = hoganUtils.template(baseTemplatesPath, 'file-diff');
27+
var filePathTemplate = hoganUtils.template(genericTemplatesPath, 'file-path');
28+
var fileIconTemplate = hoganUtils.template(iconsBaseTemplatesPath, 'file');
29+
var fileTagTemplate = hoganUtils.template(tagsBaseTemplatesPath, printerUtils.getFileTypeIcon(file));
30+
31+
return fileDiffTemplate.render({
2432
file: file,
25-
fileDiffName: printerUtils.getDiffName(file),
2633
fileHtmlId: printerUtils.getHtmlId(file),
27-
diffs: diffs
34+
diffs: diffs,
35+
filePath: filePathTemplate.render({
36+
fileDiffName: printerUtils.getDiffName(file)
37+
}, {
38+
fileIcon: fileIconTemplate,
39+
fileTag: fileTagTemplate
40+
})
2841
});
2942
};
3043

3144
LineByLinePrinter.prototype.makeLineByLineHtmlWrapper = function(content) {
32-
return hoganUtils.render(baseTemplatesPath, 'wrapper', {'content': content});
45+
return hoganUtils.render(genericTemplatesPath, 'wrapper', {'content': content});
3346
};
3447

3548
LineByLinePrinter.prototype.generateLineByLineJsonHtml = function(diffFiles) {
@@ -55,9 +68,11 @@
5568
});
5669

5770
LineByLinePrinter.prototype.makeColumnLineNumberHtml = function(block) {
58-
return hoganUtils.render(baseTemplatesPath, 'column-line-number', {
71+
return hoganUtils.render(genericTemplatesPath, 'column-line-number', {
5972
diffParser: diffParser,
60-
block: utils.escape(block.header)
73+
blockHeader: block.header,
74+
lineClass: 'd2h-code-linenumber',
75+
contentClass: 'd2h-code-line'
6176
});
6277
};
6378

@@ -170,18 +185,25 @@
170185
};
171186

172187
LineByLinePrinter.prototype.makeLineHtml = function(type, oldNumber, newNumber, content, prefix) {
173-
return hoganUtils.render(baseTemplatesPath, 'line',
188+
var lineNumberTemplate = hoganUtils.render(baseTemplatesPath, 'numbers', {
189+
oldNumber: utils.valueOrEmpty(oldNumber),
190+
newNumber: utils.valueOrEmpty(newNumber)
191+
});
192+
193+
return hoganUtils.render(genericTemplatesPath, 'line',
174194
{
175195
type: type,
176-
oldNumber: utils.valueOrEmpty(oldNumber),
177-
newNumber: utils.valueOrEmpty(newNumber),
196+
lineClass: 'd2h-code-linenumber',
197+
contentClass: 'd2h-code-line',
178198
prefix: prefix && utils.convertWhiteSpaceToNonBreakingSpace(prefix),
179-
content: content && utils.convertWhiteSpaceToNonBreakingSpace(content)
199+
content: content && utils.convertWhiteSpaceToNonBreakingSpace(content),
200+
lineNumber: lineNumberTemplate
180201
});
181202
};
182203

183204
LineByLinePrinter.prototype._generateEmptyDiff = function() {
184-
return hoganUtils.render(baseTemplatesPath, 'empty-diff', {
205+
return hoganUtils.render(genericTemplatesPath, 'empty-diff', {
206+
contentClass: 'd2h-code-line',
185207
diffParser: diffParser
186208
});
187209
};

src/printer-utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@
9595
return 'unknown/file/path';
9696
};
9797

98+
PrinterUtils.prototype.getFileTypeIcon = function(file) {
99+
var templateName = 'file-changed';
100+
101+
if (file.isRename) {
102+
templateName = 'file-renamed';
103+
} else if (file.isCopy) {
104+
templateName = 'file-renamed';
105+
} else if (file.isNew) {
106+
templateName = 'file-added';
107+
} else if (file.isDeleted) {
108+
templateName = 'file-deleted';
109+
} else if (file.newName !== file.oldName) {
110+
// If file is not Added, not Deleted and the names changed it must be a rename :)
111+
templateName = 'file-renamed';
112+
}
113+
114+
return templateName;
115+
};
116+
98117
PrinterUtils.prototype.diffHighlight = function(diffLine1, diffLine2, config) {
99118
var linePrefix1, linePrefix2, unprefixedLine1, unprefixedLine2;
100119

src/side-by-side-printer.js

Lines changed: 54 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
var utils = require('./utils.js').Utils;
1313
var Rematch = require('./rematch.js').Rematch;
1414

15+
var hoganUtils = require('./hoganjs-utils.js').HoganJsUtils;
16+
var genericTemplatesPath = 'generic';
17+
var baseTemplatesPath = 'side-by-side';
18+
var iconsBaseTemplatesPath = 'icon';
19+
var tagsBaseTemplatesPath = 'tag';
20+
1521
var matcher = Rematch.rematch(function(a, b) {
1622
var amod = a.content.substr(1);
1723
var bmod = b.content.substr(1);
@@ -24,67 +30,48 @@
2430
}
2531

2632
SideBySidePrinter.prototype.makeDiffHtml = function(file, diffs) {
27-
return '<div id="' + printerUtils.getHtmlId(file) + '" class="d2h-file-wrapper" data-lang="' + file.language + '">\n' +
28-
' <div class="d2h-file-header">\n' +
29-
' <span class="d2h-file-stats">\n' +
30-
' <span class="d2h-lines-added">\n' +
31-
' <span>+' + file.addedLines + '</span>\n' +
32-
' </span>\n' +
33-
' <span class="d2h-lines-deleted">\n' +
34-
' <span>-' + file.deletedLines + '</span>\n' +
35-
' </span>\n' +
36-
' </span>\n' +
37-
' <span class="d2h-file-name-wrapper">\n' +
38-
' <span class="d2h-file-name">' + printerUtils.getDiffName(file) + '</span>\n' +
39-
' </span>\n' +
40-
' </div>\n' +
41-
' <div class="d2h-files-diff">\n' +
42-
' <div class="d2h-file-side-diff">\n' +
43-
' <div class="d2h-code-wrapper">\n' +
44-
' <table class="d2h-diff-table">\n' +
45-
' <tbody class="d2h-diff-tbody">\n' +
46-
' ' + diffs.left +
47-
' </tbody>\n' +
48-
' </table>\n' +
49-
' </div>\n' +
50-
' </div>\n' +
51-
' <div class="d2h-file-side-diff">\n' +
52-
' <div class="d2h-code-wrapper">\n' +
53-
' <table class="d2h-diff-table">\n' +
54-
' <tbody class="d2h-diff-tbody">\n' +
55-
' ' + diffs.right +
56-
' </tbody>\n' +
57-
' </table>\n' +
58-
' </div>\n' +
59-
' </div>\n' +
60-
' </div>\n' +
61-
' </div>\n';
33+
var fileDiffTemplate = hoganUtils.template(baseTemplatesPath, 'file-diff');
34+
var filePathTemplate = hoganUtils.template(genericTemplatesPath, 'file-path');
35+
var fileIconTemplate = hoganUtils.template(iconsBaseTemplatesPath, 'file');
36+
var fileTagTemplate = hoganUtils.template(tagsBaseTemplatesPath, printerUtils.getFileTypeIcon(file));
37+
38+
return fileDiffTemplate.render({
39+
file: file,
40+
fileHtmlId: printerUtils.getHtmlId(file),
41+
diffs: diffs,
42+
filePath: filePathTemplate.render({
43+
fileDiffName: printerUtils.getDiffName(file)
44+
}, {
45+
fileIcon: fileIconTemplate,
46+
fileTag: fileTagTemplate
47+
})
48+
});
6249
};
6350

6451
SideBySidePrinter.prototype.generateSideBySideJsonHtml = function(diffFiles) {
6552
var that = this;
66-
return '<div class="d2h-wrapper">\n' +
67-
diffFiles.map(function(file) {
6853

69-
var diffs;
70-
if (file.blocks.length) {
71-
diffs = that.generateSideBySideFileHtml(file);
72-
} else {
73-
diffs = that.generateEmptyDiff();
74-
}
54+
var content = diffFiles.map(function(file) {
55+
var diffs;
56+
if (file.blocks.length) {
57+
diffs = that.generateSideBySideFileHtml(file);
58+
} else {
59+
diffs = that.generateEmptyDiff();
60+
}
7561

76-
return that.makeDiffHtml(file, diffs);
77-
}).join('\n') +
78-
'</div>\n';
62+
return that.makeDiffHtml(file, diffs);
63+
}).join('\n');
64+
65+
return hoganUtils.render(genericTemplatesPath, 'wrapper', {'content': content});
7966
};
8067

8168
SideBySidePrinter.prototype.makeSideHtml = function(blockHeader) {
82-
return '<tr>\n' +
83-
' <td class="d2h-code-side-linenumber ' + diffParser.LINE_TYPE.INFO + '"></td>\n' +
84-
' <td class="' + diffParser.LINE_TYPE.INFO + '">\n' +
85-
' <div class="d2h-code-side-line ' + diffParser.LINE_TYPE.INFO + '">' + blockHeader + '</div>\n' +
86-
' </td>\n' +
87-
'</tr>\n';
69+
return hoganUtils.render(genericTemplatesPath, 'column-line-number', {
70+
diffParser: diffParser,
71+
blockHeader: blockHeader,
72+
lineClass: 'd2h-code-side-linenumber',
73+
contentClass: 'd2h-code-side-line'
74+
});
8875
};
8976

9077
SideBySidePrinter.prototype.generateSideBySideFileHtml = function(file) {
@@ -95,7 +82,7 @@
9582

9683
file.blocks.forEach(function(block) {
9784

98-
fileHtml.left += that.makeSideHtml(utils.escape(block.header));
85+
fileHtml.left += that.makeSideHtml(block.header);
9986
fileHtml.right += that.makeSideHtml('');
10087

10188
var oldLines = [];
@@ -109,7 +96,7 @@
10996
var comparisons = oldLines.length * newLines.length;
11097
var maxComparisons = that.config.matchingMaxComparisons || 2500;
11198
var doMatching = comparisons < maxComparisons && (that.config.matching === 'lines' ||
112-
that.config.matching === 'words');
99+
that.config.matching === 'words');
113100

114101
if (doMatching) {
115102
matches = matcher(oldLines, newLines);
@@ -232,40 +219,26 @@
232219
return fileHtml;
233220
};
234221

235-
SideBySidePrinter.prototype.makeSingleLineHtml = function(type, number, htmlContent, htmlPrefix) {
236-
return '<tr>\n' +
237-
' <td class="d2h-code-side-linenumber ' + type + '">' + number + '</td>\n' +
238-
' <td class="' + type + '">' +
239-
' <div class="d2h-code-side-line ' + type + '">' + htmlPrefix + htmlContent + '</div>' +
240-
' </td>\n' +
241-
' </tr>\n';
242-
};
243-
244222
SideBySidePrinter.prototype.generateSingleLineHtml = function(type, number, content, prefix) {
245-
var htmlPrefix = '';
246-
if (prefix) {
247-
htmlPrefix = '<span class="d2h-code-line-prefix">' + prefix + '</span>';
248-
}
249-
250-
var htmlContent = '';
251-
if (content) {
252-
htmlContent = '<span class="d2h-code-line-ctn">' + content + '</span>';
253-
}
254-
255-
return this.makeSingleLineHtml(type, number, htmlContent, htmlPrefix);
223+
return hoganUtils.render(genericTemplatesPath, 'line',
224+
{
225+
type: type,
226+
lineClass: 'd2h-code-side-linenumber',
227+
contentClass: 'd2h-code-side-line',
228+
prefix: prefix && utils.convertWhiteSpaceToNonBreakingSpace(prefix),
229+
content: content && utils.convertWhiteSpaceToNonBreakingSpace(content),
230+
lineNumber: number
231+
});
256232
};
257233

258234
SideBySidePrinter.prototype.generateEmptyDiff = function() {
259235
var fileHtml = {};
260236
fileHtml.right = '';
261237

262-
fileHtml.left = '<tr>\n' +
263-
' <td class="' + diffParser.LINE_TYPE.INFO + '">' +
264-
' <div class="d2h-code-side-line ' + diffParser.LINE_TYPE.INFO + '">' +
265-
'File without changes' +
266-
' </div>' +
267-
' </td>\n' +
268-
'</tr>\n';
238+
fileHtml.left = hoganUtils.render(genericTemplatesPath, 'empty-diff', {
239+
contentClass: 'd2h-code-side-line',
240+
diffParser: diffParser
241+
});
269242

270243
return fileHtml;
271244
};

0 commit comments

Comments
 (0)