File: TableDiff.cpp

package info (click to toggle)
wikidiff2 1.2%2Bgit03ea59f-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 276 kB
  • ctags: 240
  • sloc: cpp: 1,357; php: 172; makefile: 13
file content (123 lines) | stat: -rw-r--r-- 3,118 bytes parent folder | download
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
#include <stdio.h>
#include "Wikidiff2.h"
#include "TableDiff.h"

void TableDiff::printAdd(const String & line)
{
	result += "<tr>\n"
		"  <td colspan=\"2\" class=\"diff-empty\">&#160;</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\">&#160;</td>\n"
		"</tr>\n";
}

void TableDiff::printWordDiff(const String & text1, const String & text2)
{
	WordVector words1, words2;

	explodeWords(text1, words1);
	explodeWords(text2, words2);
	WordDiff worddiff(words1, words2);

	//debugPrintWordDiff(worddiff);

	// print twice; first for left side, then for right side
	result += "<tr>\n"
		"  <td class=\"diff-marker\">−</td>\n"
		"  <td class=\"diff-deletedline\"><div>";
	printWordDiffSide(worddiff, false);
	result += "</div></td>\n"
		"  <td class=\"diff-marker\">+</td>\n"
		"  <td class=\"diff-addedline\"><div>";
	printWordDiffSide(worddiff, true);
	result += "</div></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\">&#160;</td>\n"
		"  <td class=\"diff-context\">";
	printTextWithDiv(input);
	result +=
		"</td>\n"
		"  <td class=\"diff-marker\">&#160;</td>\n"
		"  <td class=\"diff-context\">";
	printTextWithDiv(input);
	result += "</td>\n</tr>\n";
}