c2bConsole.cpp 2.64 KB
Newer Older
1
/***************************************************************************
2
 *   Copyright (C) 2004-2018 by Pere Constans
3
 *   constans@molspaces.com
4
 *   cb2Bib version 1.9.9. Licensed under the GNU GPL version 3.
5 6 7 8 9 10 11 12 13 14
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "c2bConsole.h"

#include "c2bSettings.h"
#include "c2bTests.h"
#include "c2bNetworkQuery.h"

#include <bibExtractor.h>
#include <bibParser.h>
15
#include <collectionIndex.h>
16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <metadataParser.h>
#include <texToHtml.h>

#include <QCoreApplication>
#include <QTimer>


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

c2bConsole::~c2bConsole()
{}


30
void c2bConsole::load()
31 32 33 34 35 36 37 38 39 40 41 42 43 44
{
    // Send _process to application cue
    QTimer::singleShot(0, this, SLOT(_process()));
}

void c2bConsole::_process()
{
    int code(0);
    if (c2bSettingsP->cl_html_annote)
        code = cl_html_annote();
    else if (c2bSettingsP->cl_doc2bib)
        code = cl_doc2bib();
    else if (c2bSettingsP->cl_txt2bib)
        code = cl_txt2bib();
45 46
    else if (c2bSettingsP->cl_index)
        code = cl_index();
47 48 49 50 51 52 53 54 55
    else if (c2bSettingsP->cl_cb2bib_tests)
        code = cl_cb2bib_tests();
    QCoreApplication::exit(code);
}

int c2bConsole::cl_html_annote()
{
    if (QFileInfo(c2bSettingsP->cl_annote_filename).exists())
    {
56 57
        const QString tex(c2bUtils::fileToString(c2bSettingsP->cl_annote_filename));
        const QString htmlf(c2bSettingsP->cl_annote_filename + ".html");
58 59 60 61 62 63
        texToHtml t2h;
        t2h.toHtml(tex, htmlf);
        return 0;
    }
    else
    {
64
        c2bUtils::warn(QObject::tr("Error: Could not open %1 file for reading").arg(c2bSettingsP->cl_annote_filename));
65 66 67 68 69 70 71 72 73 74
        return 1;
    }
}

int c2bConsole::cl_doc2bib()
{
    bibParser bp;
    metadataParser mp(&bp);
    c2bNetworkQuery nq(&bp); // needs c2bUtils::fromHtmlString
    bibExtractor be(&bp, &mp, &nq);
75
    const int c(be.extract(c2bSettingsP->cl_extract_input_filenames, c2bSettingsP->cl_extract_output_filename, bibExtractor::Document));
76 77 78 79 80 81 82 83 84
    return c;
}

int c2bConsole::cl_txt2bib()
{
    bibParser bp;
    metadataParser mp(&bp);
    c2bNetworkQuery nq(&bp);
    bibExtractor be(&bp, &mp, &nq);
85 86 87 88 89 90 91 92 93
    const int c(be.extract(c2bSettingsP->cl_extract_input_filenames, c2bSettingsP->cl_extract_output_filename, bibExtractor::PlainText));
    return c;
}

int c2bConsole::cl_index()
{
    bibParser bp;
    collectionIndex ci(&bp);
    const int c(ci.index(c2bSettingsP->cl_index_dirname));
94 95 96 97 98 99
    return c;
}

int c2bConsole::cl_cb2bib_tests()
{
    c2bTests* t = new c2bTests();
100
    const int passed(t->allTests());
101 102 103
    delete t;
    return passed;
}