summaryrefslogtreecommitdiffstats
path: root/src/fileexporterbibtex.cpp
blob: 240754d80cf64d87d63a98e1be75b75e880de0c8 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/***************************************************************************
*   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 <file.h>
#include <element.h>
#include <entry.h>
#include <macro.h>
#include <preamble.h>
#include <value.h>
#include <comment.h>
#include <encoderlatex.h>

#include "fileexporterbibtex.h"

namespace BibTeX
{

    FileExporterBibTeX::FileExporterBibTeX() : FileExporter(),
            m_iconvBufferSize( 16384 ), m_stringOpenDelimiter( '"' ), m_stringCloseDelimiter( '"' ), m_keywordCasing( kcCamelCase ), m_encoding( "latex" ), m_protectCasing( FALSE ), cancelFlag( FALSE )
    {
        m_iconvBuffer = new char[m_iconvBufferSize];
    }

    FileExporterBibTeX::~FileExporterBibTeX()
    {
        delete[] m_iconvBuffer;
    }

    bool FileExporterBibTeX::save( QIODevice* iodevice, const File* bibtexfile, QStringList * /*errorLog*/ )
    {
        m_mutex.lock();
        bool result = TRUE;

        /**
          * Categorize elements from the bib file into four groups,
          * to ensure that BibTeX finds all connected elements
          * in the correct order.
          */

        QValueList<Comment*> parameterCommentsList;
        QValueList<Preamble*> preambleList;
        QValueList<Macro*> macroList;
        QValueList<Entry*> crossRefingEntryList;
        QValueList<Element*> remainingList;

        for ( File::ElementList::const_iterator it = bibtexfile->elements.begin(); it != bibtexfile->elements.end() && result && !cancelFlag; it++ )
        {
            Preamble *preamble = dynamic_cast<Preamble*>( *it );
            if ( preamble != NULL )
                preambleList.append( preamble );
            else
            {
                Macro *macro = dynamic_cast<Macro*>( *it );
                if ( macro != NULL )
                    macroList.append( macro );
                else
                {
                    Entry *entry = dynamic_cast<Entry*>( *it );
                    if (( entry != NULL ) && ( entry->getField( EntryField::ftCrossRef ) != NULL ) )
                        crossRefingEntryList.append( entry );
                    else
                    {
                        Comment *comment = dynamic_cast<Comment*>( *it );
                        QString commentText = QString::null;
                        /** check if this file requests a special encoding */
                        if ( comment != NULL && comment->useCommand() && (( commentText = comment->text().lower() ) ).startsWith( "x-kbibtex-encoding=" ) )
                        {
                            m_encoding = commentText.mid( 19 );
                            qDebug( "Switching encoding to <%s>", m_encoding.latin1() );
                            parameterCommentsList.append( comment );
                        }
                        else
                            remainingList.append( *it );
                    }
                }
            }
        }

        int totalElements = ( int ) bibtexfile->count();
        int currentPos = 0;

        const char *encodingTo = m_encoding == "latex" ? "utf-8\0" : m_encoding.append( "\0" ).ascii();
        m_iconvHandle = iconv_open( encodingTo, "utf-8" );

        /** before anything else, write parameter comments */
        for ( QValueList<Comment*>::iterator it = parameterCommentsList.begin(); it != parameterCommentsList.end() && result && !cancelFlag; it++ )
        {
            result &= writeComment( *iodevice, *it );
            emit progress( ++currentPos, totalElements );
        }

        /** first, write preambles and strings (macros) at the beginning */
        for ( QValueList<Preamble*>::iterator it = preambleList.begin(); it != preambleList.end() && result && !cancelFlag; it++ )
        {
            result &= writePreamble( *iodevice, *it );
            emit progress( ++currentPos, totalElements );
        }

        for ( QValueList<Macro*>::iterator it = macroList.begin(); it != macroList.end() && result && !cancelFlag; it++ )
        {
            result &= writeMacro( *iodevice, *it );
            emit progress( ++currentPos, totalElements );
        }

        /** second, write cross-referencing elements */
        for ( QValueList<Entry*>::iterator it = crossRefingEntryList.begin(); it != crossRefingEntryList.end() && result && !cancelFlag; it++ )
        {
            result &= writeEntry( *iodevice, *it );
            emit progress( ++currentPos, totalElements );
        }

        /** third, write remaining elements */
        for ( QValueList<Element*>::iterator it = remainingList.begin(); it != remainingList.end() && result && !cancelFlag; it++ )
        {
            Entry *entry = dynamic_cast<Entry*>( *it );
            if ( entry != NULL )
                result &= writeEntry( *iodevice, entry );
            else
            {
                Comment *comment = dynamic_cast<Comment*>( *it );
                if ( comment != NULL )
                    result &= writeComment( *iodevice, comment );
            }
            emit progress( ++currentPos, totalElements );
        }

        iconv_close( m_iconvHandle );
        m_mutex.unlock();
        return result && !cancelFlag;
    }

    bool FileExporterBibTeX::save( QIODevice* iodevice, const Element* element, QStringList * /*errorLog*/ )
    {
        m_mutex.lock();
        bool result = FALSE;

        const char *encodingTo = m_encoding == "latex" ? "utf-8\0" : m_encoding.append( "\0" ).ascii();
        m_iconvHandle = iconv_open( encodingTo, "utf-8" );

        const Entry *entry = dynamic_cast<const Entry*>( element );
        if ( entry != NULL )
            result |= writeEntry( *iodevice, entry );
        else
        {
            const Macro * macro = dynamic_cast<const Macro*>( element );
            if ( macro != NULL )
                result |= writeMacro( *iodevice, macro );
            else
            {
                const Comment * comment = dynamic_cast<const Comment*>( element );
                if ( comment != NULL )
                    result |= writeComment( *iodevice, comment );
                else
                {
                    const Preamble * preamble = dynamic_cast<const Preamble*>( element );
                    if ( preamble != NULL )
                        result |= writePreamble( *iodevice, preamble );
                }
            }
        }

        iconv_close( m_iconvHandle );
        m_mutex.unlock();
        return result && !cancelFlag;
    }

    void FileExporterBibTeX::cancel()
    {
        cancelFlag = TRUE;
    }

    bool FileExporterBibTeX::writeEntry( QIODevice &device, const Entry* entry )
    {
        writeString( device, QString( "@%1{ %2" ).arg( applyKeywordCasing( entry->entryTypeString() ) ).arg( entry->id() ) );

        for ( Entry::EntryFields::ConstIterator it = entry->begin(); it != entry->end(); ++it )
        {
            EntryField *field = *it;
            QString text = valueToString( field->value(), field->fieldType(), field->fieldTypeName() );
            if ( m_protectCasing && dynamic_cast<BibTeX::PlainText*>( field->value()->items.first() ) != NULL && ( field->fieldType() == EntryField::ftTitle || field->fieldType() == EntryField::ftBookTitle || field->fieldType() == EntryField::ftSeries ) )
                addProtectiveCasing( text );
            writeString( device, QString( ",\n\t%1 = %2" ).arg( field->fieldTypeName() ).arg( text ) );
        }
        writeString( device, "\n}\n\n" );
        return TRUE;
    }

    bool FileExporterBibTeX::writeMacro( QIODevice &device, const Macro *macro )
    {
        QString text = valueToString( macro->value() );
        if ( m_protectCasing )
            addProtectiveCasing( text );

        writeString( device, QString( "@%1{ %2 = %3 }\n\n" ).arg( applyKeywordCasing( "String" ) ).arg( macro->key() ).arg( text ) );

        return TRUE;
    }

    bool FileExporterBibTeX::writeComment( QIODevice &device, const Comment *comment )
    {
        if ( !comment->useCommand() )
        {
            QString text = comment->text() ;

            if ( m_encoding == "latex" )
                text = EncoderLaTeX::currentEncoderLaTeX() ->encode( text );

            QStringList commentLines = QStringList::split( '\n', text );
            for ( QStringList::Iterator it = commentLines.begin(); it != commentLines.end(); it++ )
            {
                writeString( device, ( *it ).append( "\n" ) );
            }
            writeString( device, "\n" );
        }
        else
        {
            QString text = comment->text() ;

            if ( m_encoding == "latex" )
                text = EncoderLaTeX::currentEncoderLaTeX() ->encode( text );

            writeString( device, QString( "@%1{%2}\n\n" ).arg( applyKeywordCasing( "Comment" ) ).arg( text ) );
        }
        return TRUE;
    }

    bool FileExporterBibTeX::writePreamble( QIODevice &device, const Preamble* preamble )
    {
        writeString( device, QString( "@%1{%2}\n\n" ).arg( applyKeywordCasing( "Preamble" ) ).arg( valueToString( preamble->value() ) ) );

        return TRUE;
    }

    bool FileExporterBibTeX::writeString( QIODevice &device, const QString& text )
    {
        size_t utf8datasize = 1;
        QCString utf8 = text.utf8();
        char *utf8data = utf8.data();
        utf8datasize = utf8.length();
        char *outputdata = m_iconvBuffer;
        size_t outputdatasize = m_iconvBufferSize;

        size_t result = iconv( m_iconvHandle, &utf8data, &utf8datasize, &outputdata, &outputdatasize );
        if ( result != 0 )
        {
            qWarning( "Cannot convert string using iconv" );
            return false;
        }

        if ( device.writeBlock( m_iconvBuffer, m_iconvBufferSize - outputdatasize ) != ( int )( m_iconvBufferSize - outputdatasize ) )
        {
            qWarning( "Cannot write string to device" );
            return false;
        }

        return true;
    }

    void FileExporterBibTeX::setStringDelimiter( const QChar& stringOpenDelimiter, const QChar& stringCloseDelimiter )
    {
        m_stringOpenDelimiter = stringOpenDelimiter;
        m_stringCloseDelimiter = stringCloseDelimiter;
    }

    void FileExporterBibTeX::setKeywordCasing( const KeywordCasing keywordCasing )
    {
        m_keywordCasing = keywordCasing;
    }

    void FileExporterBibTeX::setEncoding( const QString& encoding )
    {
        m_encoding = encoding;
    }

    void FileExporterBibTeX::setEnclosingCurlyBrackets( bool protectCasing )
    {
        m_protectCasing = protectCasing;
    }

    QString FileExporterBibTeX::valueToString( const Value *value, const EntryField::FieldType fieldType, const QString &fieldTypeName )
    {
        if ( value == NULL )
            return "";

        QString result;
        bool isFirst = TRUE;
        EncoderLaTeX *encoder = EncoderLaTeX::currentEncoderLaTeX();

        for ( QValueList<ValueItem*>::ConstIterator it = value->items.begin(); it != value->items.end(); ++it )
        {
            if ( !isFirst )
                result.append( " # " );
            else
                isFirst = FALSE;

            MacroKey *macroKey = dynamic_cast<MacroKey*>( *it );
            if ( macroKey != NULL )
                result.append( macroKey->text() );
            else
            {
                QString text;
                BibTeX::PersonContainer *personContainer = dynamic_cast<BibTeX::PersonContainer*>( *it );
                BibTeX::PlainText *plainText = dynamic_cast<BibTeX::PlainText*>( *it );
                BibTeX::KeywordContainer *keywordContainer = dynamic_cast<BibTeX::KeywordContainer*>( *it );

                if ( plainText != NULL )
                    text = plainText->text();
                else if ( keywordContainer != NULL )
                {
                    bool first = TRUE;
                    for ( QValueList<Keyword*>::Iterator it = keywordContainer->keywords.begin(); it != keywordContainer->keywords.end(); ++it )
                    {
                        if ( !first )
                            text.append( ", " );
                        else
                            first = FALSE;
                        text.append(( *it )->text() );
                    }
                }
                else if ( personContainer != NULL )
                {
                    bool first = TRUE;
                    for ( QValueList<Person*>::Iterator it = personContainer->persons.begin(); it != personContainer->persons.end(); ++it )
                    {
                        if ( !first )
                            text.append( " and " );
                        else
                            first = FALSE;

                        QString v = ( *it )->firstName();
                        if ( !v.isEmpty() )
                        {
                            bool requiresQuoting = requiresPersonQuoting( v, FALSE );
                            if ( requiresQuoting ) text.append( "{" );
                            text.append( v );
                            if ( requiresQuoting ) text.append( "}" );
                            text.append( " " );
                        }

                        v = ( *it )->lastName();
                        if ( !v.isEmpty() )
                        {
                            /** Multi-part surnames (such as "Garcia Marquez") have to be enquoted.
                              * However, "von"-Parts (as in "von Hofmannsthal") must _not_ be enquoted.
                              * Examples:
                              * -- Robson de Souza
                              * -- Hartmann von der Tann
                              * -- Ronaldo de {Assis Moreira} ("Ronaldo de Assis Moreira" works as well)
                              * -- Ailton {Goncalves da Silva}
                              * -- Gloria von {Thurn und Taxis}
                              * Thus we split the von-Parts from the surname (= everything after the first upcase char).
                              * FIXME: Make the personContainer aware of von-Parts and jr-Parts, instead.
                              */
                            QStringList list = QStringList::split( " ", v );
                            QString von;
                            for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it )
                            {
                                QString str = *it;
                                if ( str != "others" && str[0].category() == QChar::Letter_Lowercase )
                                {
                                    von += *it;
                                    von += " ";
                                }
                                else
                                    break;
                            }
                            if ( !von.isEmpty() )
                            {
                                text.append( von );
                                v = v.right( v.length() - von.length() );
                            }
                            bool requiresQuoting = requiresPersonQuoting( v, TRUE );
                            if ( requiresQuoting ) text.append( "{" );
                            text.append( v );
                            if ( requiresQuoting ) text.append( "}" );
                        }
                    }
                }

                if ( m_encoding == "latex" )
                    text = encoder->encodeSpecialized( text, fieldType );

                if ( fieldType == EntryField::ftURL || fieldType == EntryField::ftDoi || ( fieldType == EntryField::ftUnknown && fieldTypeName.lower() == "slaccitation" ) )
                    removeBackslashQuoting( text );

                /** if the text to save contains a quote char ("),
                  * force string delimiters to be curly brackets,
                  * as quote chars as string delimiters would result
                  * in parser failures
                  */
                QChar stringOpenDelimiter = m_stringOpenDelimiter;
                QChar stringCloseDelimiter = m_stringCloseDelimiter;
                if ( text.contains( '"' ) && ( m_stringOpenDelimiter == '"' || m_stringCloseDelimiter == '"' ) )
                {
                    stringOpenDelimiter = '{';
                    stringCloseDelimiter = '}';
                }

                result.append( stringOpenDelimiter ).append( text ).append( stringCloseDelimiter );
            }
        }

        return result;
    }

    void FileExporterBibTeX::removeBackslashQuoting( QString &text )
    {
        text.replace( "\\&", "&" ).replace( "\\#", "#" ).replace( "\\_", "_" ).replace( "\\%", "%" );
    }

    QString FileExporterBibTeX::applyKeywordCasing( const QString &keyword )
    {
        switch ( m_keywordCasing )
        {
        case kcLowerCase: return keyword.lower();
        case kcInitialCapital: return keyword.at( 0 ) + keyword.lower().mid( 1 );
        case kcCapital: return keyword.upper();
        default: return keyword;
        }
    }

    bool FileExporterBibTeX::requiresPersonQuoting( const QString &text, bool isLastName )
    {
        if ( isLastName && !text.contains( " " ) )
            /** Last name contains NO spaces, no quoting necessary */
            return FALSE;
        else if ( isLastName && text[0].category() == QChar::Letter_Lowercase )
            /** Last name starts with lower case character (e.g. as in "van der Linden") */
            return FALSE;
        else if ( !isLastName && !text.contains( " and " ) )
            /** First name contains no " and " no quoting necessary */
            return FALSE;
        else if ( text[0] != '{' || text[text.length()-1] != '}' )
            /** as either last name contains spaces or first name contains " and " and there is no protective quoting yet, there must be a protective quoting added */
            return TRUE;

        /** check for cases like "{..}..{..}", which must be surrounded with a protective quoting, too */
        int bracketCounter = 0;
        for ( int i = text.length() - 1; i >= 0; --i )
        {
            if ( text[i] == '{' )
                ++bracketCounter;
            else if ( text[i] == '}' )
                --bracketCounter;
            if ( bracketCounter == 0 && i > 0 )
                return TRUE;
        }
        return FALSE;
    }

    void FileExporterBibTeX::addProtectiveCasing( QString &text )
    {
        if (( text[0] != '"' || text[text.length()-1] != '"' ) && ( text[0] != '{' || text[text.length()-1] != '}' ) )
        {
            /** nothing to protect, as this is no text string */
            return;
        }

        bool addBrackets = TRUE;

        if ( text[1] == '{' && text[text.length() - 2] == '}' )
        {
            addBrackets = FALSE;
            int count = 0;
            for ( int i = text.length() - 2; !addBrackets && i >= 1; --i )
                if ( text[i] == '{' )++count;
                else if ( text[i] == '}' )--count;
                else if ( count == 0 ) addBrackets = TRUE;
        }

        if ( addBrackets )
            text.insert( 1, '{' ).insert( text.length(), '}' );
    }

}