/src/qtbase/src/gui/text/qtextmarkdownimporter.cpp
Line | Count | Source |
1 | | // Copyright (C) 2019 The Qt Company Ltd. |
2 | | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | | // Qt-Security score:critical reason:data-parser |
4 | | |
5 | | #include "qtextmarkdownimporter_p.h" |
6 | | #include "qtextdocumentfragment_p.h" |
7 | | #include <QLoggingCategory> |
8 | | #if QT_CONFIG(regularexpression) |
9 | | #include <QRegularExpression> |
10 | | #include <QRegularExpressionMatchIterator> |
11 | | #endif |
12 | | #include <QTextCursor> |
13 | | #include <QTextDocument> |
14 | | #include <QTextDocumentFragment> |
15 | | #include <QTextList> |
16 | | #include <QTextTable> |
17 | | #if QT_CONFIG(system_textmarkdownreader) |
18 | | #include <md4c.h> |
19 | | #else |
20 | | #include "../../3rdparty/md4c/md4c.h" |
21 | | #endif |
22 | | |
23 | | QT_BEGIN_NAMESPACE |
24 | | |
25 | | using namespace Qt::StringLiterals; |
26 | | |
27 | | Q_STATIC_LOGGING_CATEGORY(lcMD, "qt.text.markdown") |
28 | | |
29 | | static const QChar qtmi_Newline = u'\n'; |
30 | | static const QChar qtmi_Space = u' '; |
31 | | |
32 | 0 | static constexpr auto lfMarkerString() noexcept { return "---\n"_L1; } |
33 | 0 | static constexpr auto crlfMarkerString() noexcept { return "---\r\n"_L1; } |
34 | | |
35 | | // TODO maybe eliminate the margins after all views recognize BlockQuoteLevel, CSS can format it, etc. |
36 | | static const int qtmi_BlockQuoteIndent = |
37 | | 40; // pixels, same as in QTextHtmlParserNode::initializeProperties |
38 | | |
39 | | static_assert(int(QTextMarkdownImporter::FeatureCollapseWhitespace) == MD_FLAG_COLLAPSEWHITESPACE); |
40 | | static_assert(int(QTextMarkdownImporter::FeaturePermissiveATXHeaders) == MD_FLAG_PERMISSIVEATXHEADERS); |
41 | | static_assert(int(QTextMarkdownImporter::FeaturePermissiveURLAutoLinks) == MD_FLAG_PERMISSIVEURLAUTOLINKS); |
42 | | static_assert(int(QTextMarkdownImporter::FeaturePermissiveMailAutoLinks) == MD_FLAG_PERMISSIVEEMAILAUTOLINKS); |
43 | | static_assert(int(QTextMarkdownImporter::FeatureNoIndentedCodeBlocks) == MD_FLAG_NOINDENTEDCODEBLOCKS); |
44 | | static_assert(int(QTextMarkdownImporter::FeatureNoHTMLBlocks) == MD_FLAG_NOHTMLBLOCKS); |
45 | | static_assert(int(QTextMarkdownImporter::FeatureNoHTMLSpans) == MD_FLAG_NOHTMLSPANS); |
46 | | static_assert(int(QTextMarkdownImporter::FeatureTables) == MD_FLAG_TABLES); |
47 | | static_assert(int(QTextMarkdownImporter::FeatureStrikeThrough) == MD_FLAG_STRIKETHROUGH); |
48 | | static_assert(int(QTextMarkdownImporter::FeatureUnderline) == MD_FLAG_UNDERLINE); |
49 | | static_assert(int(QTextMarkdownImporter::FeaturePermissiveWWWAutoLinks) == MD_FLAG_PERMISSIVEWWWAUTOLINKS); |
50 | | static_assert(int(QTextMarkdownImporter::FeaturePermissiveAutoLinks) == MD_FLAG_PERMISSIVEAUTOLINKS); |
51 | | static_assert(int(QTextMarkdownImporter::FeatureTasklists) == MD_FLAG_TASKLISTS); |
52 | | static_assert(int(QTextMarkdownImporter::FeatureNoHTML) == MD_FLAG_NOHTML); |
53 | | static_assert(int(QTextMarkdownImporter::DialectCommonMark) == MD_DIALECT_COMMONMARK); |
54 | | // We don't yet support all the features in MD_DIALECT_GITHUB |
55 | | static_assert(int(QTextMarkdownImporter::DialectGitHub) == |
56 | | (MD_FLAG_PERMISSIVEAUTOLINKS | MD_FLAG_TABLES | MD_FLAG_STRIKETHROUGH | MD_FLAG_TASKLISTS |
57 | | | MD_FLAG_UNDERLINE | QTextMarkdownImporter::FeatureFrontMatter)); |
58 | | |
59 | | // -------------------------------------------------------- |
60 | | // MD4C callback function wrappers |
61 | | |
62 | | static int CbEnterBlock(MD_BLOCKTYPE type, void *detail, void *userdata) |
63 | 0 | { |
64 | 0 | QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata); |
65 | 0 | return mdi->cbEnterBlock(int(type), detail); |
66 | 0 | } |
67 | | |
68 | | static int CbLeaveBlock(MD_BLOCKTYPE type, void *detail, void *userdata) |
69 | 0 | { |
70 | 0 | QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata); |
71 | 0 | return mdi->cbLeaveBlock(int(type), detail); |
72 | 0 | } |
73 | | |
74 | | static int CbEnterSpan(MD_SPANTYPE type, void *detail, void *userdata) |
75 | 0 | { |
76 | 0 | QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata); |
77 | 0 | return mdi->cbEnterSpan(int(type), detail); |
78 | 0 | } |
79 | | |
80 | | static int CbLeaveSpan(MD_SPANTYPE type, void *detail, void *userdata) |
81 | 0 | { |
82 | 0 | QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata); |
83 | 0 | return mdi->cbLeaveSpan(int(type), detail); |
84 | 0 | } |
85 | | |
86 | | static int CbText(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *userdata) |
87 | 0 | { |
88 | 0 | QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata); |
89 | 0 | return mdi->cbText(int(type), text, size); |
90 | 0 | } |
91 | | |
92 | | static void CbDebugLog(const char *msg, void *userdata) |
93 | 0 | { |
94 | 0 | Q_UNUSED(userdata); |
95 | 0 | qCDebug(lcMD) << msg; |
96 | 0 | } |
97 | | |
98 | | // MD4C callback function wrappers |
99 | | // -------------------------------------------------------- |
100 | | |
101 | | static Qt::Alignment MdAlignment(MD_ALIGN a, Qt::Alignment defaultAlignment = Qt::AlignLeft | Qt::AlignVCenter) |
102 | 0 | { |
103 | 0 | switch (a) { |
104 | 0 | case MD_ALIGN_LEFT: |
105 | 0 | return Qt::AlignLeft | Qt::AlignVCenter; |
106 | 0 | case MD_ALIGN_CENTER: |
107 | 0 | return Qt::AlignHCenter | Qt::AlignVCenter; |
108 | 0 | case MD_ALIGN_RIGHT: |
109 | 0 | return Qt::AlignRight | Qt::AlignVCenter; |
110 | 0 | default: // including MD_ALIGN_DEFAULT |
111 | 0 | return defaultAlignment; |
112 | 0 | } |
113 | 0 | } |
114 | | |
115 | | QTextMarkdownImporter::QTextMarkdownImporter(QTextDocument *doc, QTextMarkdownImporter::Features features) |
116 | 0 | : m_cursor(doc) |
117 | 0 | , m_monoFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)) |
118 | 0 | , m_features(features) |
119 | 0 | { |
120 | 0 | } |
121 | | |
122 | | QTextMarkdownImporter::QTextMarkdownImporter(QTextDocument *doc, QTextDocument::MarkdownFeatures features) |
123 | 0 | : QTextMarkdownImporter(doc, static_cast<QTextMarkdownImporter::Features>(int(features))) |
124 | 0 | { |
125 | 0 | } |
126 | | |
127 | | /*! \internal |
128 | | Split any Front Matter from the Markdown document \a md. |
129 | | Returns a pair of QStringViews: if \a md begins with qualifying Front Matter |
130 | | (according to the specification at https://jekyllrb.com/docs/front-matter/ ), |
131 | | put it into the \c frontMatter view, omitting both markers; and put the remaining |
132 | | Markdown into \c rest. If no Front Matter is found, return all of \a md in \c rest. |
133 | | */ |
134 | | static auto splitFrontMatter(QStringView md) |
135 | 0 | { |
136 | 0 | struct R { |
137 | 0 | QStringView frontMatter, rest; |
138 | 0 | explicit operator bool() const noexcept { return !frontMatter.isEmpty(); } |
139 | 0 | }; |
140 | |
|
141 | 0 | const auto NotFound = R{{}, md}; |
142 | | |
143 | | /* Front Matter must start with '---\n' or '---\r\n' on the very first line, |
144 | | and Front Matter must end with another such line. |
145 | | If that is not the case, we return NotFound: then the whole document is |
146 | | to be passed on to the Markdown parser, in which '---\n' is interpreted |
147 | | as a "thematic break" (like <hr/> in HTML). */ |
148 | 0 | QLatin1StringView marker; |
149 | 0 | if (md.startsWith(lfMarkerString())) |
150 | 0 | marker = lfMarkerString(); |
151 | 0 | else if (md.startsWith(crlfMarkerString())) |
152 | 0 | marker = crlfMarkerString(); |
153 | 0 | else |
154 | 0 | return NotFound; |
155 | | |
156 | 0 | const auto frontMatterStart = marker.size(); |
157 | 0 | const auto endMarkerPos = md.indexOf(marker, frontMatterStart); |
158 | |
|
159 | 0 | if (endMarkerPos < 0 || md[endMarkerPos - 1] != QChar::LineFeed) |
160 | 0 | return NotFound; |
161 | | |
162 | 0 | Q_ASSERT(frontMatterStart < md.size()); |
163 | 0 | Q_ASSERT(endMarkerPos < md.size()); |
164 | 0 | const auto frontMatter = md.sliced(frontMatterStart, endMarkerPos - frontMatterStart); |
165 | 0 | return R{frontMatter, md.sliced(endMarkerPos + marker.size())}; |
166 | 0 | } |
167 | | |
168 | | void QTextMarkdownImporter::import(const QString &markdown) |
169 | 0 | { |
170 | 0 | MD_PARSER callbacks = { |
171 | 0 | 0, // abi_version |
172 | 0 | unsigned(m_features) & unsigned(0xFFEFFFFF), // exclude MD_FLAG_FOOTNOTES: unsupported in Qt |
173 | 0 | &CbEnterBlock, |
174 | 0 | &CbLeaveBlock, |
175 | 0 | &CbEnterSpan, |
176 | 0 | &CbLeaveSpan, |
177 | 0 | &CbText, |
178 | 0 | &CbDebugLog, |
179 | 0 | nullptr // syntax |
180 | 0 | }; |
181 | 0 | QTextDocument *doc = m_cursor.document(); |
182 | 0 | const auto defaultFont = doc->defaultFont(); |
183 | 0 | m_paragraphMargin = defaultFont.pointSize() * 2 / 3; |
184 | 0 | doc->clear(); |
185 | 0 | qCDebug(lcMD) << "default font" << defaultFont << "mono font" << m_monoFont; |
186 | 0 | QStringView md = markdown; |
187 | |
|
188 | 0 | if (m_features.testFlag(QTextMarkdownImporter::FeatureFrontMatter)) { |
189 | 0 | if (const auto split = splitFrontMatter(md)) { |
190 | 0 | doc->setMetaInformation(QTextDocument::FrontMatter, split.frontMatter.toString()); |
191 | 0 | qCDebug(lcMD) << "extracted FrontMatter: size" << split.frontMatter.size(); |
192 | 0 | md = split.rest; |
193 | 0 | } |
194 | 0 | } |
195 | |
|
196 | 0 | const auto mdUtf8 = md.toUtf8(); |
197 | 0 | m_cursor.beginEditBlock(); |
198 | 0 | md_parse(mdUtf8.constData(), MD_SIZE(mdUtf8.size()), &callbacks, this); |
199 | 0 | m_cursor.endEditBlock(); |
200 | 0 | } |
201 | | |
202 | | int QTextMarkdownImporter::cbEnterBlock(int blockType, void *det) |
203 | 0 | { |
204 | 0 | m_blockType = blockType; |
205 | 0 | switch (blockType) { |
206 | 0 | case MD_BLOCK_P: |
207 | 0 | if (!m_listStack.isEmpty()) |
208 | 0 | qCDebug(lcMD, m_listItem ? "P of LI at level %d" : "P continuation inside LI at level %d", int(m_listStack.size())); |
209 | 0 | else |
210 | 0 | qCDebug(lcMD, "P"); |
211 | 0 | m_needsInsertBlock = true; |
212 | 0 | break; |
213 | 0 | case MD_BLOCK_QUOTE: |
214 | | // md4c delivers these callbacks in document order, so being inside a list item here means |
215 | | // the list encloses this quote: "- > quoted". The list is only pushed onto the stack in |
216 | | // insertBlock(); for the first item, m_needsInsertList tells us if we are already in one. |
217 | 0 | if (!m_blockQuoteDepth) |
218 | 0 | m_blockQuoteInsideListItem = !m_listStack.isEmpty() || m_needsInsertList; |
219 | 0 | ++m_blockQuoteDepth; |
220 | 0 | qCDebug(lcMD, "QUOTE level %d inside list item %d", m_blockQuoteDepth, |
221 | 0 | m_blockQuoteInsideListItem); |
222 | 0 | break; |
223 | 0 | case MD_BLOCK_CODE: { |
224 | 0 | MD_BLOCK_CODE_DETAIL *detail = static_cast<MD_BLOCK_CODE_DETAIL *>(det); |
225 | 0 | m_codeBlock = true; |
226 | 0 | m_blockCodeLanguage = QLatin1StringView(detail->lang.text, int(detail->lang.size)); |
227 | 0 | m_blockCodeFence = detail->fence_char; |
228 | 0 | QString info = QLatin1StringView(detail->info.text, int(detail->info.size)); |
229 | 0 | m_needsInsertBlock = true; |
230 | 0 | if (m_blockQuoteDepth) |
231 | 0 | qCDebug(lcMD, "CODE lang '%s' info '%s' fenced with '%c' inside QUOTE %d", qPrintable(m_blockCodeLanguage), qPrintable(info), m_blockCodeFence, m_blockQuoteDepth); |
232 | 0 | else |
233 | 0 | qCDebug(lcMD, "CODE lang '%s' info '%s' fenced with '%c'", qPrintable(m_blockCodeLanguage), qPrintable(info), m_blockCodeFence); |
234 | 0 | } break; |
235 | 0 | case MD_BLOCK_H: { |
236 | 0 | MD_BLOCK_H_DETAIL *detail = static_cast<MD_BLOCK_H_DETAIL *>(det); |
237 | 0 | QTextBlockFormat blockFmt; |
238 | 0 | QTextCharFormat charFmt; |
239 | 0 | int sizeAdjustment = 4 - int(detail->level); // H1 to H6: +3 to -2 |
240 | 0 | charFmt.setProperty(QTextFormat::FontSizeAdjustment, sizeAdjustment); |
241 | 0 | charFmt.setFontWeight(QFont::Bold); |
242 | 0 | blockFmt.setHeadingLevel(int(detail->level)); |
243 | 0 | m_needsInsertBlock = false; |
244 | 0 | if (m_cursor.document()->isEmpty()) { |
245 | 0 | m_cursor.setBlockFormat(blockFmt); |
246 | 0 | m_cursor.setCharFormat(charFmt); |
247 | 0 | } else { |
248 | 0 | m_cursor.insertBlock(blockFmt, charFmt); |
249 | 0 | } |
250 | 0 | qCDebug(lcMD, "H%d", detail->level); |
251 | 0 | } break; |
252 | 0 | case MD_BLOCK_LI: { |
253 | 0 | m_needsInsertBlock = true; |
254 | 0 | m_listItem = true; |
255 | 0 | MD_BLOCK_LI_DETAIL *detail = static_cast<MD_BLOCK_LI_DETAIL *>(det); |
256 | 0 | m_markerType = detail->is_task ? |
257 | 0 | (detail->task_mark == ' ' ? QTextBlockFormat::MarkerType::Unchecked : QTextBlockFormat::MarkerType::Checked) : |
258 | 0 | QTextBlockFormat::MarkerType::NoMarker; |
259 | 0 | qCDebug(lcMD) << "LI"; |
260 | 0 | } break; |
261 | 0 | case MD_BLOCK_UL: { |
262 | 0 | if (m_needsInsertList) // list nested in an empty list |
263 | 0 | m_listStack.push(m_cursor.insertList(m_listFormat)); |
264 | 0 | else |
265 | 0 | m_needsInsertList = true; |
266 | 0 | MD_BLOCK_UL_DETAIL *detail = static_cast<MD_BLOCK_UL_DETAIL *>(det); |
267 | 0 | m_listFormat = QTextListFormat(); |
268 | 0 | m_listFormat.setIndent(m_listStack.size() + 1); |
269 | 0 | switch (detail->mark) { |
270 | 0 | case '*': |
271 | 0 | m_listFormat.setStyle(QTextListFormat::ListCircle); |
272 | 0 | break; |
273 | 0 | case '+': |
274 | 0 | m_listFormat.setStyle(QTextListFormat::ListSquare); |
275 | 0 | break; |
276 | 0 | default: // including '-' |
277 | 0 | m_listFormat.setStyle(QTextListFormat::ListDisc); |
278 | 0 | break; |
279 | 0 | } |
280 | 0 | qCDebug(lcMD, "UL %c level %d", detail->mark, int(m_listStack.size()) + 1); |
281 | 0 | } break; |
282 | 0 | case MD_BLOCK_OL: { |
283 | 0 | if (m_needsInsertList) // list nested in an empty list |
284 | 0 | m_listStack.push(m_cursor.insertList(m_listFormat)); |
285 | 0 | else |
286 | 0 | m_needsInsertList = true; |
287 | 0 | MD_BLOCK_OL_DETAIL *detail = static_cast<MD_BLOCK_OL_DETAIL *>(det); |
288 | 0 | m_listFormat = QTextListFormat(); |
289 | 0 | m_listFormat.setIndent(m_listStack.size() + 1); |
290 | 0 | m_listFormat.setNumberSuffix(QChar::fromLatin1(detail->mark_delimiter)); |
291 | 0 | m_listFormat.setStyle(QTextListFormat::ListDecimal); |
292 | 0 | m_listFormat.setStart(detail->start); |
293 | 0 | qCDebug(lcMD, "OL xx%d level %d start %d", detail->mark_delimiter, int(m_listStack.size()) + 1, detail->start); |
294 | 0 | } break; |
295 | 0 | case MD_BLOCK_TD: { |
296 | 0 | MD_BLOCK_TD_DETAIL *detail = static_cast<MD_BLOCK_TD_DETAIL *>(det); |
297 | 0 | ++m_tableCol; |
298 | | // absolute movement (and storage of m_tableCol) shouldn't be necessary, but |
299 | | // movePosition(QTextCursor::NextCell) doesn't work |
300 | 0 | QTextTableCell cell = m_currentTable->cellAt(m_tableRowCount - 1, m_tableCol); |
301 | 0 | if (!cell.isValid()) { |
302 | 0 | qWarning("malformed table in Markdown input"); |
303 | 0 | return 1; |
304 | 0 | } |
305 | 0 | m_cursor = cell.firstCursorPosition(); |
306 | 0 | QTextBlockFormat blockFmt = m_cursor.blockFormat(); |
307 | 0 | blockFmt.setAlignment(MdAlignment(detail->align)); |
308 | 0 | m_cursor.setBlockFormat(blockFmt); |
309 | 0 | qCDebug(lcMD) << "TD; align" << detail->align << MdAlignment(detail->align) << "col" << m_tableCol; |
310 | 0 | } break; |
311 | 0 | case MD_BLOCK_TH: { |
312 | 0 | ++m_tableColumnCount; |
313 | 0 | ++m_tableCol; |
314 | 0 | if (m_currentTable->columns() < m_tableColumnCount) |
315 | 0 | m_currentTable->appendColumns(1); |
316 | 0 | auto cell = m_currentTable->cellAt(m_tableRowCount - 1, m_tableCol); |
317 | 0 | if (!cell.isValid()) { |
318 | 0 | qWarning("malformed table in Markdown input"); |
319 | 0 | return 1; |
320 | 0 | } |
321 | 0 | auto fmt = cell.format(); |
322 | 0 | fmt.setFontWeight(QFont::Bold); |
323 | 0 | cell.setFormat(fmt); |
324 | 0 | } break; |
325 | 0 | case MD_BLOCK_TR: { |
326 | 0 | ++m_tableRowCount; |
327 | 0 | m_nonEmptyTableCells.clear(); |
328 | 0 | if (m_currentTable->rows() < m_tableRowCount) |
329 | 0 | m_currentTable->appendRows(1); |
330 | 0 | m_tableCol = -1; |
331 | 0 | qCDebug(lcMD) << "TR" << m_currentTable->rows(); |
332 | 0 | } break; |
333 | 0 | case MD_BLOCK_TABLE: |
334 | 0 | m_tableColumnCount = 0; |
335 | 0 | m_tableRowCount = 0; |
336 | 0 | m_currentTable = m_cursor.insertTable(1, 1); // we don't know the dimensions yet |
337 | 0 | break; |
338 | 0 | case MD_BLOCK_HR: { |
339 | 0 | qCDebug(lcMD, "HR"); |
340 | 0 | QTextBlockFormat blockFmt; |
341 | 0 | blockFmt.setProperty(QTextFormat::BlockTrailingHorizontalRulerWidth, 1); |
342 | 0 | m_cursor.insertBlock(blockFmt, QTextCharFormat()); |
343 | 0 | } break; |
344 | 0 | default: |
345 | 0 | break; // nothing to do for now |
346 | 0 | } |
347 | 0 | return 0; // no error |
348 | 0 | } |
349 | | |
350 | | int QTextMarkdownImporter::cbLeaveBlock(int blockType, void *detail) |
351 | 0 | { |
352 | 0 | Q_UNUSED(detail); |
353 | 0 | switch (blockType) { |
354 | 0 | case MD_BLOCK_P: |
355 | 0 | m_listItem = false; |
356 | 0 | break; |
357 | 0 | case MD_BLOCK_UL: |
358 | 0 | case MD_BLOCK_OL: |
359 | 0 | if (Q_UNLIKELY(m_needsInsertList)) |
360 | 0 | m_listStack.push(m_cursor.createList(m_listFormat)); |
361 | 0 | if (Q_UNLIKELY(m_listStack.isEmpty())) { |
362 | 0 | qCWarning(lcMD, "list ended unexpectedly"); |
363 | 0 | } else { |
364 | 0 | qCDebug(lcMD, "list at level %d ended", int(m_listStack.size())); |
365 | 0 | m_listStack.pop(); |
366 | 0 | } |
367 | 0 | break; |
368 | 0 | case MD_BLOCK_TR: { |
369 | | // https://github.com/mity/md4c/issues/29 |
370 | | // MD4C doesn't tell us explicitly which cells are merged, so merge empty cells |
371 | | // with previous non-empty ones |
372 | 0 | int mergeEnd = -1; |
373 | 0 | int mergeBegin = -1; |
374 | 0 | for (int col = m_tableCol; col >= 0; --col) { |
375 | 0 | if (m_nonEmptyTableCells.contains(col)) { |
376 | 0 | if (mergeEnd >= 0 && mergeBegin >= 0) { |
377 | 0 | qCDebug(lcMD) << "merging cells" << mergeBegin << "to" << mergeEnd << "inclusive, on row" << m_currentTable->rows() - 1; |
378 | 0 | m_currentTable->mergeCells(m_currentTable->rows() - 1, mergeBegin - 1, 1, mergeEnd - mergeBegin + 2); |
379 | 0 | } |
380 | 0 | mergeEnd = -1; |
381 | 0 | mergeBegin = -1; |
382 | 0 | } else { |
383 | 0 | if (mergeEnd < 0) |
384 | 0 | mergeEnd = col; |
385 | 0 | else |
386 | 0 | mergeBegin = col; |
387 | 0 | } |
388 | 0 | } |
389 | 0 | } break; |
390 | 0 | case MD_BLOCK_QUOTE: { |
391 | 0 | qCDebug(lcMD, "QUOTE level %d ended", m_blockQuoteDepth); |
392 | 0 | --m_blockQuoteDepth; |
393 | 0 | if (!m_blockQuoteDepth) |
394 | 0 | m_blockQuoteInsideListItem = false; |
395 | 0 | m_needsInsertBlock = true; |
396 | 0 | } break; |
397 | 0 | case MD_BLOCK_TABLE: |
398 | 0 | qCDebug(lcMD) << "table ended with" << m_currentTable->columns() << "cols and" << m_currentTable->rows() << "rows"; |
399 | 0 | m_currentTable = nullptr; |
400 | 0 | m_cursor.movePosition(QTextCursor::End); |
401 | 0 | break; |
402 | 0 | case MD_BLOCK_LI: |
403 | 0 | qCDebug(lcMD, "LI at level %d ended", int(m_listStack.size())); |
404 | 0 | m_listItem = false; |
405 | 0 | break; |
406 | 0 | case MD_BLOCK_CODE: { |
407 | 0 | m_codeBlock = false; |
408 | 0 | m_blockCodeLanguage.clear(); |
409 | 0 | m_blockCodeFence = 0; |
410 | 0 | if (m_blockQuoteDepth) |
411 | 0 | qCDebug(lcMD, "CODE ended inside QUOTE %d", m_blockQuoteDepth); |
412 | 0 | else |
413 | 0 | qCDebug(lcMD, "CODE ended"); |
414 | 0 | m_needsInsertBlock = true; |
415 | 0 | } break; |
416 | 0 | case MD_BLOCK_H: |
417 | 0 | m_cursor.setCharFormat(QTextCharFormat()); |
418 | 0 | break; |
419 | 0 | default: |
420 | 0 | break; |
421 | 0 | } |
422 | 0 | return 0; // no error |
423 | 0 | } |
424 | | |
425 | | int QTextMarkdownImporter::cbEnterSpan(int spanType, void *det) |
426 | 0 | { |
427 | 0 | QTextCharFormat charFmt; |
428 | 0 | if (!m_spanFormatStack.isEmpty()) |
429 | 0 | charFmt = m_spanFormatStack.top(); |
430 | 0 | switch (spanType) { |
431 | 0 | case MD_SPAN_EM: |
432 | 0 | charFmt.setFontItalic(true); |
433 | 0 | break; |
434 | 0 | case MD_SPAN_STRONG: |
435 | 0 | charFmt.setFontWeight(QFont::Bold); |
436 | 0 | break; |
437 | 0 | case MD_SPAN_U: |
438 | 0 | charFmt.setFontUnderline(true); |
439 | 0 | break; |
440 | 0 | case MD_SPAN_A: { |
441 | 0 | MD_SPAN_A_DETAIL *detail = static_cast<MD_SPAN_A_DETAIL *>(det); |
442 | 0 | QString url = QString::fromUtf8(detail->href.text, int(detail->href.size)); |
443 | 0 | QString title = QString::fromUtf8(detail->title.text, int(detail->title.size)); |
444 | 0 | charFmt.setAnchor(true); |
445 | 0 | charFmt.setAnchorHref(url); |
446 | 0 | if (!title.isEmpty()) |
447 | 0 | charFmt.setToolTip(title); |
448 | 0 | charFmt.setForeground(m_palette.link()); |
449 | 0 | qCDebug(lcMD) << "anchor" << url << title; |
450 | 0 | } break; |
451 | 0 | case MD_SPAN_IMG: { |
452 | 0 | m_imageSpan = true; |
453 | 0 | m_imageFormat = QTextImageFormat(); |
454 | 0 | MD_SPAN_IMG_DETAIL *detail = static_cast<MD_SPAN_IMG_DETAIL *>(det); |
455 | 0 | m_imageFormat.setName(QString::fromUtf8(detail->src.text, int(detail->src.size))); |
456 | 0 | const QVariant tooltip = QString::fromUtf8(detail->title.text, int(detail->title.size)); |
457 | 0 | m_imageFormat.setProperty(QTextFormat::ImageTitle, tooltip); |
458 | 0 | m_imageFormat.setProperty(QTextFormat::TextToolTip, tooltip); |
459 | 0 | break; |
460 | 0 | } |
461 | 0 | case MD_SPAN_CODE: |
462 | 0 | charFmt.setFont(m_monoFont); |
463 | | // Let font size be inherited from the document's default font |
464 | 0 | charFmt.clearProperty(QTextFormat::FontPointSize); |
465 | 0 | charFmt.clearProperty(QTextFormat::FontPixelSize); |
466 | 0 | charFmt.setFontFixedPitch(true); |
467 | 0 | break; |
468 | 0 | case MD_SPAN_DEL: |
469 | 0 | charFmt.setFontStrikeOut(true); |
470 | 0 | break; |
471 | 0 | } |
472 | 0 | m_spanFormatStack.push(charFmt); |
473 | 0 | qCDebug(lcMD) << spanType << "setCharFormat" << charFmt.font().family() |
474 | 0 | << charFmt.fontWeight() << (charFmt.fontItalic() ? "italic" : "") |
475 | 0 | << charFmt.foreground().color().name(); |
476 | 0 | m_cursor.setCharFormat(charFmt); |
477 | 0 | return 0; // no error |
478 | 0 | } |
479 | | |
480 | | int QTextMarkdownImporter::cbLeaveSpan(int spanType, void *detail) |
481 | 0 | { |
482 | 0 | Q_UNUSED(detail); |
483 | 0 | QTextCharFormat charFmt; |
484 | 0 | if (!m_spanFormatStack.isEmpty()) { |
485 | 0 | m_spanFormatStack.pop(); |
486 | 0 | if (!m_spanFormatStack.isEmpty()) |
487 | 0 | charFmt = m_spanFormatStack.top(); |
488 | 0 | } |
489 | 0 | m_cursor.setCharFormat(charFmt); |
490 | 0 | qCDebug(lcMD) << spanType << "setCharFormat" << charFmt.font().family() |
491 | 0 | << charFmt.fontWeight() << (charFmt.fontItalic() ? "italic" : "") |
492 | 0 | << charFmt.foreground().color().name(); |
493 | 0 | if (spanType == int(MD_SPAN_IMG)) |
494 | 0 | m_imageSpan = false; |
495 | 0 | return 0; // no error |
496 | 0 | } |
497 | | |
498 | | int QTextMarkdownImporter::cbText(int textType, const char *text, unsigned size) |
499 | 0 | { |
500 | 0 | if (m_needsInsertBlock) |
501 | 0 | insertBlock(); |
502 | 0 | #if QT_CONFIG(regularexpression) |
503 | 0 | static const QRegularExpression openingBracket(QStringLiteral("<[a-zA-Z]")); |
504 | 0 | static const QRegularExpression closingBracket(QStringLiteral("(/>|</)")); |
505 | 0 | #endif |
506 | 0 | QString s = QString::fromUtf8(text, int(size)); |
507 | |
|
508 | 0 | switch (textType) { |
509 | 0 | case MD_TEXT_NORMAL: |
510 | 0 | #if QT_CONFIG(regularexpression) |
511 | 0 | if (m_htmlTagDepth) { |
512 | 0 | m_htmlAccumulator += s; |
513 | 0 | s = QString(); |
514 | 0 | } |
515 | 0 | #endif |
516 | 0 | break; |
517 | 0 | case MD_TEXT_NULLCHAR: |
518 | 0 | s = QString(QChar(u'\xFFFD')); // CommonMark-required replacement for null |
519 | 0 | break; |
520 | 0 | case MD_TEXT_BR: |
521 | 0 | s = QString(qtmi_Newline); |
522 | 0 | break; |
523 | 0 | case MD_TEXT_SOFTBR: |
524 | 0 | s = QString(qtmi_Space); |
525 | 0 | break; |
526 | 0 | case MD_TEXT_CODE: |
527 | | // We'll see MD_SPAN_CODE too, which will set the char format, and that's enough. |
528 | 0 | break; |
529 | 0 | #if QT_CONFIG(texthtmlparser) |
530 | 0 | case MD_TEXT_ENTITY: |
531 | 0 | if (m_htmlTagDepth) |
532 | 0 | m_htmlAccumulator += s; |
533 | 0 | else |
534 | 0 | m_cursor.insertHtml(s); |
535 | 0 | s = QString(); |
536 | 0 | break; |
537 | 0 | #endif |
538 | 0 | case MD_TEXT_HTML: |
539 | | // count how many tags are opened and how many are closed |
540 | 0 | #if QT_CONFIG(regularexpression) && QT_CONFIG(texthtmlparser) |
541 | 0 | { |
542 | 0 | QRegularExpressionMatchIterator i = openingBracket.globalMatch(s); |
543 | 0 | while (i.hasNext()) { |
544 | 0 | ++m_htmlTagDepth; |
545 | 0 | i.next(); |
546 | 0 | } |
547 | 0 | i = closingBracket.globalMatch(s); |
548 | 0 | while (i.hasNext()) { |
549 | 0 | --m_htmlTagDepth; |
550 | 0 | i.next(); |
551 | 0 | } |
552 | 0 | } |
553 | 0 | m_htmlAccumulator += s; |
554 | 0 | if (!m_htmlTagDepth) { // all open tags are now closed |
555 | 0 | qCDebug(lcMD) << "HTML" << m_htmlAccumulator; |
556 | 0 | m_cursor.insertHtml(m_htmlAccumulator); |
557 | 0 | if (m_spanFormatStack.isEmpty()) |
558 | 0 | m_cursor.setCharFormat(QTextCharFormat()); |
559 | 0 | else |
560 | 0 | m_cursor.setCharFormat(m_spanFormatStack.top()); |
561 | 0 | m_htmlAccumulator = QString(); |
562 | 0 | } |
563 | 0 | #endif |
564 | 0 | s = QString(); |
565 | 0 | break; |
566 | 0 | } |
567 | | |
568 | 0 | switch (m_blockType) { |
569 | 0 | case MD_BLOCK_TD: |
570 | 0 | m_nonEmptyTableCells.append(m_tableCol); |
571 | 0 | break; |
572 | 0 | case MD_BLOCK_CODE: |
573 | 0 | if (s == qtmi_Newline) { |
574 | | // defer a blank line until we see something else in the code block, |
575 | | // to avoid ending every code block with a gratuitous blank line |
576 | 0 | m_needsInsertBlock = true; |
577 | 0 | s = QString(); |
578 | 0 | } |
579 | 0 | break; |
580 | 0 | default: |
581 | 0 | break; |
582 | 0 | } |
583 | | |
584 | 0 | if (m_imageSpan) { |
585 | | // TODO we don't yet support alt text with formatting, because of the cases where m_cursor |
586 | | // already inserted the text above. Rather need to accumulate it in case we need it here. |
587 | 0 | m_imageFormat.setProperty(QTextFormat::ImageAltText, s); |
588 | 0 | qCDebug(lcMD) << "image" << m_imageFormat.name() |
589 | 0 | << "title" << m_imageFormat.stringProperty(QTextFormat::ImageTitle) |
590 | 0 | << "alt" << s << "relative to" << m_cursor.document()->baseUrl(); |
591 | 0 | m_cursor.insertImage(m_imageFormat); |
592 | 0 | return 0; // no error |
593 | 0 | } |
594 | | |
595 | 0 | if (!s.isEmpty()) |
596 | 0 | m_cursor.insertText(s); |
597 | 0 | if (m_cursor.currentList()) { |
598 | | // The list item will indent the list item's text, so we don't need indentation on the block. |
599 | 0 | QTextBlockFormat bfmt = m_cursor.blockFormat(); |
600 | 0 | bfmt.setIndent(0); |
601 | 0 | m_cursor.setBlockFormat(bfmt); |
602 | 0 | } |
603 | 0 | if (lcMD().isEnabled(QtDebugMsg)) { |
604 | 0 | QTextBlockFormat bfmt = m_cursor.blockFormat(); |
605 | 0 | QString debugInfo; |
606 | 0 | if (m_cursor.currentList()) |
607 | 0 | debugInfo = "in list at depth "_L1 + QString::number(m_cursor.currentList()->format().indent()); |
608 | 0 | if (bfmt.hasProperty(QTextFormat::BlockQuoteLevel)) |
609 | 0 | debugInfo += "in blockquote at depth "_L1 + |
610 | 0 | QString::number(bfmt.intProperty(QTextFormat::BlockQuoteLevel)); |
611 | 0 | if (bfmt.hasProperty(QTextFormat::BlockCodeLanguage)) |
612 | 0 | debugInfo += "in a code block"_L1; |
613 | 0 | qCDebug(lcMD) << textType << "in block" << m_blockType << s << qPrintable(debugInfo) |
614 | 0 | << "bindent" << bfmt.indent() << "tindent" << bfmt.textIndent() |
615 | 0 | << "margins" << bfmt.leftMargin() << bfmt.topMargin() << bfmt.bottomMargin() << bfmt.rightMargin(); |
616 | 0 | } |
617 | 0 | return 0; // no error |
618 | 0 | } |
619 | | |
620 | | /*! |
621 | | Insert a new block based on stored state. |
622 | | |
623 | | m_cursor cannot store the state for the _next_ block ahead of time, because |
624 | | m_cursor.setBlockFormat() controls the format of the block that the cursor |
625 | | is already in; so cbLeaveBlock() cannot call setBlockFormat() without |
626 | | altering the block that was just added. Therefore cbLeaveBlock() and the |
627 | | following cbEnterBlock() set variables to remember what formatting should |
628 | | come next, and insertBlock() is called just before the actual text |
629 | | insertion, to create a new block with the right formatting. |
630 | | */ |
631 | | void QTextMarkdownImporter::insertBlock() |
632 | 0 | { |
633 | 0 | QTextCharFormat charFormat; |
634 | 0 | if (!m_spanFormatStack.isEmpty()) |
635 | 0 | charFormat = m_spanFormatStack.top(); |
636 | 0 | QTextBlockFormat blockFormat; |
637 | 0 | if (!m_listStack.isEmpty() && !m_needsInsertList && m_listItem) { |
638 | 0 | QTextList *list = m_listStack.top(); |
639 | 0 | if (list) |
640 | 0 | blockFormat = list->item(list->count() - 1).blockFormat(); |
641 | 0 | else |
642 | 0 | qWarning() << "attempted to insert into a list that no longer exists"; |
643 | 0 | } |
644 | 0 | if (m_blockQuoteDepth) { |
645 | 0 | blockFormat.setProperty(QTextFormat::BlockQuoteLevel, m_blockQuoteDepth); |
646 | 0 | blockFormat.setLeftMargin(qtmi_BlockQuoteIndent * m_blockQuoteDepth); |
647 | 0 | blockFormat.setRightMargin(qtmi_BlockQuoteIndent); |
648 | | // Remember which way it's nested: a QTextBlock cannot contain another block, |
649 | | // but we need to distinguish "- > quoted" from "> - quoted". |
650 | 0 | if (m_blockQuoteInsideListItem) |
651 | 0 | blockFormat.setProperty(QTextFormat::BlockQuoteInsideListItem, true); |
652 | 0 | } |
653 | 0 | if (m_codeBlock) { |
654 | 0 | blockFormat.setProperty(QTextFormat::BlockCodeLanguage, m_blockCodeLanguage); |
655 | 0 | if (m_blockCodeFence) { |
656 | 0 | blockFormat.setNonBreakableLines(true); |
657 | 0 | blockFormat.setProperty(QTextFormat::BlockCodeFence, QString(QLatin1Char(m_blockCodeFence))); |
658 | 0 | } |
659 | 0 | charFormat.setFont(m_monoFont); |
660 | | // Let font size be inherited from the document's default font |
661 | 0 | charFormat.clearProperty(QTextFormat::FontPointSize); |
662 | 0 | charFormat.clearProperty(QTextFormat::FontPixelSize); |
663 | 0 | } else { |
664 | 0 | blockFormat.clearProperty(QTextFormat::BlockCodeLanguage); |
665 | 0 | blockFormat.clearProperty(QTextFormat::BlockCodeFence); |
666 | 0 | blockFormat.setNonBreakableLines(false); |
667 | 0 | blockFormat.setTopMargin(m_paragraphMargin); |
668 | 0 | blockFormat.setBottomMargin(m_paragraphMargin); |
669 | 0 | } |
670 | 0 | if (m_markerType == QTextBlockFormat::MarkerType::NoMarker) |
671 | 0 | blockFormat.clearProperty(QTextFormat::BlockMarker); |
672 | 0 | else |
673 | 0 | blockFormat.setMarker(m_markerType); |
674 | 0 | if (!m_listStack.isEmpty()) |
675 | 0 | blockFormat.setIndent(m_listStack.size()); |
676 | 0 | if (m_cursor.document()->isEmpty()) { |
677 | 0 | m_cursor.setBlockFormat(blockFormat); |
678 | 0 | m_cursor.setCharFormat(charFormat); |
679 | 0 | } else if (m_listItem) { |
680 | 0 | m_cursor.insertBlock(blockFormat, QTextCharFormat()); |
681 | 0 | m_cursor.setCharFormat(charFormat); |
682 | 0 | } else { |
683 | 0 | m_cursor.insertBlock(blockFormat, charFormat); |
684 | 0 | } |
685 | 0 | if (m_needsInsertList) { |
686 | 0 | m_listStack.push(m_cursor.createList(m_listFormat)); |
687 | 0 | } else if (!m_listStack.isEmpty() && m_listItem && m_listStack.top()) { |
688 | 0 | m_listStack.top()->add(m_cursor.block()); |
689 | 0 | } |
690 | 0 | m_needsInsertList = false; |
691 | 0 | m_needsInsertBlock = false; |
692 | | // Any further blocks in a list item (e.g. a fenced code block) are |
693 | | // continuations, even if it's a tight list and MD4C doesn't emit MD_BLOCK_P. |
694 | 0 | m_listItem = false; |
695 | 0 | } |
696 | | |
697 | | QT_END_NAMESPACE |