File: c2bExport.cpp

package info (click to toggle)
cb2bib 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,368 kB
  • sloc: cpp: 24,179; sh: 481; makefile: 16
file content (68 lines) | stat: -rw-r--r-- 2,691 bytes parent folder | download
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
/***************************************************************************
 *   Copyright (C) 2004-2015 by Pere Constans
 *   constans@molspaces.com
 *   cb2Bib version 1.9.2. Licensed under the GNU GPL version 3.
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "c2bExport.h"

#include "c2b.h"

#include <cb2bib_parameters.h>


c2bExport::c2bExport(QObject* parento) : QObject(parento)
{}

c2bExport::~c2bExport()
{}


void c2bExport::exportEntries(const export_data& ed)
{
    // Note. Assume caller performs export_data validity check for files and directories.
    // Prefer replacing filenames to a full BibTeX parsing: This will preserve the
    // original entry and possibly available non cb2Bib tags.
    bibParser* bp = c2b::bibParser();
    QStringList fields;
    if (ed.copy_documents)
        fields.append("file");
    QRegExp filere("\\bfile\\s*=\\s*[\\{\"](.*)[\\}\"]", Qt::CaseInsensitive);
    filere.setMinimal(true);
    QString export_contents("\n\n");
    QString export_log;
    bibReference ref;
    bp->initReferenceParsing(ed.bibtex_fn, fields, &ref);
    while (bp->referencesIn(ed.bibtex_contents, &ref))
    {
        if (!ed.bibtex_keys.contains(ref.citeidName, Qt::CaseSensitive))
            continue;
        if (ed.copy_documents)
        {
            QString sfn(ref.value("file"));
            if (!sfn.isEmpty())
            {
                QString dfn(c2bUtils::documentAbsoluteName(ed.relative_dir, ed.export_fn, ed.documents_dir, sfn));
                QFile source(sfn);
                if (!source.copy(dfn))
                    export_log += tr("% Unable to copy file %1 to %2. %3.\n").arg(sfn).arg(dfn).arg(source.errorString());
                if (QFileInfo(dfn).exists())
                {
                    const int pos(filere.indexIn(ref.rawReference, 0));
                    if (pos > -1)
                    {
                        QString line(filere.cap(0));
                        QString file(filere.cap(1));
                        line.replace(file, c2bUtils::documentFilename(ed.relative_dir, ed.export_fn, ed.documents_dir, sfn));
                        ref.rawReference.replace(pos, filere.matchedLength(), line);
                    }
                }
            }
        }
        export_contents += ref.rawReference + "\n\n";
    }
    if (!export_log.isEmpty())
        export_contents = "% cb2Bib " + C2B_VERSION + " / Export Error Log\n" + export_log + export_contents;
    if (!c2bUtils::stringToFile(export_contents, ed.export_fn))
        c2bUtils::warn(tr("Unable to write file %1").arg(ed.export_fn));
}