Beispiel #1
0
// Return true if the content was handled or is to be ignored.
//      false if the what has been seen is to be accumulated and considered later.
bool handleToken(std::string & text, XMLTag & token) {
        // The start token for the current entry;
    static XMLTag startTag;

        // Flags to indicate whether we are in a entry, entryFree or superentry
        static bool inEntry      = false;
        static bool inEntryFree  = false;
        static bool inSuperEntry = false;

        std::string const &  tokenName = token.name();

        static char const * splitPtr;
        static char const * splitPtr2 = nullptr;
        static std::array<char, 4096> splitBuffer;
    static SWKey tmpKey;
//-- START TAG -------------------------------------------------------------------------
    if (!token.isEndTag()) {

        // If we are not in an "entry" and we see one, then enter it.
        if (!inEntry && !inEntryFree && !inSuperEntry) {
            inEntry      = (tokenName == "entry");
            inEntryFree  = (tokenName == "entryFree");
            inSuperEntry = (tokenName == "superentry");
                        if (inEntry || inEntryFree || inSuperEntry) {
#ifdef DEBUG
                cout << "Entering " << tokenName << endl;
#endif
                startTag    = token;
                text        = "";

                                keyStr = token.attribute("n"); // P5 with linking and/or non-URI chars
                                if (keyStr.empty()) {
                                    keyStr = token.attribute("sortKey"); // P5 otherwise
                                    if (keyStr.empty()) {
                            keyStr = token.attribute("key"); // P4
                                        }
                                }

                return false; // make tag be part of the output
            }
        }
    }

//-- EMPTY and END TAG ---------------------------------------------------------------------------------------------
    else {

        // ENTRY end
        // If we see the end of an entry that we are in, then leave it
        if ((inEntry      && (tokenName == "entry"     )) ||
            (inEntryFree  && (tokenName == "entryFree" )) ||
            (inSuperEntry && (tokenName == "superentry"))) {
#ifdef DEBUG
            cout << "Leaving " << tokenName << endl;
#endif
            // Only one is false coming into here,
            // but all must be on leaving.
            inEntry       = false;
            inEntryFree   = false;
            inSuperEntry  = false;
            text         += token.toString();

                        entryCount++;
#ifdef DEBUG
            cout << "keyStr: " << keyStr << endl;
#endif
                        splitPtr = std::strchr(keyStr.c_str(), '|');
                        if (splitPtr) {
                                std::strncpy (splitBuffer.data(), keyStr.c_str(), splitPtr - keyStr.c_str());
                                splitBuffer[splitPtr - keyStr.c_str()] = 0;
                currentKey->setText(splitBuffer.data());
#ifdef DEBUG
                cout << "splitBuffer: " << splitBuffer.data() << endl;
                cout << "currentKey: " << currentKey->getText() << endl;
#endif
                writeEntry(*currentKey, text);
#if 1
                                while (splitPtr) {
                                    splitPtr += 1;
                                    splitPtr2 = std::strstr(splitPtr, "|");
                                        entryCount++;
                                        if (splitPtr2) {
                        std::strncpy (splitBuffer.data(), splitPtr, splitPtr2 - splitPtr);
                                                splitBuffer[splitPtr2 - splitPtr] = 0;
#ifdef DEBUG
                        cout << "splitBuffer: " << splitBuffer.data() << endl;
                        cout << "currentKey: " << currentKey->getText() << endl;
#endif
                        linkToEntry(currentKey->getText(), splitBuffer.data());
                                            splitPtr = splitPtr2;
                                        }
                                        else {
                        std::strcpy(splitBuffer.data(), splitPtr);
#ifdef DEBUG
                                      cout << "splitBuffer: " << splitBuffer.data() << endl;
                        cout << "currentKey: " << currentKey->getText() << endl;
#endif
                        linkToEntry(currentKey->getText(), splitBuffer.data());
                                                splitPtr = nullptr;
                                        }
                                }
#endif
                        }
                        else {
                currentKey->setText(keyStr);
                writeEntry(*currentKey, text);
                        }

            // Since we consumed the text, clear it
            // and tell the caller that the tag was consumed.
            text = "";
            return true;
        }
    }
    return false;
}
Beispiel #2
0
char GBFFootnotes::processText (std::string &text, const SWKey *key, const SWModule *module)
{
    (void) key;
    std::string token;
    bool intoken    = false;
    bool hide       = false;
    std::string tagText;
    XMLTag startTag;
    std::string refs = "";
    int footnoteNum = 1;
    char buf[254];

    std::string orig = text;
    const char *from = orig.c_str();

    //XMLTag tag;

    for (text = ""; *from; from++) {
        if (*from == '<') {
            intoken = true;
            token = "";
            continue;
        }
        if (*from == '>') {    // process tokens
            intoken = false;

            //XMLTag tag(token);
            if (!std::strncmp(token.c_str(), "RF",2)) {
//                 tag = token;

                refs = "";
                startTag = token.c_str();
                hide = true;
                tagText = "";
                continue;
            }
            else if (!std::strncmp(token.c_str(), "Rf",2)) {
                if (module->isProcessEntryAttributes()) {
                    //tag = token;

                    if((tagText.length() == 1) || (module->getName() == "IGNT")) {
                        if (option) { // for ASV marks text in verse then put explanation at end of verse
                            text.append(" <FS>[");
                            text.append(tagText);
                            text.append("]<Fs>");
                            hide = false;
                            continue;
                        }
                    }
                    std::string fc = module->getEntryAttributes()["Footnote"]["count"]["value"];
                    footnoteNum = (fc.length()) ? std::atoi(fc.c_str()) : 0;
                    sprintf(buf, "%i", ++footnoteNum);
                    module->getEntryAttributes()["Footnote"]["count"]["value"] = buf;
                    for (auto const & attr : startTag.attributeNames())
                        module->getEntryAttributes()["Footnote"][buf][attr.c_str()] =
                                startTag.attribute(attr.c_str());
                    module->getEntryAttributes()["Footnote"][buf]["body"] = tagText;
                    startTag.setAttribute("swordFootnote", buf);
                }
                hide = false;
                if (option) {
                    text.append(startTag.toString());
                    text.append(tagText);
                }
                else    continue;
            }
            if (!hide) {
                text.push_back('<');
                text.append(token);
                text.push_back('>');
            }
            else {
                tagText.push_back('<');
                tagText.append(token);
                tagText.push_back('>');
            }
            continue;
        }
        if (intoken) { //copy token
            token.push_back(*from);
        }
        else if (!hide) { //copy text which is not inside a token
            text.push_back(*from);
        }
        else tagText.push_back(*from);
    }
    return 0;

    /*
    if (!option) {    // if we don't want footnotes
        char token[4096]; // cheese.  Fix.
        int tokpos = 0;
        bool intoken = false;
        int len;
        bool hide = false;

        const char *from;
        std::string orig = text;
        from = orig.c_str();
        for (text = ""; *from; from++) {
            if (*from == '<') {
                intoken = true;
                tokpos = 0;
//                std::memset(token, 0, 4096);
                token[0] = 0;
                token[1] = 0;
                token[2] = 0;
                continue;
            }
            if (*from == '>') {    // process tokens
                intoken = false;
                switch (*token) {
                case 'R':                // Reference
                    switch(token[1]) {
                    case 'F':               // Begin footnote
                        hide = true;
                        break;
                    case 'f':               // end footnote
                        hide = false;
                        break;
                    }
                    continue;    // skip token
                case 'W':
                    if (token[1] == 'T') {
                        switch (token[2]) {
                        case 'P':
                        case 'S':
                        case 'A':
                            continue; // remove this token
                        default:
                            break;
                        }
                    }
                }
                // if not a footnote token, keep token in text
                if (!hide) {
                    text += '<';
                    text += token;
                    text += '>';
                }
                continue;
            }
            if (intoken) {
                if (tokpos < 4090)
                    token[tokpos++] = *from;
                    token[tokpos+2] = 0;    // +2 cuz we init token with 2 extra '0' because of switch statement
            }
            else    {
                if (!hide) {
                    text += *from;
                }
            }
        }
    }
    return 0;*/
}