Example #1
0
void UnquoteParser::tag_start(const QString &tag, const list<QString> &options)
{
    if (tag == "br"){
        res += "\n";
    }else if (tag == "p"){
        if (m_bPar){
            res += "\n";
            m_bPar = false;
        }
    }else if (tag == "img"){
        QString src;
        for (list<QString>::const_iterator it = options.begin(); it != options.end(); ++it){
            QString opt   = *it;
            ++it;
            QString value = *it;
            if (opt == "src")
                src = value;
        }
        if (src.left(10) != "icon:smile")
            return;
        bool bOk;
        unsigned nSmile = src.mid(10).toUInt(&bOk, 16);
        if (!bOk)
            return;
        const smile *s = smiles(nSmile);
        if (s){
            res += s->paste;
            return;
        }
        s = defaultSmiles(nSmile);
        if (s)
            res += s->paste;
    }
}
Example #2
0
ViewParser::ViewParser(bool bIgnoreColors, bool bUseSmiles)
{
    m_bIgnoreColors = bIgnoreColors;
    m_bUseSmiles    = bUseSmiles;
    m_bInLink       = false;
    m_bInHead       = false;
    m_bFirst     	= true;
    m_bSpan			= false;
    m_bPara			= false;
    m_bParaStart	= false;
    m_bRTL			= false;
    m_bParaEnd		= false;
    RTL				= false;
    if (m_bUseSmiles){
        for (unsigned i = 0; ;i++){
            const smile *s = smiles(i);
            if (s == NULL)
                break;
#if COMPAT_QT_VERSION < 0x030000
            string str;
            for (const char *p = s->exp;; p++){
                if ((*p == 0) || (*p == '|')){
                    if (!str.empty()){
                        Smile ss;
                        ss.nSmile = i;
                        ss.re = QRegExp(str.c_str());
                        if (ss.re.isValid())
                            m_smiles.push_back(ss);
                    }
                    if (*p == 0)
                        break;
                    str = "";
                    continue;
                }
                if (*p == '\\'){
                    if (*(++p) == 0)
                        break;
                    str += '\\';
                    str += *p;
                    continue;
                }
                str += *p;
            }
#else
            if (*(s->exp)){
                Smile ss;
                ss.nSmile = i;
                ss.re = QRegExp(s->exp);
                if (ss.re.isValid())
                    m_smiles.push_back(ss);
            }
#endif
        }
    }
}
Example #3
0
void ViewParser::tag_start(const QString &tag, const list<QString> &options)
{
	if (tag == "img"){
		QString src;
        for (list<QString>::const_iterator it = options.begin(); it != options.end(); ++it){
            QString opt = *it;
            ++it;
            QString val = *it;
			if (opt == "src"){
				src = val;
				break;
			}
		}
		if (src.left(10) == "icon:smile"){
			bool bOK;
			unsigned nSmile = src.mid(10).toUInt(&bOK, 16);
			if (bOK){
				const smile *s = smiles(nSmile);
				if (s == NULL)
					return;
				if (*s->exp == 0){
					res += quoteString(s->paste);
					return;
				}
			}
		}
	}
    if (m_bIgnoreColors){
        const char **p;
        for (p = formatTags; *p; p++)
            if (tag == *p)
                break;
        if (*p == NULL)
            return;
    }
    res += "<";
    res += tag;
    if (!m_bIgnoreColors){
        for (list<QString>::const_iterator it = options.begin(); it != options.end(); ++it){
            QString opt = *it;
            ++it;
            QString val = *it;
            res += " ";
            res += opt;
            if (!val.isEmpty()){
                res += "=\"";
                res += quoteString(val);
                res += "\"";
            }
        }
    }
    res += ">";
}
Example #4
0
void JabberImageParser::tag_start(const QString &tag, const list<QString> &attrs)
{
    if (tag == "img"){
        QString src;
        for (list<QString>::const_iterator it = attrs.begin(); it != attrs.end(); ++it){
            QString name = *it;
            ++it;
            QString value = *it;
            if (name == "src"){
                src = value;
                break;
            }
        }
        if (src.left(10) != "icon:smile")
            return;
        bool bOK;
        unsigned nIcon = src.mid(10).toUInt(&bOK, 16);
        if (!bOK)
            return;
        const smile *p = smiles(nIcon);
            if (p){
                res += p->paste;
                return;
            }
    }
    if (tag == "p"){
        if (m_bPara){
            res += "<br/>";
            m_bPara = false;
        }
        return;
    }
	if (tag == "br"){
		res += "<br/>";
		return;
	}
    res += "<";
    res += tag;
    for (list<QString>::const_iterator it = attrs.begin(); it != attrs.end(); ++it){
        QString name = *it;
        ++it;
        QString value = *it;
        res += " ";
        res += name;
        if (!value.isEmpty()){
            res += "=\"";
            res += quoteString(value);
            res += "\"";
        }
    }
    res += ">";
}
Example #5
0
void UnquoteParser::tag_start(const QString &tag, const list<QString> &options)
{
    if (tag == "pre"){
        if (!m_bPre)
            res += "\n";
    }else if (tag == "br"){
        res += "\n";
    }else if (tag == "hr"){
        if (!res.isEmpty() && (res[(int)(res.length() - 1)] != '\n'))
            res += "\n";
        res += "---------------------------------------------------\n";
    }else if (tag == "td"){
        if (m_bTD){
            res += "\t";
            m_bTD = false;
        }
    }else if (tag == "tr"){
        if (m_bTR){
            res += "\n";
            m_bTR = false;
        }
    }else if (tag == "p"){
        if (m_bPar){
            res += "\n";
            m_bPar = false;
        }
    }else if (tag == "img"){
        QString src;
        for (list<QString>::const_iterator it = options.begin(); it != options.end(); ++it){
            QString opt   = *it;
            ++it;
            QString value = *it;
            if (opt == "src")
                src = value;
        }
        if (src.left(10) != "icon:smile")
            return;
        bool bOk;
        unsigned nSmile = src.mid(10).toUInt(&bOk, 16);
        if (!bOk)
            return;
        const smile *s = smiles(nSmile);
        if (s){
            res += s->paste;
            return;
        }
        s = defaultSmiles(nSmile);
        if (s)
            res += s->paste;
    }
}
Example #6
0
void ViewParser::text(const QString &text)
{
    if (!m_bUseSmiles)
        res += text;
    QString str = text;
    list<Smile> s;
    for (unsigned i = 0;; i++){
        const char *p = smiles(i);
        if (p == 0)
            break;
        for (; *p; p += strlen(p) + 1){
            Smile sm;
            sm.nSmile = i;
            sm.smile  = p;
            sm.pos    = str.find(p);
            s.push_back(sm);
        }
    }
    for (;;){
        unsigned pos = (unsigned)(-1);
        Smile *curSmile = NULL;
        list<Smile>::iterator it;
        for (it = s.begin(); it != s.end(); ++it){
            if ((*it).pos < 0)
                continue;
            if ((unsigned)((*it).pos) < pos){
                pos = (*it).pos;
                curSmile = &(*it);
            }
        }
        if (curSmile == NULL)
            break;
        if (pos)
            res += quoteString(str.left(pos));
        res += "<img src=\"icon:smile";
        res += QString::number(curSmile->nSmile, 16).upper();
        res += "\">";
        int len = pos + curSmile->smile.size();
        str = str.mid(len);
        for (it = s.begin(); it != s.end(); ++it){
            if ((*it).pos < 0)
                continue;
            (*it).pos -= len;
            if ((*it).pos < 0)
                (*it).pos = str.find((*it).smile.c_str());
        }
    }
    res += quoteString(str);
}
Example #7
0
ViewParser::ViewParser(bool bIgnoreColors, bool bUseSmiles)
{
    m_bIgnoreColors = bIgnoreColors;
    m_bUseSmiles    = bUseSmiles;
    if (m_bUseSmiles){
        for (unsigned i = 0; ;i++){
            const smile *s = smiles(i);
            if (s == NULL)
                break;
#if QT_VERSION < 300
            string str;
            for (const char *p = s->exp;; p++){
                if ((*p == 0) || (*p == '|')){
                    if (!str.empty()){
                        Smile ss;
                        ss.nSmile = i;
                        ss.re = QRegExp(str.c_str());
                        if (ss.re.isValid())
                            m_smiles.push_back(ss);
                    }
                    if (*p == 0)
                        break;
                    continue;
                }
                if (*p == '\\'){
                    if (*(++p) == 0)
                        break;
                    str += '\\';
                    str += *p;
                    continue;
                }
                str += *p;
            }
#else
            if (*(s->exp)){
                Smile ss;
                ss.nSmile = i;
                ss.re = QRegExp(s->exp);
                if (ss.re.isValid())
                    m_smiles.push_back(ss);
            }
#endif
        }
    }
}
Example #8
0
void YahooParser::tag_start(const QString &tag, const list<QString> &options)
{
    if (tag == "img") {
        QString src;
        for (list<QString>::const_iterator it = options.begin(); it != options.end(); ++it) {
            QString name = (*it);
            ++it;
            QString value = (*it);
            if (name == "src") {
                src = value;
                break;
            }
        }
        if (src.left(10) != "icon:smile")
            return;
        bool bOK;
        unsigned nSmile = src.mid(10).toUInt(&bOK, 16);
        if (!bOK)
            return;
        const smile *p = smiles(nSmile);
        if (p)
            text(p->paste);
        return;
    }
    if (tag == "br") {
        res += "\n";
        return;
    }
    style s = curStyle;
    s.tag = tag;
    tags.push(s);
    if (tag == "p") {
        if (!m_bFirst)
            res += "\n";
        m_bFirst = false;
    }
    if (tag == "font") {
        for (list<QString>::const_iterator it = options.begin(); it != options.end(); ++it) {
            QString name = *it;
            ++it;
            if (name == "color") {
                QColor c;
                c.setNamedColor(*it);
                s.color = c.rgb() & 0xFFFFFF;
            }
        }
    }
    if (tag == "b") {
        s.state |= 1;
        return;
    }
    if (tag == "i") {
        s.state |= 2;
        return;
    }
    if (tag == "u") {
        s.state |= 4;
        return;
    }
    for (list<QString>::const_iterator it = options.begin(); it != options.end(); ++it) {
        QString name = *it;
        ++it;
        if (name != "style")
            continue;
        list<QString> styles = parseStyle(*it);
        for (list<QString>::iterator its = styles.begin(); its != styles.end(); ++its) {
            QString name = *its;
            ++its;
            if (name == "color") {
                QColor c;
                c.setNamedColor(*its);
                s.color = c.rgb() & 0xFFFFFF;
            }
            if (name == "font-size") {
                unsigned size = atol((*its).latin1());
                if (size)
                    s.size = size;
            }
            if (name == "font-family")
                s.face = (*its);
            if (name == "font-weight")
                s.state &= ~1;
            if (atol((*its).latin1()) >= 600)
                s.state |= 1;
            if ((name == "font-style") && ((*its) == "italic"))
                s.state |= 2;
            if ((name == "text-decoration") && ((*its) == "underline"))
                s.state |= 4;
        }
    }
    set_style(s);
}
Example #9
0
void ViewParser::tag_start(const QString &tag, const list<QString> &attrs)
{
    // the tag that will be actually written out
    QString oTag = tag;

    if (m_bInHead)
        return;

    QString style;

    if (tag == "img"){
        QString src;
        for (list<QString>::const_iterator it = attrs.begin(); it != attrs.end(); ++it){
            QString name = (*it).lower();
            ++it;
            QString value = *it;
            if (name == "src"){
                src = value;
                break;
            }
        }
        if (src.left(10) == "icon:smile"){
            bool bOK;
            unsigned nSmile = src.mid(10).toUInt(&bOK, 16);
            if (bOK){
                const smile *s = smiles(nSmile);
                if (s == NULL)
                    return;
                if (*s->exp == 0){
                    res += quoteString(s->paste);
                    return;
                }
            }
        }
    }else if (tag == "a"){
        m_bInLink = true;
    }else if (tag == "html"){ // we display as a part of a larger document
        return;
    }else if (tag == "head"){
        m_bInHead = 1;
        return;
    }else if (tag == "body"){ // we display as a part of a larger document
        oTag = "span";
    }else if (tag == "p"){
        bool bRTL = false;
        m_bParaEnd = false;
        for (list<QString>::const_iterator it = attrs.begin(); it != attrs.end(); ++it){
            QString name = (*it).lower();
            ++it;
            QString value = *it;
            if ((name == "dir") && (value.lower() == "rtl"))
                bRTL = true;
        }
        if (m_bPara){
            if (bRTL == m_bRTL){
                res += "<br/>";
            }else{
                if (m_bParaStart)
                    res += "</p>";
                res += "<p dir=\"";
                res += bRTL ? "rtl" : "ltr";
                res += "\">";
                m_bParaStart = true;
            }
        }else{
            RTL     = bRTL;
            m_bRTL  = bRTL;
            m_bPara = true;
        }
        return;
    }
    if (m_bParaEnd){
        res += "<br/>";
        m_bParaEnd = false;
    }
    QString tagText;
    tagText += "<";
    tagText += oTag;
    for (list<QString>::const_iterator it = attrs.begin(); it != attrs.end(); ++it){
        QString name = (*it).lower();
        ++it;
        QString value = *it;

        // Handling for attributes of specific tags.
        if (tag == "body"){
            if (name == "bgcolor"){
                style += "background-color:" + value + ";";
                continue;
            }
        }else if (tag == "font"){
            if (name == "color" && m_bIgnoreColors)
                continue;
        }

        // Handle for generic attributes.
        if (name == "style"){
            style += value;
            continue;
        }

        tagText += " ";
        tagText += name;
        if (!value.isEmpty()){
            tagText += "=\"";
            tagText += value;
            tagText += "\"";
        }
    }

    // Quite crude but working CSS to remove color styling.
    // It won't filter out colors as part of 'background', but life's tough.
    // (If it's any comfort, Qt probably won't display it either.)
    if (!style.isEmpty()){
        if (m_bIgnoreColors){
            list<QString> opt = parseStyle(style);
            list<QString> new_opt;
            for (list<QString>::iterator it = opt.begin(); it != opt.end(); ++it){
                QString name = *it;
                it++;
                if (it == opt.end())
                    break;
                QString value = *it;
                if ((name == "color") ||
                        (name == "background-color") ||
                        (name == "font-size") ||
                        (name == "font-style") ||
                        (name == "font-weight") ||
                        (name == "font-family"))
                    continue;
                new_opt.push_back(name);
                new_opt.push_back(value);
            }
            style = makeStyle(new_opt);
        }
        if (!style.isEmpty())
            tagText += " style=\"" + style + "\"";
    }
    tagText += ">";
    res += tagText;
}
Example #10
0
bool FastSearchFormat::ObtainTarget(OBConversion* pConv, OBMol& patternMol, const string& indexname)
{
    //Obtains an OBMol
    //   either from the SMARTS string in the -s option
    //   or by converting the file in the -S option
    //or, if neither option is provided, displays information on the index file.

    stringstream smiles(stringstream::out);
    ifstream patternstream;
    OBConversion PatternConv(&patternstream,&smiles);

    const char* p = pConv->IsOption("s",OBConversion::GENOPTIONS);
    string txt;
    if(p)
    {
        // Use the -s option
        txt=p;
        stringstream smarts(txt, stringstream::in);
        OBConversion Convsm(&smarts);
        if(!Convsm.SetInFormat("smi")) return false;
        Convsm.Read(&patternMol);

        //erase -s option in GeneralOptions since it will be rewritten
        pConv->RemoveOption("s",OBConversion::GENOPTIONS);
        if(patternMol.Empty())
        {
            obErrorLog.ThrowError(__FUNCTION__,
                                  "Could not make a molecule from " + smarts.str()
                                  + "\nThis needs to be valid SMILES when using fastsearch."
                                  "You can use the more versatile SMARTS in a normal substructure search." , obError);
            return false;
        }
    }
    else
    {
        // or Make OBMol from file in -S option or -aS option
        p = pConv->IsOption("S",OBConversion::GENOPTIONS);
        if(!p)
            p = pConv->IsOption("S",OBConversion::INOPTIONS);//for GUI mainly
    }

    if(!p)
    {
        //neither -s or -S options provided. Output info rather than doing search
        const FptIndexHeader& header = fs.GetIndexHeader();
        string id(header.fpid);
        if(id.empty())
            id = "default";
        clog << indexname << " is an index of\n " << header.datafilename
             << ".\n It contains " << header.nEntries
             << " molecules. The fingerprint type is " << id << " with "
             << OBFingerprint::Getbitsperint() * header.words << " bits.\n"
             << "Typical usage for a substructure search:\n"
             << "babel indexfile.fs -osmi -sSMILES" << endl;
        return false;
    }

    if(p && patternMol.Empty())
    {
        txt=p;
        string::size_type pos = txt.find_last_of('.');
        if(pos==string::npos)
        {
            obErrorLog.ThrowError(__FUNCTION__, "Filename of pattern molecule in -S option must have an extension", obError);
            return false;
        }
        patternstream.open(txt.c_str());
        if(!patternstream)
        {
            stringstream errorMsg;

            errorMsg << "Cannot open " << txt << endl;
            obErrorLog.ThrowError(__FUNCTION__, errorMsg.str(), obError);
            return false;
        }

        PatternConv.SetOneObjectOnly();
        if(PatternConv.SetInFormat(txt.substr(pos+1).c_str()))
            PatternConv.Read(&patternMol);
    }

    if(patternMol.Empty())
    {
        obErrorLog.ThrowError(__FUNCTION__, "Cannot derive a molecule from the -s or -S options", obWarning);
        return false;
    }
    patternMol.ConvertDativeBonds();//use standard form for dative bonds

    //Convert to SMILES and generate a -s option for use in the final filtering
    if(!PatternConv.SetOutFormat("smi"))
        return false;
    PatternConv.Write(&patternMol);
    //remove name to leave smiles string
    string smilesstr(smiles.str());
    string::size_type pos = smilesstr.find_first_of(" \t\r\n");
    if(pos!=string::npos)
        smilesstr = smilesstr.substr(0,pos);
    pConv->AddOption("s", OBConversion::GENOPTIONS, smilesstr.c_str());

    return true;
}