|
| 1 | +#include "PandocExporter.h" |
| 2 | + |
| 3 | +#include <QDir> |
| 4 | +#include <QFileInfo> |
| 5 | +#include <QProcess> |
| 6 | +#include <QRegularExpression> |
| 7 | +#include <QStandardPaths> |
| 8 | +#include <QTimer> |
| 9 | + |
| 10 | +static QString s_pandocPath; |
| 11 | +static int s_versionMajor = -1; |
| 12 | +static int s_versionMinor = -1; |
| 13 | +static bool s_versionChecked = false; |
| 14 | + |
| 15 | +static QStringList pandocFallbackLocations() |
| 16 | +{ |
| 17 | + QStringList candidates; |
| 18 | +#if defined(Q_OS_WIN) |
| 19 | + const QString localAppData = qEnvironmentVariable("LOCALAPPDATA"); |
| 20 | + if (!localAppData.isEmpty()) |
| 21 | + candidates << localAppData + QStringLiteral("/Pandoc/pandoc.exe"); |
| 22 | + const QString programFiles = qEnvironmentVariable("ProgramFiles"); |
| 23 | + if (!programFiles.isEmpty()) |
| 24 | + candidates << programFiles + QStringLiteral("/Pandoc/pandoc.exe"); |
| 25 | + const QString programFilesX86 = qEnvironmentVariable("ProgramFiles(x86)"); |
| 26 | + if (!programFilesX86.isEmpty()) |
| 27 | + candidates << programFilesX86 + QStringLiteral("/Pandoc/pandoc.exe"); |
| 28 | +#elif defined(Q_OS_MAC) |
| 29 | + candidates << QStringLiteral("/opt/homebrew/bin/pandoc") |
| 30 | + << QStringLiteral("/usr/local/bin/pandoc") |
| 31 | + << QStringLiteral("/usr/bin/pandoc"); |
| 32 | +#else |
| 33 | + candidates << QStringLiteral("/usr/bin/pandoc") |
| 34 | + << QStringLiteral("/usr/local/bin/pandoc") |
| 35 | + << QDir::homePath() + QStringLiteral("/.local/bin/pandoc"); |
| 36 | +#endif |
| 37 | + return candidates; |
| 38 | +} |
| 39 | + |
| 40 | +PandocExporter::PandocExporter(QWidget *parentWidget, QObject *parent) |
| 41 | + : QObject(parent) |
| 42 | + , m_parentWidget(parentWidget) |
| 43 | +{ |
| 44 | +} |
| 45 | + |
| 46 | +QString PandocExporter::pandocExecutable() |
| 47 | +{ |
| 48 | + QString path = QStandardPaths::findExecutable(QStringLiteral("pandoc")); |
| 49 | + if (!path.isEmpty()) |
| 50 | + return path; |
| 51 | + |
| 52 | + for (const QString &candidate : pandocFallbackLocations()) { |
| 53 | + if (QFileInfo(candidate).isExecutable()) |
| 54 | + return candidate; |
| 55 | + } |
| 56 | + return QString(); |
| 57 | +} |
| 58 | + |
| 59 | +bool PandocExporter::checkVersion() |
| 60 | +{ |
| 61 | + const QString exe = pandocExecutable(); |
| 62 | + if (exe.isEmpty()) |
| 63 | + return false; |
| 64 | + |
| 65 | + if (s_versionChecked && s_pandocPath == exe) |
| 66 | + return s_versionMajor > 2 || (s_versionMajor == 2 && s_versionMinor >= 19); |
| 67 | + |
| 68 | + s_pandocPath = exe; |
| 69 | + s_versionChecked = true; |
| 70 | + s_versionMajor = -1; |
| 71 | + s_versionMinor = -1; |
| 72 | + |
| 73 | + QProcess proc; |
| 74 | + proc.setProgram(exe); |
| 75 | + proc.setArguments({QStringLiteral("--version")}); |
| 76 | + proc.start(QIODevice::ReadOnly); |
| 77 | + if (!proc.waitForFinished(5000)) |
| 78 | + return false; |
| 79 | + |
| 80 | + const QString output = QString::fromUtf8(proc.readAllStandardOutput()); |
| 81 | + static const QRegularExpression re(QStringLiteral(R"(pandoc\s+(\d+)\.(\d+))")); |
| 82 | + const QRegularExpressionMatch match = re.match(output); |
| 83 | + if (!match.hasMatch()) |
| 84 | + return false; |
| 85 | + |
| 86 | + s_versionMajor = match.captured(1).toInt(); |
| 87 | + s_versionMinor = match.captured(2).toInt(); |
| 88 | + return s_versionMajor > 2 || (s_versionMajor == 2 && s_versionMinor >= 19); |
| 89 | +} |
| 90 | + |
| 91 | +bool PandocExporter::isAvailable() |
| 92 | +{ |
| 93 | + return checkVersion(); |
| 94 | +} |
| 95 | + |
| 96 | +QStringList PandocExporter::buildArguments(const QString &inputPath, |
| 97 | + const QString &outputPath, |
| 98 | + Format format) const |
| 99 | +{ |
| 100 | + QStringList args; |
| 101 | + args << inputPath; |
| 102 | + if (format == Html) |
| 103 | + args << QStringLiteral("--embed-resources") << QStringLiteral("--standalone"); |
| 104 | + args << QStringLiteral("-o") << outputPath; |
| 105 | + return args; |
| 106 | +} |
| 107 | + |
| 108 | +void PandocExporter::exportFile(const QString &inputMdPath, |
| 109 | + const QString &outputPath, |
| 110 | + Format format) |
| 111 | +{ |
| 112 | + const QString exe = pandocExecutable(); |
| 113 | + if (exe.isEmpty()) { |
| 114 | + emit finished(false, tr("pandoc not found on PATH")); |
| 115 | + deleteLater(); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + m_proc = new QProcess(this); |
| 120 | + m_proc->setProgram(exe); |
| 121 | + m_proc->setArguments(buildArguments(inputMdPath, outputPath, format)); |
| 122 | + m_proc->setWorkingDirectory(QFileInfo(inputMdPath).absolutePath()); |
| 123 | + |
| 124 | + connect(m_proc, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), |
| 125 | + this, &PandocExporter::onProcessFinished); |
| 126 | + connect(m_proc, &QProcess::errorOccurred, |
| 127 | + this, &PandocExporter::onProcessError); |
| 128 | + |
| 129 | + m_timeout = new QTimer(this); |
| 130 | + m_timeout->setSingleShot(true); |
| 131 | + m_timeout->setInterval(30000); |
| 132 | + connect(m_timeout, &QTimer::timeout, this, &PandocExporter::onTimeout); |
| 133 | + |
| 134 | + m_proc->start(QIODevice::ReadOnly); |
| 135 | + m_timeout->start(); |
| 136 | +} |
| 137 | + |
| 138 | +void PandocExporter::onProcessFinished(int exitCode, QProcess::ExitStatus status) |
| 139 | +{ |
| 140 | + m_timeout->stop(); |
| 141 | + if (status == QProcess::CrashExit || exitCode != 0) { |
| 142 | + const QString err = QString::fromUtf8(m_proc->readAllStandardError()).trimmed(); |
| 143 | + emit finished(false, err.isEmpty() ? tr("pandoc exited with code %1").arg(exitCode) : err); |
| 144 | + } else { |
| 145 | + emit finished(true, QString()); |
| 146 | + } |
| 147 | + deleteLater(); |
| 148 | +} |
| 149 | + |
| 150 | +void PandocExporter::onProcessError(QProcess::ProcessError error) |
| 151 | +{ |
| 152 | + if (error == QProcess::FailedToStart) { |
| 153 | + m_timeout->stop(); |
| 154 | + emit finished(false, tr("Failed to start pandoc: %1").arg(m_proc->errorString())); |
| 155 | + deleteLater(); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +void PandocExporter::onTimeout() |
| 160 | +{ |
| 161 | + if (m_proc && m_proc->state() != QProcess::NotRunning) { |
| 162 | + m_proc->kill(); |
| 163 | + m_proc->waitForFinished(1000); |
| 164 | + } |
| 165 | + emit finished(false, tr("pandoc timed out after 30 seconds")); |
| 166 | + deleteLater(); |
| 167 | +} |
0 commit comments