Line data Source code
1 : #ifndef FORMATTER_H
2 : #define FORMATTER_H
3 :
4 : #include <string>
5 : #include <sstream>
6 :
7 : #include "wd2_allocator.h"
8 : #include "DiffEngine.h"
9 :
10 : namespace wikidiff2 {
11 :
12 : class Formatter {
13 : public:
14 : typedef std::basic_string<char, std::char_traits<char>, WD2_ALLOCATOR<char> > String;
15 : typedef String::const_iterator StringIterator;
16 : typedef std::basic_stringstream<char, std::char_traits<char>, WD2_ALLOCATOR<char> > StringStream;
17 : typedef Diff<Word> WordDiff;
18 :
19 : virtual const char * getName() = 0;
20 :
21 : /**
22 : * Append a whole added line to the output
23 : *
24 : * @param line The text of the added line
25 : * @param leftLine The 1-based line number on the LHS
26 : * @param rightLine The 1-based line number on the RHS
27 : * @param offsetFrom The 0-based byte offset in the LHS input string
28 : * @param offsetTo The 0-based byte offset in the RHS input string
29 : */
30 : virtual void printAdd(const String & line, int leftLine, int rightLine, int offsetFrom, int offsetTo) = 0;
31 :
32 : /**
33 : * Append a whole deleted line to the output
34 : *
35 : * @param line The text of the deleted line
36 : * @param leftLine The 1-based line number on the LHS
37 : * @param rightLine The 1-based line number on the RHS
38 : * @param offsetFrom The 0-based byte offset in the LHS input string
39 : * @param offsetTo The 0-based byte offset in the RHS input string
40 : */
41 : virtual void printDelete(const String & line, int leftLine, int rightLine, int offsetFrom, int offsetTo) = 0;
42 :
43 : /**
44 : * Append the word diff for a changed line to the output
45 : *
46 : * @param wordDiff The word diff
47 : * @param leftLine The 1-based line number on the LHS
48 : * @param rightLine The 1-based line number on the RHS
49 : * @param offsetFrom The 0-based byte offset in the LHS input string
50 : * @param offsetTo The 0-based byte offset in the RHS input string
51 : * @param printLeft This is false to suppress LHS output when formatting
52 : * a moved line.
53 : * @param printRight This is false to suppress RHS output when formatting
54 : * a moved line
55 : * @param srcAnchor The HTML ID of the line currently being printed.
56 : * This is safe for output into HTML without escaping.
57 : * @param dstAnchor The HTML ID of the for a link to the source of the
58 : * moved line. This is safe for output into HTML without escaping.
59 : * @param moveDirectionDownwards True if the move is downwards, false
60 : * if the move is upwards. Ignore if the operation is not a move.
61 : */
62 : virtual void printWordDiff(
63 : const WordDiff & wordDiff,
64 : int leftLine, int rightLine,
65 : int offsetFrom, int offsetTo,
66 : bool printLeft = true, bool printRight = true,
67 : const String & srcAnchor = "", const String & dstAnchor = "",
68 : bool moveDirectionDownwards = false) = 0;
69 :
70 : /**
71 : * Append a word diff to the output which compares one line on the LHS
72 : * with multiple lines on the RHS.
73 : *
74 : * @param wordDiff The word diff
75 : * @param leftLine The 1-based line number on the LHS
76 : * @param rightLine The 1-based line number of the first line on the RHS
77 : * @param offsetFrom The 0-based byte offset in the LHS input string
78 : * @param offsetTo The 0-based byte offset in the RHS input string
79 : */
80 : virtual void printConcatDiff(
81 : const WordDiff & wordDiff,
82 : int leftLine, int rightLine,
83 : int offsetFrom, int offsetTo);
84 :
85 : /**
86 : * This is called once before any other output to give the subclass a
87 : * chance to add a header. No-op by default.
88 : */
89 : virtual void printFileHeader();
90 :
91 : /**
92 : * This is called after all other output, to give the subclass a chance
93 : * to add a footer. No-op by default.
94 : */
95 : virtual void printFileFooter();
96 :
97 : /**
98 : * This is called after context lines were omitted but before the
99 : * subsequent call to printContext().
100 : *
101 : * @param leftLine The 1-based line number on the LHS
102 : * @param rightLine The 1-based line number on the RHS
103 : */
104 : virtual void printBlockHeader(int leftLine, int rightLine) = 0;
105 :
106 : /**
107 : * Append to the output a line which was identical in the LHS and RHS.
108 : *
109 : * @param input The text of the line
110 : * @param leftLine The 1-based line number on the LHS
111 : * @param rightLine The 1-based line number on the RHS
112 : * @param offsetFrom The 0-based byte offset in the LHS input string
113 : * @param offsetTo The 0-based byte offset in the RHS input string
114 : */
115 : virtual void printContext(const String & input, int leftLine, int rightLine, int offsetFrom, int offsetTo) = 0;
116 :
117 : /**
118 : * Get the StringStream to which results were appended.
119 : */
120 47 : const StringStream & getResult() const {
121 47 : return result;
122 : }
123 :
124 : protected:
125 : /**
126 : * Dump a word diff to the output in a format suitable for debugging
127 : */
128 : void debugPrintWordDiff(const WordDiff & worddiff);
129 :
130 : /**
131 : * Encode a Word for HTML and add it to the output
132 : */
133 44221 : void printHtmlEncodedText(const Word & input)
134 : {
135 44221 : printHtmlEncodedText(input.start, input.end);
136 44221 : }
137 :
138 : /**
139 : * Encode a String for HTML and add it to the output
140 : */
141 291 : void printHtmlEncodedText(const String & input)
142 : {
143 291 : printHtmlEncodedText(input.cbegin(), input.cend());
144 291 : }
145 :
146 : /**
147 : * Encode a string range for HTML and add it to the output
148 : */
149 : void printHtmlEncodedText(StringIterator inputStart, StringIterator inputEnd);
150 :
151 : /**
152 : * Convert an integer to a string.
153 : */
154 : String toString(long input) const;
155 :
156 : /**
157 : * Determine whether an op from a concat diff is a newline insertion marker
158 : */
159 : static bool isNewlineMarker(const DiffOp<Word> & op);
160 :
161 : /**
162 : * The stream to write the result to.
163 : */
164 : StringStream result;
165 : };
166 :
167 : } // namespace wikidiff2
168 :
169 : #endif
|