| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2019 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtGui module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QTEXTMARKDOWNIMPORTER_H |
| 41 | #define QTEXTMARKDOWNIMPORTER_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <QtGui/qfont.h> |
| 55 | #include <QtGui/qtguiglobal.h> |
| 56 | #include <QtGui/qpalette.h> |
| 57 | #include <QtGui/qtextdocument.h> |
| 58 | #include <QtGui/qtextlist.h> |
| 59 | #include <QtCore/qpointer.h> |
| 60 | #include <QtCore/qstack.h> |
| 61 | |
| 62 | QT_BEGIN_NAMESPACE |
| 63 | |
| 64 | class QTextCursor; |
| 65 | class QTextDocument; |
| 66 | class QTextTable; |
| 67 | |
| 68 | class Q_GUI_EXPORT QTextMarkdownImporter |
| 69 | { |
| 70 | public: |
| 71 | enum Feature { |
| 72 | FeatureCollapseWhitespace = 0x0001, |
| 73 | = 0x0002, |
| 74 | FeaturePermissiveURLAutoLinks = 0x0004, |
| 75 | FeaturePermissiveMailAutoLinks = 0x0008, |
| 76 | FeatureNoIndentedCodeBlocks = 0x0010, |
| 77 | FeatureNoHTMLBlocks = 0x0020, |
| 78 | FeatureNoHTMLSpans = 0x0040, |
| 79 | FeatureTables = 0x0100, |
| 80 | FeatureStrikeThrough = 0x0200, |
| 81 | FeaturePermissiveWWWAutoLinks = 0x0400, |
| 82 | FeatureTasklists = 0x0800, |
| 83 | // composite flags |
| 84 | FeaturePermissiveAutoLinks = FeaturePermissiveMailAutoLinks |
| 85 | | FeaturePermissiveURLAutoLinks | FeaturePermissiveWWWAutoLinks, |
| 86 | FeatureNoHTML = QTextDocument::MarkdownNoHTML, |
| 87 | DialectCommonMark = QTextDocument::MarkdownDialectCommonMark, |
| 88 | DialectGitHub = QTextDocument::MarkdownDialectGitHub |
| 89 | }; |
| 90 | Q_DECLARE_FLAGS(Features, Feature) |
| 91 | |
| 92 | QTextMarkdownImporter(Features features); |
| 93 | QTextMarkdownImporter(QTextDocument::MarkdownFeatures features); |
| 94 | |
| 95 | void import(QTextDocument *doc, const QString &markdown); |
| 96 | |
| 97 | public: |
| 98 | // MD4C callbacks |
| 99 | int cbEnterBlock(int blockType, void* detail); |
| 100 | int cbLeaveBlock(int blockType, void* detail); |
| 101 | int cbEnterSpan(int spanType, void* detail); |
| 102 | int cbLeaveSpan(int spanType, void* detail); |
| 103 | int cbText(int textType, const char* text, unsigned size); |
| 104 | |
| 105 | private: |
| 106 | void insertBlock(); |
| 107 | |
| 108 | private: |
| 109 | QTextDocument *m_doc = nullptr; |
| 110 | QTextCursor *m_cursor = nullptr; |
| 111 | QTextTable *m_currentTable = nullptr; // because m_cursor->currentTable() doesn't work |
| 112 | #if QT_CONFIG(regularexpression) |
| 113 | QString m_htmlAccumulator; |
| 114 | #endif |
| 115 | QString m_blockCodeLanguage; |
| 116 | QList<int> m_nonEmptyTableCells; // in the current row |
| 117 | QStack<QPointer<QTextList>> m_listStack; |
| 118 | QStack<QTextCharFormat> m_spanFormatStack; |
| 119 | QFont m_monoFont; |
| 120 | QPalette m_palette; |
| 121 | #if QT_CONFIG(regularexpression) |
| 122 | int m_htmlTagDepth = 0; |
| 123 | #endif |
| 124 | int m_blockQuoteDepth = 0; |
| 125 | int m_tableColumnCount = 0; |
| 126 | int m_tableRowCount = 0; |
| 127 | int m_tableCol = -1; // because relative cell movements (e.g. m_cursor->movePosition(QTextCursor::NextCell)) don't work |
| 128 | int m_paragraphMargin = 0; |
| 129 | int m_blockType = 0; |
| 130 | char m_blockCodeFence = 0; |
| 131 | Features m_features; |
| 132 | QTextImageFormat m_imageFormat; |
| 133 | QTextListFormat m_listFormat; |
| 134 | QTextBlockFormat::MarkerType m_markerType = QTextBlockFormat::MarkerType::NoMarker; |
| 135 | bool m_needsInsertBlock = false; |
| 136 | bool m_needsInsertList = false; |
| 137 | bool m_listItem = false; // true from the beginning of LI to the end of the first P |
| 138 | bool m_codeBlock = false; |
| 139 | bool m_imageSpan = false; |
| 140 | }; |
| 141 | |
| 142 | Q_DECLARE_OPERATORS_FOR_FLAGS(QTextMarkdownImporter::Features) |
| 143 | |
| 144 | QT_END_NAMESPACE |
| 145 | |
| 146 | #endif // QTEXTMARKDOWNIMPORTER_H |
| 147 | |