Bibletime – a bible study tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cswordkey.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*********
  2. *
  3. * This file is part of BibleTime's source code, http://www.bibletime.info/.
  4. *
  5. * Copyright 1999-2006 by the BibleTime developers.
  6. * The BibleTime source code is licensed under the GNU General Public License version 2.0.
  7. *
  8. **********/
  9. //own includes
  10. #include "cswordkey.h"
  11. #include "cswordmoduleinfo.h"
  12. #include "cswordversekey.h"
  13. #include "cswordldkey.h"
  14. #include "cswordtreekey.h"
  15. #include "util/ctoolclass.h"
  16. //Sword includes
  17. #include <swmodule.h>
  18. #include <swkey.h>
  19. #include <versekey.h>
  20. #include <treekey.h>
  21. #include <treekeyidx.h>
  22. //Qt includes
  23. #include <tqregexp.h>
  24. #include <tqstring.h>
  25. #include <tqtextcodec.h>
  26. CSwordKey::CSwordKey(CSwordModuleInfo* const module) : m_module(module) {}
  27. CSwordKey::CSwordKey(const CSwordKey& k) {
  28. m_module = k.m_module;
  29. }
  30. const TQString CSwordKey::rawText() {
  31. if (!m_module) {
  32. return TQString::null;
  33. }
  34. if (dynamic_cast<sword::SWKey*>(this)) {
  35. char * buffer = new char[strlen(rawKey()) + 1];
  36. strcpy(buffer, rawKey());
  37. m_module->module()->getKey()->setText( buffer );
  38. delete buffer;
  39. }
  40. if (key().isNull()) {
  41. return TQString::null;
  42. }
  43. // qWarning("rawText: %s", m_module->module()->getRawEntry());
  44. return TQString::fromUtf8( m_module->module()->getRawEntry() );
  45. }
  46. const TQString CSwordKey::renderedText( const CSwordKey::TextRenderType mode ) {
  47. Q_ASSERT(m_module);
  48. if (!m_module) {
  49. return TQString::null;
  50. }
  51. using namespace sword;
  52. SWKey* const k = dynamic_cast<SWKey*>(this);
  53. if (k) {
  54. char * buffer = new char[strlen(rawKey()) + 1];
  55. strcpy(buffer, rawKey());
  56. VerseKey* vk_mod = dynamic_cast<VerseKey*>(m_module->module()->getKey());
  57. if (vk_mod) {
  58. vk_mod->Headings(1);
  59. }
  60. m_module->module()->getKey()->setText( buffer );
  61. if (m_module->type() == CSwordModuleInfo::Lexicon) {
  62. m_module->snap();
  63. /* In lexicons make sure that our key (e.g. 123) was successfully set to the module,
  64. i.e. the module key contains this key (e.g. 0123 contains 123) */
  65. if ( strcasecmp(m_module->module()->getKey()->getText(), buffer)
  66. && !strstr(m_module->module()->getKey()->getText(), buffer)
  67. ) {
  68. qDebug("return an empty key for %s", m_module->module()->getKey()->getText());
  69. return TQString::null;
  70. }
  71. }
  72. delete buffer;
  73. }
  74. Q_ASSERT(!key().isNull());
  75. if (!key().isNull()) { //we have valid text
  76. TQString text = TQString::fromUtf8( m_module->module()->RenderText() );
  77. // This is yucky, but if we want strong lexicon refs we have to do it here.
  78. if (m_module->type() == CSwordModuleInfo::Lexicon) {
  79. TQString t(text);
  80. TQRegExp rx("(GREEK|HEBREW) for 0*([1-9]\\d*)"); // ignore 0's before number
  81. int pos = 0;
  82. while( (pos = rx.search(t, pos)) != -1 ) {
  83. TQString language = rx.cap(1);
  84. TQString langcode = TQString(language.at(0)); // "G" or "H"
  85. TQString number = rx.cap(2);
  86. TQString paddednumber = number.rightJustify(5, '0'); // Form 00123
  87. text.replace(
  88. TQRegExp( TQString(
  89. "(>[^<>]+)" // Avoid replacing inside tags
  90. "\\b(0*%1)\\b").arg(number) ), // And span around 0's
  91. TQString("\\1<span lemma=\"%1%2\"><a href=\"strongs://%3/%4\">\\2</a></span>")
  92. .arg(langcode, paddednumber, language, paddednumber)
  93. );
  94. pos += rx.matchedLength();
  95. }
  96. }
  97. if (mode == HTMLEscaped) {
  98. //we have to encode all UTF-8 in HTML escapes
  99. // go though every character and write down the escaped HTML unicode entity
  100. // form is &#<decimal unicode value here>;
  101. TQString ret;
  102. TQChar c;
  103. const unsigned int length = text.length();
  104. for (unsigned int i = 0; i < length; ++i) {
  105. c = text.at(i);
  106. if (c.latin1()) { //normal latin1 character
  107. ret.append(c);
  108. }
  109. else {//unicode character, needs to be escaped
  110. ret.append("&#")
  111. .append(c.unicode())
  112. .append(";");
  113. }
  114. }
  115. return ret;
  116. }
  117. else {
  118. return text;
  119. }
  120. }
  121. return TQString::null;
  122. }
  123. const TQString CSwordKey::strippedText() {
  124. if (!m_module) {
  125. return TQString::null;
  126. }
  127. if (/*sword::SWKey* k =*/ dynamic_cast<sword::SWKey*>(this)) {
  128. // m_module->module()->SetKey(k);
  129. //m_module->module()->getKey()->setText( (const char*)key().utf8() );
  130. char * buffer = new char[strlen(rawKey()) + 1];
  131. strcpy(buffer, rawKey());
  132. m_module->module()->getKey()->setText(buffer);
  133. delete buffer;
  134. }
  135. return TQString::fromUtf8( m_module->module()->StripText() );
  136. }
  137. /** This will create a proper key object from a given module */
  138. CSwordKey* CSwordKey::createInstance( CSwordModuleInfo* const module ) {
  139. if (!module) {
  140. return 0;
  141. }
  142. switch( module->type() ) {
  143. case CSwordModuleInfo::Bible://fall through
  144. case CSwordModuleInfo::Commentary:
  145. return new CSwordVerseKey( (sword::VerseKey *) ( (sword::SWKey *)(*module->module()) ), module );
  146. case CSwordModuleInfo::Lexicon:
  147. return new CSwordLDKey( (sword::SWKey *)(*module->module()), module);
  148. case CSwordModuleInfo::GenericBook:
  149. return new CSwordTreeKey( (sword::TreeKeyIdx*)((sword::SWKey *)(*module->module())), module );
  150. default:
  151. return 0;
  152. }
  153. }