Add %(a|b|c|..)random to formatter
This commit is contained in:
parent
bd2adc5e53
commit
f7ec1f44f2
@ -3,15 +3,27 @@
|
||||
#include <QDateTime>
|
||||
#include <QStringList>
|
||||
|
||||
QString formatter::format(QString toFormat, QString ext) {
|
||||
QRegExp dateRegex("%\\((.+)\\)date");
|
||||
QString formatter::format(QString toFormat, QString ext, QMap<QString, QString> variables) {
|
||||
QString formatted(toFormat);
|
||||
|
||||
QRegExp dateRegex("%(?!%)\\((.+)\\)date");
|
||||
dateRegex.indexIn(toFormat);
|
||||
QStringList capturedTexts(dateRegex.capturedTexts());
|
||||
QString formatted(toFormat);
|
||||
QDateTime date = QDateTime::currentDateTime();
|
||||
for (int i = 0; i < capturedTexts.length(); i += 2) {
|
||||
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);
|
||||
return formatted;
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
#ifndef FORMATTER_HPP
|
||||
#define FORMATTER_HPP
|
||||
|
||||
#include <QMap>
|
||||
#include <QRegExp>
|
||||
#include <QString>
|
||||
|
||||
namespace formatter {
|
||||
QString format(QString toFormat, QString ext);
|
||||
QString format(QString toFormat, QString ext, QMap<QString, QString> variables = QMap<QString, QString>());
|
||||
}
|
||||
|
||||
#endif // FORMATTER_HPP
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <QJsonDocument>
|
||||
#include <QNetworkReply>
|
||||
#include <formats.hpp>
|
||||
#include <formatter.hpp>
|
||||
#include <io/ioutils.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) {
|
||||
if (result.isObject()) {
|
||||
QString url = urlPrepend + parsePathspec(result, returnPathspec) + urlAppend;
|
||||
QString url
|
||||
= formatter::format(urlPrepend, "") + parsePathspec(result, returnPathspec) + formatter::format(urlAppend, "");
|
||||
if (!url.isEmpty()) {
|
||||
QApplication::clipboard()->setText(url);
|
||||
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("/")) {
|
||||
arr = arr.mid(1, arr.length() - 2);
|
||||
|
||||
arr = arr.replace("%contenttype", mime.toUtf8());
|
||||
arr = arr.replace("%FORMAT", format.toUpper().toUtf8());
|
||||
arr = arr.replace("%format", format.toLower().toUtf8());
|
||||
arr = formatter::format(QString::fromUtf8(arr), format.toLower(),
|
||||
{ { "format", format.toLower() }, { "FORMAT", format }, { "contenttype", mime } })
|
||||
.toUtf8();
|
||||
|
||||
if (imgData.isNull()) return arr;
|
||||
return arr.replace("%imagedata", imgData);
|
||||
|
Loading…
Reference in New Issue
Block a user