The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
corediatheke.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * corediatheke.cpp -
4  *
5  * $Id: corediatheke.cpp 3579 2018-03-27 22:39:16Z scribe $
6  *
7  * Copyright 2001-2014 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 /******************************************************************************
24  * Diatheke by Chris Little <chrislit@crosswire.org>
25  * http://www.crosswire.org/sword/diatheke
26  */
27 
28 #include "corediatheke.h"
29 #include <regex.h>
30 #include <list>
31 #include <utilstr.h>
32 #include <versekey.h>
33 #include <swmodule.h>
34 
35 using std::list;
36 using std::cout;
37 using std::endl;
38 using std::ostream;
39 
40 void systemquery(const char * key, ostream* output){
41  DiathekeMgr manager;
42  ModMap::iterator it;
43 
44  SWModule *target;
45 
46  bool types = false, descriptions = false, names = false, bibliography = false;
47 
48  if (!::stricmp(key, "localelist")) {
50  list<SWBuf> loclist = lm->getAvailableLocales();
51  for (list<SWBuf>::iterator li = loclist.begin(); li != loclist.end(); ++li) {
52  *output << li->c_str() << endl;
53  }
54  }
55  else if (!::stricmp(key, "modulelist")) {
56  types = true;
57  descriptions = true;
58  names = true;
59  }
60  else if (!::stricmp(key, "modulelistnames")) {
61  names = true;
62  }
63  else if (!::stricmp(key, "modulelistdescriptions")) {
64  descriptions = true;
65  }
66  else if (!::stricmp(key, "bibliography")) {
67  types = true;
68  names = true;
69  bibliography = true;
70  }
71 
72  if (types || descriptions || names || bibliography) {
73  const char *modTypes[] = {
79  0
80  };
81 
82  for (int i = 0; modTypes[i]; ++i) {
83 
84  if (types) *output << modTypes[i] << ":\n";
85  for (it = manager.Modules.begin(); it != manager.Modules.end(); it++) {
86  target = it->second;
87  if (!strcmp(target->getType(), modTypes[i])) {
88  if (names) *output << target->getName();
89  if (names && (descriptions || bibliography)) *output << " : ";
90  if (descriptions) *output << target->getDescription();
91  if (bibliography) *output << target->getBibliography();
92  *output << endl;
93  }
94  }
95  }
96 
97  }
98 }
99 
100 
101 void doquery(unsigned long maxverses = -1, unsigned char outputformat = FMT_PLAIN, unsigned char outputencoding = ENC_UTF8, unsigned long optionfilters = 0, unsigned char searchtype = ST_NONE, const char *range = 0, const char *text = 0, const char *locale = 0, const char *ref = 0, ostream* output = &cout, const char *script = 0, signed char variants = 0) {
102 
103  static DiathekeMgr manager(NULL, NULL, false, outputencoding, outputformat, ((OP_BIDI & optionfilters) == OP_BIDI), ((OP_ARSHAPE & optionfilters) == OP_ARSHAPE));
104 
105  ListKey listkey;
106  const char *DEFAULT_FONT = "Gentium";
107  SWModule *target;
108 
109  const char *font = 0;
110  SWBuf modlanguage;
111  SWBuf modlocale;
112  SWBuf syslanguage;
113  SWBuf syslocale;
114  SWBuf header;
115 
116  char inputformat = 0;
117  char querytype = 0;
118 
119  if (!locale) { locale = "en";
120  }
121 
122 
123  syslocale = SWBuf(locale);
124  syslocale.append(".en");
126  lom->setDefaultLocaleName(syslocale);
127  syslanguage = lom->translate(syslocale, "locales");
128 
129 
130  //deal with queries to "system"
131  if (!::stricmp(text, "system")) {
132  querytype = QT_SYSTEM;
133  systemquery(ref, output);
134  }
135  if (!strnicmp(text, "info", 4)) {
136  querytype = QT_INFO;
137  text = ref;
138  }
139  //otherwise, we have a real book
140  target = manager.getModule(text);
141  if (!target) return;
142 
143  if (target->getLanguage()) {
144  modlocale = target->getLanguage();
146  modlanguage = lm->translate(modlocale.append(".en"), "locales");
147  modlocale -= 3;
148  }
149  else {
150  modlocale = "en";
151  modlanguage = "English";
152  }
153 
154 
155  SWBuf sourceType = target->getConfigEntry("SourceType");
156  if (sourceType == "GBF") inputformat = FMT_GBF;
157  else if (sourceType == "ThML") inputformat = FMT_THML;
158  else if (sourceType == "OSIS") inputformat = FMT_OSIS;
159  else if (sourceType == "TEI") inputformat = FMT_TEI;
160 
161  SWBuf encoding = target->getConfigEntry("Encoding");
162 
163 
164  if (querytype == QT_INFO) {
165  switch (inputformat) {
166  case FMT_THML : *output << "ThML"; break;
167  case FMT_GBF : *output << "GBF" ; break;
168  case FMT_OSIS : *output << "OSIS"; break;
169  case FMT_TEI : *output << "TEI" ; break;
170  default: *output << "Other";
171  }
172  *output << ";";
173  *output << target->getType();
174  *output << ";";
175  return;
176  }
177 
178  if (searchtype) querytype = QT_SEARCH;
179 
180  manager.setGlobalOption("Footnotes", (optionfilters & OP_FOOTNOTES) ? "On": "Off");
181  manager.setGlobalOption("Headings", (optionfilters & OP_HEADINGS) ? "On": "Off");
182  manager.setGlobalOption("Strong's Numbers", (optionfilters & OP_STRONGS) ? "On": "Off");
183  manager.setGlobalOption("Morphological Tags", (optionfilters & OP_MORPH) ? "On": "Off");
184  manager.setGlobalOption("Hebrew Cantillation", (optionfilters & OP_CANTILLATION) ? "On": "Off");
185  manager.setGlobalOption("Hebrew Vowel Points", (optionfilters & OP_HEBREWPOINTS) ? "On": "Off");
186  manager.setGlobalOption("Greek Accents", (optionfilters & OP_GREEKACCENTS) ? "On": "Off");
187  manager.setGlobalOption("Lemmas", (optionfilters & OP_LEMMAS) ? "On": "Off");
188  manager.setGlobalOption("Cross-references", (optionfilters & OP_SCRIPREF) ? "On": "Off");
189  manager.setGlobalOption("Words of Christ in Red", (optionfilters & OP_REDLETTERWORDS) ? "On": "Off");
190  manager.setGlobalOption("Arabic Vowel Points", (optionfilters & OP_ARABICPOINTS) ? "On": "Off");
191  manager.setGlobalOption("Glosses", (optionfilters & OP_GLOSSES) ? "On": "Off");
192  manager.setGlobalOption("Transliterated Forms", (optionfilters & OP_XLIT) ? "On": "Off");
193  manager.setGlobalOption("Enumerations", (optionfilters & OP_ENUM) ? "On": "Off");
194  manager.setGlobalOption("Morpheme Segmentation", (optionfilters & OP_MORPHSEG) ? "On": "Off");
195  manager.setGlobalOption("Transliteration", (optionfilters & OP_TRANSLITERATOR && script) ? script : "Off");
196 
197  VerseKey *parser = (SWDYNAMIC_CAST(VerseKey, target->getKey())) ? (VerseKey *)target->createKey() : 0;
198  if (parser && (optionfilters & OP_INTROS)) { parser->setIntros(true); ((VerseKey *)target->getKey())->setIntros(true); }
199 
200 
201  if ((optionfilters & OP_VARIANTS) && variants) {
202  if (variants == -1)
203  manager.setGlobalOption("Textual Variants", "All Readings");
204  else if (variants == 1)
205  manager.setGlobalOption("Textual Variants", "Secondary Reading");
206  }
207  else manager.setGlobalOption("Textual Variants", "Primary Reading");
208 
209 
210  if (querytype == QT_SEARCH) {
211  //do search stuff
212  char st = 1 - searchtype;
213  if (querytype == QT_BIBLE) {
214  *output << "Verses containing \"";
215  }
216  else *output << "Entries containing \"";
217  *output << ref;
218  *output << "\"-- ";
219 
220  if (range && parser) {
221  ListKey scope = parser->parseVerseList(range, "Gen 1:1", true);
222  listkey = target->search(ref, st, REG_ICASE, &scope);
223  }
224  else listkey = target->search(ref, st, REG_ICASE);
225 
226  bool first = true;
227  if (listkey.getCount()) {
228  for (listkey = TOP; !listkey.popError(); listkey++) {
229  if (!listkey.popError()) {
230  if (outputformat == FMT_CGI) *output << "<entry>";
231  *output << listkey.getText();
232  if (outputformat == FMT_CGI) *output << "</entry>";
233  }
234  if (first) first = false;
235  else *output << " ; ";
236  }
237  *output << " -- ";
238 
239  *output << listkey.getCount() << " matches total (";
240  }
241  else {
242  *output << "none (";
243  }
244  *output << target->getName();
245  *output << ")\n";
246  }
247  else {
248 
249  if (parser) {
250  listkey = parser->parseVerseList(ref, "Gen1:1", true);
251  }
252  else listkey << ref;
253 
254  font = target->getConfigEntry("Font");
255  if (!font) font = DEFAULT_FONT;
256 
257  if (outputformat == FMT_RTF) {
258  *output << "{\\rtf1\\ansi{\\fonttbl{\\f0\\froman\\fcharset0\\fprq2 Times New Roman;}{\\f1\\fdecor\\fprq2 ";
259  *output << font;
260  *output << ";}{\\f7\\froman\\fcharset2\\fprq2 Symbol;}}";
261  }
262 
263  else if (outputformat == FMT_LATEX) {
264  *output << "\\documentclass{bibletext}\n"
265  "\\usepackage{sword}\n"
266  "\\title{" << target->getDescription() << " \\\\\\small " << ref << "}\n";
267 
268  if (syslanguage.size()) {
269  syslanguage[0] = tolower(syslanguage[0]);
270  *output << "\\setmainlanguage{" << syslanguage << "}\n";
271  }
272 
273  if (modlanguage.size()) {
274  modlanguage[0] = tolower(modlanguage[0]);
275  }
276  else {
277  modlanguage = "english";
278  }
279 
280  if (!(modlanguage == syslanguage)) {
281  *output << "\\setotherlanguage{" << modlanguage << "}\n";
282  }
283 
284 
285  *output << "\\date{}\n"
286  "\\begin{document}\n"
287  "\\maketitle\n";
288 
289  if (!(modlanguage == syslanguage)) {
290  *output << "\\begin{" << modlanguage << "}\n";
291  }
292  }
293 
294 
295  else if (outputformat == FMT_HTML || outputformat == FMT_HTMLHREF || outputformat == FMT_XHTML) {
296  *output << "<html><head><meta http-equiv=\"content-type\" content=\"text/html\" charset=\"UTF-8\""
297  " lang=\"" << locale << "\" xml:lang=\"" << locale << "\"/>\n"
298  "<style type=\"text/css\">" << target->getRenderHeader() << "</style></head><body>";
299 
300  }
301 
302  for (listkey = TOP; !listkey.popError() && maxverses; listkey++) {
303  target->setKey(listkey);
304  VerseKey *vk = SWDYNAMIC_CAST(VerseKey, target->getKey());
305  // Call this before all the pre-verse handling, as it needs to be
306  // executed before the preverse headers are populated
307  SWBuf text = target->renderText();
308 
309  // if we've got a VerseKey (Bible or Commentary)
310  if (vk) {
311  // let's do some special processing if we're at verse 1
312  if (vk->getVerse() == 1) {
313  if (vk->getChapter() == 1) {
314  if (vk->getBook() == 1) {
315  if (vk->getTestament() == 1) {
316  // MODULE START SPECIAL PROCESSING
317  if (outputformat == FMT_LATEX)
318  { *output << "\\swordmodule\n";
319  // put your latex module start stuff here
320  }
321  }
322  // TESTAMENT START SPECIAL PROCESSING
323  if (outputformat == FMT_LATEX) {
324  // put your latex testament start stuff here
325  *output << "\\swordtestament\n";
326  }
327  }
328  // BOOK START SPECIAL PROCESSING
329  if (outputformat == FMT_LATEX) {
330  // put your latex book start stuff here
331  *output << "\\swordbook\n";
332  }
333  }
334  // CHAPTER START SPECIAL PROCESSING
335  if (outputformat == FMT_LATEX) {
336  *output << "\n\\swordchapter{"
337  << vk->getOSISRef() << "}{"
338  << vk->getText() << "}{"
339  << vk->getChapter() << "}";
340  }
341  }
342 
343  // PREVERSE MATTER
344  header = target->getEntryAttributes()["Heading"]["Preverse"]["0"];
345  *output << target->renderText(header);
346 
347  // VERSE PROCESSING
348  if (outputformat == FMT_LATEX) {
349  *output << "\\swordverse{"
350  << vk->getOSISRef() << "}{"
351  << vk->getText() << "}{"
352  << vk->getVerse() << "} ";
353  }
354  // no special format processing default: just show the key
355  else {
356  *output << target->getKeyText();
357  }
358  }
359  // if we're not a VerseKey, then simply output the key
360  else {
361  *output << target->getKeyText();
362  }
363 
364  // OUTPUT RENDER ENTRY
365  if (outputformat == FMT_HTML || outputformat == FMT_HTMLHREF || outputformat == FMT_XHTML || outputformat == FMT_THML || outputformat == FMT_CGI) {
366  *output << ": <span ";
367  *output << "style=\"font:" << font << ";\" ";
368  if (strcmp(modlocale,locale) !=0 ) { *output << "lang=\"" << modlocale << "\"";}
369  *output << ">";
370  }
371  else if (outputformat == FMT_RTF) {
372  *output << ": {\\f1 ";
373  }
374  else if (outputformat == FMT_LATEX) {
375  *output << " ";
376  }
377  else {
378  *output << ": ";
379  }
380 
381  *output << text;
382 
383 
384  if (outputformat == FMT_HTML || outputformat == FMT_HTMLHREF || outputformat == FMT_XHTML || outputformat == FMT_THML || outputformat == FMT_CGI) {
385  *output << "</span>";
386  }
387  else if (outputformat == FMT_RTF) {
388  *output << "}";
389  }
390 
391  if (inputformat != FMT_THML && (outputformat == FMT_HTML || outputformat == FMT_HTMLHREF || outputformat == FMT_XHTML || outputformat == FMT_THML || outputformat == FMT_CGI))
392  *output << "<br />";
393  else if (outputformat == FMT_OSIS)
394  *output << "<milestone type=\"line\"/>";
395  else if (outputformat == FMT_RTF)
396  *output << "\\par ";
397  else if (outputformat == FMT_GBF)
398  *output << "<CM>";
399 
400  *output << "\n";
401 
402  maxverses--;
403  }
404 
405  if ((outputformat == FMT_LATEX) && (!(modlanguage == syslanguage))) {
406  *output << "\\end{" << modlanguage << "}\n";
407  }
408 
409 
410  *output << "(";
411  *output << target->getName();
412 
413  if (outputformat == FMT_LATEX) {
414  *output << ", ";
415  *output << target->getConfigEntry("DistributionLicense");
416 
417  }
418 
419  *output << ")\n";
420 
421  if (outputformat == FMT_RTF) {
422  *output << "}";
423  }
424  else if (outputformat == FMT_LATEX) {
425  *output << "\\end{document}\n";
426  }
427  else if (outputformat == FMT_HTML || outputformat == FMT_HTMLHREF || outputformat == FMT_XHTML || outputformat == FMT_THML || outputformat == FMT_CGI) {
428  *output << "</body></html>\n";
429  }
430  }
431  delete parser;
432 }
433 
#define TOP
Definition: swkey.h:68
#define QT_BIBLE
Definition: corediatheke.h:35
virtual void setDefaultLocaleName(const char *name)
Definition: localemgr.cpp:251
static const char * MODTYPE_GENBOOKS
Definition: swmgr.h:276
#define OP_ARSHAPE
Definition: corediatheke.h:53
#define OP_VARIANTS
Definition: corediatheke.h:55
static const char * MODTYPE_LEXDICTS
Definition: swmgr.h:275
#define OP_MORPH
Definition: corediatheke.h:46
#define OP_STRONGS
Definition: corediatheke.h:43
#define REG_ICASE
Definition: regex.h:261
#define QT_SYSTEM
Definition: corediatheke.h:39
int stricmp(const char *s1, const char *s2)
Definition: utilstr.cpp:194
#define OP_GLOSSES
Definition: corediatheke.h:58
#define OP_LEMMAS
Definition: corediatheke.h:51
static const char * MODTYPE_BIBLES
Definition: swmgr.h:273
int range
Definition: regex.c:5043
#define OP_ARABICPOINTS
Definition: corediatheke.h:57
#define OP_CANTILLATION
Definition: corediatheke.h:47
#define OP_BIDI
Definition: corediatheke.h:54
return NULL
Definition: regex.c:7953
#define FMT_CGI
Definition: diafiltmgr.h:26
#define OP_XLIT
Definition: corediatheke.h:59
static LocaleMgr * getSystemLocaleMgr()
Definition: localemgr.cpp:54
static const char * MODTYPE_COMMENTARIES
Definition: swmgr.h:274
#define OP_INTROS
Definition: corediatheke.h:62
void systemquery(const char *key, ostream *output)
#define QT_SEARCH
Definition: corediatheke.h:38
#define QT_INFO
Definition: corediatheke.h:40
virtual const char * translate(const char *text, const char *localeName=0)
Definition: localemgr.cpp:234
#define SWDYNAMIC_CAST(className, object)
Definition: defs.h:47
#define OP_SCRIPREF
Definition: corediatheke.h:52
#define OP_REDLETTERWORDS
Definition: corediatheke.h:56
static const char * MODTYPE_DAILYDEVOS
Definition: swmgr.h:277
#define OP_HEADINGS
Definition: corediatheke.h:45
#define OP_HEBREWPOINTS
Definition: corediatheke.h:48
virtual StringList getAvailableLocales()
Definition: localemgr.cpp:222
#define OP_FOOTNOTES
Definition: corediatheke.h:44
int strnicmp(const char *s1, const char *s2, int len)
Definition: utilstr.cpp:180
#define OP_GREEKACCENTS
Definition: corediatheke.h:49
#define OP_MORPHSEG
Definition: corediatheke.h:61
#define ST_NONE
Definition: corediatheke.h:64
void doquery(unsigned long maxverses=-1, unsigned char outputformat=FMT_PLAIN, unsigned char outputencoding=ENC_UTF8, unsigned long optionfilters=0, unsigned char searchtype=ST_NONE, const char *range=0, const char *text=0, const char *locale=0, const char *ref=0, ostream *output=&cout, const char *script=0, signed char variants=0)
#define OP_TRANSLITERATOR
Definition: corediatheke.h:50
#define OP_ENUM
Definition: corediatheke.h:60