summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2019-05-19 13:06:05 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2019-05-30 16:37:23 +0000
commit0eb26c144346dcb12008e5b3e9441073f09668a8 (patch)
tree4134706a0eba2a22312ef2eb8350a28231c5c936 /src
parenta5b373e0db45a28c133bf413e00f15271aa20200 (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')
-rw-r--r--src/gui/text/qtextdocument.cpp1
-rw-r--r--src/gui/text/qtextdocument.h1
-rw-r--r--src/widgets/widgets/qtextbrowser.cpp20
3 files changed, 21 insertions, 1 deletions
diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp
index 3266819bf3..c800cea3f6 100644
--- a/src/gui/text/qtextdocument.cpp
+++ b/src/gui/text/qtextdocument.cpp
@@ -2076,6 +2076,7 @@ void QTextDocument::print(QPagedPaintDevice *printer) const
The icon needs to be converted to one of the supported types first,
for example using QIcon::pixmap.
\value StyleSheetResource The resource contains CSS.
+ \value MarkdownResource The resource contains Markdown.
\value UserResource The first available value for user defined
resource types.
diff --git a/src/gui/text/qtextdocument.h b/src/gui/text/qtextdocument.h
index 31c06976a5..edb6bd9310 100644
--- a/src/gui/text/qtextdocument.h
+++ b/src/gui/text/qtextdocument.h
@@ -228,6 +228,7 @@ public:
HtmlResource = 1,
ImageResource = 2,
StyleSheetResource = 3,
+ MarkdownResource = 4,
UserResource = 100
};
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)