~ubuntu-branches/debian/squeeze/sword/squeeze

« back to all changes in this revision

Viewing changes to src/modules/filters/thmlfootnotes.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Glassey
  • Date: 2004-01-15 15:50:07 UTC
  • Revision ID: james.westby@ubuntu.com-20040115155007-n9mz4x0zxrs1isd3
Tags: upstream-1.5.7
ImportĀ upstreamĀ versionĀ 1.5.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 *
 
3
 * thmlfootnotes -      SWFilter descendant to hide or show footnotes
 
4
 *                      in a ThML module.
 
5
 */
 
6
 
 
7
 
 
8
#include <stdlib.h>
 
9
#include <thmlfootnotes.h>
 
10
#include <swmodule.h>
 
11
#include <swbuf.h>
 
12
#include <versekey.h>
 
13
#include <utilxml.h>
 
14
#ifndef __GNUC__
 
15
#else
 
16
#include <unixstr.h>
 
17
#endif
 
18
 
 
19
SWORD_NAMESPACE_START
 
20
 
 
21
const char oName[] = "Footnotes";
 
22
const char oTip[] = "Toggles Footnotes On and Off if they exist";
 
23
 
 
24
const SWBuf choices[3] = {"On", "Off", ""};
 
25
const StringList oValues(&choices[0], &choices[2]);
 
26
 
 
27
ThMLFootnotes::ThMLFootnotes() : SWOptionFilter(oName, oTip, &oValues) {
 
28
        setOptionValue("Off");
 
29
}
 
30
 
 
31
 
 
32
ThMLFootnotes::~ThMLFootnotes() {
 
33
}
 
34
 
 
35
 
 
36
char ThMLFootnotes::processText(SWBuf &text, const SWKey *key, const SWModule *module) {
 
37
        SWBuf token;
 
38
        bool intoken    = false;
 
39
        bool hide       = false;
 
40
        SWBuf tagText;
 
41
        XMLTag startTag;
 
42
        SWBuf refs = "";
 
43
        int footnoteNum = 1;
 
44
        char buf[254];
 
45
        VerseKey parser = key->getText();
 
46
 
 
47
        SWBuf orig = text;
 
48
        const char *from = orig.c_str();
 
49
 
 
50
        for (text = ""; *from; from++) {
 
51
                if (*from == '<') {
 
52
                        intoken = true;
 
53
                        token = "";
 
54
                        continue;
 
55
                }
 
56
                if (*from == '>') {     // process tokens
 
57
                        intoken = false;
 
58
 
 
59
                        XMLTag tag(token);
 
60
                        if (!strcmp(tag.getName(), "note")) {
 
61
                                if (!tag.isEndTag()) {
 
62
                                        if (!tag.isEmpty()) {
 
63
                                                refs = "";
 
64
                                                startTag = tag;
 
65
                                                hide = true;
 
66
                                                tagText = "";
 
67
                                                continue;
 
68
                                        }
 
69
                                }
 
70
                                if (hide && tag.isEndTag()) {
 
71
                                        if (module->isProcessEntryAttributes()) {
 
72
                                                SWBuf fc = module->getEntryAttributes()["Footnote"]["count"]["value"];
 
73
                                                footnoteNum = (fc.length()) ? atoi(fc.c_str()) : 0;
 
74
                                                sprintf(buf, "%i", ++footnoteNum);
 
75
                                                module->getEntryAttributes()["Footnote"]["count"]["value"] = buf;
 
76
                                                StringList attributes = startTag.getAttributeNames();
 
77
                                                for (StringList::iterator it = attributes.begin(); it != attributes.end(); it++) {
 
78
                                                        module->getEntryAttributes()["Footnote"][buf][it->c_str()] = startTag.getAttribute(it->c_str());
 
79
                                                }
 
80
                                                module->getEntryAttributes()["Footnote"][buf]["body"] = tagText;
 
81
                                                startTag.setAttribute("swordFootnote", buf);
 
82
                                                if ((startTag.getAttribute("type")) && (!strcmp(startTag.getAttribute("type"), "crossReference"))) {
 
83
                                                        if (!refs.length())
 
84
                                                                refs = parser.ParseVerseList(tagText.c_str(), parser, true).getRangeText();
 
85
                                                        module->getEntryAttributes()["Footnote"][buf]["refList"] = refs.c_str();
 
86
                                                }
 
87
                                        }
 
88
                                        hide = false;
 
89
                                        if ((option) || ((startTag.getAttribute("type") && (!strcmp(startTag.getAttribute("type"), "crossReference"))))) {      // we want the tag in the text; crossReferences are handled by another filter
 
90
                                                text += startTag;
 
91
                                                text.append(tagText);
 
92
                                        }
 
93
                                        else    continue;
 
94
                                }
 
95
                        }
 
96
 
 
97
                        // if not a note token, keep token in text
 
98
                        if ((!strcmp(tag.getName(), "scripRef")) && (!tag.isEndTag())) {
 
99
                                SWBuf osisRef = tag.getAttribute("passage");
 
100
                                if (refs.length())
 
101
                                        refs += "; ";
 
102
                                refs += osisRef;
 
103
                        }
 
104
                        if (!hide) {
 
105
                                text += '<';
 
106
                                text.append(token);
 
107
                                text += '>';
 
108
                        }
 
109
                        else {
 
110
                                tagText += '<';
 
111
                                tagText.append(token);
 
112
                                tagText += '>';
 
113
                        }
 
114
                        continue;
 
115
                }
 
116
                if (intoken) { //copy token
 
117
                        token += *from;
 
118
                }
 
119
                else if (!hide) { //copy text which is not inside a token
 
120
                        text += *from;
 
121
                }
 
122
                else tagText += *from;
 
123
        }
 
124
        return 0;
 
125
}
 
126
 
 
127
SWORD_NAMESPACE_END