summaryrefslogtreecommitdiffstats
path: root/bibletime/backend/cdisplayrendering.cpp
blob: 48445a97a2c85001772d24b7638369f75bbd9e0d (plain)
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
//
// C++ Implementation: cdisplayrendering
//
// Description:
//
//
// Author: The BibleTime team <info@bibletime.info>, (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//
//

//Backend includes
#include "cdisplayrendering.h"

#include "cdisplaytemplatemgr.h"
#include "creferencemanager.h"
#include "cswordkey.h"
#include "cswordversekey.h"

//TQt includes
#include <tqstring.h>
#include <tqregexp.h>

namespace Rendering {

	CDisplayRendering::CDisplayRendering(CSwordBackend::DisplayOptions displayOptions, CSwordBackend::FilterOptions filterOptions)
: CHTMLExportRendering(CHTMLExportRendering::Settings(true), displayOptions, filterOptions) {}

	const TQString CDisplayRendering::entryLink( const KeyTreeItem& item, CSwordModuleInfo*  module ) {
		TQString linkText;

		const bool isBible = module && (module->type() == CSwordModuleInfo::Bible);
		CSwordVerseKey vk(module); //only valid for bible modules, i.e. isBible == true
		vk.Headings(true);

		if (isBible) {
			vk = item.key();
		}

		if (isBible && (vk.Verse() == 0)) {
			return TQString(); //Warning: return already here
		}

		switch (item.settings().keyRenderingFace) {

			case KeyTreeItem::Settings::NoKey: {
				linkText = TQString();
				break; //no key is valid for all modules
			}

			case KeyTreeItem::Settings::CompleteShort: {
				if (isBible) {
					linkText = TQString::fromUtf8(vk.getShortText());
					break;
				}

				//fall through for non-Bible modules
			}

			case KeyTreeItem::Settings::CompleteLong: {
				if (isBible) {
					linkText = vk.key();
					break;
				}

				//fall through for non-Bible modules
			}

			case KeyTreeItem::Settings::SimpleKey: {
				if (isBible) {
					linkText = TQString::number(vk.Verse());
					break;
				}

				//fall through for non-Bible modules
			}

			default: { //default behaviour to return the passed key
				linkText = item.key();
				break;
			}
		}

		if (linkText.isEmpty()) {
			return TQString("<a name=\"").append(keyToHTMLAnchor(item.key())).append("\" />");
		}
		else {
			return TQString("<a name=\"").append(keyToHTMLAnchor(item.key())).append("\" ")
				   .append("href=\"")
				   .append(CReferenceManager::encodeHyperlink(
							   module->name(), item.key(), CReferenceManager::typeFromModule(module->type()))
						  )
				   .append("\">").append(linkText).append("</a>\n");
		}

		return TQString();
	}

	const TQString CDisplayRendering::keyToHTMLAnchor(const TQString& key) {
		TQString ret = key;
		ret = ret.stripWhiteSpace().remove(TQRegExp("[^A-Za-z0-9]+"));
		ret = ret.remove(TQRegExp("^\\d+|"));

		return ret;
	}

	const TQString CDisplayRendering::finishText( const TQString& oldText, KeyTree& tree ) {
		ListCSwordModuleInfo modules = tree.collectModules();


		//marking words is very slow, we have to find a better solution

		/*
		 //mark all words by spans

		 TQString text = oldText;

		 TQRegExp re("(\\b)(?=\\w)"); //word begin marker
		 int pos = text.find(re, 0);

		 while (pos != -1) { //word begin found
		  //qWarning("found word at %i in %i", pos, text.length());
		  int endPos = pos + 1;
		  if (!CToolClass::inHTMLTag(pos+1, text)) { //the re has a positive look ahead which matches one char before the word start
		   //qWarning("matched %s", text.mid(pos+1, 4).latin1());

		   //find end of word and put a marker around it
		   endPos = text.find(TQRegExp("\\b|[,.:]"), pos+1);
		   if ((endPos != -1) && !CToolClass::inHTMLTag(endPos, text) && (endPos - pos >= 3)) { //reuire wordslonger than 3 chars
		    text.insert(endPos, "</span>");
		    text.insert(pos, "<span class=\"word\">");

		    endPos += 26;
		   }
		  }
		  pos = text.find(re, endPos);
		 }
		*/
		const CLanguageMgr::Language* const lang =
			(modules.count() >= 1)
			? modules.first()->language()
			: CPointers::languageMgr()->defaultLanguage();

		CDisplayTemplateMgr* tMgr = CPointers::displayTemplateManager();

		Q_ASSERT(modules.count() >= 1);

		CDisplayTemplateMgr::Settings settings;
		settings.modules = modules;
		settings.langAbbrev = ((modules.count() == 1) && lang->isValid())
							  ? lang->abbrev()
							  : TQString();
		settings.pageDirection = (modules.count() == 1)
								 ? ((modules.first()->textDirection() == CSwordModuleInfo::LeftToRight) ? "ltr"  : "rtl")
						 : TQString();

		return tMgr->fillTemplate(CBTConfig::get
									  (CBTConfig::displayStyle), oldText, settings);


	}
}