The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
osislemma.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * osislemma.cpp - SWFilter descendant to hide or show lemmata
4  * in a OSIS module
5  *
6  * $Id: osislemma.cpp 3482 2017-06-25 14:36:23Z scribe $
7  *
8  * Copyright 2003-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 #include <stdlib.h>
25 #include <osislemma.h>
26 #include <utilxml.h>
27 
28 
30 
31 namespace {
32 
33  static const char oName[] = "Lemmas";
34  static const char oTip[] = "Toggles Lemmas On and Off if they exist";
35 
36  static const StringList *oValues() {
37  static const SWBuf choices[3] = {"Off", "On", ""};
38  static const StringList oVals(&choices[0], &choices[2]);
39  return &oVals;
40  }
41 }
42 
43 
45 }
46 
47 
49 }
50 
51 
52 char OSISLemma::processText(SWBuf &text, const SWKey *key, const SWModule *module) {
53  SWBuf token;
54  bool intoken = false;
55 
56  const SWBuf orig = text;
57  const char * from = orig.c_str();
58 
59  if (!option) {
60  for (text = ""; *from; ++from) {
61  if (*from == '<') {
62  intoken = true;
63  token = "";
64  continue;
65  }
66  if (*from == '>') { // process tokens
67  intoken = false;
68  if (token.startsWith("w ")) { // Word
69  XMLTag wtag(token);
70 
71  // always save off lemma if we haven't yet
72  if (!wtag.getAttribute("savlm")) {
73  const char *l = wtag.getAttribute("lemma");
74  if (l) {
75  wtag.setAttribute("savlm", l);
76  }
77  }
78 
79  int count = wtag.getAttributePartCount("lemma", ' ');
80  for (int i = 0; i < count; i++) {
81  SWBuf a = wtag.getAttribute("lemma", i, ' ');
82  const char *prefix = a.stripPrefix(':');
83  if ((!prefix) || ((SWBuf)prefix).startsWith("lemma.")) {
84  // remove attribute part
85  wtag.setAttribute("lemma", 0, i, ' ');
86  i--;
87  count--;
88  }
89  }
90 
91  token = wtag;
92  token.trim();
93  // drop <>
94  token << 1;
95  token--;
96  }
97 
98  // keep token in text
99  text.append('<');
100  text.append(token);
101  text.append('>');
102 
103  continue;
104  }
105  if (intoken) {
106  token += *from;
107  }
108  else {
109  text.append(*from);
110  }
111  }
112  }
113  return 0;
114 }
115 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
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
virtual ~OSISLemma()
Definition: osislemma.cpp:48
bool startsWith(const SWBuf &prefix) const
Definition: swbuf.h:486
static const StringList * oValues()
virtual char processText(SWBuf &text, const SWKey *key=0, const SWModule *module=0)
Definition: osislemma.cpp:52
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[]
SWBuf & trim()
Definition: swbuf.h:443
const char * stripPrefix(char separator, bool endOfStringAsSeparator=false)
Definition: swbuf.h:457
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[]
#define SWORD_NAMESPACE_END
Definition: defs.h:40
Definition: swkey.h:77
int getAttributePartCount(const char *attribName, char partSplit= '|') const
Definition: utilxml.cpp:218