summaryrefslogtreecommitdiff
path: root/src/modules/filters/gbfrtf.cpp
blob: eb396121a1dc448cd2365d4a8a616d4b01f0d08b (plain)
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
/******************************************************************************
 *
 * gbfrtf -	SWFilter descendant to convert all GBF tags to RTF tags
 */


#include <gbfrtf.h>
#include <utilstr.h>
#include <ctype.h>
#include <swbuf.h>

SWORD_NAMESPACE_START

GBFRTF::GBFRTF() {
}


char GBFRTF::processText(SWBuf &text, const SWKey *key, const SWModule *module)
{
	char token[2048];
	char val[128];
	char *valto;
	char *num;
	int tokpos = 0;
	bool intoken = false;
	const char *tok;
	SWBuf strongnum;
	SWBuf strongtense;
	bool hideText = false;
	int wordLen = 0;
	int wordCount = 0;

	const char *from;
	SWBuf orig = text;
	from = orig.c_str();
	for (text = ""; *from; from++) {
		if (*from == '<') {
			wordLen = wordCount;
			wordCount = 0;
			intoken = true;
			tokpos = 0;
			token[0] = 0;
			token[1] = 0;
			token[2] = 0;
			continue;
		}
		if (*from == '>') {
			intoken = false;
						// process desired tokens
			// deal with OSIS note tags.  Just hide till OSISRTF
			if (!strncmp(token, "note ", 5)) {
				hideText = true;
			}
			if (!strncmp(token, "/note", 5)) {
				hideText = false;
			}

			switch (*token) {
			case 'w':	// OSIS Word (temporary until OSISRTF is done)
				strongnum = "";
				strongtense = "";
				valto = val;
				num = strstr(token, "lemma=\"x-Strongs:");
				if (num) {
					for (num+=17; ((*num) && (*num != '\"')); num++)
						*valto++ = *num;
					*valto = 0;
					if (atoi((!isdigit(*val))?val+1:val) < 5627) {
						// normal strongs number
						strongnum += "{\\cf3 \\sub <";
						for (tok = (!isdigit(*val))?val+1:val; *tok; tok++)
							strongnum += *tok;
						strongnum += ">}";
					}
					/*	forget these for now
					else {
						// verb morph
						sprintf(wordstr, "%03d", word-1);
						module->getEntryAttributes()["Word"][wordstr]["Morph"] = val;
					}
					*/
				}
				else {
					num = strstr(token, "lemma=\"strong:");
					if (num) {
						for (num+=14; ((*num) && (*num != '\"')); num++)
							*valto++ = *num;
						*valto = 0;
						if (atoi((!isdigit(*val))?val+1:val) < 5627) {
							// normal strongs number
							strongnum += "{\\cf3 \\sub <";
							for (tok = (!isdigit(*val))?val+1:val; *tok; tok++)
								strongnum += *tok;
							strongnum += ">}";
						}
						/*	forget these for now
						else {
							// verb morph
							sprintf(wordstr, "%03d", word-1);
							module->getEntryAttributes()["Word"][wordstr]["Morph"] = val;
						}
						*/
					}
				}
				valto = val;
				num = strstr(token, "morph=\"x-Robinson:");
				if (num) {
					for (num+=18; ((*num) && (*num != '\"')); num++)
						*valto++ = *num;
					*valto = 0;
					// normal robinsons tense
					strongtense += "{\\cf4 \\sub (";
					for (tok = val; *tok; tok++)
						strongtense += *tok;
					strongtense += ")}";
				}
				continue;

			case '/':
				if (token[1] == 'w') {
					if ((wordCount > 0) || (strongnum != "{\\cf3 \\sub <3588>}")) {
						//for (i = 0; i < strongnum.length(); i++)
							text += strongnum;
					//for (i = 0; i < strongtense.length(); i++)
						text += strongtense;
					}
				}
				continue;

			case 'W':	// Strongs
				switch(token[1]) {
				case 'G':               // Greek
				case 'H':               // Hebrew
					text += "{\\cf3 \\sub <";
					for (tok = token + 2; *tok; tok++)
						text += *tok;
					text += ">}";
					continue;

				case 'T':               // Tense
					text += "{\\cf4 \\sub (";
					bool separate = false;
					for (tok = token + 2; *tok; tok++) {
						if (separate) {
							text += "; ";
							separate = false;
						}
						switch (*tok) {
						case 'G':
						case 'H':
							for (tok++; *tok; tok++) {
								if (isdigit(*tok)) {
									text += *tok;
									separate = true;
								}
								else {
									tok--;
									break;
								}
							}
							break;
						default:
							for (; *tok; tok++) {
							       text += *tok;
							}
						}
					}
					text += ")}";
					continue;
				}
				break;
			case 'R':
				switch(token[1]) {
				case 'X':
					text += "<a href=\"\">";
				  continue;
				case 'x':
					text += "</a>";
				  continue;
				case 'F':               // footnote begin
					text += "{\\i1 \\sub [ ";
					continue;
				case 'f':               // footnote end
					text += " ] }";
					continue;
				}
				break;
			case 'F':			// font tags
				switch(token[1]) {
				case 'I':		// italic start
					text += "\\i1 ";
					continue;
				case 'i':		// italic end
					text += "\\i0 ";
					continue;
				case 'B':		// bold start
					text += "\\b1 ";
					continue;
				case 'b':		// bold end
					text += "\\b0 ";
					continue;
				case 'N':
					text += '{';
					if (!strnicmp(token+2, "Symbol", 6))
						text += "\\f7 ";
                                        if (!strnicmp(token+2, "Courier", 7))
						text += "\\f8 ";
					continue;
				case 'n':
					text += '}';
					continue;
				case 'S':
					text += "{\\super ";
					continue;
				case 's':
					text += '}';
					continue;
				case 'R':
					text += "{\\cf6 ";
					continue;
				case 'r':
					text += '}';
					continue;
				case 'O':
				case 'C':
					text += "\\scaps1 ";
					continue;
				case 'o':
				case 'c':
					text += "\\scaps0 ";
					continue;
				case 'V':
					text += "{\\sub ";
					continue;
				case 'v':
					text += '}';
					continue;
				case 'U':
					text += "\\ul1 ";
					continue;
				case 'u':
					text += "\\ul0 ";
					continue;
				}
				break;
			case 'C':			// special character tags
				switch(token[1]) {
				case 'A':               // ASCII value
					text += (char)atoi(&token[2]);
					continue;
				case 'G':
					text += '>';
					continue;
				case 'L':               // line break
					text += "\\line ";
					continue;
				case 'M':               // new paragraph
					text += "\\par ";
					continue;
				case 'T':
					text += '<';
				}
				break;
			case 'T':			// title formatting
			  switch(token[1])
			    {
			    case 'T':               // Book title begin
					text += "{\\large ";
				 continue;
			    case 't':
				 text += '}';
				 continue;
			    case 'S':
					text += "\\par {\\i1\\b1 ";
			      continue;
			    case 's':
					text += "}\\par ";
			      continue;
			    }
			  break;
			case 'J':	// Strongs
				switch(token[1]) {
				case 'L':
					text += "\\ql ";
				case 'C':
					text += "\\qc ";
				case 'R':
					text += "\\qr ";
                                case 'F':
                                	text += "\\qj ";
                                }
			}
			continue;
		}
		if (intoken) {
			if (tokpos < 2045)
				token[tokpos++] = *from;
				token[tokpos+2] = 0;
		}
		else {
			if (!hideText) {
				wordCount++;
				text += *from;
			}
		}
	}
	return 0;
}

SWORD_NAMESPACE_END