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.

bt_thmlhtml.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. //BibleTime includes
  10. #include "backend/bt_thmlhtml.h"
  11. #include "backend/clanguagemgr.h"
  12. #include "backend/cswordmoduleinfo.h"
  13. #include "backend/creferencemanager.h"
  14. #include "frontend/cbtconfig.h"
  15. #include "util/cpointers.h"
  16. #include "util/scoped_resource.h"
  17. #include <iostream>
  18. //Sword includes
  19. #include <swmodule.h>
  20. #include <utilxml.h>
  21. #include "versekey.h"
  22. //Qt includes
  23. #include <tqstring.h>
  24. #include <tqregexp.h>
  25. //System includes
  26. #include <stdlib.h>
  27. using namespace Filters;
  28. BT_ThMLHTML::BT_ThMLHTML() {
  29. setEscapeStringCaseSensitive(true);
  30. setPassThruUnknownEscapeString(true); //the HTML widget will render the HTML escape codes
  31. setTokenStart("<");
  32. setTokenEnd(">");
  33. setTokenCaseSensitive(true);
  34. addTokenSubstitute("/foreign", "</span>");
  35. removeTokenSubstitute("note");
  36. removeTokenSubstitute("/note");
  37. }
  38. char BT_ThMLHTML::processText(sword::SWBuf& buf, const sword::SWKey* key, const sword::SWModule* module) {
  39. ThMLHTML::processText(buf, key, module);
  40. CSwordModuleInfo* m = CPointers::backend()->findModuleByName( module->Name() );
  41. if (m && !(m->has(CSwordModuleInfo::lemmas) || m->has(CSwordModuleInfo::strongNumbers))) { //only parse if the module has strongs or lemmas
  42. return 1;
  43. }
  44. TQString result;
  45. TQString t = TQString::fromUtf8(buf.c_str());
  46. TQRegExp tag("([.,;]?<sync[^>]+(type|value)=\"([^\"]+)\"[^>]+(type|value)=\"([^\"]+)\"([^<]*)>)+");
  47. TQStringList list;
  48. int lastMatchEnd = 0;
  49. int pos = tag.search(t,0);
  50. if (pos == -1) { //no strong or morph code found in this text
  51. return 1; //WARNING: Return alread here
  52. }
  53. while (pos != -1) {
  54. list.append(t.mid(lastMatchEnd, pos+tag.matchedLength()-lastMatchEnd));
  55. lastMatchEnd = pos+tag.matchedLength();
  56. pos = tag.search(t,pos+tag.matchedLength());
  57. }
  58. if (!t.right(t.length() - lastMatchEnd).isEmpty()) {
  59. list.append(t.right(t.length() - lastMatchEnd));
  60. }
  61. tag = TQRegExp("<sync[^>]+(type|value|class)=\"([^\"]+)\"[^>]+(type|value|class)=\"([^\"]+)\"[^>]+((type|value|class)=\"([^\"]+)\")*([^<]*)>");
  62. for (TQStringList::iterator it = list.begin(); it != list.end(); ++it) {
  63. TQString e( *it );
  64. const bool textPresent = (e.stripWhiteSpace().remove(TQRegExp("[.,;:]")).left(1) != "<");
  65. if (!textPresent) {
  66. continue;
  67. }
  68. bool hasLemmaAttr = false;
  69. bool hasMorphAttr = false;
  70. int pos = tag.search(e, 0);
  71. bool insertedTag = false;
  72. TQString value;
  73. TQString valueClass;
  74. while (pos != -1) {
  75. bool isMorph = false;
  76. bool isStrongs = false;
  77. value = TQString::null;
  78. valueClass = TQString::null;
  79. // check 3 attribute/value pairs
  80. for (int i = 1; i < 6; i += 2) {
  81. if (i > 4)
  82. i++;
  83. if (tag.cap(i) == "type") {
  84. isMorph = (tag.cap(i+1) == "morph");
  85. isStrongs = (tag.cap(i+1) == "Strongs");
  86. }
  87. else if (tag.cap(i) == "value") {
  88. value = tag.cap(i+1);
  89. }
  90. else if (tag.cap(i) == "class") {
  91. valueClass = tag.cap(i+1);
  92. }
  93. }
  94. // prepend the class qualifier to the value
  95. if (!valueClass.isEmpty()) {
  96. value = valueClass + ":" + value;
  97. // value.append(":").append(value);
  98. }
  99. if (value.isEmpty()) {
  100. break;
  101. }
  102. //insert the span
  103. if (!insertedTag) {
  104. e.replace(pos, tag.matchedLength(), "</span>");
  105. pos += 7;
  106. TQString rep;
  107. rep.setLatin1("<span lemma=\"").append(value).append("\">");
  108. int startPos = 0;
  109. TQChar c = e[startPos];
  110. while ((startPos < pos) && (c.isSpace() || c.isPunct())) {
  111. ++startPos;
  112. c = e[startPos];
  113. }
  114. hasLemmaAttr = isStrongs;
  115. hasMorphAttr = isMorph;
  116. e.insert( startPos, rep );
  117. pos += rep.length();
  118. }
  119. else { //add the attribute to the existing tag
  120. e.remove(pos, tag.matchedLength());
  121. if ((!isMorph && hasLemmaAttr) || (isMorph && hasMorphAttr)) { //we append another attribute value, e.g. 3000 gets 3000|5000
  122. //search the existing attribute start
  123. TQRegExp attrRegExp( isMorph ? "morph=\".+(?=\")" : "lemma=\".+(?=\")" );
  124. attrRegExp.setMinimal(true);
  125. const int foundAttrPos = e.find(attrRegExp, pos);
  126. if (foundAttrPos != -1) {
  127. e.insert(foundAttrPos + attrRegExp.matchedLength(), TQString("|").append(value));
  128. pos += value.length() + 1;
  129. hasLemmaAttr = !isMorph;
  130. hasMorphAttr = isMorph;
  131. }
  132. }
  133. else { //attribute was not yet inserted
  134. const int attrPos = e.find(TQRegExp("morph=|lemma="), 0);
  135. if (attrPos >= 0) {
  136. TQString attr;
  137. attr.append(isMorph ? "morph" : "lemma").append("=\"").append(value).append("\" ");
  138. e.insert(attrPos, attr);
  139. hasMorphAttr = isMorph;
  140. hasLemmaAttr = !isMorph;
  141. pos += attr.length();
  142. }
  143. }
  144. }
  145. insertedTag = true;
  146. pos = tag.search(e, pos);
  147. }
  148. result.append( e );
  149. }
  150. if (list.count()) {
  151. buf = (const char*)result.utf8();
  152. }
  153. return 1;
  154. }
  155. bool BT_ThMLHTML::handleToken(sword::SWBuf &buf, const char *token, sword::BasicFilterUserData *userData) {
  156. if (!substituteToken(buf, token) && !substituteEscapeString(buf, token)) {
  157. sword::XMLTag tag(token);
  158. BT_UserData* myUserData = dynamic_cast<BT_UserData*>(userData);
  159. sword::SWModule* myModule = const_cast<sword::SWModule*>(myUserData->module); //hack to be able to call stuff like Lang()
  160. if ( tag.getName() && !strcasecmp(tag.getName(), "foreign") ) { // a text part in another language, we have to set the right font
  161. if (tag.getAttribute("lang")) {
  162. const char* abbrev = tag.getAttribute("lang");
  163. //const CLanguageMgr::Language* const language = CPointers::languageMgr()->languageForAbbrev( TQString::fromLatin1(abbrev) );
  164. buf.append("<span class=\"foreign\" lang=\"");
  165. buf.append(abbrev);
  166. buf.append("\">");
  167. }
  168. }
  169. else if (tag.getName() && !strcasecmp(tag.getName(), "sync")) { //lemmas, morph codes or strongs
  170. if (tag.getAttribute("type") && (!strcasecmp(tag.getAttribute("type"), "morph") || !strcasecmp(tag.getAttribute("type"), "Strongs") || !strcasecmp(tag.getAttribute("type"), "lemma"))) { // Morph or Strong
  171. buf.append('<');
  172. buf.append(token);
  173. buf.append('>');
  174. }
  175. }
  176. else if (tag.getName() && !strcasecmp(tag.getName(), "note")) { // <note> tag
  177. if (!tag.isEndTag() && !tag.isEmpty()) {
  178. //appending is faster than appendFormatted
  179. buf.append(" <span class=\"footnote\" note=\"");
  180. buf.append(myModule->Name());
  181. buf.append('/');
  182. buf.append(myUserData->key->getShortText());
  183. buf.append('/');
  184. buf.append( TQString::number(myUserData->swordFootnote++).latin1() );
  185. buf.append("\">*</span> ");
  186. myUserData->suspendTextPassThru = true;
  187. myUserData->inFootnoteTag = true;
  188. }
  189. else if (tag.isEndTag() && !tag.isEmpty()) { //end tag
  190. //buf += ")</span>";
  191. myUserData->suspendTextPassThru = false;
  192. myUserData->inFootnoteTag = false;
  193. }
  194. }
  195. else if (tag.getName() && !strcasecmp(tag.getName(), "scripRef")) { // a scripRef
  196. //scrip refs which are embeded in footnotes may not be displayed!
  197. if (!myUserData->inFootnoteTag) {
  198. if (tag.isEndTag()) {
  199. if (myUserData->inscriptRef) { // like "<scripRef passage="John 3:16">See John 3:16</scripRef>"
  200. buf.append("</a></span>");
  201. myUserData->inscriptRef = false;
  202. myUserData->suspendTextPassThru = false;
  203. }
  204. else { // like "<scripRef>John 3:16</scripRef>"
  205. CSwordModuleInfo* mod = CBTConfig::get(CBTConfig::standardBible);
  206. Q_ASSERT(mod);
  207. if (mod) {
  208. CReferenceManager::ParseOptions options;
  209. options.refBase = TQString::fromUtf8(myUserData->key->getText()); //current module key
  210. options.refDestinationModule = TQString(mod->name());
  211. options.sourceLanguage = TQString(myModule->Lang());
  212. options.destinationLanguage = TQString("en");
  213. //it's ok to split the reference, because to descriptive text is given
  214. bool insertSemicolon = false;
  215. buf.append("<span class=\"crossreference\">");
  216. TQStringList refs = TQStringList::split(";", TQString::fromUtf8(myUserData->lastTextNode.c_str()));
  217. TQString oldRef; //the previous reference to use as a base for the next refs
  218. for (TQStringList::iterator it(refs.begin()); it != refs.end(); ++it) {
  219. if (! oldRef.isEmpty() ){
  220. options.refBase = oldRef; //use the last ref as a base, e.g. Rom 1,2-3, when the next ref is only 3:3-10
  221. }
  222. const TQString completeRef( CReferenceManager::parseVerseReference((*it), options) );
  223. oldRef = completeRef; //use the parsed result as the base for the next ref.
  224. if (insertSemicolon) { //prepend a ref divider if we're after the first one
  225. buf.append("; ");
  226. }
  227. buf.append("<a href=\"");
  228. buf.append(
  229. CReferenceManager::encodeHyperlink(
  230. mod->name(),
  231. completeRef,
  232. CReferenceManager::typeFromModule(mod->type())
  233. ).utf8()
  234. );
  235. buf.append("\" crossrefs=\"");
  236. buf.append((const char*)completeRef.utf8());
  237. buf.append("\">");
  238. buf.append((const char*)(*it).utf8());
  239. buf.append("</a>");
  240. insertSemicolon = true;
  241. }
  242. buf.append("</span>"); //crossref end
  243. }
  244. myUserData->suspendTextPassThru = false;
  245. }
  246. }
  247. else if (tag.getAttribute("passage") ) { //the passage was given as a parameter value
  248. myUserData->inscriptRef = true;
  249. myUserData->suspendTextPassThru = false;
  250. const char* ref = tag.getAttribute("passage");
  251. Q_ASSERT(ref);
  252. CSwordModuleInfo* mod = CBTConfig::get(CBTConfig::standardBible);
  253. Q_ASSERT(mod);
  254. CReferenceManager::ParseOptions options;
  255. options.refBase = TQString::fromUtf8(myUserData->key->getText());
  256. options.refDestinationModule = TQString(mod->name());
  257. options.sourceLanguage = myModule->Lang();
  258. options.destinationLanguage = TQString("en");
  259. const TQString completeRef = CReferenceManager::parseVerseReference(TQString::fromUtf8(ref), options);
  260. if (mod) {
  261. buf.append("<span class=\"crossreference\">");
  262. buf.append("<a href=\"");
  263. buf.append(
  264. CReferenceManager::encodeHyperlink(
  265. mod->name(),
  266. completeRef,
  267. CReferenceManager::typeFromModule(mod->type())
  268. ).utf8()
  269. );
  270. buf.append("\" crossrefs=\"");
  271. buf.append((const char*)completeRef.utf8());
  272. buf.append("\">");
  273. }
  274. else {
  275. buf.append("<span><a>");
  276. }
  277. }
  278. else if ( !tag.getAttribute("passage") ) { // we're starting a scripRef like "<scripRef>John 3:16</scripRef>"
  279. myUserData->inscriptRef = false;
  280. // let's stop text from going to output, the text get's added in the -tag handler
  281. myUserData->suspendTextPassThru = true;
  282. }
  283. }
  284. }
  285. else if (tag.getName() && !strcasecmp(tag.getName(), "div")) {
  286. if (tag.isEndTag()) {
  287. buf.append("</div>");
  288. }
  289. else if ( tag.getAttribute("class") && !strcasecmp(tag.getAttribute("class"),"sechead") ) {
  290. buf.append("<div class=\"sectiontitle\">");
  291. }
  292. else if (tag.getAttribute("class") && !strcasecmp(tag.getAttribute("class"), "title")) {
  293. buf.append("<div class=\"booktitle\">");
  294. }
  295. }
  296. else if (tag.getName() && !strcasecmp(tag.getName(), "img") && tag.getAttribute("src")) {
  297. const char* value = tag.getAttribute("src");
  298. if (value[0] == '/') {
  299. value++; //strip the first /
  300. }
  301. buf.append("<img src=\"file:");
  302. buf.append(myUserData->module->getConfigEntry("AbsoluteDataPath"));
  303. buf.append('/');
  304. buf.append(value);
  305. buf.append("\" />");
  306. }
  307. else { // let unknown token pass thru
  308. return sword::ThMLHTML::handleToken(buf, token, userData);
  309. }
  310. }
  311. return true;
  312. }