File indexing completed on 2024-05-19 05:05:34
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 #include "fileexporterrtf.h" 0021 0022 #include <QFile> 0023 #include <QStringList> 0024 #include <QTextStream> 0025 #include <QDir> 0026 0027 #include <KBibTeX> 0028 #include <Preferences> 0029 #include <Element> 0030 #include "fileexporterbibtex.h" 0031 #include "fileexporter_p.h" 0032 #include "logging_io.h" 0033 0034 FileExporterRTF::FileExporterRTF(QObject *parent) 0035 : FileExporterToolchain(parent) 0036 { 0037 m_fileBasename = QStringLiteral("bibtex-to-rtf"); 0038 m_fileStem = tempDir.path() + QDir::separator() + m_fileBasename; 0039 } 0040 0041 FileExporterRTF::~FileExporterRTF() 0042 { 0043 /// nothing 0044 } 0045 0046 bool FileExporterRTF::save(QIODevice *iodevice, const File *bibtexfile) 0047 { 0048 check_if_bibtexfile_or_iodevice_invalid(bibtexfile, iodevice); 0049 0050 bool result = false; 0051 0052 QFile output(m_fileStem + KBibTeX::extensionBibTeX); 0053 if (output.open(QIODevice::WriteOnly)) { 0054 FileExporterBibTeX bibtexExporter(this); 0055 bibtexExporter.setEncoding(QStringLiteral("latex")); 0056 result = bibtexExporter.save(&output, bibtexfile); 0057 output.close(); 0058 } 0059 0060 if (result) 0061 result = generateRTF(iodevice); 0062 0063 return result; 0064 } 0065 0066 bool FileExporterRTF::save(QIODevice *iodevice, const QSharedPointer<const Element> &element, const File *bibtexfile) 0067 { 0068 check_if_iodevice_invalid(iodevice); 0069 0070 bool result = false; 0071 0072 QFile output(m_fileStem + KBibTeX::extensionBibTeX); 0073 if (output.open(QIODevice::WriteOnly)) { 0074 FileExporterBibTeX bibtexExporter(this); 0075 bibtexExporter.setEncoding(QStringLiteral("latex")); 0076 result = bibtexExporter.save(&output, element, bibtexfile); 0077 output.close(); 0078 } 0079 0080 if (result) 0081 result = generateRTF(iodevice); 0082 0083 return result; 0084 } 0085 0086 bool FileExporterRTF::generateRTF(QIODevice *iodevice) 0087 { 0088 QStringList cmdLines {QStringLiteral("latex -halt-on-error bibtex-to-rtf.tex"), QStringLiteral("bibtex bibtex-to-rtf"), QStringLiteral("latex -halt-on-error bibtex-to-rtf.tex"), QString(QStringLiteral("latex2rtf -i %1 bibtex-to-rtf.tex")).arg(Preferences::instance().laTeXBabelLanguage())}; 0089 0090 return writeLatexFile(m_fileStem + KBibTeX::extensionTeX) && runProcesses(cmdLines) && writeFileToIODevice(m_fileStem + KBibTeX::extensionRTF, iodevice); 0091 } 0092 0093 bool FileExporterRTF::writeLatexFile(const QString &filename) 0094 { 0095 QFile latexFile(filename); 0096 if (latexFile.open(QIODevice::WriteOnly)) { 0097 QTextStream ts(&latexFile); 0098 // https://forum.qt.io/topic/135724/qt-6-replacement-for-qtextcodec 0099 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) 0100 ts.setCodec("UTF-8"); 0101 #else 0102 ts.setEncoding(QStringConverter::Utf8); 0103 #endif 0104 #if QT_VERSION >= 0x050e00 0105 ts << "\\documentclass{article}" << Qt::endl; 0106 ts << "\\usepackage[T1]{fontenc}" << Qt::endl; 0107 ts << "\\usepackage[utf8]{inputenc}" << Qt::endl; 0108 #else // QT_VERSION < 0x050e00 0109 ts << "\\documentclass{article}" << endl; 0110 ts << "\\usepackage[T1]{fontenc}" << endl; 0111 ts << "\\usepackage[utf8]{inputenc}" << endl; 0112 #endif // QT_VERSION >= 0x050e00 0113 if (kpsewhich(QStringLiteral("babel.sty"))) 0114 #if QT_VERSION >= 0x050e00 0115 ts << "\\usepackage[" << Preferences::instance().laTeXBabelLanguage() << "]{babel}" << Qt::endl; 0116 #else // QT_VERSION < 0x050e00 0117 ts << "\\usepackage[" << Preferences::instance().laTeXBabelLanguage() << "]{babel}" << endl; 0118 #endif // QT_VERSION >= 0x050e00 0119 if (kpsewhich(QStringLiteral("url.sty"))) 0120 #if QT_VERSION >= 0x050e00 0121 ts << "\\usepackage{url}" << Qt::endl; 0122 #else // QT_VERSION < 0x050e00 0123 ts << "\\usepackage{url}" << endl; 0124 #endif // QT_VERSION >= 0x050e00 0125 const QString bibliographyStyle = Preferences::instance().bibTeXBibliographyStyle(); 0126 if (bibliographyStyle == QStringLiteral("dcu") && kpsewhich(QStringLiteral("harvard.sty")) && kpsewhich(QStringLiteral("html.sty"))) 0127 #if QT_VERSION >= 0x050e00 0128 ts << "\\usepackage{html}" << Qt::endl << "\\usepackage[dcucite]{harvard}" << Qt::endl << "\\renewcommand{\\harvardurl}{URL: \\url}" << Qt::endl; 0129 #else // QT_VERSION < 0x050e00 0130 ts << "\\usepackage{html}" << endl << "\\usepackage[dcucite]{harvard}" << endl << "\\renewcommand{\\harvardurl}{URL: \\url}" << endl; 0131 #endif // QT_VERSION >= 0x050e00 0132 if (kpsewhich(QStringLiteral("geometry.sty"))) 0133 #if QT_VERSION >= 0x050e00 0134 ts << "\\usepackage[paper=" << pageSizeToLaTeXName(Preferences::instance().pageSize()) << "]{geometry}" << Qt::endl; 0135 #else // QT_VERSION < 0x050e00 0136 ts << "\\usepackage[paper=" << pageSizeToLaTeXName(Preferences::instance().pageSize()) << "]{geometry}" << endl; 0137 #endif // QT_VERSION >= 0x050e00 0138 #if QT_VERSION >= 0x050e00 0139 ts << "\\bibliographystyle{" << bibliographyStyle << "}" << Qt::endl; 0140 ts << "\\begin{document}" << Qt::endl; 0141 ts << "\\nocite{*}" << Qt::endl; 0142 ts << "\\bibliography{bibtex-to-rtf}" << Qt::endl; 0143 ts << "\\end{document}" << Qt::endl; 0144 #else // QT_VERSION < 0x050e00 0145 ts << "\\bibliographystyle{" << bibliographyStyle << "}" << endl; 0146 ts << "\\begin{document}" << endl; 0147 ts << "\\nocite{*}" << endl; 0148 ts << "\\bibliography{bibtex-to-rtf}" << endl; 0149 ts << "\\end{document}" << endl; 0150 #endif // QT_VERSION >= 0x050e00 0151 latexFile.close(); 0152 return true; 0153 } 0154 0155 return false; 0156 }