The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
thmlscripref.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * thmlscripref.cpp - SWFilter descendant to hide or show scripture
4  * referebces in a ThML module
5  *
6  * $Id: thmlscripref.cpp 3045 2014-03-02 07:53:52Z chrislit $
7  *
8  * Copyright 2001-2014 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 #include <stdlib.h>
25 #include <stdio.h>
26 #include <thmlscripref.h>
27 #include <utilxml.h>
28 #include <versekey.h>
29 #include <swmodule.h>
30 
31 
33 
34 namespace {
35 
36  static const char oName[] = "Cross-references";
37  static const char oTip[] = "Toggles Scripture Cross-references On and Off if they exist";
38 
39  static const StringList *oValues() {
40  static const SWBuf choices[3] = {"Off", "On", ""};
41  static const StringList oVals(&choices[0], &choices[2]);
42  return &oVals;
43  }
44 }
45 
46 
48 }
49 
50 
52 }
53 
54 
55 char ThMLScripref::processText(SWBuf &text, const SWKey *key, const SWModule *module) {
56  SWBuf token;
57  bool intoken = false;
58  bool hide = false;
59  SWBuf tagText;
60  XMLTag startTag;
61  SWBuf refs = "";
62  int footnoteNum = 1;
63  char buf[254];
64  SWKey *p = (module) ? module->createKey() : (key) ? key->clone() : new VerseKey();
65  VerseKey *parser = SWDYNAMIC_CAST(VerseKey, p);
66  if (!parser) {
67  delete p;
68  parser = new VerseKey();
69  }
70  *parser = key->getText();
71 
72  SWBuf orig = text;
73  const char *from = orig.c_str();
74 
75  for (text = ""; *from; from++) {
76  if (*from == '<') {
77  intoken = true;
78  token = "";
79  continue;
80  }
81  if (*from == '>') { // process tokens
82  intoken = false;
83 
84  XMLTag tag(token);
85  if (!strcmp(tag.getName(), "scripRef")) {
86  if (!tag.isEndTag()) {
87  if (!tag.isEmpty()) {
88  refs = "";
89  startTag = tag;
90  hide = true;
91  tagText = "";
92  continue;
93  }
94  }
95  if (hide && tag.isEndTag()) {
96  if (module->isProcessEntryAttributes()) {
97  SWBuf fc = module->getEntryAttributes()["Footnote"]["count"]["value"];
98  footnoteNum = (fc.length()) ? atoi(fc.c_str()) : 0;
99  sprintf(buf, "%i", ++footnoteNum);
100  module->getEntryAttributes()["Footnote"]["count"]["value"] = buf;
101  StringList attributes = startTag.getAttributeNames();
102  for (StringList::iterator it = attributes.begin(); it != attributes.end(); it++) {
103  module->getEntryAttributes()["Footnote"][buf][it->c_str()] = startTag.getAttribute(it->c_str());
104  }
105  module->getEntryAttributes()["Footnote"][buf]["body"] = tagText;
106  startTag.setAttribute("swordFootnote", buf);
107  SWBuf passage = startTag.getAttribute("passage");
108  if (passage.length())
109  refs = parser->parseVerseList(passage.c_str(), *parser, true).getRangeText();
110  else refs = parser->parseVerseList(tagText.c_str(), *parser, true).getRangeText();
111  module->getEntryAttributes()["Footnote"][buf]["refList"] = refs.c_str();
112  }
113  hide = false;
114  if (option) { // we want the tag in the text
115  text += startTag;
116  text.append(tagText);
117  }
118  else continue;
119  }
120  }
121 
122  // if not a scripRef token, keep token in text
123  if ((!strcmp(tag.getName(), "scripRef")) && (!tag.isEndTag())) {
124  SWBuf osisRef = tag.getAttribute("passage");
125  if (refs.length())
126  refs += "; ";
127  refs += osisRef;
128  }
129  if (!hide) {
130  text += '<';
131  text.append(token);
132  text += '>';
133  }
134  else {
135  tagText += '<';
136  tagText.append(token);
137  tagText += '>';
138  }
139  continue;
140  }
141  if (intoken) { //copy token
142  token += *from;
143  }
144  else if (!hide) { //copy text which is not inside a token
145  text += *from;
146  }
147  else tagText += *from;
148  }
149  delete parser;
150  return 0;
151 }
152 
#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
const char * getName() const
Definition: utilxml.h:58
SWText * module
Definition: osis2mod.cpp:105
Definition: utilxml.h:38
bool isEmpty() const
Definition: utilxml.h:60
static const StringList * oValues()
virtual SWKey * clone() const
Definition: swkey.cpp:75
virtual const char * getText() const
Definition: swkey.cpp:184
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 ListKey parseVerseList(const char *buf, const char *defaultKey=0, bool expandRange=false, bool useChapterAsVerse=false)
Definition: versekey.cpp:515
virtual char processText(SWBuf &text, const SWKey *key=0, const SWModule *module=0)
#define SWDYNAMIC_CAST(className, object)
Definition: defs.h:47
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
static const char oTip[]
bool isEndTag(const char *eID=0) const
Definition: utilxml.cpp:323
virtual ~ThMLScripref()
#define SWORD_NAMESPACE_END
Definition: defs.h:40
virtual SWKey * createKey() const
Definition: swmodule.cpp:173
Definition: swkey.h:77