File indexing completed on 2024-05-19 05:05:33

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2023 Thomas Fischer <fischer@unix-ag.uni-kl.de>
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
0018  ***************************************************************************/
0019 
0020 #ifndef KBIBTEX_IO_FILEEXPORTERBIBTEX_H
0021 #define KBIBTEX_IO_FILEEXPORTERBIBTEX_H
0022 
0023 #include <QTextStream>
0024 
0025 #include <Preferences>
0026 #include <Element>
0027 #include <Value>
0028 #include <FileExporter>
0029 
0030 #ifdef HAVE_KF
0031 #include "kbibtexio_export.h"
0032 #endif // HAVE_KF
0033 
0034 class QChar;
0035 
0036 class Comment;
0037 class Preamble;
0038 class Macro;
0039 class Entry;
0040 
0041 #include "encoder.h"
0042 
0043 /**
0044  * @author Thomas Fischer <fischer@unix-ag.uni-kl.de>
0045  */
0046 class KBIBTEXIO_EXPORT FileExporterBibTeX : public FileExporter
0047 {
0048     Q_OBJECT
0049 
0050 public:
0051 
0052     explicit FileExporterBibTeX(QObject *parent);
0053     ~FileExporterBibTeX() override;
0054 
0055     /**
0056      * Set the text encoding used in the resulting bibliography file.
0057      * Example values for encoding include 'UTF-8' or 'ISO-8859-1'.
0058      * Special encoding 'LaTeX' corresponds to 'UTF-8' but tries to
0059      * replace non-ASCII characters with LaTeX equivalents (which are
0060      * ASCII only).
0061      * This setting both overrules the Preferences' global setting
0062      * as well as the encoding that may have been set in the bibliography's
0063      * properties (which may got set when loading the bibliography from
0064      * a file).
0065      *
0066      * @param[in] encoding encoding to be forced upon the output by this exporter
0067      */
0068     void setEncoding(const QString &encoding);
0069 
0070     QString toString(const QSharedPointer<const Element> &element, const File *bibtexfile) override;
0071     QString toString(const File *bibtexfile) override;
0072 
0073     bool save(QIODevice *iodevice, const File *bibtexfile) override;
0074     bool save(QIODevice *iodevice, const QSharedPointer<const Element> &element, const File *bibtexfile) override;
0075 
0076     /**
0077      * Write a Value object into a BibTeX or BibLaTeX strign representation.
0078      * The transcription may follow field type-specific rules. The generated string
0079      * may keep non-ASCII characters or have them rewritten into a LaTeX representation.
0080      * @param[in] value Value to be written out
0081      * @param[in] fieldType optionally specified field type, e.g. @c url to enable field type-specific rules
0082      * @param[in] useLaTeXEncoding optional parameter how to handle non-ASCII characters
0083      * @return string representation of the Value object
0084      */
0085     static QString valueToBibTeX(const Value &value, Encoder::TargetEncoding targetEncoding, const QString &fieldType = QString());
0086 
0087     /**
0088      * Convert a positive int into a textual representation.
0089      * If the requested bibliography system is BibTeX, conversion follows BibTeX's
0090      * documentation: editions 1 to 5 get rewritten into text like 'fourth',
0091      * larger editions are numbers followed by an English ordinal suffix,
0092      * resulting in, for example, '42nd'.
0093      * If the requested bibliography system is BibLaTeX, conversion follows this
0094      * system's documentation: edition numbers are simply converted into a string
0095      * for use in a @c PlainText.
0096      * If conversion fails, e.g. for negative values of @c edition, an empty
0097      * string is returned.
0098      * @param[in] edition edition as positive number
0099      * @return Textual representation of the ordinal value or empty string if conversion failed
0100      */
0101     static QString editionNumberToString(const int edition, const Preferences::BibliographySystem bibliographySystem = Preferences::instance().bibliographySystem());
0102 
0103     /**
0104      * Cheap and fast test if another FileExporter is a FileExporterBibTeX object.
0105      * @param other another FileExporter object to test against
0106      * @return @c true if FileExporter is actually a FileExporterBibTeX, else @c false
0107      */
0108     static bool isFileExporterBibTeX(const FileExporter &other);
0109 
0110 public Q_SLOTS:
0111     void cancel() override;
0112 
0113 private:
0114     class Private;
0115     Private *d;
0116 };
0117 
0118 #endif // KBIBTEX_IO_FILEEXPORTERBIBTEX_H