-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableDiff.cpp
149 lines (137 loc) · 3.91 KB
/
TableDiff.cpp
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
#include <stdio.h>
#include "Wikidiff2.h"
#include "TableDiff.h"
void TableDiff::printAdd(const String & line)
{
result += "<tr>\n"
" <td colspan=\"2\" class=\"diff-empty\"> </td>\n"
" <td class=\"diff-marker\">+</td>\n"
" <td class=\"diff-addedline\">";
printTextWithDiv(line);
result += "</td>\n</tr>\n";
}
void TableDiff::printDelete(const String & line)
{
result += "<tr>\n"
" <td class=\"diff-marker\">−</td>\n"
" <td class=\"diff-deletedline\">";
printTextWithDiv(line);
result += "</td>\n"
" <td colspan=\"2\" class=\"diff-empty\"> </td>\n"
"</tr>\n";
}
void TableDiff::printWordDiff(const String & text1, const String & text2, bool printLeft, bool printRight, const String & srcAnchor, const String & dstAnchor)
{
WordVector words1, words2;
TextUtil::explodeWords(text1, words1);
TextUtil::explodeWords(text2, words2);
WordDiff worddiff(words1, words2, MAX_WORD_LEVEL_DIFF_COMPLEXITY);
//debugPrintWordDiff(worddiff);
result += "<tr>\n";
// print left side or blank placeholder.
if (printLeft) {
result += " <td class=\"diff-marker\">";
if(dstAnchor != "")
result += "<a class=\"mw-diff-movedpara-left\" href=\"#" + dstAnchor + "\">⚫</a>";
else
result += "−";
result += "</td>\n";
result += " <td class=\"diff-deletedline\"><div>";
if(srcAnchor != "")
result += "<a name=\"" + srcAnchor + "\"></a>";
printWordDiffSide(worddiff, false);
result += "</div></td>\n";
} else {
result += " <td colspan=\"2\" class=\"diff-empty\"> </td>\n";
}
// print right side or blank placeholder.
if (printRight) {
result += " <td class=\"diff-marker\">";
if(dstAnchor != "")
result += "<a class=\"mw-diff-movedpara-right\" href=\"#" + dstAnchor + "\">⚫</a>";
else
result += "+";
result += "</td>\n";
result += " <td class=\"diff-addedline\"><div>";
if(srcAnchor != "")
result += "<a name=\"" + srcAnchor + "\"></a>";
printWordDiffSide(worddiff, true);
result += "</div></td>\n"
"</tr>\n";
} else {
result += " <td colspan=\"2\" class=\"diff-empty\"> </td>\n"
"</tr>\n";
}
}
void TableDiff::printWordDiffSide(WordDiff &worddiff, bool added)
{
String word;
for (unsigned i = 0; i < worddiff.size(); ++i) {
DiffOp<Word> & op = worddiff[i];
int n, j;
if (op.op == DiffOp<Word>::copy) {
n = op.from.size();
if (added) {
for (j=0; j<n; j++) {
op.to[j]->get_whole(word);
printText(word);
}
} else {
for (j=0; j<n; j++) {
op.from[j]->get_whole(word);
printText(word);
}
}
} else if (!added && (op.op == DiffOp<Word>::del || op.op == DiffOp<Word>::change)) {
n = op.from.size();
result += "<del class=\"diffchange diffchange-inline\">";
for (j=0; j<n; j++) {
op.from[j]->get_whole(word);
printText(word);
}
result += "</del>";
} else if (added && (op.op == DiffOp<Word>::add || op.op == DiffOp<Word>::change)) {
n = op.to.size();
result += "<ins class=\"diffchange diffchange-inline\">";
for (j=0; j<n; j++) {
op.to[j]->get_whole(word);
printText(word);
}
result += "</ins>";
}
}
}
void TableDiff::printTextWithDiv(const String & input)
{
// Wrap string in a <div> if it's not empty
if (input.size() > 0) {
result.append("<div>");
printText(input);
result.append("</div>");
}
}
void TableDiff::printBlockHeader(int leftLine, int rightLine)
{
char buf[256]; // should be plenty
snprintf(buf, sizeof(buf),
"<tr>\n"
" <td colspan=\"2\" class=\"diff-lineno\"><!--LINE %u--></td>\n"
" <td colspan=\"2\" class=\"diff-lineno\"><!--LINE %u--></td>\n"
"</tr>\n",
leftLine, rightLine);
result += buf;
}
void TableDiff::printContext(const String & input)
{
result +=
"<tr>\n"
" <td class=\"diff-marker\"> </td>\n"
" <td class=\"diff-context\">";
printTextWithDiv(input);
result +=
"</td>\n"
" <td class=\"diff-marker\"> </td>\n"
" <td class=\"diff-context\">";
printTextWithDiv(input);
result += "</td>\n</tr>\n";
}