8#include "plaintextmarkupbuilder.h"
12class PlainTextMarkupBuilderPrivate
15 PlainTextMarkupBuilderPrivate(PlainTextMarkupBuilder *b)
30 [[nodiscard]]
QString getLetterString(
int itemNumber);
32 [[nodiscard]]
QString getRomanString(
int itemNumber);
38 [[nodiscard]]
QString getReferences();
50 PlainTextMarkupBuilder *
const q_ptr;
52 Q_DECLARE_PUBLIC(PlainTextMarkupBuilder)
56using namespace KPIMTextEdit;
57PlainTextMarkupBuilder::PlainTextMarkupBuilder()
58 : d_ptr(new PlainTextMarkupBuilderPrivate(this))
62QString PlainTextMarkupBuilderPrivate::getRomanString(
int item)
70 auto romanSymbols = QStringLiteral(
"iiivixxxlxcccdcmmmm");
71 int c[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
73 for (
auto i = 12; i >= 0; n %= c[i], i--) {
76 auto startDigit = i + (i + 3) / 4;
97 result = romanNumeral;
99 result = QStringLiteral(
"?");
104QString PlainTextMarkupBuilderPrivate::getLetterString(
int itemNumber)
115 letterString.
prepend(QStringLiteral(
"%1").arg((itemNumber % LETTERSINALPHABET) + DIGITSOFFSET,
117 LETTERSINALPHABET + DIGITSOFFSET));
118 if ((itemNumber >= LETTERSINALPHABET)) {
119 itemNumber = itemNumber / LETTERSINALPHABET;
128QString PlainTextMarkupBuilderPrivate::getReferences()
132 refs.
append(QStringLiteral(
"\n--------\n"));
136 refs.
append(QStringLiteral(
"[%1] %2\n").arg(index++).arg(mUrls.
takeFirst()));
142PlainTextMarkupBuilder::~PlainTextMarkupBuilder()
147void PlainTextMarkupBuilder::setQuotePrefix(
const QString &prefix)
149 Q_D(PlainTextMarkupBuilder);
150 d->mQuoteprefix = prefix;
153void PlainTextMarkupBuilder::beginStrong()
155 Q_D(PlainTextMarkupBuilder);
159void PlainTextMarkupBuilder::endStrong()
161 Q_D(PlainTextMarkupBuilder);
165void PlainTextMarkupBuilder::beginEmph()
167 Q_D(PlainTextMarkupBuilder);
171void PlainTextMarkupBuilder::endEmph()
173 Q_D(PlainTextMarkupBuilder);
177void PlainTextMarkupBuilder::beginUnderline()
179 Q_D(PlainTextMarkupBuilder);
183void PlainTextMarkupBuilder::endUnderline()
185 Q_D(PlainTextMarkupBuilder);
189void PlainTextMarkupBuilder::beginStrikeout()
191 Q_D(PlainTextMarkupBuilder);
195void PlainTextMarkupBuilder::endStrikeout()
197 Q_D(PlainTextMarkupBuilder);
201void PlainTextMarkupBuilder::beginAnchor(
const QString &href,
const QString &name)
203 Q_D(PlainTextMarkupBuilder);
205 if (!d->mUrls.contains(href)) {
206 d->mUrls.append(href);
208 d->activeLink = href;
211void PlainTextMarkupBuilder::endAnchor()
213 Q_D(PlainTextMarkupBuilder);
214 d->mText.append(QStringLiteral(
"[%1]").arg(d->mUrls.indexOf(d->activeLink) + 1));
217void PlainTextMarkupBuilder::endParagraph()
219 Q_D(PlainTextMarkupBuilder);
223void PlainTextMarkupBuilder::addNewline()
225 Q_D(PlainTextMarkupBuilder);
229void PlainTextMarkupBuilder::insertHorizontalRule(
int width)
232 Q_D(PlainTextMarkupBuilder);
234 d->mText.append(QStringLiteral(
"--------------------\n"));
237int PlainTextMarkupBuilder::addReference(
const QString &reference)
239 Q_D(PlainTextMarkupBuilder);
241 if (!d->mUrls.contains(reference)) {
242 d->mUrls.append(reference);
244 return d->mUrls.indexOf(reference) + 1;
247void PlainTextMarkupBuilder::insertImage(
const QString &src, qreal width, qreal height)
249 Q_D(PlainTextMarkupBuilder);
253 const auto ref = addReference(src);
255 d->mText.append(QStringLiteral(
"[%1]").arg(ref));
260 Q_D(PlainTextMarkupBuilder);
261 d->currentListItemStyles.append(style);
262 d->currentListItemNumbers.append(0);
265void PlainTextMarkupBuilder::endList()
267 Q_D(PlainTextMarkupBuilder);
268 if (!d->currentListItemNumbers.isEmpty()) {
269 d->currentListItemStyles.removeLast();
270 d->currentListItemNumbers.removeLast();
274void PlainTextMarkupBuilder::beginListItem()
276 Q_D(PlainTextMarkupBuilder);
277 for (
int i = 0, total = d->currentListItemNumbers.size(); i < total; ++i) {
278 d->mText.append(QStringLiteral(
" "));
281 auto itemNumber = d->currentListItemNumbers.last();
283 switch (d->currentListItemStyles.last()) {
285 d->mText.append(QStringLiteral(
" * "));
288 d->mText.append(QStringLiteral(
" o "));
291 d->mText.append(QStringLiteral(
" - "));
294 d->mText.append(QStringLiteral(
" %1. ").arg(itemNumber + 1));
297 d->mText.append(QStringLiteral(
" %1. ").arg(d->getLetterString(itemNumber)));
300 d->mText.append(QStringLiteral(
" %1. ").arg(d->getLetterString(itemNumber).toUpper()));
303 d->mText.append(QStringLiteral(
" %1. ").arg(d->getRomanString(itemNumber + 1)));
306 d->mText.append(QStringLiteral(
" %1. ").arg(d->getRomanString(itemNumber + 1).toUpper()));
313void PlainTextMarkupBuilder::endListItem()
315 Q_D(PlainTextMarkupBuilder);
316 d->currentListItemNumbers.last() = d->currentListItemNumbers.last() + 1;
320void PlainTextMarkupBuilder::beginSuperscript()
322 Q_D(PlainTextMarkupBuilder);
323 d->mText.append(QStringLiteral(
"^{"));
326void PlainTextMarkupBuilder::endSuperscript()
328 Q_D(PlainTextMarkupBuilder);
332void PlainTextMarkupBuilder::beginSubscript()
334 Q_D(PlainTextMarkupBuilder);
335 d->mText.append(QStringLiteral(
"_{"));
338void PlainTextMarkupBuilder::endSubscript()
340 Q_D(PlainTextMarkupBuilder);
344void PlainTextMarkupBuilder::appendLiteralText(
const QString &text)
346 Q_D(PlainTextMarkupBuilder);
347 d->mText.append(text);
350void PlainTextMarkupBuilder::appendRawText(
const QString &text)
352 Q_D(PlainTextMarkupBuilder);
353 d->mText.append(text);
356QString PlainTextMarkupBuilder::getResult()
358 Q_D(PlainTextMarkupBuilder);
360 ret.append(d->getReferences());
365void PlainTextMarkupBuilder::beginParagraph(
Qt::Alignment a, qreal top, qreal bottom, qreal left, qreal right,
bool leftToRightText)
368 Q_UNUSED(leftToRightText)
369 Q_D(PlainTextMarkupBuilder);
370 if (isQuoteBlock(top, bottom, left, right)) {
371 d->mText.append(d->mQuoteprefix);
375bool PlainTextMarkupBuilder::isQuoteBlock(qreal top, qreal bottom, qreal left, qreal right)
const
379 return (
left == 40) && (right == 40);
382void PlainTextMarkupBuilder::beginBackground(
const QBrush &brush)
387void PlainTextMarkupBuilder::beginFontFamily(
const QString &family)
392void PlainTextMarkupBuilder::beginFontPointSize(
int size)
397void PlainTextMarkupBuilder::beginForeground(
const QBrush &brush)
402void PlainTextMarkupBuilder::beginHeader(
int level)
404 Q_D(PlainTextMarkupBuilder);
407 d->mText.append(QStringLiteral(
"# "));
410 d->mText.append(QStringLiteral(
"## "));
413 d->mText.append(QStringLiteral(
"### "));
416 d->mText.append(QStringLiteral(
"#### "));
419 d->mText.append(QStringLiteral(
"##### "));
422 d->mText.append(QStringLiteral(
"###### "));
429void PlainTextMarkupBuilder::beginTable(qreal cellpadding, qreal cellspacing,
const QString &width)
431 Q_UNUSED(cellpadding)
432 Q_UNUSED(cellspacing)
436void PlainTextMarkupBuilder::beginTableCell(
const QString &width,
int colSpan,
int rowSpan)
443void PlainTextMarkupBuilder::beginTableHeaderCell(
const QString &width,
int colSpan,
int rowSpan)
450void PlainTextMarkupBuilder::beginTableRow()
454void PlainTextMarkupBuilder::endBackground()
458void PlainTextMarkupBuilder::endFontFamily()
462void PlainTextMarkupBuilder::endFontPointSize()
466void PlainTextMarkupBuilder::endForeground()
470void PlainTextMarkupBuilder::endHeader(
int level)
472 Q_D(PlainTextMarkupBuilder);
473 qDebug() <<
" void PlainTextMarkupBuilder::endHeader(int level)" <<
level;
476 d->mText.append(QStringLiteral(
" #\n"));
479 d->mText.append(QStringLiteral(
" ##\n"));
482 d->mText.append(QStringLiteral(
" ###\n"));
485 d->mText.append(QStringLiteral(
" ####\n"));
488 d->mText.append(QStringLiteral(
" #####\n"));
491 d->mText.append(QStringLiteral(
" ######\n"));
498void PlainTextMarkupBuilder::endTable()
502void PlainTextMarkupBuilder::endTableCell()
506void PlainTextMarkupBuilder::endTableHeaderCell()
510void PlainTextMarkupBuilder::endTableRow()
514void PlainTextMarkupBuilder::addSingleBreakLine()
516 Q_D(PlainTextMarkupBuilder);
char * toString(const EngineQuery &query)
QStringView level(QStringView ifopt)
bool isEmpty() const const
QString & append(QChar ch)
QString & prepend(QChar ch)
QTextStream & left(QTextStream &stream)