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 "fileexporterps.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 "logging_io.h"
0032 
0033 FileExporterPS::FileExporterPS(QObject *parent)
0034         : FileExporterToolchain(parent)
0035 {
0036     m_fileBasename = QStringLiteral("bibtex-to-ps");
0037     m_fileStem = tempDir.path() + QDir::separator() + m_fileBasename;
0038 }
0039 
0040 FileExporterPS::~FileExporterPS()
0041 {
0042     /// nothing
0043 }
0044 
0045 bool FileExporterPS::save(QIODevice *iodevice, const File *bibtexfile)
0046 {
0047     check_if_bibtexfile_or_iodevice_invalid(bibtexfile, iodevice);
0048 
0049     bool result = false;
0050 
0051     QFile output(m_fileStem + KBibTeX::extensionBibTeX);
0052     if (output.open(QIODevice::WriteOnly)) {
0053         FileExporterBibTeX bibtexExporter(this);
0054         bibtexExporter.setEncoding(QStringLiteral("latex"));
0055         result = bibtexExporter.save(&output, bibtexfile);
0056         output.close();
0057     }
0058 
0059     if (result)
0060         result = generatePS(iodevice);
0061 
0062     return result;
0063 }
0064 
0065 bool FileExporterPS::save(QIODevice *iodevice, const QSharedPointer<const Element> &element, const File *bibtexfile)
0066 {
0067     check_if_iodevice_invalid(iodevice);
0068 
0069     bool result = false;
0070 
0071     QFile output(m_fileStem + KBibTeX::extensionBibTeX);
0072     if (output.open(QIODevice::WriteOnly)) {
0073         FileExporterBibTeX bibtexExporter(this);
0074         bibtexExporter.setEncoding(QStringLiteral("latex"));
0075         result = bibtexExporter.save(&output, element, bibtexfile);
0076         output.close();
0077     }
0078 
0079     if (result)
0080         result = generatePS(iodevice);
0081 
0082     return result;
0083 }
0084 
0085 bool FileExporterPS::generatePS(QIODevice *iodevice)
0086 {
0087     QStringList cmdLines {QStringLiteral("latex -halt-on-error bibtex-to-ps.tex"), QStringLiteral("bibtex bibtex-to-ps"), QStringLiteral("latex -halt-on-error bibtex-to-ps.tex"), QStringLiteral("latex -halt-on-error bibtex-to-ps.tex"), QStringLiteral("dvips -R2 -o bibtex-to-ps.ps bibtex-to-ps.dvi")};
0088 
0089     return writeLatexFile(m_fileStem + KBibTeX::extensionTeX) && runProcesses(cmdLines) && beautifyPostscriptFile(m_fileStem + KBibTeX::extensionPostScript, QStringLiteral("Exported Bibliography")) && writeFileToIODevice(m_fileStem + KBibTeX::extensionPostScript, iodevice);
0090 }
0091 
0092 bool FileExporterPS::writeLatexFile(const QString &filename)
0093 {
0094     QFile latexFile(filename);
0095     if (latexFile.open(QIODevice::WriteOnly)) {
0096         QTextStream ts(&latexFile);
0097         // https://forum.qt.io/topic/135724/qt-6-replacement-for-qtextcodec
0098 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
0099         ts.setCodec("UTF-8");
0100 #else
0101         ts.setEncoding(QStringConverter::Utf8);
0102 #endif
0103 #if QT_VERSION >= 0x050e00
0104         ts << "\\documentclass{article}" << Qt::endl;
0105         ts << "\\usepackage[T1]{fontenc}" << Qt::endl;
0106         ts << "\\usepackage[utf8]{inputenc}" << Qt::endl;
0107 #else // QT_VERSION < 0x050e00
0108         ts << "\\documentclass{article}" << endl;
0109         ts << "\\usepackage[T1]{fontenc}" << endl;
0110         ts << "\\usepackage[utf8]{inputenc}" << endl;
0111 #endif // QT_VERSION >= 0x050e00
0112         if (kpsewhich(QStringLiteral("babel.sty")))
0113 #if QT_VERSION >= 0x050e00
0114             ts << "\\usepackage[" << Preferences::instance().laTeXBabelLanguage() << "]{babel}" << Qt::endl;
0115 #else // QT_VERSION < 0x050e00
0116             ts << "\\usepackage[" << Preferences::instance().laTeXBabelLanguage() << "]{babel}" << endl;
0117 #endif // QT_VERSION >= 0x050e00
0118         if (kpsewhich(QStringLiteral("url.sty")))
0119 #if QT_VERSION >= 0x050e00
0120             ts << "\\usepackage{url}" << Qt::endl;
0121 #else // QT_VERSION < 0x050e00
0122             ts << "\\usepackage{url}" << endl;
0123 #endif // QT_VERSION >= 0x050e00
0124         const QString bibliographyStyle = Preferences::instance().bibTeXBibliographyStyle();
0125         if ((bibliographyStyle == QStringLiteral("agsm") || bibliographyStyle == QStringLiteral("dcu") || bibliographyStyle == QStringLiteral("jmr") || bibliographyStyle == QStringLiteral("jphysicsB") || bibliographyStyle == QStringLiteral("kluwer") || bibliographyStyle == QStringLiteral("nederlands") || bibliographyStyle == QStringLiteral("dcu") || bibliographyStyle == QStringLiteral("dcu")) && kpsewhich(QStringLiteral("harvard.sty")) && kpsewhich(QStringLiteral("html.sty")))
0126 #if QT_VERSION >= 0x050e00
0127             ts << "\\usepackage{html}" << Qt::endl << "\\usepackage[dcucite]{harvard}" << Qt::endl << "\\renewcommand{\\harvardurl}{URL: \\url}" << Qt::endl;
0128 #else // QT_VERSION < 0x050e00
0129             ts << "\\usepackage{html}" << endl << "\\usepackage[dcucite]{harvard}" << endl << "\\renewcommand{\\harvardurl}{URL: \\url}" << endl;
0130 #endif // QT_VERSION >= 0x050e00
0131         if (kpsewhich(QStringLiteral("geometry.sty")))
0132 #if QT_VERSION >= 0x050e00
0133             ts << "\\usepackage[paper=" << pageSizeToLaTeXName(Preferences::instance().pageSize()) << "]{geometry}" << Qt::endl;
0134 #else // QT_VERSION < 0x050e00
0135             ts << "\\usepackage[paper=" << pageSizeToLaTeXName(Preferences::instance().pageSize()) << "]{geometry}" << endl;
0136 #endif // QT_VERSION >= 0x050e00
0137 #if QT_VERSION >= 0x050e00
0138         ts << "\\bibliographystyle{" << bibliographyStyle << "}" << Qt::endl;
0139         ts << "\\begin{document}" << Qt::endl;
0140         ts << "\\nocite{*}" << Qt::endl;
0141         ts << "\\bibliography{bibtex-to-ps}" << Qt::endl;
0142         ts << "\\end{document}" << Qt::endl;
0143 #else // QT_VERSION < 0x050e00
0144         ts << "\\bibliographystyle{" << bibliographyStyle << "}" << endl;
0145         ts << "\\begin{document}" << endl;
0146         ts << "\\nocite{*}" << endl;
0147         ts << "\\bibliography{bibtex-to-ps}" << endl;
0148         ts << "\\end{document}" << endl;
0149 #endif // QT_VERSION >= 0x050e00
0150         latexFile.close();
0151         return true;
0152     } else
0153         return false;
0154 }
0155 
0156 bool FileExporterPS::beautifyPostscriptFile(const QString &filename, const QString &title)
0157 {
0158     QFile postscriptFile(filename);
0159     if (postscriptFile.open(QFile::ReadOnly)) {
0160         QTextStream ts(&postscriptFile);
0161         QStringList lines;
0162         QString line;
0163         int i = 0;
0164         while (!(line = ts.readLine()).isNull()) {
0165             if (i < 32 && line.startsWith(QStringLiteral("%%Title:")))
0166                 line = QStringLiteral("%%Title: ") + title;
0167             else if (i < 32 && line.startsWith(QStringLiteral("%%Creator:")))
0168                 line += QStringLiteral("; exported from within KBibTeX: https://userbase.kde.org/KBibTeX");
0169             lines += line;
0170             ++i;
0171         }
0172         postscriptFile.close();
0173 
0174         if (postscriptFile.open(QFile::WriteOnly)) {
0175             QTextStream ts(&postscriptFile);
0176             for (const QString &line : const_cast<const QStringList &>(lines))
0177 #if QT_VERSION >= 0x050e00
0178                 ts << line << Qt::endl;
0179 #else // QT_VERSION < 0x050e00
0180                 ts << line << endl;
0181 #endif // QT_VERSION >= 0x050e00
0182             postscriptFile.close();
0183         } else
0184             return false;
0185     } else
0186         return false;
0187 
0188     return true;
0189 }