Logo Search packages:      
Sourcecode: cb2bib version File versions

int bibExtractor::extract ( const QStringList &  input_fns,
const QString &  output_fn,
const InputType &  type 
)

Extracts a bibliographic reference from files input_fns and writes it to the BibTeX file output_fn

Definition at line 56 of file bibExtractor.cpp.

References bibParser::currentReference(), c2bUtils::documentAbsoluteName(), bibParser::documentFilename(), extract(), bibParser::fieldCount(), network::getFile(), networkQuery::networkPtr(), bibParser::setField(), and bibParser::toBibTeX().

{
    _input_type = type;
    QString bibtex;
    for (int i = 0; i < input_fns.count(); ++i)
    {
        const QString& input_fn = input_fns.at(i);
        if (!QFileInfo(input_fn).exists())
        {
            logMessage(tr("Error: File %1 does not exist.").arg(input_fn));
            continue;
        }
        logMessage(tr("Processing %1...").arg(input_fn));
        if (_input_type == Document)
        {
            _document_fn = input_fn;
            const QString text = documentToString(input_fn);
            extract(text);
        }
        else
        {
            _document_fn.clear();
            const QString text = c2bUtils::fileToString(input_fn);
            extract(text);
        }
        if (_settingsP->value("cb2Bib/MovePdf").toBool() && !_document_fn.isEmpty() && _bpP->fieldCount() > 0)
        {
            const QString file = _bpP->documentFilename(_document_fn, output_fn);
            const QString target_doc_fn = c2bUtils::documentAbsoluteName(_settingsP->value("cb2Bib/RelativePdfDirectory").toBool(),
                                          output_fn, _settingsP->fileName("cb2Bib/PdfDirectory"), file);
            const QString doc_dirname = QFileInfo(target_doc_fn).absolutePath();
            QDir doc_dir(doc_dirname);
            if (!doc_dir.exists())
                if (!doc_dir.mkpath(doc_dirname))
                    logMessage(tr("Unable to create directory %1.").arg(doc_dirname));
            if (doc_dir.exists())
            {
                const QString CopyMovePdf = _settingsP->value("cb2Bib/CopyMovePdf").toString();
                logMessage(tr("Copying/moving %1...").arg(_document_fn));
                _networkQP->networkPtr()->getFile(_document_fn, target_doc_fn, CopyMovePdf, this, SLOT(copyMoveEnded(bool)));
                _event_loop->exec();
                if (_is_copymove_ok)
                {
                    if (_settingsP->value("cb2Bib/InsertMetadata").toBool())
                    {
                        QString mp_error;
                        if (!_mpP->insertMetadata(_bpP->currentReference(), target_doc_fn, &mp_error))
                        {
                            logMessage(tr("Unable to insert metadata to file %1.").arg(target_doc_fn));
                            logMessage(mp_error);
                        }
                    }
                    _document_fn = file;
                }
                else
                {
                    logMessage(tr("Skipping file field %1.").arg(_document_fn));
                    _document_fn.clear();
                }
            }
        }
        if (_bpP->fieldCount() > 0)
        {
            if (!_document_fn.isEmpty())
                _bpP->setField("file", _document_fn);
            bibtex += _bpP->toBibTeX();
        }
        else
            logMessage(tr("Empty reference for file %1.").arg(input_fn));
        logMessage("");
    }
    if (bibtex.isEmpty())
        logMessage(tr("No reference extracted."));
    else
    {
        logMessage(tr("Writing %1...").arg(output_fn));
        if (c2bUtils::stringToFile(bibtex, output_fn))
            return 0;
        else
            logMessage(tr("Error: Unable to write file %1.").arg(output_fn));
    }
    return 1;
}


Generated by  Doxygen 1.6.0   Back to index