1
/******************************************************************************
3
* gbfstrongs - SWFilter descendant to hide or show strongs number
7
* Copyright 2009 CrossWire Bible Society (http://www.crosswire.org)
8
* CrossWire Bible Society
10
* Tempe, AZ 85280-2528
12
* This program is free software; you can redistribute it and/or modify it
13
* under the terms of the GNU General Public License as published by the
14
* Free Software Foundation version 2.
16
* This program is distributed in the hope that it will be useful, but
17
* WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* General Public License for more details.
25
#include <gbfwordjs.h>
33
const char oName[] = "Word Javascript";
34
const char oTip[] = "Toggles Word Javascript data";
36
const SWBuf choices[3] = {"Off", "On", ""};
37
const StringList oValues(&choices[0], &choices[2]);
40
GBFWordJS::GBFWordJS() : SWOptionFilter(oName, oTip, &oValues) {
41
setOptionValue("Off");
45
defaultGreekParse = 0;
51
GBFWordJS::~GBFWordJS() {
55
char GBFWordJS::processText(SWBuf &text, const SWKey *key, const SWModule *module) {
57
char token[2112]; // cheese. Fix.
60
bool lastspace = false;
64
unsigned int textStart = 0, lastAppendLen = 0, textEnd = 0;
67
bool needWordOut = false;
68
AttributeValue *wordAttrs = 0;
69
SWBuf modName = (module)?module->Name():"";
70
SWBuf wordSrcPrefix = modName;
72
const SWBuf orig = text;
73
const char * from = orig.c_str();
76
vkey = SWDYNAMIC_CAST(VerseKey, key);
79
for (text = ""; *from; from++) {
86
textEnd = text.length();
89
if (*from == '>') { // process tokens
91
if (*token == 'W' && (token[1] == 'G' || token[1] == 'H')) { // Strongs
93
if (atoi((!isdigit(*val))?val+1:val) < 5627) {
94
// normal strongs number
95
sprintf(wordstr, "%03d", word++);
96
needWordOut = (word > 2);
97
wordAttrs = &(module->getEntryAttributes()["Word"][wordstr]);
98
(*wordAttrs)["Lemma"] = val;
99
//printf("Adding: [\"Word\"][%s][\"Strongs\"] = %s\n", wordstr, val);
101
tmp.append(text.c_str()+textStart, (int)(textEnd - textStart));
102
(*wordAttrs)["Text"] = tmp;
103
text.append("</span>");
105
ts.appendFormatted("%d", textStart);
106
(*wordAttrs)["TextStart"] = ts;
107
//printf("Adding: [\"Word\"][%s][\"Text\"] = %s\n", wordstr, tmp.c_str());
113
(*wordAttrs)["Morph"] = val;
115
//printf("Adding: [\"Word\"][%s][\"Morph\"] = %s\n", wordstr, val);
119
if (*token == 'W' && token[1] == 'T') { // Morph
120
if (token[2] == 'G' || token[2] == 'H') {
121
strcpy(val, token+2);
123
else strcpy(val, token+1);
125
(*wordAttrs)["Morph"] = val;
126
(*wordAttrs)["MorphClass"] = "StrongsMorph";
130
// if not a strongs token, keep token in text
136
sprintf(wstr, "%03d", word-2);
137
AttributeValue *wAttrs = &(module->getEntryAttributes()["Word"][wstr]);
139
SWBuf strong = (*wAttrs)["Lemma"];
140
SWBuf morph = (*wAttrs)["Morph"];
141
SWBuf morphClass = (*wAttrs)["MorphClass"];
142
SWBuf wordText = (*wAttrs)["Text"];
143
SWBuf textSt = (*wAttrs)["TextStart"];
146
gh = isdigit(strong[0]) ? 0:strong[0];
149
gh = vkey->Testament() ? 'H' : 'G';
155
SWModule *sMorph = 0;
157
sLex = defaultGreekLex;
158
sMorph = defaultGreekParse;
161
sLex = defaultHebLex;
162
sMorph = defaultHebParse;
166
// we can pass the real lex name in, but we have some
167
// aliases in the javascript to optimize bandwidth
168
lexName = sLex->Name();
169
if (lexName == "StrongsGreek")
171
if (lexName == "StrongsHebrew")
176
// optimize for bandwidth and use only the verse as the unique entry id
177
wordID.appendFormatted("%d", vkey->Verse());
180
wordID = key->getText();
182
for (unsigned int i = 0; i < wordID.size(); i++) {
183
if ((!isdigit(wordID[i])) && (!isalpha(wordID[i]))) {
187
wordID.appendFormatted("_%s%d", wordSrcPrefix.c_str(), atoi(wstr));
189
int textStr = atoi(textSt.c_str());
190
textStr += lastAppendLen;
191
SWBuf spanStart = "";
197
SWBuf popMorph = "<a onclick=\"";
198
popMorph.appendFormatted("p(\'%s\',\'%s\','%s','');\" >%s</a>", sMorph->Name(), morph.c_str(), wordID.c_str(), morph.c_str());
203
// 'p' = 'fillpop' to save bandwidth
204
const char *m = strchr(morph.c_str(), ':');
206
else m = morph.c_str();
207
spanStart.appendFormatted("<span class=\"clk\" onclick=\"p('%s','%s','%s','%s','','%s');\" >", lexName.c_str(), strong.c_str(), wordID.c_str(), m, modName.c_str());
208
text.insert(textStr, spanStart);
209
lastAppendLen = spanStart.length();
215
textStart = text.length(); newText = false;
221
token[tokpos++] = *from;
226
lastspace = (*from == ' ');
231
sprintf(wstr, "%03d", word-1);
232
AttributeValue *wAttrs = &(module->getEntryAttributes()["Word"][wstr]);
234
SWBuf strong = (*wAttrs)["Lemma"];
235
SWBuf morph = (*wAttrs)["Morph"];
236
SWBuf morphClass = (*wAttrs)["MorphClass"];
237
SWBuf wordText = (*wAttrs)["Text"];
238
SWBuf textSt = (*wAttrs)["TextStart"];
241
gh = isdigit(strong[0]) ? 0:strong[0];
244
gh = vkey->Testament() ? 'H' : 'G';
251
sLex = defaultGreekLex;
254
sLex = defaultHebLex;
258
// we can pass the real lex name in, but we have some
259
// aliases in the javascript to optimize bandwidth
260
lexName = sLex->Name();
261
if (lexName == "StrongsGreek")
263
if (lexName == "StrongsHebrew")
268
// optimize for bandwidth and use only the verse as the unique entry id
269
wordID.appendFormatted("%d", vkey->Verse());
272
wordID = key->getText();
274
for (unsigned int i = 0; i < wordID.size(); i++) {
275
if ((!isdigit(wordID[i])) && (!isalpha(wordID[i]))) {
279
wordID.appendFormatted("_%s%d", wordSrcPrefix.c_str(), atoi(wstr));
281
int textStr = atoi(textSt.c_str());
282
textStr += lastAppendLen;
283
SWBuf spanStart = "";
284
// 'p' = 'fillpop' to save bandwidth
285
const char *m = strchr(morph.c_str(), ':');
287
else m = morph.c_str();
288
spanStart.appendFormatted("<span class=\"clk\" onclick=\"p('%s','%s','%s','%s','','%s');\" >", lexName.c_str(), strong.c_str(), wordID.c_str(), m, modName.c_str());
289
text.insert(textStr, spanStart);