File: textprocessor.cpp

package info (click to toggle)
aethera 0.9.3-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,588 kB
  • ctags: 7,282
  • sloc: cpp: 64,544; sh: 9,913; perl: 1,756; makefile: 1,680; python: 258
file content (330 lines) | stat: -rw-r--r-- 8,327 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <textprocessor.h>
#include <qtextstream.h>
#include <qregexp.h>
#include <qvaluelist.h>
#include <stdio.h>

QString TextProcessor::quoteText(QString &text, const QString &quoteSeq)
{
	QString outstr;
	QTextStream lines(&text, IO_ReadOnly);
	
	while(!lines.atEnd())
		outstr+=quoteSeq+lines.readLine()+'\n';
	
	return outstr;
}

QString TextProcessor::unquoteText(QString &text, const QStringList &quoteSeqList)
{
	QString outstr;
	QTextStream lines(&text, IO_ReadOnly);
	
	while(!lines.atEnd())
	{
		QString line=lines.readLine();
		bool unquoted=false;
		for(int i=0;i<quoteSeqList.count() && !unquoted; i++)
		{
			QRegExp rexp(quoteSeqList[i]);
			int len;
			if(!rexp.match(line, 0, &len))
			{
				line.remove(0, len);
				unquoted=true;
			}
		}
		outstr+=line+'\n';
	}
	
	return outstr;
}

QString TextProcessor::indentText(QString &text, int spaces)
{
	QString strspaces;
	strspaces.fill(' ', spaces);
	return TextProcessor::quoteText(text, strspaces);
}

QString TextProcessor::unindentText(QString &text, int spaces)
{
	QString strspaces="^";
	for(int i=0;i<spaces;i++) strspaces+="\\s";
	return TextProcessor::unquoteText(text, strspaces);
}

QString TextProcessor::indentTabText(QString &text, int tabs)
{
	QString strspaces;
	strspaces.fill('\t', tabs);
	return TextProcessor::quoteText(text, strspaces);
}

QString TextProcessor::unindentTabText(QString &text, int tabs)
{
	QString strspaces="^";
	for(int i=0;i<tabs;i++) strspaces+="\t";
	return TextProcessor::unquoteText(text, strspaces);
}

QString TextProcessor::metaFormat(QString text, const MetaFormatControl &controlData)
{
	if(text.isEmpty()) return text;

	// any sequences
	if(controlData.enableAnySequence)
	{
		QStringList textLines=QStringList::split("\n", text, true);
		bool rebuild=false;
		for(int i=0;i<textLines.count();i++)
		{
			// try all patterns
			for(int j=0;j<controlData.anyData.count();j++)
			{
				if(controlData.anyData[j].strip)
				{
					bool retry=true;
					QRegExp br(controlData.anyData[j].beginPattern), tr(controlData.anyData[j].textPattern), er(controlData.anyData[j].endPattern);
					int blen, tlen, elen;
					while(retry)
					{
						retry=false;
						int bpos, tpos=-1, epos=-1;
						bpos=br.match(textLines[i], 0, &blen);
						if(bpos!=-1) epos=er.match(textLines[i], bpos+blen, &elen);
						if(epos!=-1) tpos=tr.match(textLines[i].mid(bpos+blen, epos-bpos-blen), 0, &tlen);
						if(bpos!=-1 && tpos!=-1 && epos!=-1)
						{
							// got a complete match
							QString mText="<"+controlData.anyData[j].tag+">"+textLines[i].mid(bpos+blen+tpos, tlen)+"</"+controlData.anyData[j].tag+">";
							printf("replacement text: %s\n", (const char *)mText);
							textLines[i].remove(bpos, blen+tlen+elen);
							textLines[i].insert(bpos, mText);
							retry=true;
							rebuild=true;
						}
					}
				}
				else
				{
					bool retry=true;
					QRegExp regexp(controlData.anyData[j].textPattern);
					int len, pos=0;
					while(retry)
					{
						retry=false;
						pos=regexp.match(textLines[i], pos, &len);
						if(pos!=-1)
						{
							textLines[i].insert(pos+len, "</"+controlData.anyData[j].tag+">");
							textLines[i].insert(pos, "<"+controlData.anyData[j].tag+">");
							pos+=controlData.anyData[j].tag.length()+2;
							retry=true;
							rebuild=true;
						}
					}
				}
			}
		}
		if(rebuild)
		{
			text="";
			for(int i=0;i<textLines.count()-1;i++) text+=textLines[i]+"\n";
		}
	}
		
	// split into chunks, according to the reply quote depth
	QStringList replyChunks;
	QValueList<int> replyChunkDepths;
	
	if(controlData.enableReplyTags)
	{
		QTextStream lines(&text, IO_ReadOnly);
		QString currentChunk=lines.readLine();
		QStringList seq=TextProcessor::depth(currentChunk, QStringList::fromStrList(controlData.replyData));
		int currentDepth=seq.count();
		currentChunk=TextProcessor::strip(currentChunk, seq);
		currentChunk+="\n";
				
		while(!lines.atEnd())
		{
			QString line=lines.readLine();
			seq=TextProcessor::depth(line, QStringList::fromStrList(controlData.replyData));
			int newDepth=seq.count();
			line=TextProcessor::strip(line, seq);
			line+="\n";
			
			if(newDepth==currentDepth)
			{
				currentChunk+=line;
			}
			else
			{
				replyChunks.append(currentChunk);
				replyChunkDepths.append(currentDepth);
				currentChunk=line;
				currentDepth=newDepth;
			}
		}
		
		replyChunks.append(currentChunk);
		replyChunkDepths.append(currentDepth);
	}
	else
	{
		replyChunks.append(text);
		replyChunkDepths.append(0);
	}
	
	// check next-line folding sequences
	if(controlData.enableNextLineFolding)
	{
		int i=1;
		while(i<replyChunks.count())
		{
			if(replyChunkDepths[i-1]!=replyChunkDepths[i] && !replyChunkDepths[i]) // got reply depth difference, second is not indented
			{
				QRegExp regexp("^\\s*[^A-Z]"); // if it begins with capital letter don't process, it might be "Stephan Kulow-style" reply formatting
				int pos=regexp.match(replyChunks[i]), crpos;
				
				// make sure it's on the first line
				if(pos!=-1 && pos<(crpos=replyChunks[i].find('\n'))) // (...remember that we always end chunks with <cr>)
				{
					// ...and that the next line is empty (or does not exist)
					int nextcrpos=replyChunks[i].find('\n', crpos+1);
					if(nextcrpos==-1 || replyChunks[i].mid(crpos, nextcrpos-crpos-1).stripWhiteSpace().isEmpty())
					{
						// all conditions are met, wrap line into previous chunk
						replyChunks[i-1]+=replyChunks[i].left(crpos+1); // +1 adds the last <cr>
						
						// dissolve next chunk if empty
						if(nextcrpos==-1)
						{
							replyChunks.remove(replyChunks.at(i));
							replyChunkDepths.remove(replyChunkDepths.at(i));
						}
						else
						{
							// ...or else just remove the line we've wrapped
							replyChunks[i].remove(0, crpos+1);
							
							i++;
						}
					}
					else
					{
						i++;
					}
				}
				else
				{
					i++;
				}
			}
			else
			{
				// if we've dissolved one chunk between two chunks with equal depth, collapse chunks to get optimal output
				if(replyChunkDepths[i-1]==replyChunkDepths[i])
				{
					replyChunks[i-1]+=replyChunks[i];
					replyChunks.remove(replyChunks.at(i));
					replyChunkDepths.remove(replyChunkDepths.at(i));
				}
				else
				{
					i++;
				}
			}
		}
	}
	
	// check wrote sequences
	if(controlData.enableWroteSequence)
	{
		for(int i=0;i<replyChunks.count();i++)
		{
			QStringList chunkLines=QStringList::split("\n", replyChunks[i], true);
			bool rebuild=false;
			for(int j=0;j<chunkLines.count();j++)
			{
				bool found=false;
				for(QStrListIterator it(controlData.wroteData); it.current() && !found; ++it)
				{
					QRegExp regexp((*it));
					if(regexp.match(chunkLines[j])!=-1)
					{
						chunkLines[j]="<wrote>"+chunkLines[j]+"</wrote>";
						found=true;
						rebuild=true;
					}
				}
			}
			if(rebuild)
			{
				replyChunks[i]="";
				for(int j=0;j<chunkLines.count()-1;j++) replyChunks[i]+=chunkLines[j]+"\n";
			}
		}
	}
	
	// add reply tags
	if(controlData.enableReplyTags)
	{
		QString outtext;
		if(controlData.enableBlockReplyTags)
		{
			for(int i=0;i<replyChunks.count();i++)
				if(replyChunkDepths[i])
					outtext+="<reply "+QString::number(replyChunkDepths[i])+">"+replyChunks[i]+"</reply>";
				else
					outtext+="<normal>"+replyChunks[i]+"</normal>";
		}
		else
		{
			for(int i=0;i<replyChunks.count();i++)
			{
				if(replyChunkDepths[i])
				{
					QStringList chunkLines=QStringList::split('\n', replyChunks[i], true);
					for(int j=0;j<chunkLines.count()-1;j++)
						outtext+="<reply "+QString::number(replyChunkDepths[i])+">"+chunkLines[j]+"</reply>";
				}
				else
				{
					outtext+="<normal>"+replyChunks[i]+"</normal>";
				}
			}
		}
		return outtext;
	}
}

QStringList TextProcessor::depth(QString text, const QStringList &quoteList)
{
	QStringList maxseq;
	
	for(int i=0;i<quoteList.count();i++)
	{
		QRegExp regexp(quoteList[i]);
		int len;
		
		if(!regexp.match(text, 0, &len))
		{
			QString nexttext(text.mid(len));
			QStringList nextseq(quoteList[i]);
			nextseq+=depth(nexttext, quoteList);
			if(nextseq.count()>maxseq.count()) maxseq=nextseq;
		}
	}
	
	return maxseq;
}

QString TextProcessor::strip(QString text, const QStringList &quoteList)
{
	QRegExp regexp(quoteList.join(""));
	int len;
	if(!regexp.match(text, 0, &len)) text.remove(0, len);
	return text;
}