Markdown importer: use Unicode decoding
Given we feed UTF-8 data into the importer, it must be able to cope with Unicode. Build md4c with UTF-8 support, advertise it at usage site, and change a couple of broken decodings. Driveby: the textedit example used the wrong codec to decode a Markdown file. While the Markdown spec doesn't deal with encodings, using the default one for HTML is certainly wrong. Port the loading of both markdown and plaintext to UTF-8, as that what _saving_ via QTextDocumentWriter would use by default. Change-Id: I51c6214cfe45ebfc5a67a7366f7866a5328366ec Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
parent
77e8023eaf
commit
c29fac453f
@ -419,18 +419,18 @@ bool TextEdit::load(const QString &f)
|
|||||||
QByteArray data = file.readAll();
|
QByteArray data = file.readAll();
|
||||||
QTextCodec *codec = Qt::codecForHtml(data);
|
QTextCodec *codec = Qt::codecForHtml(data);
|
||||||
QString str = codec->toUnicode(data);
|
QString str = codec->toUnicode(data);
|
||||||
QUrl baseUrl = (f.front() == QLatin1Char(':') ? QUrl(f) : QUrl::fromLocalFile(f)).adjusted(QUrl::RemoveFilename);
|
|
||||||
textEdit->document()->setBaseUrl(baseUrl);
|
|
||||||
if (Qt::mightBeRichText(str)) {
|
if (Qt::mightBeRichText(str)) {
|
||||||
|
QUrl baseUrl = (f.front() == QLatin1Char(':') ? QUrl(f) : QUrl::fromLocalFile(f)).adjusted(QUrl::RemoveFilename);
|
||||||
|
textEdit->document()->setBaseUrl(baseUrl);
|
||||||
textEdit->setHtml(str);
|
textEdit->setHtml(str);
|
||||||
} else {
|
} else {
|
||||||
#if QT_CONFIG(textmarkdownreader)
|
#if QT_CONFIG(textmarkdownreader)
|
||||||
QMimeDatabase db;
|
QMimeDatabase db;
|
||||||
if (db.mimeTypeForFileNameAndData(f, data).name() == QLatin1String("text/markdown"))
|
if (db.mimeTypeForFileNameAndData(f, data).name() == QLatin1String("text/markdown"))
|
||||||
textEdit->setMarkdown(str);
|
textEdit->setMarkdown(QString::fromUtf8(data));
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
textEdit->setPlainText(QString::fromLocal8Bit(data));
|
textEdit->setPlainText(QString::fromUtf8(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
setCurrentFileName(f);
|
setCurrentFileName(f);
|
||||||
|
1
src/3rdparty/md4c.pri
vendored
1
src/3rdparty/md4c.pri
vendored
@ -1,3 +1,4 @@
|
|||||||
INCLUDEPATH += $$PWD/md4c
|
INCLUDEPATH += $$PWD/md4c
|
||||||
HEADERS += $$PWD/md4c/md4c.h
|
HEADERS += $$PWD/md4c/md4c.h
|
||||||
SOURCES += $$PWD/md4c/md4c.c
|
SOURCES += $$PWD/md4c/md4c.c
|
||||||
|
DEFINES += MD4C_USE_UTF8
|
||||||
|
@ -397,8 +397,8 @@ int QTextMarkdownImporter::cbEnterSpan(int spanType, void *det)
|
|||||||
break;
|
break;
|
||||||
case MD_SPAN_A: {
|
case MD_SPAN_A: {
|
||||||
MD_SPAN_A_DETAIL *detail = static_cast<MD_SPAN_A_DETAIL *>(det);
|
MD_SPAN_A_DETAIL *detail = static_cast<MD_SPAN_A_DETAIL *>(det);
|
||||||
QString url = QString::fromLatin1(detail->href.text, int(detail->href.size));
|
QString url = QString::fromUtf8(detail->href.text, int(detail->href.size));
|
||||||
QString title = QString::fromLatin1(detail->title.text, int(detail->title.size));
|
QString title = QString::fromUtf8(detail->title.text, int(detail->title.size));
|
||||||
charFmt.setAnchorHref(url);
|
charFmt.setAnchorHref(url);
|
||||||
charFmt.setAnchorNames(QStringList(title));
|
charFmt.setAnchorNames(QStringList(title));
|
||||||
charFmt.setForeground(m_palette.link());
|
charFmt.setForeground(m_palette.link());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user