Skip to content

Commit 2015436

Browse files
committed
major refactor with classes
1 parent 39730ad commit 2015436

File tree

9 files changed

+497
-466
lines changed

9 files changed

+497
-466
lines changed

diff2html.js

Lines changed: 9 additions & 461 deletions
Large diffs are not rendered by default.

diff2html.min.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

diff2html.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css">
1515
<link rel="stylesheet" type="text/css" href="diff2html.css">
1616

17-
1817
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
1918

20-
<!--<script type="text/javascript" src="jsdiff.js"></script>-->
21-
<script type="text/javascript" src="diff.js"></script>
19+
<!--<script type="text/javascript" src="lib/jsdiff.js"></script>-->
20+
<script type="text/javascript" src="lib/diff.js"></script>
21+
22+
<script type="text/javascript" src="src/utils.js"></script>
23+
<script type="text/javascript" src="src/diff-parser.js"></script>
24+
<script type="text/javascript" src="src/html-printer.js"></script>
2225
<script type="text/javascript" src="diff2html.js"></script>
2326
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
2427
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/languages/scala.min.js"></script>
File renamed without changes.
File renamed without changes.

src/diff-parser.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
*
3+
* Diff Parser (diff-parser.js)
4+
* Author: rtfpessoa
5+
*
6+
*/
7+
8+
var utils = new Utils();
9+
10+
var LINE_TYPE = {
11+
INSERTS: "d2h-ins",
12+
DELETES: "d2h-del",
13+
CONTEXT: "d2h-cntx",
14+
INFO: "d2h-info"
15+
};
16+
17+
function DiffParser() {
18+
}
19+
20+
DiffParser.prototype.LINE_TYPE = LINE_TYPE;
21+
22+
DiffParser.prototype.generateDiffJson = function (diffInput) {
23+
var files = [],
24+
currentFile = null,
25+
currentBlock = null,
26+
oldLine = null,
27+
newLine = null;
28+
29+
var saveBlock = function () {
30+
/* add previous block(if exists) before start a new file */
31+
if (currentBlock) {
32+
currentFile.blocks.push(currentBlock);
33+
currentBlock = null;
34+
}
35+
};
36+
37+
var saveFile = function () {
38+
/*
39+
* add previous file(if exists) before start a new one
40+
* if it has name (to avoid binary files errors)
41+
*/
42+
if (currentFile && currentFile.newName) {
43+
files.push(currentFile);
44+
currentFile = null;
45+
}
46+
};
47+
48+
var startFile = function () {
49+
saveBlock();
50+
saveFile();
51+
52+
/* create file structure */
53+
currentFile = {};
54+
currentFile.blocks = [];
55+
currentFile.deletedLines = 0;
56+
currentFile.addedLines = 0;
57+
};
58+
59+
var startBlock = function (line) {
60+
saveBlock();
61+
62+
var values;
63+
64+
if (values = /^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line)) {
65+
currentFile.isTripleDiff = false;
66+
} else if (values = /^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line)) {
67+
currentFile.isTripleDiff = true;
68+
} else {
69+
values = [0, 0];
70+
currentFile.isTripleDiff = false;
71+
}
72+
73+
oldLine = values[1];
74+
newLine = values[2];
75+
76+
/* create block metadata */
77+
currentBlock = {};
78+
currentBlock.lines = [];
79+
currentBlock.oldStartLine = oldLine;
80+
currentBlock.newStartLine = newLine;
81+
currentBlock.header = line;
82+
};
83+
84+
var createLine = function (line) {
85+
var currentLine = {};
86+
currentLine.content = line;
87+
88+
/* fill the line data */
89+
if (utils.startsWith(line, "+") || utils.startsWith(line, " +")) {
90+
currentFile.addedLines++;
91+
92+
currentLine.type = LINE_TYPE.INSERTS;
93+
currentLine.oldNumber = null;
94+
currentLine.newNumber = newLine++;
95+
96+
currentBlock.lines.push(currentLine);
97+
98+
} else if (utils.startsWith(line, "-") || utils.startsWith(line, " -")) {
99+
currentFile.deletedLines++;
100+
101+
currentLine.type = LINE_TYPE.DELETES;
102+
currentLine.oldNumber = oldLine++;
103+
currentLine.newNumber = null;
104+
105+
currentBlock.lines.push(currentLine);
106+
107+
} else {
108+
currentLine.type = LINE_TYPE.CONTEXT;
109+
currentLine.oldNumber = oldLine++;
110+
currentLine.newNumber = newLine++;
111+
112+
currentBlock.lines.push(currentLine);
113+
}
114+
};
115+
116+
var diffLines = diffInput.split("\n");
117+
diffLines.forEach(function (line) {
118+
// Unmerged paths, and possibly other non-diffable files
119+
// https://github.com/scottgonzalez/pretty-diff/issues/11
120+
// Also, remove some useless lines
121+
if (!line || utils.startsWith(line, "*") ||
122+
utils.startsWith(line, "new") || utils.startsWith(line, "index")) {
123+
return;
124+
}
125+
126+
var values = [];
127+
if (utils.startsWith(line, "diff")) {
128+
startFile();
129+
} else if (currentFile && !currentFile.oldName && (values = /^--- a\/(\S+).*$/.exec(line))) {
130+
currentFile.oldName = values[1];
131+
} else if (currentFile && !currentFile.newName && (values = /^\+\+\+ [b]?\/(\S+).*$/.exec(line))) {
132+
currentFile.newName = values[1];
133+
134+
var fileSplit = currentFile.newName.split(".");
135+
currentFile.language = fileSplit[fileSplit.length - 1];
136+
} else if (currentFile && utils.startsWith(line, "@@")) {
137+
startBlock(line);
138+
} else if (currentBlock) {
139+
createLine(line);
140+
}
141+
});
142+
143+
saveBlock();
144+
saveFile();
145+
146+
return files;
147+
};

0 commit comments

Comments
 (0)