summaryrefslogtreecommitdiffstats
path: root/src/ks_thmlhtml.cpp
blob: 6dc51befa726ce85f18261c707b1726457fd131d (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/***************************************************************************
    File:         ks_thmlhtml.cpp
    Project:      Kio-Sword -- An ioslave for SWORD and KDE
    Copyright:    Copyright (C) 2004-2005 Luke Plant
                  and CrossWire Bible Society 2001
                  (file based on thmlhtmlhref.cpp)
		  and the BibleTime team (bits taken from bt_thmlhtml.cpp)
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "ks_thmlhtml.h"

#include "utils.h"

#include <swmodule.h>
#include <utilxml.h>
#include <versekey.h>

#include <stdlib.h>

using namespace sword;

namespace KioSword {
	ThMLHTML::MyUserData::MyUserData(const SWModule *module, const SWKey *key) : BasicFilterUserData(module, key) {
		if (module) {
			version = module->Name();
			BiblicalText = (!strcmp(module->Type(), "Biblical Texts"));
		}	
	}
	
	
	ThMLHTML::ThMLHTML() {
		setTokenStart("<");
		setTokenEnd(">");
	
		setTokenCaseSensitive(true);
		addTokenSubstitute("scripture", "<i> ");
		addTokenSubstitute("/scripture", "</i> ");
	}
	
	
	bool ThMLHTML::handleToken(SWBuf &buf, const char *token, BasicFilterUserData *userData) {
		const char *tok;
		if (!substituteToken(buf, token)) { // manually process if it wasn't a simple substitution
			MyUserData *u = (MyUserData *)userData;		
	
			XMLTag tag(token);
			if ((!tag.isEndTag()) && (!tag.isEmpty()))
				u->startTag = tag;
	
			if (tag.getName() && !strcasecmp(tag.getName(), "sync")) {       //lemmas, morph codes or strongs
				if (tag.getAttribute("type") && !strcasecmp(tag.getAttribute("type"), "lemma")) {       // Lemma
					const char *value = tag.getAttribute("value");
					if (strlen(value)) {
						buf.appendFormatted
							(" <span class='lemma'>&lt;%s&gt;</span>", value);
					}
				} else if (tag.getAttribute("type") && !strcasecmp(tag.getAttribute("type"), "morph")) {  // Morph
					const char *value = tag.getAttribute("value");
					if (value) {
						buf.append(TQString(" <span class='morph'>(<a href=\"%2\">%1</a>)</span>")
								.arg(value)
								.arg(swordUrlForSearch(GREEKMORPH, value, m_swordoptions))
								.utf8());
					}
				} else if (tag.getAttribute("type") && !strcasecmp(tag.getAttribute("type"), "Strongs")) {      // Strongs
					const char *value =
						tag.getAttribute("value");
					if (value && value[0] == 'H') { // hewbrew strongs number
						// value + 1 => skip the H
						buf.append(TQString(" <span class='strongs'>&lt;<a href=\"%2\">%1</a>&gt;</span>")
								.arg(value + 1)
								.arg(swordUrlForSearch(HEBREWSTRONGS, value + 1, m_swordoptions))
								.utf8());
					} else if (value && value[0] == 'G') {  //greek strongs number
						// value + 1 => skip the G
						buf.append(TQString(" <span class='strongs'>&lt;<a href=\"%2\">%1</a>&gt;</span>")
								.arg(value + 1)
								.arg(swordUrlForSearch(GREEKSTRONGS, value + 1, m_swordoptions))
								.utf8());
					}
				}
			}
			// FIXME - modify for Kio-Sword
			// <note> tag
			else if (!strcmp(tag.getName(), "note")) {
				if (!tag.isEndTag()) {
					if (!tag.isEmpty()) {
						SWBuf type = tag.getAttribute("type");
						SWBuf footnoteNumber = tag.getAttribute("swordFootnote");
						VerseKey *vkey;
						// see if we have a VerseKey * or descendant
						try {
							vkey = SWDYNAMIC_CAST(VerseKey, u->key);
						}
						catch ( ... ) {	}
						if (vkey) {
							// leave this special osis type in for crossReference notes types?  Might thml use this some day? Doesn't hurt.
							char ch = ((tag.getAttribute("type") && ((!strcmp(tag.getAttribute("type"), "crossReference")) || (!strcmp(tag.getAttribute("type"), "x-cross-ref")))) ? 'x':'n');
							buf.appendFormatted("<a href=\"noteID=%s.%c.%s\"><small><sup>*%c</sup></small></a> ", vkey->getText(), ch, footnoteNumber.c_str(), ch);
						}
						u->suspendTextPassThru = true;
					}
				}
				if (tag.isEndTag()) {
					u->suspendTextPassThru = false;
				}
			}
			// <scripRef> tag
			else if (!strcmp(tag.getName(), "scripRef")) {
				if (!tag.isEndTag()) {
					if (!tag.isEmpty()) {
						u->suspendTextPassThru = true;
					}
				}
				if (tag.isEndTag()) {	//	</scripRef>
					if (!u->BiblicalText) {
						SWBuf refList = u->startTag.getAttribute("passage");
						if (!refList.length())
							refList = u->lastTextNode;
						SWBuf version = tag.getAttribute("version");
						buf += "<a href=\"";
						buf += swordUrlForSearch(DEFBIBLE, refList.c_str(), m_swordoptions).utf8();
						/* TODO - can we do with in KioSword?
						if (version.length()) {
							buf += "&version=";
							buf += version;
							buf += " ";
						}*/
						buf += "\">";
						buf += u->lastTextNode.c_str();
						buf += "</a>";
					}
					// FIXME for Kio-Sword
					else {
						SWBuf footnoteNumber = u->startTag.getAttribute("swordFootnote");
						VerseKey *vkey;
						// see if we have a VerseKey * or descendant
						try {
							vkey = SWDYNAMIC_CAST(VerseKey, u->key);
						}
						catch ( ... ) {}
						if (vkey) {
							// leave this special osis type in for crossReference notes types?  Might thml use this some day? Doesn't hurt.
							buf.appendFormatted("<a href=\"noteID=%s.x.%s\"><small><sup>*x</sup></small></a> ", vkey->getText(), footnoteNumber.c_str());
						}
					}
	
					// let's let text resume to output again
					u->suspendTextPassThru = false;
				}
			}
			else if (tag.getName() && !strcasecmp(tag.getName(), "div")) {
				if (tag.isEndTag() && u->SecHead) {
					buf += "</i></b><br />";
					u->SecHead = false;
				}
				else if (tag.getAttribute("class")) {
					if (!strcasecmp(tag.getAttribute("class"), "sechead")) {
						u->SecHead = true;
						buf += "<br /><b><i>";
					}
					else if (!strcasecmp(tag.getAttribute("class"), "title")) {
						u->SecHead = true;
						buf += "<br /><b><i>";
					}
				}
			}
			else if (tag.getName() && (!strcasecmp(tag.getName(), "img") || !strcasecmp(tag.getName(), "image"))) {
				const char *src = strstr(token, "src");
				if (!src)		// assert we have a src attribute
					return false;
	
				buf += '<';
				for (const char *c = token; *c; c++) {
					if (c == src) {
						for (;((*c) && (*c != '"')); c++)
							buf += *c;
	
						if (!*c) { c--; continue; }
	
						buf += '"';
						if (*(c+1) == '/') {
							buf += "file:";
							buf += userData->module->getConfigEntry("AbsoluteDataPath");
							if (buf[buf.length()-2] == '/')
								c++;		// skip '/'
						}
						continue;
					}
					buf += *c;
				}
				buf += '>';
			}
			else {
				buf += '<';
				/*for (const char *tok = token; *tok; tok++)
					buf += *tok;*/
				buf += token;
				buf += '>';
				//return false;  // we still didn't handle token
			}
		}
		return true;
	}
}