2014-11-13 01:04:59 +02:00
|
|
|
|
/* This file is part of the KDE libraries
|
2023-08-17 01:41:38 +03:00
|
|
|
|
Copyright (C) 2023 Ivailo Monev <xakepa10@gmail.com>
|
2014-11-13 01:04:59 +02:00
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Library General Public
|
2023-08-17 01:41:38 +03:00
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
2014-11-13 01:04:59 +02:00
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-08-17 01:41:38 +03:00
|
|
|
|
#include "kuitsemantics_p.h"
|
|
|
|
|
#include "kglobal.h"
|
|
|
|
|
#include "klocale.h"
|
|
|
|
|
#include "kdebug.h"
|
|
|
|
|
|
2023-08-17 19:43:51 +03:00
|
|
|
|
const QLatin1String KuitSemantics::s_numargs = QLatin1String(KUIT_NUMARGS);
|
2023-08-17 14:33:44 +03:00
|
|
|
|
const QLatin1String KuitSemantics::s_numintg = QLatin1String(KUIT_NUMINTG);
|
|
|
|
|
const QLatin1String KuitSemantics::s_numreal = QLatin1String(KUIT_NUMREAL);
|
|
|
|
|
const QLatin1String KuitSemantics::s_title = QLatin1String("title");
|
|
|
|
|
|
2023-08-17 01:41:38 +03:00
|
|
|
|
KuitSemantics::KuitSemantics(const QString &lang)
|
|
|
|
|
: m_catalog(QString::fromLatin1("kdelibs4"), lang)
|
|
|
|
|
{
|
|
|
|
|
KuitFormat format;
|
2023-08-17 14:33:44 +03:00
|
|
|
|
format.tag = QString::fromLatin1("filename");
|
2023-08-17 01:41:38 +03:00
|
|
|
|
format.plain = m_catalog.translate("@filename/plain", "‘%1’");
|
|
|
|
|
format.rich = m_catalog.translate("@filename/rich", "<tt>%1</tt>");
|
2023-08-17 14:33:44 +03:00
|
|
|
|
m_formats.append(format);
|
|
|
|
|
format.tag = QString::fromLatin1("email");
|
2023-08-20 19:20:12 +03:00
|
|
|
|
format.plain = m_catalog.translate("@email/plain", "<%1>");
|
2023-08-17 01:41:38 +03:00
|
|
|
|
format.rich = m_catalog.translate("@email/rich", "<<a href=\"mailto:%1\">%1</a>>");
|
2023-08-17 14:33:44 +03:00
|
|
|
|
m_formats.append(format);
|
|
|
|
|
format.tag = QString::fromLatin1("title");
|
2023-08-17 01:41:38 +03:00
|
|
|
|
format.plain = m_catalog.translate("@title/plain", "== %1 ==");
|
|
|
|
|
format.rich = m_catalog.translate("@title/rich", "<h2>%1</h2>");
|
2023-08-17 14:33:44 +03:00
|
|
|
|
m_formats.append(format);
|
|
|
|
|
format.tag = QString::fromLatin1("warning");
|
2023-08-17 01:41:38 +03:00
|
|
|
|
format.plain = m_catalog.translate("@warning/plain", "WARNING: %1");
|
|
|
|
|
format.rich = m_catalog.translate("@warning/rich", "<b>Warning</b>: %1");
|
2023-08-17 14:33:44 +03:00
|
|
|
|
m_formats.append(format);
|
|
|
|
|
format.tag = QString::fromLatin1("command");
|
2023-08-17 01:41:38 +03:00
|
|
|
|
format.plain = m_catalog.translate("@command/plain", "%1");
|
|
|
|
|
format.rich = m_catalog.translate("@command/rich", "<tt>%1</tt>");
|
2023-08-17 14:33:44 +03:00
|
|
|
|
m_formats.append(format);
|
|
|
|
|
format.tag = QString::fromLatin1("resource");
|
2023-08-17 01:41:38 +03:00
|
|
|
|
format.plain = m_catalog.translate("@resource/plain", "“%1”");
|
|
|
|
|
format.rich = m_catalog.translate("@resource/rich", "“%1”");
|
2023-08-17 14:33:44 +03:00
|
|
|
|
m_formats.append(format);
|
|
|
|
|
format.tag = QString::fromLatin1("message");
|
2023-08-17 01:41:38 +03:00
|
|
|
|
format.plain = m_catalog.translate("@message/plain", "/%1/");
|
|
|
|
|
format.rich = m_catalog.translate("@message/rich", "<i>%1</i>");
|
2023-08-17 14:33:44 +03:00
|
|
|
|
m_formats.append(format);
|
2023-08-17 01:41:38 +03:00
|
|
|
|
// special cases
|
2023-08-17 19:43:51 +03:00
|
|
|
|
format.tag = QString::fromLatin1(KUIT_NUMARGS);
|
2023-08-17 01:41:38 +03:00
|
|
|
|
format.plain = QString();
|
|
|
|
|
format.rich = QString();
|
2023-08-17 14:33:44 +03:00
|
|
|
|
m_formats.append(format);
|
|
|
|
|
format.tag = QString::fromLatin1(KUIT_NUMREAL);
|
|
|
|
|
m_formats.append(format);
|
2023-08-17 19:43:51 +03:00
|
|
|
|
format.tag = QString::fromLatin1(KUIT_NUMINTG);
|
2023-08-17 14:33:44 +03:00
|
|
|
|
m_formats.append(format);
|
2023-08-17 01:41:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString KuitSemantics::format(const QString &text, const QString &ctxt) const
|
|
|
|
|
{
|
|
|
|
|
QString result = text;
|
|
|
|
|
|
|
|
|
|
bool checkifrich = true;
|
|
|
|
|
bool isrich = false;
|
|
|
|
|
if (ctxt.startsWith(QLatin1Char('@'))) {
|
2023-08-18 23:38:25 +03:00
|
|
|
|
if (ctxt.startsWith(QLatin1String("@info"))) {
|
|
|
|
|
// rich by default (compat)
|
|
|
|
|
isrich = true;
|
|
|
|
|
checkifrich = false;
|
|
|
|
|
}
|
2023-08-17 22:08:20 +03:00
|
|
|
|
if (ctxt.contains(QLatin1String("/plain"))) {
|
2023-08-17 01:41:38 +03:00
|
|
|
|
isrich = false;
|
|
|
|
|
checkifrich = false;
|
2023-08-17 22:08:20 +03:00
|
|
|
|
} else if (ctxt.contains(QLatin1String("/rich"))) {
|
2023-08-17 01:41:38 +03:00
|
|
|
|
isrich = true;
|
|
|
|
|
checkifrich = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (checkifrich) {
|
|
|
|
|
isrich = Qt::mightBeRichText(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// qDebug() << Q_FUNC_INFO << "formatting" << ctxt << result << isrich;
|
2023-08-17 14:33:44 +03:00
|
|
|
|
foreach (const KuitFormat &format, m_formats) {
|
|
|
|
|
// exceptions
|
2023-08-18 00:39:06 +03:00
|
|
|
|
if (format.tag == s_title || format.tag == s_numargs
|
|
|
|
|
|| format.tag == s_numintg || format.tag == s_numreal) {
|
2023-08-17 01:41:38 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-08-17 14:33:44 +03:00
|
|
|
|
if (ctxt.startsWith(QLatin1String("@") + format.tag)) {
|
2023-08-17 01:41:38 +03:00
|
|
|
|
if (isrich) {
|
2023-08-17 14:33:44 +03:00
|
|
|
|
result = format.rich.arg(result);
|
2023-08-17 01:41:38 +03:00
|
|
|
|
} else {
|
2023-08-17 14:33:44 +03:00
|
|
|
|
result = format.plain.arg(result);
|
2023-08-17 01:41:38 +03:00
|
|
|
|
}
|
2023-08-18 23:31:36 +03:00
|
|
|
|
break;
|
2023-08-17 01:41:38 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 19:43:51 +03:00
|
|
|
|
int numprec = -1;
|
|
|
|
|
int numwidth = 0;
|
|
|
|
|
QChar numfill = QLatin1Char(' ');
|
2023-08-17 14:33:44 +03:00
|
|
|
|
foreach (const KuitFormat &format, m_formats) {
|
|
|
|
|
const QString startformat = QLatin1String("<") + format.tag + QLatin1String(">");
|
|
|
|
|
const QString endformat = QLatin1String("</") + format.tag + QLatin1String(">");
|
|
|
|
|
int tagstartpos = result.indexOf(startformat);
|
|
|
|
|
// qDebug() << Q_FUNC_INFO << format.tag << tagstartpos;
|
2023-08-17 01:41:38 +03:00
|
|
|
|
while (tagstartpos >= 0) {
|
2023-08-18 20:04:16 +03:00
|
|
|
|
const int tagendpos = result.indexOf(endformat, tagstartpos + startformat.size());
|
2023-08-17 23:08:48 +03:00
|
|
|
|
if (Q_LIKELY(tagendpos >= tagstartpos)) {
|
2023-08-17 14:33:44 +03:00
|
|
|
|
const QString tagvalue = result.mid(tagstartpos + startformat.size(), tagendpos - tagstartpos - startformat.size());
|
|
|
|
|
// qDebug() << Q_FUNC_INFO << "tagvalue" << format.tag << tagvalue << tagstartpos << tagendpos;
|
2023-08-17 01:41:38 +03:00
|
|
|
|
QString tagsubstitute;
|
2023-08-17 19:43:51 +03:00
|
|
|
|
if (format.tag == s_numargs) {
|
|
|
|
|
// split alert!
|
|
|
|
|
const QStringList numargs = tagvalue.split(QLatin1Char(':'));
|
|
|
|
|
if (Q_LIKELY(numargs.size() == 3)) {
|
|
|
|
|
numprec = numargs.at(0).toInt();
|
|
|
|
|
numwidth = numargs.at(1).toInt();
|
|
|
|
|
numfill = numargs.at(2)[0];
|
|
|
|
|
} else {
|
|
|
|
|
kWarning() << "invalid number arguments tag" << format.tag;
|
|
|
|
|
}
|
2023-08-17 01:41:38 +03:00
|
|
|
|
tagsubstitute = QString();
|
2023-08-17 14:33:44 +03:00
|
|
|
|
} else if (format.tag == s_numintg || format.tag == s_numreal) {
|
2023-08-17 19:43:51 +03:00
|
|
|
|
// qDebug() << Q_FUNC_INFO << "tagnum" << format.tag << tagvalue << numprec << numwidth << numfill;
|
|
|
|
|
tagsubstitute = QString::fromLatin1("%1").arg(
|
|
|
|
|
KGlobal::locale()->formatNumber(tagvalue, false, numprec),
|
|
|
|
|
numwidth, numfill
|
|
|
|
|
);
|
|
|
|
|
numprec = -1;
|
|
|
|
|
numwidth = 0;
|
|
|
|
|
numfill = QLatin1Char(' ');
|
2023-08-17 01:41:38 +03:00
|
|
|
|
} else if (isrich) {
|
2023-08-17 14:33:44 +03:00
|
|
|
|
tagsubstitute = format.rich.arg(tagvalue);
|
2014-11-13 01:04:59 +02:00
|
|
|
|
} else {
|
2023-08-17 14:33:44 +03:00
|
|
|
|
tagsubstitute = format.plain.arg(tagvalue);
|
2014-11-13 01:04:59 +02:00
|
|
|
|
}
|
2023-08-17 14:33:44 +03:00
|
|
|
|
// qDebug() << Q_FUNC_INFO << "replacing" << result.mid(tagstartpos, tagendpos - tagstartpos + endformat.size()) << tagsubstitute;
|
|
|
|
|
result.replace(tagstartpos, tagendpos - tagstartpos + endformat.size(), tagsubstitute);
|
2023-08-17 01:41:38 +03:00
|
|
|
|
} else {
|
2023-08-17 19:43:51 +03:00
|
|
|
|
kWarning() << "found starting but no ending tag for" << format.tag;
|
2023-08-18 01:02:44 +03:00
|
|
|
|
break;
|
2014-11-13 01:04:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 14:33:44 +03:00
|
|
|
|
tagstartpos = result.indexOf(startformat);
|
2014-11-13 01:04:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 01:41:38 +03:00
|
|
|
|
return result;
|
2014-11-13 01:04:59 +02:00
|
|
|
|
}
|