You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kbibtex/src/idsuggestions.cpp

364 lines
14 KiB

12 years ago
/***************************************************************************
* Copyright (C) 2004-2009 by Thomas Fischer *
* fischer@unix-ag.uni-kl.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <ntqregexp.h>
12 years ago
#include <tdelocale.h>
12 years ago
#include <encoderlatex.h>
#include <file.h>
#include <settings.h>
#include <entry.h>
#include "idsuggestions.h"
namespace KBibTeX
{
const TQRegExp IdSuggestions::unwantedChars = TQRegExp( "[^-_:/=+a-zA-Z0-9]+" );
12 years ago
IdSuggestions::IdSuggestions()
{
// nothing
}
IdSuggestions::~IdSuggestions()
{
// nothing
}
/**
* Determine list of authors or editors for a given entry
*/
TQStringList IdSuggestions::authorsLastName( BibTeX::Entry *entry )
12 years ago
{
TQStringList result;
12 years ago
/** retrieve field holding authors information for entry */
BibTeX::EntryField *field = entry->getField( BibTeX::EntryField::ftAuthor );
if ( field == NULL )
{
/** no author field available, try editor
patch by Jurgen Spitzmuller <juergen.sp@t-online.de> */
field = entry->getField( BibTeX::EntryField::ftEditor );
if ( field == NULL )
{
return result; /** neither author nor editor available */
}
}
/** fetch container holding list of author names */
BibTeX::PersonContainer *personContainer = field != NULL ? dynamic_cast<BibTeX::PersonContainer*>( field->value()->items.isEmpty() ? NULL : field->value()->items.first() ) : NULL;
if ( personContainer == NULL || personContainer->persons.isEmpty() )
return result; /** container not found or is empty */
/** iterate through container and fetch each author's last name */
for ( TQValueList<BibTeX::Person*>::ConstIterator it = personContainer->persons.begin(); it != personContainer->persons.end(); ++it )
12 years ago
{
result.append( normalizeText(( *it )->lastName() ) );
}
return result;
}
/**
* Normalize a given text by removing spaces or other unwanted characters,
* replace some accented and special characters by plain ASCII transliterations
*/
TQString IdSuggestions::normalizeText( const TQString& text )
12 years ago
{
TQString result = text;
12 years ago
for ( int i = text.length() - 1; i >= 0; --i )
result[i] = TQChar( BibTeX::EncoderLaTeX::unicodeToASCII( result[i].unicode() ) );
12 years ago
return result.replace( unwantedChars, "" );
}
TQString IdSuggestions::resolveConflict( BibTeX::File *file, const TQString &id, BibTeX::Element *element )
12 years ago
{
TQString result = id;
12 years ago
BibTeX::Element *hit = file->containsKey( id ) ;
if ( hit != NULL && hit != element )
{
int i = 0;
do
{
result = TQString( "%1-%2" ).arg( id ).arg( ++i );
12 years ago
hit = file->containsKey( result );
}
while ( hit != NULL && hit != element );
}
return result;
}
/**
* Determine year for a given entry
*/
int IdSuggestions::extractYear( BibTeX::Entry *entry )
{
/** retrieve field holding year information for entry */
BibTeX::EntryField *field = entry->getField( BibTeX::EntryField::ftYear );
if ( field == NULL )
return -1; /** no year field available */
/** fetch value item holding year */
BibTeX::ValueItem *valueItem = field != NULL ? ( field->value()->items.isEmpty() ? NULL : field->value()->items.first() ) : NULL;
if ( valueItem == NULL )
return -1; /** no value item found or is empty */
/** parse value item's text */
bool ok = FALSE;
TQRegExp yearRegExp( "\\b(\\d{2})?\\d{2}\\b" );
12 years ago
yearRegExp.search( valueItem->text() );
int year = TQString( yearRegExp.cap( 0 ) ).toInt( &ok );
12 years ago
if ( !ok ) year = -1;
return year;
}
/**
* Determine title for a given entry
*/
TQString IdSuggestions::extractTitle( BibTeX::Entry *entry )
12 years ago
{
/** retrieve field holding title information for entry */
BibTeX::EntryField *field = entry->getField( BibTeX::EntryField::ftTitle );
if ( field == NULL )
return TQString::null; /** no title field available */
12 years ago
/** *fetch value item holding title */
BibTeX::ValueItem *valueItem = field->value()->items.isEmpty() ? NULL : field->value()->items.first();
if ( valueItem == NULL )
return TQString::null; /** no value item found or is empty */
12 years ago
return valueItem->text();
}
TQStringList IdSuggestions::createSuggestions( BibTeX::File *file, BibTeX::Entry *entry )
12 years ago
{
Settings * settings = Settings::self();
const TQStringList& formatStrList = settings->idSuggestions_formatStrList;
TQStringList result;
TQStringList allKeys = file != NULL ? file->allKeys() : TQStringList();
12 years ago
entry = new BibTeX::Entry( entry );
if ( file != NULL )
file->completeReferencedFields( entry );
for ( TQStringList::ConstIterator it = formatStrList.begin(); it != formatStrList.end(); ++it )
12 years ago
{
TQString id = formatId( entry, *it );
12 years ago
if ( id.isEmpty() || result.contains( id ) )
continue;
if ( !result.contains( id ) )
result.append( id );
}
delete entry;
return result;
}
TQString IdSuggestions::createDefaultSuggestion( BibTeX::File *file, BibTeX::Entry *entry )
12 years ago
{
Settings * settings = Settings::self();
if ( settings->idSuggestions_default < 0 || settings->idSuggestions_default >= ( int )settings->idSuggestions_formatStrList.size() )
return TQString::null;
12 years ago
entry = new BibTeX::Entry( entry );
if ( file != NULL )
file->completeReferencedFields( entry );
TQString result = formatId( entry, settings->idSuggestions_formatStrList[settings->idSuggestions_default] );
12 years ago
delete entry;
return result;
}
TQString IdSuggestions::formatId( BibTeX::Entry *entry, const TQString& formatStr )
12 years ago
{
TQString id;
TQStringList tokenList = TQStringList::split( '|', formatStr );
for ( TQStringList::ConstIterator tit = tokenList.begin(); tit != tokenList.end(); ++tit )
12 years ago
id.append( translateToken( entry, *tit ) );
return id;
}
TQString IdSuggestions::translateToken( BibTeX::Entry *entry, const TQString& token )
12 years ago
{
switch ( token[0] )
{
case 'a': return translateAuthorsToken( entry, token.mid( 1 ), aOnlyFirst );
case 'A': return translateAuthorsToken( entry, token.mid( 1 ), aAll );
case 'z': return translateAuthorsToken( entry, token.mid( 1 ), aNotFirst );
case 'y':
{
int year = extractYear( entry );
if ( year > -1 )
return TQString::number( year % 100 + 100 ).mid( 1 );
12 years ago
else
return TQString::null;
12 years ago
}
case 'Y':
{
int year = extractYear( entry );
if ( year > -1 )
return TQString::number( year % 10000 + 10000 ).mid( 1 );
12 years ago
else
return TQString::null;
12 years ago
}
case 't': return translateTitleToken( entry, token.mid( 1 ), FALSE );
case 'T': return translateTitleToken( entry, token.mid( 1 ), TRUE );
case '"': return token.mid( 1 );
default: return TQString::null;
12 years ago
}
}
TQString IdSuggestions::translateAuthorsToken( BibTeX::Entry *entry, const TQString& token, Authors selectAuthors )
12 years ago
{
struct IdSuggestionTokenInfo ati = evalToken( token );
TQString result;
12 years ago
bool first = true, firstInserted = true;
TQStringList authors = authorsLastName( entry );
for ( TQStringList::ConstIterator it = authors.begin(); it != authors.end(); ++it )
12 years ago
{
TQString author = normalizeText( *it ).left( ati.len );
12 years ago
if ( selectAuthors == aAll || ( selectAuthors == aOnlyFirst && first ) || ( selectAuthors == aNotFirst && !first ) )
{
if ( !firstInserted )
result.append( ati.inBetween );
result.append( author );
firstInserted = false;
}
first = false;
}
if ( ati.toUpper )
result = result.upper();
else if ( ati.toLower )
result = result.lower();
return result;
}
struct IdSuggestionTokenInfo IdSuggestions::evalToken( const TQString& token )
12 years ago
{
unsigned int pos = 0;
struct IdSuggestionTokenInfo result;
result.len = 0x00ffffff;
result.toLower = FALSE;
result.toUpper = FALSE;
result.inBetween = TQString::null;
12 years ago
if ( token.length() > pos )
{
int dv = token[pos].digitValue();
if ( dv > -1 )
{
result.len = dv;
++pos;
}
}
if ( token.length() > pos )
{
result.toLower = token[pos] == 'l';
result.toUpper = token[pos] == 'u';
if ( result.toUpper || result.toLower )
++pos;
}
if ( token.length() > pos + 1 && token[pos] == '"' )
result.inBetween = token.mid( pos + 1 );
return result;
}
TQString IdSuggestions::translateTitleToken( BibTeX::Entry *entry, const TQString& token, bool removeSmallWords )
12 years ago
{
struct IdSuggestionTokenInfo tti = evalToken( token );
Settings * settings = Settings::self();
const TQStringList smallWords = settings->idSuggestions_smallWords;
12 years ago
TQString result;
12 years ago
bool first = TRUE;
TQStringList titleWords = TQStringList::split( TQRegExp( "\\s+" ), extractTitle( entry ) );
for ( TQStringList::ConstIterator it = titleWords.begin(); it != titleWords.end(); ++it )
12 years ago
{
if ( first )
first = FALSE;
else
result.append( tti.inBetween );
TQString lowerText = ( *it ).lower();
12 years ago
if ( !removeSmallWords || !smallWords.contains( lowerText ) )
result.append( normalizeText( *it ).left( tti.len ) );
}
if ( tti.toUpper )
result = result.upper();
else if ( tti.toLower )
result = result.lower();
return result;
}
/** convert a formatting string into a human readable version (even translated) */
TQString IdSuggestions::formatStrToHuman( const TQString& formatStr )
12 years ago
{
bool first = TRUE;
TQString text;
TQStringList elements = TQStringList::split( '|', formatStr );
for ( TQStringList::iterator it = elements.begin();it != elements.end();++it )
12 years ago
{
if ( first ) first = FALSE; else text.append( "\n" );
if (( *it )[0] == 'a' || ( *it )[0] == 'A' || ( *it )[0] == 'z' )
{
struct IdSuggestionTokenInfo info = evalToken(( *it ).mid( 1 ) );
if (( *it )[0] == 'a' ) text.append( i18n( "First author only" ) );
else if (( *it )[0] == 'z' ) text.append( i18n( "All but first author" ) );
else text.append( i18n( "All authors" ) );
int n = info.len;
if ( info.len < 0x00ffffff ) text.append( i18n( ", but only first letter of each last name", ", but only first %n letters of each last name", n ) );
if ( info.toUpper ) text.append( i18n( ", in upper case" ) );
else if ( info.toLower ) text.append( i18n( ", in lower case" ) );
if ( info.inBetween != TQString::null ) text.append( TQString( i18n( ", with '%1' in between" ) ).arg( info.inBetween ) );
12 years ago
}
else if (( *it )[0] == 'y' ) text.append( i18n( "Year (2 digits)" ) );
else if (( *it )[0] == 'Y' ) text.append( i18n( "Year (4 digits)" ) );
else if (( *it )[0] == 't' || ( *it )[0] == 'T' )
{
struct IdSuggestionTokenInfo info = evalToken(( *it ).mid( 1 ) );
text.append( i18n( "Title" ) );
int n = info.len;
if ( info.len < 0x00ffffff ) text.append( i18n( ", but only first letter of each word", ", but only first %n letters of each word", n ) );
if ( info.toUpper ) text.append( i18n( ", in upper case" ) );
else if ( info.toLower ) text.append( i18n( ", in lower case" ) );
if ( info.inBetween != TQString::null ) text.append( TQString( i18n( ", with '%1' in between" ) ).arg( info.inBetween ) );
12 years ago
if (( *it )[0] == 'T' ) text.append( i18n( ", small words removed" ) );
}
else if (( *it )[0] == '"' ) text.append( TQString( i18n( "Text: '%1'" ) ).arg(( *it ).mid( 1 ) ) );
12 years ago
else text.append( "?" );
}
return text;
}
}