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 #include "fileexporterbibtex2html.h" 0021 0022 #include <QFile> 0023 #include <QStandardPaths> 0024 0025 #ifdef HAVE_KFI18N 0026 #include <KLocalizedString> 0027 #else // HAVE_KFI18N 0028 #include <QObject> 0029 #define i18n(text) QObject::tr(text) 0030 #endif // HAVE_KFI18N 0031 0032 #include "fileexporterbibtex.h" 0033 #include "fileexporter_p.h" 0034 #include "logging_io.h" 0035 0036 class FileExporterBibTeX2HTML::FileExporterBibTeX2HTMLPrivate 0037 { 0038 private: 0039 FileExporterBibTeX2HTML *p; 0040 public: 0041 QString bibTeXFilename; 0042 QString outputFilename; 0043 QString bibStyle; 0044 0045 FileExporterBibTeX2HTMLPrivate(FileExporterBibTeX2HTML *parent, const QString &workingDir) 0046 : p(parent) { 0047 bibTeXFilename = QString(workingDir).append(QStringLiteral("/bibtex-to-html.bib")); 0048 outputFilename = QString(workingDir).append(QStringLiteral("/bibtex-to-html.html")); 0049 bibStyle = QStringLiteral("plain"); 0050 } 0051 0052 bool generateHTML(QIODevice *iodevice) { 0053 if (!checkBSTexists(iodevice)) return false; 0054 if (!checkBibTeX2HTMLexists(iodevice)) return false; 0055 0056 /// bibtex2html automatically appends ".html" to output filenames 0057 QString outputFilenameNoEnding = outputFilename; 0058 outputFilenameNoEnding.remove(QStringLiteral(".html")); 0059 0060 QStringList args; 0061 args << QStringLiteral("-s") << bibStyle; /// BibTeX style (plain, alpha, ...) 0062 args << QStringLiteral("-o") << outputFilenameNoEnding; /// redirect the output 0063 args << QStringLiteral("-nokeys"); /// do not print the BibTeX keys 0064 args << QStringLiteral("-nolinks"); /// do not print any web link 0065 args << QStringLiteral("-nodoc"); /// only produces the body of the HTML documents 0066 args << QStringLiteral("-nobibsource"); /// do not produce the BibTeX entries file 0067 args << QStringLiteral("-debug"); /// verbose mode (to find incorrect BibTeX entries) 0068 args << bibTeXFilename; 0069 0070 bool result = p->runProcess(QStringLiteral("bibtex2html"), args) && p->writeFileToIODevice(outputFilename, iodevice); 0071 0072 return result; 0073 } 0074 0075 bool checkBibTeX2HTMLexists(QIODevice *iodevice) { 0076 if (!QStandardPaths::findExecutable(QStringLiteral("bibtex2html")).isEmpty()) 0077 return true; 0078 0079 QTextStream ts(iodevice); 0080 ts << QStringLiteral("<div style=\"color: red; background: white;\">"); 0081 ts << i18n("The program <strong>bibtex2html</strong> is not available."); 0082 #if QT_VERSION >= 0x050e00 0083 ts << QStringLiteral("</div>") << Qt::endl; 0084 #else // QT_VERSION < 0x050e00 0085 ts << QStringLiteral("</div>") << endl; 0086 #endif // QT_VERSION >= 0x050e00 0087 ts.flush(); 0088 return false; 0089 } 0090 0091 0092 bool checkBSTexists(QIODevice *iodevice) { 0093 if (p->kpsewhich(bibStyle + QStringLiteral(".bst"))) 0094 return true; 0095 0096 QTextStream ts(iodevice); 0097 ts << QStringLiteral("<div style=\"color: red; background: white;\">"); 0098 #ifdef HAVE_KFI18N 0099 ts << i18n("The BibTeX style <strong>%1</strong> is not available.", bibStyle); 0100 #else // HAVE_KFI18N 0101 ts << i18n("The BibTeX style <strong>PLACEHOLDER</strong> is not available.").replace(QStringLiteral("PLACEHOLDER"), bibStyle); 0102 #endif // HAVE_KFI18N 0103 #if QT_VERSION >= 0x050e00 0104 ts << QStringLiteral("</div>") << Qt::endl; 0105 #else // QT_VERSION < 0x050e00 0106 ts << QStringLiteral("</div>") << endl; 0107 #endif // QT_VERSION >= 0x050e00 0108 ts.flush(); 0109 return false; 0110 } 0111 }; 0112 0113 FileExporterBibTeX2HTML::FileExporterBibTeX2HTML(QObject *parent) 0114 : FileExporterToolchain(parent), d(new FileExporterBibTeX2HTMLPrivate(this, tempDir.path())) 0115 { 0116 /// nothing 0117 } 0118 0119 FileExporterBibTeX2HTML::~FileExporterBibTeX2HTML() 0120 { 0121 delete d; 0122 } 0123 0124 bool FileExporterBibTeX2HTML::save(QIODevice *iodevice, const File *bibtexfile) 0125 { 0126 check_if_bibtexfile_or_iodevice_invalid(bibtexfile, iodevice); 0127 0128 bool result = false; 0129 0130 QFile output(d->bibTeXFilename); 0131 if (output.open(QIODevice::WriteOnly)) { 0132 FileExporterBibTeX bibtexExporter(this); 0133 bibtexExporter.setEncoding(QStringLiteral("latex")); 0134 result = bibtexExporter.save(&output, bibtexfile); 0135 output.close(); 0136 } 0137 0138 if (result) 0139 result = d->generateHTML(iodevice); 0140 0141 return result; 0142 } 0143 0144 bool FileExporterBibTeX2HTML::save(QIODevice *iodevice, const QSharedPointer<const Element> &element, const File *bibtexfile) 0145 { 0146 check_if_iodevice_invalid(iodevice); 0147 0148 bool result = false; 0149 0150 QFile output(d->bibTeXFilename); 0151 if (output.open(QIODevice::WriteOnly)) { 0152 FileExporterBibTeX bibtexExporter(this); 0153 bibtexExporter.setEncoding(QStringLiteral("latex")); 0154 result = bibtexExporter.save(&output, element, bibtexfile); 0155 output.close(); 0156 } 0157 0158 if (result) 0159 result = d->generateHTML(iodevice); 0160 0161 return result; 0162 } 0163 0164 void FileExporterBibTeX2HTML::setLaTeXBibliographyStyle(const QString &bibStyle) 0165 { 0166 d->bibStyle = bibStyle; 0167 } 0168 0169 QStringList FileExporterBibTeX2HTML::availableLaTeXBibliographyStyles() 0170 { 0171 static QStringList listOfBibStyles; 0172 if (listOfBibStyles.isEmpty()) { 0173 static const QStringList stylesToTestFor {QStringLiteral("abbrv"), QStringLiteral("acm"), QStringLiteral("alpha"), QStringLiteral("apalike"), QStringLiteral("ieeetr"), QStringLiteral("plain"), QStringLiteral("siam"), QStringLiteral("unsrt")}; 0174 for (const QString &bibStyle : stylesToTestFor) 0175 if (kpsewhich(bibStyle + QStringLiteral(".bst"))) 0176 listOfBibStyles.append(bibStyle); 0177 } 0178 return listOfBibStyles; 0179 }