Add %(a|b|c|..)random to formatter

This commit is contained in:
ArsenArsen 2017-09-13 01:47:41 +02:00
parent bd2adc5e53
commit f7ec1f44f2
3 changed files with 23 additions and 8 deletions

View File

@ -3,15 +3,27 @@
#include <QDateTime> #include <QDateTime>
#include <QStringList> #include <QStringList>
QString formatter::format(QString toFormat, QString ext) { QString formatter::format(QString toFormat, QString ext, QMap<QString, QString> variables) {
QRegExp dateRegex("%\\((.+)\\)date"); QString formatted(toFormat);
QRegExp dateRegex("%(?!%)\\((.+)\\)date");
dateRegex.indexIn(toFormat); dateRegex.indexIn(toFormat);
QStringList capturedTexts(dateRegex.capturedTexts()); QStringList capturedTexts(dateRegex.capturedTexts());
QString formatted(toFormat);
QDateTime date = QDateTime::currentDateTime(); QDateTime date = QDateTime::currentDateTime();
for (int i = 0; i < capturedTexts.length(); i += 2) { for (int i = 0; i < capturedTexts.length(); i += 2) {
formatted = formatted.replace(capturedTexts.at(i), date.toString(capturedTexts.at(i + 1))); formatted = formatted.replace(capturedTexts.at(i), date.toString(capturedTexts.at(i + 1)));
} }
QRegExp randomRegex("%(?!%)\\((.+)\\)random");
randomRegex.indexIn(toFormat);
QStringList randomTexts(randomRegex.capturedTexts());
for (int i = 0; i < randomTexts.length(); i += 2) {
QStringList list = randomTexts.at(i + 1).split('|');
formatted = formatted.replace(randomTexts.at(i), list.at(rand() % (list.length())));
}
for (QString var : variables.keys()) formatted.replace("%" + var, variables[var]);
formatted = formatted.replace(QRegExp("%(?!%)ext"), ext); formatted = formatted.replace(QRegExp("%(?!%)ext"), ext);
return formatted; return formatted;
} }

View File

@ -1,11 +1,12 @@
#ifndef FORMATTER_HPP #ifndef FORMATTER_HPP
#define FORMATTER_HPP #define FORMATTER_HPP
#include <QMap>
#include <QRegExp> #include <QRegExp>
#include <QString> #include <QString>
namespace formatter { namespace formatter {
QString format(QString toFormat, QString ext); QString format(QString toFormat, QString ext, QMap<QString, QString> variables = QMap<QString, QString>());
} }
#endif // FORMATTER_HPP #endif // FORMATTER_HPP

View File

@ -9,6 +9,7 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QNetworkReply> #include <QNetworkReply>
#include <formats.hpp> #include <formats.hpp>
#include <formatter.hpp>
#include <io/ioutils.hpp> #include <io/ioutils.hpp>
#include <notifications.hpp> #include <notifications.hpp>
@ -209,7 +210,8 @@ QString parsePathspec(QJsonDocument &response, QString &pathspec) {
void CustomUploader::parseResult(QJsonDocument result, QByteArray data, QString returnPathspec, QString name) { void CustomUploader::parseResult(QJsonDocument result, QByteArray data, QString returnPathspec, QString name) {
if (result.isObject()) { if (result.isObject()) {
QString url = urlPrepend + parsePathspec(result, returnPathspec) + urlAppend; QString url
= formatter::format(urlPrepend, "") + parsePathspec(result, returnPathspec) + formatter::format(urlAppend, "");
if (!url.isEmpty()) { if (!url.isEmpty()) {
QApplication::clipboard()->setText(url); QApplication::clipboard()->setText(url);
notifications::notify(tr("KShare Custom Uploader ") + name, tr("Copied upload link to clipboard!")); notifications::notify(tr("KShare Custom Uploader ") + name, tr("Copied upload link to clipboard!"));
@ -230,9 +232,9 @@ QByteArray substituteArgs(QByteArray arr, QString format, QByteArray imgData = Q
if (arr.startsWith("/") && arr.endsWith("/")) { if (arr.startsWith("/") && arr.endsWith("/")) {
arr = arr.mid(1, arr.length() - 2); arr = arr.mid(1, arr.length() - 2);
arr = arr.replace("%contenttype", mime.toUtf8()); arr = formatter::format(QString::fromUtf8(arr), format.toLower(),
arr = arr.replace("%FORMAT", format.toUpper().toUtf8()); { { "format", format.toLower() }, { "FORMAT", format }, { "contenttype", mime } })
arr = arr.replace("%format", format.toLower().toUtf8()); .toUtf8();
if (imgData.isNull()) return arr; if (imgData.isNull()) return arr;
return arr.replace("%imagedata", imgData); return arr.replace("%imagedata", imgData);