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.

chtmlexportrendering.cpp 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // C++ Implementation: chtmlexportrendering
  3. //
  4. // Description:
  5. //
  6. //
  7. // Author: The BibleTime team <info@bibletime.info>, (C) 2004
  8. //
  9. // Copyright: See COPYING file that comes with this distribution
  10. //
  11. //
  12. //Backend
  13. #include "chtmlexportrendering.h"
  14. #include "cdisplaytemplatemgr.h"
  15. #include "clanguagemgr.h"
  16. #include "cswordkey.h"
  17. #include "cswordversekey.h"
  18. #include "cswordmoduleinfo.h"
  19. //Util
  20. #include "util/cpointers.h"
  21. #include "util/scoped_resource.h"
  22. //KDE includes
  23. #include <klocale.h>
  24. namespace Rendering {
  25. CHTMLExportRendering::CHTMLExportRendering(const CHTMLExportRendering::Settings& settings, CSwordBackend::DisplayOptions displayOptions, CSwordBackend::FilterOptions filterOptions)
  26. : m_displayOptions(displayOptions),
  27. m_filterOptions(filterOptions),
  28. m_settings(settings) {}
  29. CHTMLExportRendering::~CHTMLExportRendering() {}
  30. const TQString CHTMLExportRendering::renderEntry( const KeyTreeItem& i, CSwordKey* k) {
  31. // qDebug("CHTMLExportRendering::renderEntry");
  32. if (i.hasAlternativeContent()) {
  33. TQString ret;
  34. ret.setLatin1(i.settings().highlight ? "<div class=\"currententry\">" : "<div class=\"entry\">");
  35. ret.append(i.getAlternativeContent());
  36. // Q_ASSERT(i.hasChildItems());
  37. if (i.hasChildItems()) {
  38. KeyTree const * tree = i.childList();
  39. const ListCSwordModuleInfo& modules( tree->collectModules() );
  40. if (modules.count() == 1) { //insert the direction into the sorrounding div
  41. ret.insert( 5, TQString("dir=\"%1\" ").arg((modules.first()->textDirection() == CSwordModuleInfo::LeftToRight) ? "ltr" : "rtl" ));
  42. }
  43. for ( KeyTreeItem* c = tree->first(); c; c = tree->next() ) {
  44. ret.append( renderEntry( *c ) );
  45. }
  46. }
  47. ret.append("</div>");
  48. return ret; //WARNING: Return already here!
  49. }
  50. const ListCSwordModuleInfo& modules( i.modules() );
  51. Q_ASSERT(modules.count() >= 1);
  52. util::scoped_ptr<CSwordKey> scoped_key( !k ? CSwordKey::createInstance(modules.first()) : 0 );
  53. CSwordKey* key = k ? k : scoped_key;
  54. Q_ASSERT(key);
  55. CSwordVerseKey* myVK = dynamic_cast<CSwordVerseKey*>(key);
  56. if ( myVK ) {
  57. myVK->Headings(1);
  58. }
  59. TQString renderedText( (modules.count() > 1) ? "<tr>" : "" );
  60. if (modules.count() == 0) {
  61. return TQString(""); //no module present for rendering
  62. }
  63. // Only insert the table stuff if we are displaying parallel.
  64. // Otherwise, strip out he table stuff -> the whole chapter will be rendered in one cell!
  65. //declarations out of the loop for optimization
  66. TQString entry;
  67. TQString keyText;
  68. bool isRTL;
  69. //taken out of the loop for optimization
  70. TQString preverseHeading;
  71. TQString langAttr;
  72. ListCSwordModuleInfo::const_iterator end_modItr = modules.end();
  73. for (ListCSwordModuleInfo::const_iterator mod_Itr(modules.begin()); mod_Itr != end_modItr; ++mod_Itr) {
  74. key->module(*mod_Itr);
  75. key->key( i.key() );
  76. keyText = key->key();
  77. isRTL = ((*mod_Itr)->textDirection() == CSwordModuleInfo::RightToLeft);
  78. entry = TQString::null;
  79. if ((*mod_Itr)->language()->isValid()) {
  80. langAttr.setLatin1("xml:lang=\"")
  81. .append((*mod_Itr)->language()->abbrev())
  82. .append("\" lang=\"")
  83. .append((*mod_Itr)->language()->abbrev())
  84. .append("\"");
  85. }
  86. else {
  87. langAttr.setLatin1("xml:lang=\"")
  88. .append((*mod_Itr)->module()->Lang())
  89. .append("\" lang=\"")
  90. .append((*mod_Itr)->module()->Lang())
  91. .append("\"");
  92. }
  93. const TQString key_renderedText = key->renderedText();
  94. // qWarning(key_renderedText.latin1());
  95. if (m_filterOptions.headings) {
  96. AttributeValue::const_iterator it =
  97. (*mod_Itr)->module()->getEntryAttributes()["Heading"]["Preverse"].begin();
  98. const AttributeValue::const_iterator end =
  99. (*mod_Itr)->module()->getEntryAttributes()["Heading"]["Preverse"].end();
  100. for (; it != end; ++it) {
  101. preverseHeading = TQString::fromUtf8(it->second.c_str());
  102. //TODO: Take care of the heading type!
  103. if (!preverseHeading.isEmpty()) {
  104. entry.append("<div ")
  105. .append(langAttr)
  106. .append(" class=\"sectiontitle\">")
  107. .append(preverseHeading)
  108. .append("</div>");
  109. }
  110. }
  111. }
  112. entry.append(m_displayOptions.lineBreaks ? "<div " : "<span ");
  113. if (modules.count() == 1) { //insert only the class if we're not in a td
  114. entry.append( i.settings().highlight ? "class=\"currententry\" " : "class=\"entry\" " );
  115. }
  116. entry.append(langAttr).append(isRTL ? " dir=\"rtl\"" : " dir=\"ltr\"").append(">");
  117. //keys should normally be left-to-right, but this doesn't apply in all cases
  118. entry.append("<span class=\"entryname\" dir=\"ltr\">").append(entryLink(i, *mod_Itr)).append("</span>");
  119. if (m_settings.addText) {
  120. //entry.append( TQString::fromLatin1("<span %1>%2</span>").arg(langAttr).arg(key_renderedText) );
  121. entry.append( key_renderedText );
  122. }
  123. if (i.hasChildItems()) {
  124. KeyTree const * tree = i.childList();
  125. for (KeyTreeItem* c = tree->first(); c; c = tree->next()) {
  126. entry.append( renderEntry(*c) );
  127. }
  128. }
  129. entry.append(m_displayOptions.lineBreaks ? "</div>\n" : "</span>\n");
  130. if (modules.count() == 1) {
  131. renderedText.append( entry );
  132. }
  133. else {
  134. renderedText.append("<td class=\"")
  135. .append(i.settings().highlight ? "currententry" : "entry")
  136. .append("\" ")
  137. .append(langAttr)
  138. .append(" dir=\"")
  139. .append(isRTL ? "rtl" : "ltr")
  140. .append("\">")
  141. .append(entry)
  142. .append("</td>\n");
  143. }
  144. }
  145. if (modules.count() > 1) {
  146. renderedText.append("</tr>\n");
  147. }
  148. // qDebug("CHTMLExportRendering: %s", renderedText.latin1());
  149. return renderedText;
  150. }
  151. void CHTMLExportRendering::initRendering() {
  152. CPointers::backend()->setDisplayOptions( m_displayOptions );
  153. CPointers::backend()->setFilterOptions( m_filterOptions );
  154. }
  155. const TQString CHTMLExportRendering::finishText( const TQString& text, KeyTree& tree ) {
  156. ListCSwordModuleInfo modules = tree.collectModules();
  157. const CLanguageMgr::Language* const lang = modules.first()->language();
  158. CDisplayTemplateMgr* tMgr = CPointers::displayTemplateManager();
  159. CDisplayTemplateMgr::Settings settings;
  160. settings.modules = modules;
  161. settings.langAbbrev = ((modules.count() == 1) && lang->isValid())
  162. ? lang->abbrev()
  163. : "unknown";
  164. settings.pageDirection = (modules.count() == 1)
  165. ? ((modules.first()->textDirection() == CSwordModuleInfo::LeftToRight) ? "ltr" : "rtl")
  166. : TQString::null;
  167. return tMgr->fillTemplate(i18n("Export"), text, settings);
  168. }
  169. /*!
  170. \fn CHTMLExportRendering::entryLink( KeyTreeItem& item )
  171. */
  172. const TQString CHTMLExportRendering::entryLink( const KeyTreeItem& item, CSwordModuleInfo* ) {
  173. return item.key();
  174. }
  175. }
  176. ; //end of namespace "Rendering"