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
|
#include "InlineDiff.h"
void InlineDiff::printAdd(const String& line)
{
printWrappedLine("<div class=\"mw-diff-inline-added\"><ins>", line, "</ins></div>\n");
}
void InlineDiff::printDelete(const String& line)
{
printWrappedLine("<div class=\"mw-diff-inline-deleted\"><del>", line, "</del></div>\n");
}
void InlineDiff::printWordDiff(const String& text1, const String& text2)
{
WordVector words1, words2;
explodeWords(text1, words1);
explodeWords(text2, words2);
WordDiff worddiff(words1, words2);
String word;
result += "<div class=\"mw-diff-inline-changed\">";
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();
for (j=0; j<n; j++) {
op.from[j]->get_whole(word);
printText(word);
}
} else if (op.op == DiffOp<Word>::del) {
n = op.from.size();
result += "<del>";
for (j=0; j<n; j++) {
op.from[j]->get_whole(word);
printText(word);
}
result += "</del>";
} else if (op.op == DiffOp<Word>::add) {
n = op.to.size();
result += "<ins>";
for (j=0; j<n; j++) {
op.to[j]->get_whole(word);
printText(word);
}
result += "</ins>";
} else if (op.op == DiffOp<Word>::change) {
n = op.from.size();
result += "<del>";
for (j=0; j<n; j++) {
op.from[j]->get_whole(word);
printText(word);
}
result += "</del>";
n = op.to.size();
result += "<ins>";
for (j=0; j<n; j++) {
op.to[j]->get_whole(word);
printText(word);
}
result += "</ins>";
}
}
result += "</div>\n";
}
void InlineDiff::printBlockHeader(int leftLine, int rightLine)
{
char buf[256]; // should be plenty
snprintf(buf, sizeof(buf),
"<div class=\"mw-diff-inline-header\"><!-- LINES %u,%u --></div>\n",
leftLine, rightLine);
result += buf;
}
void InlineDiff::printContext(const String & input)
{
printWrappedLine("<div class=\"mw-diff-inline-context\">", input, "</div>\n");
}
void InlineDiff::printWrappedLine(const char* pre, const String& line, const char* post)
{
result += pre;
if (line.empty()) {
result += " ";
} else {
printText(line);
}
result += post;
}
|