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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
/*
Copyright (©) 2003-2020 Teus Benschop.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <filter/passage.h>
#include <filter/string.h>
#include <filter/url.h>
#include <filter/usfm.h>
#include <database/books.h>
#include <database/bibles.h>
#include <database/config/bible.h>
#include <locale/translate.h>
#include <assets/view.h>
Passage::Passage ()
{
book = 0;
chapter = 0;
}
Passage::Passage (string bible_in, int book_in, int chapter_in, string verse_in)
{
bible = bible_in;
book = book_in;
chapter = chapter_in;
verse = verse_in;
}
bool Passage::equal (Passage & passage)
{
if (bible != passage.bible) return false;
if (book != passage.book) return false;
if (chapter != passage.chapter) return false;
if (verse != passage.verse) return false;
return true;
}
// This method converts the passage of the object into text, like e.g. so:
// "hexadecimal Bible _1_2_3".
// First the hexadecimal Bible comes, then the book identifier, then the chapter number, and finally the verse number.
string Passage::encode ()
{
string text;
// The encoded passage can be used as an attribute in the HTML DOM.
// Therefore it will be encoded such that any Bible name will be acceptable as an attribute in the DOM.
text.append (bin2hex (bible));
text.append ("_");
text.append (convert_to_string (book));
text.append ("_");
text.append (convert_to_string (chapter));
text.append ("_");
text.append (verse);
if (verse.empty()) text.append ("0");
return text;
}
// This method converts encoded text into a passage.
// The text is in the format as its complementary function, "encode", produces.
Passage Passage::decode (const string& encoded)
{
Passage passage;
vector <string> bits = filter_string_explode (encoded, '_');
if (!bits.empty ()) {
string verse = bits.back ();
if (!verse.empty ()) passage.verse = verse;
bits.pop_back ();
}
if (!bits.empty ()) {
string chapter = bits.back ();
if (!chapter.empty()) passage.chapter = convert_to_int (chapter);
bits.pop_back ();
}
if (!bits.empty ()) {
string book = bits.back ();
if (!book.empty()) passage.book = convert_to_int (book);
bits.pop_back ();
}
if (!bits.empty ()) {
passage.bible = hex2bin (bits.back ());
bits.pop_back ();
}
return passage;
}
string filter_passage_display (int book, int chapter, string verse)
{
string display;
display.append (translate (Database_Books::getEnglishFromId (book).c_str()));
display.append (" ");
display.append (convert_to_string (chapter));
if (!verse.empty ()) display.append (":" + verse);
return display;
}
// Returns the display string for the $passages as one line.
string filter_passage_display_inline (vector <Passage> passages)
{
string display;
for (Passage & passage : passages) {
if (display != "") display.append (" | ");
display.append (filter_passage_display (passage.book, passage.chapter, passage.verse));
}
return display;
}
// Returns the display string for the $passages as several lines.
string filter_passage_display_multiline (vector <Passage> passages)
{
string display;
for (Passage & passage : passages) {
display.append (filter_passage_display (passage.book, passage.chapter, passage.verse));
display.append ("\n");
}
return display;
}
// This function converts $passage to an integer, so that passages can be compared or stored.
int filter_passage_to_integer (Passage passage)
{
return 1000000 * passage.book + 1000 * passage.chapter + convert_to_int (passage.verse);
}
// This converts the $integer, created above, to a passage.
Passage filter_integer_to_passage (int integer)
{
int book = round (integer / 1000000);
integer -= book * 1000000;
int chapter = round (integer / 1000);
integer -= chapter * 1000;
string verse = convert_to_string (integer);
return Passage ("", book, chapter, verse);
}
// This filter takes $books as a string,
// and looks whether it can be interpreted as a valid book in any way.
// It returns a valid book identifier, or 0 in case no book could be interpreted.
int filter_passage_interpret_book (string book)
{
book = filter_string_trim (book);
int identifier = 0;
// Recognize names like "I Peter", where the "I" can also be "II" or "III".
// Do the longest ones first.
book = filter_string_str_replace ("III ", "3 ", book);
book = filter_string_str_replace ("II ", "2 ", book);
book = filter_string_str_replace ("I ", "1 ", book);
// Check on names entered like "Genesis" or "1 Corinthians", the full English name.
// A bug was discovered so that "Judges" was interpreted as "Jude", because
// of the three letters "Jud". Solved by checking on full English name first.
identifier = Database_Books::getIdFromEnglish (book);
if (identifier) {
return identifier;
}
// Recognise the USFM book abbreviations.
identifier = Database_Books::getIdFromUsfm (book);
if (identifier) return identifier;
// Try the OSIS abbreviations.
identifier = Database_Books::getIdFromOsis (book);
if (identifier) return identifier;
// Try the abbreviations of BibleWorks.
identifier = Database_Books::getIdFromBibleworks (book);
if (identifier) return identifier;
// Handle names from BibleWorks when copying the verse list to the clipboard.
// These are not handled elsewhere.
if (book == "Cant") return 22;
if (book == "Mk") return 41;
if (book == "Lk") return 42;
if (book == "Jn") return 43;
if (book == "1 Jn") return 62;
if (book == "2 Jn") return 63;
if (book == "3 Jn") return 64;
// Try the abbreviations of the Online Bible.
identifier = Database_Books::getIdFromOnlinebible (book);
if (identifier) return identifier;
// Do a case-insensitive search in the books database for something like the book given.
identifier = Database_Books::getIdLikeText (book);
if (identifier) return identifier;
// Remove any spaces from the book name, and try again.
book = filter_string_str_replace (" ", "", book);
identifier = Database_Books::getIdFromOsis (book);
if (identifier) return identifier;
identifier = Database_Books::getIdFromBibleworks (book);
if (identifier) return identifier;
identifier = Database_Books::getIdFromOnlinebible (book);
if (identifier) return identifier;
identifier = Database_Books::getIdLikeText (book);
if (identifier) return identifier;
// Last effort to find a book.
identifier = Database_Books::getIdLastEffort (book);
if (identifier) return identifier;
// Sorry, no book found.
return 0;
}
string filter_passage_clean_passage (string text)
{
// Trim text.
text = filter_string_trim (text);
// As references could be, e.g.: Genesis 10.2, or Genesis 10:2,
// it needs to convert a the full stop and the colon to a space.
text = filter_string_str_replace (".", " ", text);
text = filter_string_str_replace (":", " ", text);
// Change double spaces into single ones.
text = filter_string_str_replace (" ", " ", text);
text = filter_string_str_replace (" ", " ", text);
// Trim again.
text = filter_string_trim (text);
// Result.
return text;
}
// Takes the passage in $text, and explodes it into book, chapter, verse.
// The book is the numerical identifier, not the string, e.g.,
// it would not return "Genesis", but identifier 1.
Passage filter_passage_explode_passage (string text)
{
text = filter_passage_clean_passage (text);
// Cut the text in its parts.
vector <string> bits = filter_string_explode (text, ' ');
// Defaults to empty passage.
Passage passage;
// Take the bits.
if (!bits.empty ()) {
string verse = bits.back ();
if (!verse.empty ()) passage.verse = verse;
bits.pop_back ();
}
if (!bits.empty ()) {
string chapter = bits.back ();
if (!chapter.empty()) passage.chapter = convert_to_int (chapter);
bits.pop_back ();
}
string book = filter_string_implode (bits, " ");
if (!book.empty()) {
int bk = filter_passage_interpret_book (book);
passage.book = bk;
}
// Return the result.
return passage;
}
// Takes the passage in $rawPassage, and tries to interpret it.
// The following situations can occur:
// - Only book given, e.g. "Genesis".
// - One number given, e.g. "10".
// - Two numbers given, e.g. "1 2".
// - Book and one number given, e.g. "Exodus 10".
// - Book and two numbers given, e.g. "Song of Solomon 2 3".
// It deals with these situations.
// If needed, it bases the interpreted passage on $currentPassage.
Passage filter_passage_interpret_passage (Passage currentPassage, string rawPassage)
{
rawPassage = filter_passage_clean_passage (rawPassage);
// Create an array with the bits of the raw input.
vector <string> input = filter_string_explode (rawPassage, ' ');
// Go through the array from verse to chapter to book.
// Check how many numerals it has after the book part.
vector <int> numerals;
string book = "";
vector <string> invertedInput (input.begin(), input.end ());
reverse (invertedInput.begin (), invertedInput.end());
for (string & bit : invertedInput) {
int integer = convert_to_int (bit);
if (bit == convert_to_string (integer)) {
numerals.push_back (integer);
input.pop_back ();
} else {
book = filter_string_implode (input, " ");
break;
}
}
// Deal with: Only book given, e.g. "Genesis".
if ((book != "") && (numerals.size () == 0)) {
return filter_passage_explode_passage (book + " 1 1");
}
// Deal with: One number given, e.g. "10".
else if ((book == "") && (numerals.size () == 1)) {
int bk = currentPassage.book;
int chapter = currentPassage.chapter;
string verse = convert_to_string (numerals [0]);
Passage passage = filter_passage_explode_passage ("Unknown " + convert_to_string (chapter) + " " + verse);
passage.book = bk;
return passage;
}
// Deal with: Two numbers given, e.g. "1 2".
else if ((book == "") && (numerals.size () == 2)) {
int bk = currentPassage.book;
int chapter = numerals [1];
string verse = convert_to_string (numerals [0]);
Passage passage = filter_passage_explode_passage ("Unknown " + convert_to_string (chapter) + " " + verse);
passage.book = bk;
return passage;
}
// Deal with: Book and one number given, e.g. "Exodus 10".
else if ((book != "") && (numerals.size () == 1)) {
int chapter = numerals [0];
return filter_passage_explode_passage (book + " " + convert_to_string (chapter) + " 1");
}
// Deal with: Book and two numbers given, e.g. "Song of Solomon 2 3".
else if ((book != "") && (numerals.size () == 2)) {
return filter_passage_explode_passage (rawPassage);
}
// Give up.
return currentPassage;
}
// This deals with sequences and ranges of verses, like the following:
// Exod. 37:4-5, 14-15, 27-28
// It puts each verse on a separate line.
vector <string> filter_passage_handle_sequences_ranges (const string& passage)
{
// A passage like Exod. 37:4-5, 14-15, 27-28 will be cut at the comma.
// The resulting array contains the following:
// Exod. 37:4-5
// 14-15
// 27-28
// It implies that the first sequence has book and chapter.
vector <string> sequences = filter_string_explode (passage, ',');
for (string & line : sequences) line = filter_string_trim (line);
// Store output lines.
vector <string> output;
// Cut the passages at the hyphen.
for (unsigned int offset = 0; offset < sequences.size(); offset++) {
string sequence = sequences [offset];
vector <string> range = filter_string_explode (sequence, '-');
if (range.size () == 1) {
output.push_back (filter_string_trim (range [0]));
} else {
string start = filter_string_trim (range [0]);
output.push_back (start);
if (offset == 0) {
// Since the first bit contains book / chapter / verse,
// extract the verse number.
start = string (start.rbegin(), start.rend());
start = convert_to_string (convert_to_int (start));
start = string (start.rbegin(), start.rend());
}
unsigned int end = convert_to_int (filter_string_trim (range [1]));
for (size_t i = convert_to_int (start) + 1; i <= end; i++) {
output.push_back (convert_to_string (i));
}
}
}
// Result.
return output;
}
string filter_passage_link_for_opening_editor_at (int book, int chapter, string verse)
{
string display = filter_passage_display (book, chapter, verse);
Passage passage = Passage ("", book, chapter, verse);
string numeric = convert_to_string (filter_passage_to_integer (passage));
string link = "<a class=\"starteditor\" href=\"" + numeric + "\">" + display + "</a> <span></span>";
return link;
}
// A Bible has a standard order for the books, and it can have their books in a custom order.
// This function returns either the standard order, or a custom order in case it is available for the $bible.
vector <int> filter_passage_get_ordered_books (const string& bible)
{
Database_Bibles database_bibles;
// The available books from the Bible.
vector <int> projectbooks = database_bibles.getBooks (bible);
// The book order from the settings, if any.
string s_orderedbooks = Database_Config_Bible::getBookOrder (bible);
vector <string> vs_orderedbooks = filter_string_explode (s_orderedbooks, ' ');
// Keep books available in the Bible.
vector <int> orderedbooks;
for (string & book : vs_orderedbooks) {
int bk = convert_to_int (book);
if (find (projectbooks.begin(), projectbooks.end(), bk) != projectbooks.end()) {
orderedbooks.push_back (bk);
}
}
// Books in the Bible but not in the settings: Add them to the end.
for (int book : projectbooks) {
if (find (orderedbooks.begin(), orderedbooks.end(), book) == orderedbooks.end()) {
orderedbooks.push_back (book);
}
}
return orderedbooks;
}
|