Cutelee 6.1.0
plaintextmarkupbuilder.cpp
1/*
2 This file is part of the Cutelee template system.
3
4 Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either version
9 2.1 of the Licence, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library. If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include "plaintextmarkupbuilder.h"
22
23namespace Cutelee
24{
25
27{
28public:
30
40 QString getLetterString(int itemNumber);
41
42 QString getRomanString(int itemNumber);
43
49
50 QStringList m_urls;
51 QList<QTextListFormat::Style> currentListItemStyles;
52 QList<int> currentListItemNumbers;
53
54 QString activeLink;
55
56 QString m_text;
57
59
60 Q_DECLARE_PUBLIC(PlainTextMarkupBuilder)
61};
62}
63
64using namespace Cutelee;
65
66QString PlainTextMarkupBuilderPrivate::getRomanString(int item)
67{
68 QString result;
69 // Code based to gui/text/qtextlist.cpp
70 if (item < 5000) {
71 QString romanNumeral;
72
73 // works for up to 4999 items
74 auto romanSymbols = QStringLiteral("iiivixxxlxcccdcmmmm");
75 int c[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
76 auto n = item;
77 for (auto i = 12; i >= 0; n %= c[i], i--) {
78 auto q = n / c[i];
79 if (q > 0) {
80 auto startDigit = i + (i + 3) / 4;
81 int numDigits;
82 if (i % 4) {
83 // c[i] == 4|5|9|40|50|90|400|500|900
84 if ((i - 2) % 4) {
85 // c[i] == 4|9|40|90|400|900
86 // => with subtraction (IV, IX, XL, XC, ...)
87 numDigits = 2;
88 } else {
89 // c[i] == 5|50|500 (V, L, D)
90 numDigits = 1;
91 }
92 } else {
93 // c[i] == 1|10|100|1000 (I, II, III, X, XX, ...)
94 numDigits = q;
95 }
96
97 romanNumeral.append(romanSymbols.mid(startDigit, numDigits));
98 }
99 }
100 result = romanNumeral;
101 } else {
102 result = QStringLiteral("?");
103 }
104 return result;
105}
106
108{
109 QString letterString;
110 while (true) {
111 // Create the letter string by prepending one char at a time.
112 // The itemNumber is converted to a number in the base 36 (number of
113 // letters in the alphabet plus 10) after being increased by 10
114 // (to pass out the digits 0 to 9).
115 letterString.prepend(QStringLiteral("%1").arg(
116 (itemNumber % LETTERSINALPHABET) + DIGITSOFFSET,
117 0, // no padding while building this string.
118 LETTERSINALPHABET + DIGITSOFFSET));
119 if ((itemNumber >= LETTERSINALPHABET)) {
120 itemNumber = itemNumber / LETTERSINALPHABET;
121 itemNumber--;
122 } else {
123 break;
124 }
125 }
126 return letterString;
127}
128
130{
131 QString refs;
132 if (!m_urls.isEmpty()) {
133 refs.append(QStringLiteral("\n--------\n"));
134
135 auto index = 1;
136 while (!m_urls.isEmpty()) {
137 refs.append(
138 QStringLiteral("[%1] %2\n").arg(index++).arg(m_urls.takeFirst()));
139 }
140 }
141 return refs;
142}
143
144PlainTextMarkupBuilder::PlainTextMarkupBuilder()
145 : d_ptr(new PlainTextMarkupBuilderPrivate(this))
146{
147}
148
149PlainTextMarkupBuilder::~PlainTextMarkupBuilder() { delete d_ptr; }
150
152{
154 d->m_text.append(QLatin1Char('*'));
155}
156
158{
160 d->m_text.append(QLatin1Char('*'));
161}
162
164{
166 d->m_text.append(QLatin1Char('/'));
167}
168
170{
172 d->m_text.append(QLatin1Char('/'));
173}
174
176{
178 d->m_text.append(QLatin1Char('_'));
179}
180
182{
184 d->m_text.append(QLatin1Char('_'));
185}
186
188{
190 d->m_text.append(QLatin1Char('-'));
191}
192
194{
196 d->m_text.append(QLatin1Char('-'));
197}
198
200 const QString &name)
201{
203 Q_UNUSED(name);
204 if (!d->m_urls.contains(href)) {
205
206 d->m_urls.append(href);
207 }
208 d->activeLink = href;
209}
210
212{
214 d->m_text.append(
215 QStringLiteral("[%1]").arg(d->m_urls.indexOf(d->activeLink) + 1));
216}
217
219{
221 d->m_text.append(QLatin1Char('\n'));
222}
223
225{
227 d->m_text.append(QLatin1Char('\n'));
228}
229
231{
232 Q_UNUSED(width)
234
235 d->m_text.append(QStringLiteral("--------------------\n"));
236}
237
239{
241
242 if (!d->m_urls.contains(reference))
243 d->m_urls.append(reference);
244 return d->m_urls.indexOf(reference) + 1;
245}
246
247void PlainTextMarkupBuilder::insertImage(const QString &src, qreal width,
248 qreal height)
249{
251 Q_UNUSED(width)
252 Q_UNUSED(height)
253
254 auto ref = addReference(src);
255
256 d->m_text.append(QStringLiteral("[%1]").arg(ref));
257}
258
259void PlainTextMarkupBuilder::beginList(QTextListFormat::Style style)
260{
262 d->currentListItemStyles.append(style);
263 d->currentListItemNumbers.append(0);
264}
265
267{
269 if (!d->currentListItemNumbers.isEmpty()) {
270 d->currentListItemStyles.removeLast();
271 d->currentListItemNumbers.removeLast();
272 }
273}
274
276{
278 for (auto i = 0; i < d->currentListItemNumbers.size(); i++) {
279 d->m_text.append(QStringLiteral(" "));
280 }
281
282 auto itemNumber = d->currentListItemNumbers.last();
283
284 switch (d->currentListItemStyles.last()) {
285 case QTextListFormat::ListDisc:
286 d->m_text.append(QStringLiteral(" * "));
287 break;
288 case QTextListFormat::ListCircle:
289 d->m_text.append(QStringLiteral(" o "));
290 break;
291 case QTextListFormat::ListSquare:
292 d->m_text.append(QStringLiteral(" - "));
293 break;
294 case QTextListFormat::ListDecimal:
295 d->m_text.append(QStringLiteral(" %1. ").arg(itemNumber + 1));
296 break;
297 case QTextListFormat::ListLowerAlpha:
298 d->m_text.append(
299 QStringLiteral(" %1. ").arg(d->getLetterString(itemNumber)));
300 break;
301 case QTextListFormat::ListUpperAlpha:
302 d->m_text.append(
303 QStringLiteral(" %1. ").arg(d->getLetterString(itemNumber).toUpper()));
304 break;
305 case QTextListFormat::ListLowerRoman:
306 d->m_text.append(
307 QStringLiteral(" %1. ").arg(d->getRomanString(itemNumber + 1)));
308 break;
309 case QTextListFormat::ListUpperRoman:
310 d->m_text.append(QStringLiteral(" %1. ").arg(
311 d->getRomanString(itemNumber + 1).toUpper()));
312 break;
313 default:
314 break;
315 }
316}
317
319{
321 d->currentListItemNumbers.last() = d->currentListItemNumbers.last() + 1;
322 d->m_text.append(QLatin1Char('\n'));
323}
324
326{
328 d->m_text.append(QStringLiteral("^{"));
329}
330
332{
334 d->m_text.append(QLatin1Char('}'));
335}
336
338{
340 d->m_text.append(QStringLiteral("_{"));
341}
342
344{
346 d->m_text.append(QLatin1Char('}'));
347}
348
350{
352 d->m_text.append(text);
353}
354
356{
358 d->m_text.append(text);
359}
360
362{
364 auto ret = d->m_text;
365 ret.append(d->getReferences());
366 d->m_text.clear();
367 return ret;
368}
369
371{
372 Q_UNUSED(brush);
373}
374
376{
377 Q_UNUSED(family);
378}
379
380void PlainTextMarkupBuilder::beginFontPointSize(int size) { Q_UNUSED(size); }
381
383{
384 Q_UNUSED(brush);
385}
386
387void PlainTextMarkupBuilder::beginHeader(int level) { Q_UNUSED(level); }
388
389void PlainTextMarkupBuilder::beginParagraph(Qt::Alignment a, qreal top,
390 qreal bottom, qreal left,
391 qreal right)
392{
393 Q_UNUSED(a);
394 Q_UNUSED(top);
395 Q_UNUSED(bottom);
396 Q_UNUSED(left);
397 Q_UNUSED(right);
398}
399
400void PlainTextMarkupBuilder::beginTable(qreal cellpadding, qreal cellspacing,
401 const QString &width)
402{
403 Q_UNUSED(cellpadding);
404 Q_UNUSED(cellspacing);
405 Q_UNUSED(width);
406}
407
408void PlainTextMarkupBuilder::beginTableCell(const QString &width, int colSpan,
409 int rowSpan)
410{
411 Q_UNUSED(width);
412 Q_UNUSED(colSpan);
413 Q_UNUSED(rowSpan);
414}
415
417 int colSpan, int rowSpan)
418{
419 Q_UNUSED(width);
420 Q_UNUSED(colSpan);
421 Q_UNUSED(rowSpan);
422}
423
425
427
429
431
433
434void PlainTextMarkupBuilder::endHeader(int level) { Q_UNUSED(level) }
435
437
439
441
Creates a simple marked up plain text document.
void beginTableCell(const QString &width, int colSpan, int rowSpan) override
void appendLiteralText(const QString &text) override
void beginFontFamily(const QString &family) override
void beginForeground(const QBrush &brush) override
void appendRawText(const QString &text) override
void beginBackground(const QBrush &brush) override
void beginTable(qreal cellpadding, qreal cellspacing, const QString &width) override
int addReference(const QString &reference)
void beginParagraph(Qt::Alignment a=Qt::AlignLeft, qreal top=0.0, qreal bottom=0.0, qreal left=0.0, qreal right=0.0) override
void beginTableHeaderCell(const QString &width, int colSpan, int rowSpan) override
void beginList(QTextListFormat::Style style) override
void insertImage(const QString &src, qreal width, qreal height) override
void beginAnchor(const QString &href={}, const QString &name={}) override
void insertHorizontalRule(int width=-1) override
The Cutelee namespace holds all public Cutelee API.
Definition Mainpage.dox:8