summaryrefslogtreecommitdiff
path: root/src/backend/filters/btosismorphsegmentation.cpp
blob: 167547639046c41ecdc6af25c696d568dd5fd4b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2014 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "backend/filters/btosismorphsegmentation.h"

// Sword includes:
#include <utilxml.h>


const char Filters::BtOSISMorphSegmentation::oName[] = "Morph segmentation";
const char Filters::BtOSISMorphSegmentation::oTip[] = "Toggles morph "
                                                      "segmentation On and Off "
                                                      "if they exist";

const sword::SWBuf Filters::BtOSISMorphSegmentation::choices[3] = { "Off",
                                                                    "On",
                                                                    "" };

const sword::StringList Filters::BtOSISMorphSegmentation::oValues(&choices[0],
                                                                  &choices[2]);

Filters::BtOSISMorphSegmentation::BtOSISMorphSegmentation()
    : sword::SWOptionFilter(oName, oTip, &oValues)
{
    setOptionValue("Off");
}

char Filters::BtOSISMorphSegmentation::processText(sword::SWBuf & text,
                                                   const sword::SWKey * key,
                                                   const sword::SWModule * mod)
{
    (void) key;
    (void) mod;

    sword::SWBuf token;
    bool intoken    = false;
    bool hide       = false;

    sword::SWBuf orig(text);
    const char * from = orig.c_str();

    sword::XMLTag tag;

    for (text = ""; *from; ++from) {
        if (*from == '<') {
            intoken = true;
            token = "";
            continue;
        }

        if (*from == '>') { // process tokens
            intoken = false;

            if (!strncmp(token.c_str(), "seg ", 4)
                || !strncmp(token.c_str(), "/seg", 4))
            {
                tag = token;

                if (!tag.isEndTag()
                    && tag.getAttribute("type")
                    && !strcmp("morph", tag.getAttribute("type")))
                { // <seg type="morph"> start tag
                    hide = (option == 0); // only hide if option is Off
                }

                if (hide) { // hides start and end tags as long as hide is set
                    if (tag.isEndTag()) //</seg>
                        hide = false;
                    continue; // leave out the current token
                }
            } // end of seg tag handling

            text.append('<');
            text.append(token);
            text.append('>');
            // hide = false; // not right, because there may be child tags in seg. Only /seg may disable the seg hiding.
            continue;
        } // end of intoken part

        if (intoken) {
            token.append(*from); // copy token
        } else {
            text.append(*from); // copy text which is not inside of a tag
        }
    }

    return 0;
}