File indexing completed on 2024-05-19 05:05:36
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_FILEIMPORTERBIBTEX_H 0021 #define KBIBTEX_IO_FILEIMPORTERBIBTEX_H 0022 0023 #include <QTextStream> 0024 #include <QSharedPointer> 0025 #include <QStringList> 0026 #include <QSet> 0027 0028 #include <KBibTeX> 0029 #include <FileImporter> 0030 0031 #ifdef HAVE_KF 0032 #include "kbibtexio_export.h" 0033 #endif // HAVE_KF 0034 0035 class Element; 0036 class Comment; 0037 class Preamble; 0038 class Macro; 0039 class Value; 0040 class Keyword; 0041 0042 /** 0043 * This class reads a BibTeX file from a QIODevice (such as a QFile) and 0044 * creates a File object which can be used to access the BibTeX elements. 0045 * @see File 0046 * @author Thomas Fischer <fischer@unix-ag.uni-kl.de> 0047 */ 0048 class KBIBTEXIO_EXPORT FileImporterBibTeX : public FileImporter 0049 { 0050 Q_OBJECT 0051 0052 public: 0053 enum class CommentHandling {Ignore, Keep}; 0054 0055 /** 0056 * Creates an importer class to read a BibTeX file. 0057 */ 0058 explicit FileImporterBibTeX(QObject *parent); 0059 ~FileImporterBibTeX(); 0060 0061 virtual File *fromString(const QString &text) override; 0062 0063 /** 0064 * Read data from the given device and construct a File object holding 0065 * the bibliographic data. 0066 * @param iodevice opened QIODevice instance ready to read from 0067 * @return @c valid File object with elements, @c NULL if reading failed for some reason 0068 */ 0069 File *load(QIODevice *iodevice) override; 0070 0071 /** TODO 0072 */ 0073 static bool guessCanDecode(const QString &text); 0074 0075 /** 0076 * Split a list of keyword separated by ";" or "," into single Keyword objects. 0077 * @param text Text containing the keyword list 0078 * @param usedSplitChar The split char that is used to separate the keywords 0079 * @return A list of Keyword object containing the keywords 0080 * @see Keyword 0081 */ 0082 static QList<QSharedPointer<Keyword> > splitKeywords(const QString &text, char *usedSplitChar = nullptr); 0083 0084 /** 0085 * Split a list of names into single Person objects. 0086 * Examples: "Smith, John, Fulkerson, Ford, and Johnson, Tim" 0087 * or "John Smith and Tim Johnson" 0088 * @param text Text containing the persons' names 0089 * @param line_number Line number to use in error message if splitting failed 0090 * @param parent The parent object 0091 * @return A list of Person object containing the names 0092 * @see Person 0093 */ 0094 static QList<QSharedPointer<Person> > splitNames(const QString &text); 0095 0096 /** 0097 * Split a person's name into its parts and construct a Person object from them. 0098 * This is a functions specialized on the properties of (La)TeX code considering 0099 * e.g. curly brackets. 0100 * @param name The persons name 0101 * @param line_number Line number to use in error message if splitting failed 0102 * @param parent The parent object 0103 * @return A Person object containing the name 0104 * @see Person 0105 */ 0106 static QSharedPointer<Person> personFromString(const QString &name, const int line_number = 1, QObject *parent = nullptr); 0107 0108 static void parsePersonList(const QString &text, Value &value, const int line_number = 1, QObject *parent = nullptr); 0109 0110 void setCommentHandling(CommentHandling commentHandling); 0111 0112 public Q_SLOTS: 0113 void cancel() override; 0114 0115 private: 0116 class Private; 0117 Private *d; 0118 0119 bool m_cancelFlag; 0120 0121 /// high-level parsing functions 0122 Comment *readCommentElement(); 0123 Comment *readPlainCommentElement(const QString &initialRead); 0124 0125 /** 0126 * Split a string into white-space separated chunks, 0127 * but keep parts intact which are protected by {...}. 0128 * Example: "aa bb ccc {dd ee ff}" 0129 * will be split into "aa", "bb", "ccc", "{dd ee ff}" 0130 * 0131 * @param text input string to be split 0132 * @param segments list where chunks will be added to 0133 */ 0134 static void contextSensitiveSplit(const QString &text, QStringList &segments); 0135 0136 static QString bibtexAwareSimplify(const QString &text); 0137 static QString rstrip(const QString &text); 0138 }; 0139 0140 Q_DECLARE_METATYPE(FileImporterBibTeX::CommentHandling) 0141 0142 #endif // KBIBTEX_IO_FILEIMPORTERBIBTEX_H