The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gbffootnotes.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * gbffootnotes.cpp - SWFilter descendant to hide or show footnotes
4  * in a GBF module
5  *
6  * $Id: gbffootnotes.cpp 2980 2013-09-14 21:51:47Z scribe $
7  *
8  * Copyright 1999-2013 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <gbffootnotes.h>
28 #include <swmodule.h>
29 #include <swbuf.h>
30 #include <versekey.h>
31 #include <utilxml.h>
32 
33 
35 
36 namespace {
37 
38  static const char oName[] = "Footnotes";
39  static const char oTip[] = "Toggles Footnotes On and Off if they exist";
40 
41  static const StringList *oValues() {
42  static const SWBuf choices[3] = {"Off", "On", ""};
43  static const StringList oVals(&choices[0], &choices[2]);
44  return &oVals;
45  }
46 }
47 
48 
50 }
51 
52 
54 }
55 
56 
57 char GBFFootnotes::processText (SWBuf &text, const SWKey *key, const SWModule *module)
58 {
59 
60  SWBuf token;
61  bool intoken = false;
62  bool hide = false;
63  SWBuf tagText;
64  XMLTag startTag;
65  SWBuf refs = "";
66  int footnoteNum = 1;
67  char buf[254];
68 
69  SWBuf orig = text;
70  const char *from = orig.c_str();
71 
72  //XMLTag tag;
73 
74  for (text = ""; *from; from++) {
75  if (*from == '<') {
76  intoken = true;
77  token = "";
78  continue;
79  }
80  if (*from == '>') { // process tokens
81  intoken = false;
82 
83  //XMLTag tag(token);
84  if (!strncmp(token, "RF",2)) {
85 // tag = token;
86 
87  refs = "";
88  startTag = token;
89  hide = true;
90  tagText = "";
91  continue;
92  }
93  else if (!strncmp(token, "Rf",2)) {
94  if (module->isProcessEntryAttributes()) {
95  //tag = token;
96 
97  if((tagText.length() == 1) || !strcmp(module->getName(), "IGNT")) {
98  if (option) { // for ASV marks text in verse then put explanation at end of verse
99  text.append(" <FS>[");
100  text.append(tagText);
101  text.append("]<Fs>");
102  hide = false;
103  continue;
104  }
105  }
106  SWBuf fc = module->getEntryAttributes()["Footnote"]["count"]["value"];
107  footnoteNum = (fc.length()) ? atoi(fc.c_str()) : 0;
108  sprintf(buf, "%i", ++footnoteNum);
109  module->getEntryAttributes()["Footnote"]["count"]["value"] = buf;
110  StringList attributes = startTag.getAttributeNames();
111  for (StringList::const_iterator it = attributes.begin(); it != attributes.end(); it++) {
112  module->getEntryAttributes()["Footnote"][buf][it->c_str()] = startTag.getAttribute(it->c_str());
113  }
114  module->getEntryAttributes()["Footnote"][buf]["body"] = tagText;
115  startTag.setAttribute("swordFootnote", buf);
116  }
117  hide = false;
118  if (option) {
119  text.append(startTag);
120  text.append(tagText);
121  }
122  else continue;
123  }
124  if (!hide) {
125  text.append('<');
126  text.append(token);
127  text.append('>');
128  }
129  else {
130  tagText.append('<');
131  tagText.append(token);
132  tagText.append('>');
133  }
134  continue;
135  }
136  if (intoken) { //copy token
137  token.append(*from);
138  }
139  else if (!hide) { //copy text which is not inside a token
140  text.append(*from);
141  }
142  else tagText.append(*from);
143  }
144  return 0;
145 
146  /*
147  if (!option) { // if we don't want footnotes
148  char token[4096]; // cheese. Fix.
149  int tokpos = 0;
150  bool intoken = false;
151  int len;
152  bool hide = false;
153 
154  const char *from;
155  SWBuf orig = text;
156  from = orig.c_str();
157  for (text = ""; *from; from++) {
158  if (*from == '<') {
159  intoken = true;
160  tokpos = 0;
161 // memset(token, 0, 4096);
162  token[0] = 0;
163  token[1] = 0;
164  token[2] = 0;
165  continue;
166  }
167  if (*from == '>') { // process tokens
168  intoken = false;
169  switch (*token) {
170  case 'R': // Reference
171  switch(token[1]) {
172  case 'F': // Begin footnote
173  hide = true;
174  break;
175  case 'f': // end footnote
176  hide = false;
177  break;
178  }
179  continue; // skip token
180  case 'W':
181  if (token[1] == 'T') {
182  switch (token[2]) {
183  case 'P':
184  case 'S':
185  case 'A':
186  continue; // remove this token
187  default:
188  break;
189  }
190  }
191  }
192  // if not a footnote token, keep token in text
193  if (!hide) {
194  text += '<';
195  text += token;
196  text += '>';
197  }
198  continue;
199  }
200  if (intoken) {
201  if (tokpos < 4090)
202  token[tokpos++] = *from;
203  token[tokpos+2] = 0; // +2 cuz we init token with 2 extra '0' because of switch statement
204  }
205  else {
206  if (!hide) {
207  text += *from;
208  }
209  }
210  }
211  }
212  return 0;*/
213 }
214 
const char * getName() const
Definition: swmodule.cpp:204
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
const char * setAttribute(const char *attribName, const char *attribValue, int partNum=-1, char partSplit= '|')
Definition: utilxml.cpp:248
SWText * module
Definition: osis2mod.cpp:105
Definition: utilxml.h:38
static const StringList * oValues()
const StringList getAttributeNames() const
Definition: utilxml.cpp:188
const char * c_str() const
Definition: swbuf.h:158
std::list< SWBuf > StringList
Definition: swmodule.cpp:91
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
static const char oName[]
virtual bool isProcessEntryAttributes() const
Definition: swmodule.h:832
virtual AttributeTypeList & getEntryAttributes() const
Definition: swmodule.h:817
static const char * choices[4]
const char * getAttribute(const char *attribName, int partNum=-1, char partSplit= '|') const
Definition: utilxml.cpp:230
virtual ~GBFFootnotes()
static const char oTip[]
virtual char processText(SWBuf &text, const SWKey *key=0, const SWModule *module=0)
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77