Line data Source code
1 : #include "TableFormatter.h"
2 :
3 : namespace wikidiff2 {
4 :
5 0 : const char * TableFormatter::getName()
6 : {
7 0 : return "table";
8 : }
9 :
10 27 : void TableFormatter::printAdd(const String & line, int leftLine, int rightLine, int offsetFrom,
11 : int offsetTo)
12 : {
13 : result << "<tr>\n"
14 : " <td colspan=\"2\" class=\"diff-empty diff-side-deleted\"></td>\n"
15 : " <td class=\"diff-marker\" data-marker=\"+\"></td>\n"
16 27 : " <td class=\"diff-addedline diff-side-added\">";
17 27 : printTextWithDiv(line);
18 27 : result << "</td>\n</tr>\n";
19 27 : }
20 :
21 27 : void TableFormatter::printDelete(const String & line, int leftLine, int rightLine, int offsetFrom,
22 : int offsetTo)
23 : {
24 : result << "<tr>\n"
25 : " <td class=\"diff-marker\" data-marker=\"−\"></td>\n"
26 27 : " <td class=\"diff-deletedline diff-side-deleted\">";
27 27 : printTextWithDiv(line);
28 : result << "</td>\n"
29 : " <td colspan=\"2\" class=\"diff-empty diff-side-added\"></td>\n"
30 27 : "</tr>\n";
31 27 : }
32 :
33 61 : void TableFormatter::printWordDiff(const WordDiff & worddiff, int leftLine,
34 : int rightLine, int offsetFrom, int offsetTo, bool printLeft, bool printRight,
35 : const String & srcAnchor, const String & dstAnchor, bool moveDirectionDownwards)
36 : {
37 61 : result << "<tr>\n";
38 :
39 : // print left side or blank placeholder.
40 61 : if (printLeft) {
41 39 : if(dstAnchor != "")
42 : result << " <td class=\"diff-marker\">"
43 : "<a class=\"mw-diff-movedpara-left\" href=\"#" << dstAnchor << "\">⚫</a>"
44 22 : "</td>\n";
45 : else
46 17 : result << " <td class=\"diff-marker\" data-marker=\"−\"></td>\n";
47 :
48 39 : result << " <td class=\"diff-deletedline diff-side-deleted\"><div>";
49 39 : if(srcAnchor != "")
50 22 : result << "<a name=\"" << srcAnchor << "\"></a>";
51 39 : printWordDiffSide(worddiff, false);
52 39 : result << "</div></td>\n";
53 : } else {
54 22 : result << " <td colspan=\"2\" class=\"diff-empty diff-side-deleted\"></td>\n";
55 : }
56 :
57 : // print right side or blank placeholder.
58 61 : if (printRight) {
59 39 : if(dstAnchor != "")
60 : result << " <td class=\"diff-marker\">"
61 : "<a class=\"mw-diff-movedpara-right\" href=\"#" << dstAnchor << "\">⚫</a>"
62 22 : "</td>\n";
63 : else
64 17 : result << " <td class=\"diff-marker\" data-marker=\"+\"></td>\n";
65 :
66 39 : result << " <td class=\"diff-addedline diff-side-added\"><div>";
67 39 : if(srcAnchor != "")
68 22 : result << "<a name=\"" << srcAnchor << "\"></a>";
69 39 : printWordDiffSide(worddiff, true);
70 : result << "</div></td>\n"
71 39 : "</tr>\n";
72 : } else {
73 : result << " <td colspan=\"2\" class=\"diff-empty diff-side-added\"></td>\n"
74 22 : "</tr>\n";
75 : }
76 61 : }
77 :
78 78 : size_t TableFormatter::printWordDiffSegment(const WordDiff &worddiff, size_t offset, bool added)
79 : {
80 564 : for (size_t i = offset; i < worddiff.size(); ++i) {
81 486 : const DiffOp<Word> & op = worddiff[i];
82 : int n, j;
83 486 : if (added && isNewlineMarker(op)) {
84 : // If the line break is at the end of the word diff, we need to add
85 : // a blank line to the output. However if we are at the start of the
86 : // segment, we've already done the blank output.
87 0 : return (i > offset && i == worddiff.size() - 1) ? i : i + 1;
88 : }
89 486 : if (op.op == DiffOp<Word>::copy) {
90 264 : n = op.from.size();
91 264 : if (added) {
92 5811 : for (j=0; j<n; j++) {
93 5679 : printHtmlEncodedText(*op.to[j]);
94 : }
95 : } else {
96 5811 : for (j=0; j<n; j++) {
97 5679 : printHtmlEncodedText(*op.from[j]);
98 : }
99 : }
100 222 : } else if (!added && (op.op == DiffOp<Word>::del || op.op == DiffOp<Word>::change)) {
101 79 : n = op.from.size();
102 79 : result << "<del class=\"diffchange diffchange-inline\">";
103 272 : for (j=0; j<n; j++) {
104 193 : printHtmlEncodedText(*op.from[j]);
105 : }
106 79 : result << "</del>";
107 143 : } else if (added && (op.op == DiffOp<Word>::add || op.op == DiffOp<Word>::change)) {
108 89 : n = op.to.size();
109 89 : result << "<ins class=\"diffchange diffchange-inline\">";
110 255 : for (j=0; j<n; j++) {
111 166 : printHtmlEncodedText(*op.to[j]);
112 : }
113 89 : result << "</ins>";
114 : }
115 : }
116 78 : return worddiff.size();
117 : }
118 :
119 0 : void TableFormatter::printConcatDiff(const WordDiff & wordDiff,
120 : int leftLine, int rightLine,
121 : int offsetFrom, int offsetTo)
122 : {
123 0 : size_t segmentStart = 0;
124 0 : do {
125 0 : result << "<tr>\n";
126 :
127 : // print left side or blank placeholder.
128 0 : if (segmentStart == 0) {
129 : result << " <td class=\"diff-marker\" data-marker=\"−\"></td>\n"
130 0 : << " <td class=\"diff-deletedline diff-side-deleted\"><div>";
131 0 : printWordDiffSegment(wordDiff, 0, false);
132 0 : result << "</div></td>\n";
133 : } else {
134 0 : result << " <td colspan=\"2\" class=\"diff-empty diff-side-deleted\"></td>\n";
135 : }
136 :
137 : // print right side
138 : result << " <td class=\"diff-marker\" data-marker=\"+\"></td>\n"
139 0 : << " <td class=\"diff-addedline diff-side-added\"><div>";
140 0 : segmentStart = printWordDiffSegment(wordDiff, segmentStart, true);
141 : result << "</div></td>\n"
142 0 : "</tr>\n";
143 0 : } while (segmentStart < wordDiff.size());
144 0 : }
145 :
146 212 : void TableFormatter::printTextWithDiv(const String & input)
147 : {
148 : // Wrap string in a <div> if it's not empty
149 212 : if (input.size() > 0) {
150 175 : result << "<div>";
151 175 : printHtmlEncodedText(input);
152 175 : result << "</div>";
153 : } else {
154 : // Else add a <br> to preserve line breaks when copying
155 37 : result << "<br />";
156 : }
157 212 : }
158 :
159 23 : void TableFormatter::printBlockHeader(int leftLine, int rightLine)
160 : {
161 : result << "<tr>\n"
162 23 : " <td colspan=\"2\" class=\"diff-lineno\"><!--LINE " << leftLine << "--></td>\n"
163 23 : " <td colspan=\"2\" class=\"diff-lineno\"><!--LINE " << rightLine << "--></td>\n"
164 23 : "</tr>\n";
165 23 : }
166 :
167 79 : void TableFormatter::printContext(const String & input, int leftLine, int rightLine, int offsetFrom,
168 : int offsetTo)
169 : {
170 : result <<
171 : "<tr>\n"
172 : " <td class=\"diff-marker\"></td>\n"
173 79 : " <td class=\"diff-context diff-side-deleted\">";
174 79 : printTextWithDiv(input);
175 : result <<
176 : "</td>\n"
177 : " <td class=\"diff-marker\"></td>\n"
178 79 : " <td class=\"diff-context diff-side-added\">";
179 79 : printTextWithDiv(input);
180 79 : result << "</td>\n</tr>\n";
181 79 : }
182 :
183 : } // namespace wikidiff2
|