diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-05-19 13:06:05 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-05-30 16:37:23 +0000 |
commit | 0eb26c144346dcb12008e5b3e9441073f09668a8 (patch) | |
tree | 4134706a0eba2a22312ef2eb8350a28231c5c936 /src/widgets | |
parent | a5b373e0db45a28c133bf413e00f15271aa20200 (diff) |
QTextBrowser: detect and load markdown rather than assuming HTML
So we add the QTextDocument::ResourceType::MarkdownResource enum value to indicate
the type. QMimeDatabase is generally unable to detect markdown by "magic", so we
need to use the common file extensions to detect it (the same extensions as declared
in the mime database XML).
Change-Id: Ib71f03abd535c17e5a8c99bd92d0a6062e972837
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/widgets')
-rw-r--r-- | src/widgets/widgets/qtextbrowser.cpp | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/src/widgets/widgets/qtextbrowser.cpp b/src/widgets/widgets/qtextbrowser.cpp index 91c5f62246..8e06efc75e 100644 --- a/src/widgets/widgets/qtextbrowser.cpp +++ b/src/widgets/widgets/qtextbrowser.cpp @@ -57,9 +57,12 @@ #endif #include <qtextobject.h> #include <qdesktopservices.h> +#include <qmimedatabase.h> QT_BEGIN_NAMESPACE +Q_LOGGING_CATEGORY(lcBrowser, "qt.text.browser") + class QTextBrowserPrivate : public QTextEditPrivate { Q_DECLARE_PUBLIC(QTextBrowser) @@ -288,10 +291,18 @@ void QTextBrowserPrivate::setSource(const QUrl &url) currentUrlWithoutFragment.setFragment(QString()); QUrl newUrlWithoutFragment = currentURL.resolved(url); newUrlWithoutFragment.setFragment(QString()); + QTextDocument::ResourceType type = QTextDocument::HtmlResource; + QString fileName = url.fileName(); +#if QT_CONFIG(textmarkdownreader) + if (fileName.endsWith(QLatin1String(".md")) || + fileName.endsWith(QLatin1String(".mkd")) || + fileName.endsWith(QLatin1String(".markdown"))) + type = QTextDocument::MarkdownResource; +#endif if (url.isValid() && (newUrlWithoutFragment != currentUrlWithoutFragment || forceLoadOnSourceChange)) { - QVariant data = q->loadResource(QTextDocument::HtmlResource, resolveUrl(url)); + QVariant data = q->loadResource(type, resolveUrl(url)); if (data.type() == QVariant::String) { txt = data.toString(); } else if (data.type() == QVariant::ByteArray) { @@ -334,6 +345,12 @@ void QTextBrowserPrivate::setSource(const QUrl &url) if (!baseUrl.path().isEmpty()) q->document()->setBaseUrl(baseUrl); q->document()->setMetaInformation(QTextDocument::DocumentUrl, currentURL.toString()); + qCDebug(lcBrowser) << "loading" << currentURL << "base" << q->document()->baseUrl() << "type" << type << txt.size() << "chars"; +#if QT_CONFIG(textmarkdownreader) + if (type == QTextDocument::MarkdownResource) + q->QTextEdit::setMarkdown(txt); + else +#endif #ifndef QT_NO_TEXTHTMLPARSER q->QTextEdit::setHtml(txt); #else @@ -1092,6 +1109,7 @@ void QTextBrowser::paintEvent(QPaintEvent *e) \row \li QTextDocument::HtmlResource \li QString or QByteArray \row \li QTextDocument::ImageResource \li QImage, QPixmap or QByteArray \row \li QTextDocument::StyleSheetResource \li QString or QByteArray + \row \li QTextDocument::MarkdownResource \li QString or QByteArray \endtable */ QVariant QTextBrowser::loadResource(int /*type*/, const QUrl &name) |