Logo Search packages:      
Sourcecode: cb2bib version File versions


 *   Copyright (C) 2004-2009 by Pere Constans
 *   constans@molspaces.com
 *   cb2Bib version 1.3.0. Licensed under the GNU GPL version 3.
 *   See the LICENSE file that comes with this distribution.
#include "c2bSaveRegExp.h"

#include "c2b.h"
#include "c2bSaveREHighlighter.h"
#include "c2bSettings.h"
#include "c2bUtils.h"

#include <QPushButton>

/** \page regexpedit The Regular Expression Editor

    Once a manual processing is done, cb2Bib clipboard area contains the
    extraction tags, plus, possibly, some other cb2Bib tags introduced during
    the preprocessing (see \ref bibproc). The <b>RegExp Editor</b> will
    generate a guess regular expression or matching pattern usable for
    automated extractions.

    The cb2Bib matching patterns consist of four lines: a brief description,
    the reference type, an ordered list of captured fields, and the regular
    expression itself.

<pre style="background : #d5d5d5; border : #6D8CC3 solid 1px;">
# cb2Bib GET_VERSION Pattern:
American Chemical Society Publications
journal volume pages year title author abstract
^(.+), (\d+) \(.+\), ([\d|\-|\s]+),(\d\d\d\d)\..+&#060;NewLine3&#062;(.+)&#060;NewLine4&#062;

    The Regular Expression Editor provides the basic skeleton and a set of
    predefined suggestions. The regular expressions follow a Perl-like sintax.
    There are, however, some slight differences and minor limitations.
    Information about the basics on the editing and working with Regular
    Expressions as used by cb2Bib can be found at the Qt document file
<a href="http://doc.trolltech.com/4.5/qregexp.html#introduction"

    <b>Remember when creating and editing regular expressions:</b>

    - Switch the clipboard mode to 'Tagged Clipboard Data', using the clipboard
    panel context menu.

    - Extract the bibliographic reference manually. On the clipboard panel
    will appear some cb2Bib tags that indicate which fields are being
    extracted. Once done, type Alt+I to enter to the regular expression editor.
    In the editor, there are the four line edits that define a cb2Bib pattern,
    one copy of the clipboard panel, and an information panel. The
    information panel displays possible issues, and, once everything is
    correct, the actual extracted fields. The clipboard panel highlights the
    captures for the current regular expression and current input text.

    - Patterns can be modified at any time by typing Alt+E to edit the regular
    expression file. Patterns are reloaded each time the automatic pattern
    recognition is started. This permits editing and testing.

    - The cb2bib processes sequentially the list of regular expressions as
    found in the regular expression file. It stops and picks the first match
    for the current input. <b>Therefore, the order of the regular expressions
    is important</b>. Consequently, to avoid possible clashing among similar
    patterns, consider sorting them from the most restrictive pattern to the
    less one. As a rule of thumb, the more captions it has the most restrictive
    a pattern is.

    - <b>The cb2Bib proposed patterns are general, and not necessarily the most
    appropriate for a particular capture</b>. E.g. tag <tt>pages</tt> becomes
    <tt>([\\d|\\-|\\s]+)</tt>, which considers digits, hyphens, and spaces. It
    must be modified accordingly for reference sources with, e.g.,
    <tt>pages</tt> written as Roman ordinals.

    - <b>Avoid whenever possible general patterns <tt>(.+)</tt></b>. There is a
    risk that such a caption could include text intended for a posterior
    caption. This is why, sometimes, the cb2Bib proposed pattern is not hit by
    the input stream that originated it. <b>Use, whenever possible, cb2Bib
    anchors like <tt><NewLine1></tt> instead of <tt><NewLine\\d+></tt>.
    They prevent <tt>(.+)</tt> captions to overextend</b>.

    - To debug a large regular expression it might be useful to break it to the
    first capturing parenthesis. For instance, the above pattern will be

<pre style="background : #d5d5d5; border : #6D8CC3 solid 1px;">
# cb2Bib GET_VERSION Pattern:
American Chemical Society Publications

    - Then, check if anything is captured and if this corresponds to

    - Add on successive steps your set of captions and BibTeX fields.

c2bSaveRegExp::c2bSaveRegExp(const QStringList& pattern, const QString& input, QWidget* parentw) : QDialog(parentw)
    Q_ASSERT_X(pattern.count() == 3, "c2bSaveRegExp", "expected exactly three strings for pattern");
    connect(ui.buttonBox, SIGNAL(helpRequested()), this, SLOT(help()));
    c2bSettings* settings = c2bSettingsP;
    _sreS = new c2bSaveREHighlighter(_pattern_rx, ui.Input->document());
    connect(ui.Type, SIGNAL(textChanged(const QString&)), this, SLOT(setInformation()));
    connect(ui.Fields, SIGNAL(textChanged(const QString&)), this, SLOT(setInformation()));
    connect(ui.RegExp, SIGNAL(textChanged(const QString&)), this, SLOT(setInformation()));
    connect(ui.Input, SIGNAL(textChanged()), this, SLOT(inputMightHaveChanged()));


void c2bSaveRegExp::setInformation()
    QString info;
    bool can_save = false;
    const QStringList field_list = ui.Fields->text().split(' ', QString::SkipEmptyParts);
    const int fields = field_list.count();
    const int captures = _pattern_rx.numCaptures();
    if (_pattern_rx.isValid() && fields > 0 && fields == captures && !ui.Type->text().isEmpty())
        info += tr("Reference type: %1\n").arg(ui.Type->text());
        info += tr("Number of fields: %1\n").arg(fields);
        can_save = true;
        if (ui.Type->text().isEmpty())
            info += tr("[Error] Invalid pattern: empty reference type\n");
        if (fields == 0)
            info += tr("[Error] Invalid pattern: no fields declared\n");
        if (_pattern_rx.isValid())
            if (captures == 0)
                if (_pattern_rx.pattern().isEmpty())
                    info += tr("[Error] Invalid pattern: empty regular expression\n");
                    info += tr("[Error] Invalid pattern: no captures defined in the regular expression\n");
            else if (fields != captures)
                info += tr("[Error] Invalid pattern: declared %1 fields while the regular expression has %2 captures\n").
            info += tr("[Error] Invalid regular expression: %1\n").arg(_pattern_rx.errorString());
    if (can_save)
        if (_pattern_rx.indexIn(ui.Input->toPlainText()) == -1 || _pattern_rx.matchedLength() < 1)
            info += tr("[Info] Regular expression does not match input text\n");
            bibParser* bp = c2b::bibParser();
            for (int i = 0; i < fields; ++i)
                const QString& f = field_list.at(i);
                const QString v = bp->parse(f, _pattern_rx.cap(i + 1));
                info += QString("[%1]: '%2'\n").arg(f).arg(v);

void c2bSaveRegExp::updateInput(const QString& text)
    if (!ui.Input->textCursor().hasSelection())

void c2bSaveRegExp::inputMightHaveChanged()
    // Avoid recursively calling setInformation due to syntax highlighting
    if (_input_text == ui.Input->toPlainText())
    _input_text = ui.Input->toPlainText();

void c2bSaveRegExp::accept()
    const QString rx = ui.Type->text() + '\n' + ui.Fields->text() + '\n' + ui.RegExp->text();
    emit savePatternInfo(rx, ui.Name->text());

void c2bSaveRegExp::help()

Generated by  Doxygen 1.6.0   Back to index