| 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 | |
| 4 | #ifndef QTEXTMARKDOWNIMPORTER_H |
| 5 | #define QTEXTMARKDOWNIMPORTER_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtGui/qfont.h> |
| 19 | #include <QtGui/qtguiglobal.h> |
| 20 | #include <QtGui/qpalette.h> |
| 21 | #include <QtGui/qtextdocument.h> |
| 22 | #include <QtGui/qtextlist.h> |
| 23 | #include <QtCore/qpointer.h> |
| 24 | #include <QtCore/qstack.h> |
| 25 | #include <QtCore/private/qglobal_p.h> |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | class QTextCursor; |
| 30 | class QTextDocument; |
| 31 | class QTextTable; |
| 32 | |
| 33 | class Q_GUI_EXPORT QTextMarkdownImporter |
| 34 | { |
| 35 | public: |
| 36 | enum Feature { |
| 37 | FeatureCollapseWhitespace = 0x0001, |
| 38 | = 0x0002, |
| 39 | FeaturePermissiveURLAutoLinks = 0x0004, |
| 40 | FeaturePermissiveMailAutoLinks = 0x0008, |
| 41 | FeatureNoIndentedCodeBlocks = 0x0010, |
| 42 | FeatureNoHTMLBlocks = 0x0020, |
| 43 | FeatureNoHTMLSpans = 0x0040, |
| 44 | FeatureTables = 0x0100, |
| 45 | FeatureStrikeThrough = 0x0200, |
| 46 | FeaturePermissiveWWWAutoLinks = 0x0400, |
| 47 | FeatureTasklists = 0x0800, |
| 48 | FeatureUnderline = 0x4000, |
| 49 | FeatureFrontMatter = 0x100000, // Qt feature, not yet in MD4C |
| 50 | // composite flags |
| 51 | FeaturePermissiveAutoLinks = FeaturePermissiveMailAutoLinks |
| 52 | | FeaturePermissiveURLAutoLinks | FeaturePermissiveWWWAutoLinks, |
| 53 | FeatureNoHTML = QTextDocument::MarkdownNoHTML, |
| 54 | DialectCommonMark = QTextDocument::MarkdownDialectCommonMark, |
| 55 | DialectGitHub = QTextDocument::MarkdownDialectGitHub |
| 56 | }; |
| 57 | Q_DECLARE_FLAGS(Features, Feature) |
| 58 | |
| 59 | QTextMarkdownImporter(QTextDocument *doc, Features features); |
| 60 | QTextMarkdownImporter(QTextDocument *doc, QTextDocument::MarkdownFeatures features); |
| 61 | |
| 62 | void import(const QString &markdown); |
| 63 | |
| 64 | public: |
| 65 | // MD4C callbacks |
| 66 | int cbEnterBlock(int blockType, void* detail); |
| 67 | int cbLeaveBlock(int blockType, void* detail); |
| 68 | int cbEnterSpan(int spanType, void* detail); |
| 69 | int cbLeaveSpan(int spanType, void* detail); |
| 70 | int cbText(int textType, const char* text, unsigned size); |
| 71 | |
| 72 | private: |
| 73 | void insertBlock(); |
| 74 | |
| 75 | private: |
| 76 | QTextCursor m_cursor; |
| 77 | QTextTable *m_currentTable = nullptr; // because m_cursor->currentTable() doesn't work |
| 78 | #if QT_CONFIG(regularexpression) |
| 79 | QString m_htmlAccumulator; |
| 80 | #endif |
| 81 | QString m_blockCodeLanguage; |
| 82 | QList<int> m_nonEmptyTableCells; // in the current row |
| 83 | QStack<QPointer<QTextList>> m_listStack; |
| 84 | QStack<QTextCharFormat> m_spanFormatStack; |
| 85 | QFont m_monoFont; |
| 86 | QPalette m_palette; |
| 87 | #if QT_CONFIG(regularexpression) |
| 88 | int m_htmlTagDepth = 0; |
| 89 | #endif |
| 90 | int m_blockQuoteDepth = 0; |
| 91 | int m_tableColumnCount = 0; |
| 92 | int m_tableRowCount = 0; |
| 93 | int m_tableCol = -1; // because relative cell movements (e.g. m_cursor->movePosition(QTextCursor::NextCell)) don't work |
| 94 | int m_paragraphMargin = 0; |
| 95 | int m_blockType = 0; |
| 96 | char m_blockCodeFence = 0; |
| 97 | Features m_features; |
| 98 | QTextImageFormat m_imageFormat; |
| 99 | QTextListFormat m_listFormat; |
| 100 | QTextBlockFormat::MarkerType m_markerType = QTextBlockFormat::MarkerType::NoMarker; |
| 101 | bool m_needsInsertBlock = false; |
| 102 | bool m_needsInsertList = false; |
| 103 | bool m_listItem = false; // true from the beginning of LI to the end of the first P |
| 104 | bool m_codeBlock = false; |
| 105 | bool m_imageSpan = false; |
| 106 | }; |
| 107 | |
| 108 | Q_DECLARE_OPERATORS_FOR_FLAGS(QTextMarkdownImporter::Features) |
| 109 | |
| 110 | QT_END_NAMESPACE |
| 111 | |
| 112 | #endif // QTEXTMARKDOWNIMPORTER_H |
| 113 | |