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/qtextlist.h>
58#include <QtCore/qstack.h>
59
60QT_BEGIN_NAMESPACE
61
62class QTextCursor;
63class QTextDocument;
64class QTextTable;
65
66class Q_GUI_EXPORT QTextMarkdownImporter
67{
68public:
69 enum Feature {
70 // Must be kept in sync with MD_FLAG_* in md4c.h
71 FeatureCollapseWhitespace = 0x0001, // MD_FLAG_COLLAPSEWHITESPACE
72 FeaturePermissiveATXHeaders = 0x0002, // MD_FLAG_PERMISSIVEATXHEADERS
73 FeaturePermissiveURLAutoLinks = 0x0004, // MD_FLAG_PERMISSIVEURLAUTOLINKS
74 FeaturePermissiveMailAutoLinks = 0x0008, // MD_FLAG_PERMISSIVEEMAILAUTOLINKS
75 FeatureNoIndentedCodeBlocks = 0x0010, // MD_FLAG_NOINDENTEDCODEBLOCKS
76 FeatureNoHTMLBlocks = 0x0020, // MD_FLAG_NOHTMLBLOCKS
77 FeatureNoHTMLSpans = 0x0040, // MD_FLAG_NOHTMLSPANS
78 FeatureTables = 0x0100, // MD_FLAG_TABLES
79 FeatureStrikeThrough = 0x0200, // MD_FLAG_STRIKETHROUGH
80 FeaturePermissiveWWWAutoLinks = 0x0400, // MD_FLAG_PERMISSIVEWWWAUTOLINKS
81 FeatureTasklists = 0x0800, // MD_FLAG_TASKLISTS
82 // composite flags
83 FeaturePermissiveAutoLinks = FeaturePermissiveMailAutoLinks | FeaturePermissiveURLAutoLinks | FeaturePermissiveWWWAutoLinks, // MD_FLAG_PERMISSIVEAUTOLINKS
84 FeatureNoHTML = FeatureNoHTMLBlocks | FeatureNoHTMLSpans, // MD_FLAG_NOHTML
85 DialectCommonMark = 0, // MD_DIALECT_COMMONMARK
86 DialectGitHub = FeaturePermissiveAutoLinks | FeatureTables | FeatureStrikeThrough | FeatureTasklists // MD_DIALECT_GITHUB
87 };
88 Q_DECLARE_FLAGS(Features, Feature)
89
90 QTextMarkdownImporter(Features features);
91
92 void import(QTextDocument *doc, const QString &markdown);
93
94public:
95 // MD4C callbacks
96 int cbEnterBlock(int blockType, void* detail);
97 int cbLeaveBlock(int blockType, void* detail);
98 int cbEnterSpan(int spanType, void* detail);
99 int cbLeaveSpan(int spanType, void* detail);
100 int cbText(int textType, const char* text, unsigned size);
101
102private:
103 void insertBlock();
104
105private:
106 QTextDocument *m_doc = nullptr;
107 QTextCursor *m_cursor = nullptr;
108 QTextTable *m_currentTable = nullptr; // because m_cursor->currentTable() doesn't work
109#if QT_CONFIG(regularexpression)
110 QString m_htmlAccumulator;
111#endif
112 QString m_blockCodeLanguage;
113 QVector<int> m_nonEmptyTableCells; // in the current row
114 QStack<QTextList *> m_listStack;
115 QStack<QTextCharFormat> m_spanFormatStack;
116 QFont m_monoFont;
117 QPalette m_palette;
118#if QT_CONFIG(regularexpression)
119 int m_htmlTagDepth = 0;
120#endif
121 int m_blockQuoteDepth = 0;
122 int m_tableColumnCount = 0;
123 int m_tableRowCount = 0;
124 int m_tableCol = -1; // because relative cell movements (e.g. m_cursor->movePosition(QTextCursor::NextCell)) don't work
125 int m_paragraphMargin = 0;
126 int m_blockType = 0;
127 char m_blockCodeFence = 0;
128 Features m_features;
129 QTextImageFormat m_imageFormat;
130 QTextListFormat m_listFormat;
131 QTextBlockFormat::MarkerType m_markerType = QTextBlockFormat::NoMarker;
132 bool m_needsInsertBlock = false;
133 bool m_needsInsertList = false;
134 bool m_listItem = false; // true from the beginning of LI to the end of the first P
135 bool m_codeBlock = false;
136 bool m_imageSpan = false;
137};
138
139Q_DECLARE_OPERATORS_FOR_FLAGS(QTextMarkdownImporter::Features)
140
141QT_END_NAMESPACE
142
143#endif // QTEXTMARKDOWNIMPORTER_H
144