Skip to content

Commit

Permalink
Remove 3rdparty include from QTextMarkdownImporter header file
Browse files Browse the repository at this point in the history
It's a private header; but to be able to use it in a test, it has to
be as clean as a public header.

Change-Id: I868372406e62acc24051a6523fee89bb911a61f9
Reviewed-by: Gatis Paeglis <[email protected]>
  • Loading branch information
ec1oud committed May 8, 2019
1 parent c1721cc commit 6a58a68
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
49 changes: 25 additions & 24 deletions src/gui/text/qtextmarkdownimporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <QTextDocumentFragment>
#include <QTextList>
#include <QTextTable>
#include "../../3rdparty/md4c/md4c.h"

QT_BEGIN_NAMESPACE

Expand All @@ -57,31 +58,31 @@ Q_LOGGING_CATEGORY(lcMD, "qt.text.markdown")
static int CbEnterBlock(MD_BLOCKTYPE type, void *detail, void *userdata)
{
QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata);
return mdi->cbEnterBlock(type, detail);
return mdi->cbEnterBlock(int(type), detail);
}

static int CbLeaveBlock(MD_BLOCKTYPE type, void *detail, void *userdata)
{
QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata);
return mdi->cbLeaveBlock(type, detail);
return mdi->cbLeaveBlock(int(type), detail);
}

static int CbEnterSpan(MD_SPANTYPE type, void *detail, void *userdata)
{
QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata);
return mdi->cbEnterSpan(type, detail);
return mdi->cbEnterSpan(int(type), detail);
}

static int CbLeaveSpan(MD_SPANTYPE type, void *detail, void *userdata)
{
QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata);
return mdi->cbLeaveSpan(type, detail);
return mdi->cbLeaveSpan(int(type), detail);
}

static int CbText(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *userdata)
{
QTextMarkdownImporter *mdi = static_cast<QTextMarkdownImporter *>(userdata);
return mdi->cbText(type, text, size);
return mdi->cbText(int(type), text, size);
}

static void CbDebugLog(const char *msg, void *userdata)
Expand Down Expand Up @@ -131,15 +132,15 @@ void QTextMarkdownImporter::import(QTextDocument *doc, const QString &markdown)
doc->clear();
qCDebug(lcMD) << "default font" << doc->defaultFont() << "mono font" << m_monoFont;
QByteArray md = markdown.toUtf8();
md_parse(md.constData(), md.size(), &callbacks, this);
md_parse(md.constData(), MD_SIZE(md.size()), &callbacks, this);
delete m_cursor;
m_cursor = nullptr;
}

int QTextMarkdownImporter::cbEnterBlock(MD_BLOCKTYPE type, void *det)
int QTextMarkdownImporter::cbEnterBlock(int blockType, void *det)
{
m_blockType = type;
switch (type) {
m_blockType = blockType;
switch (blockType) {
case MD_BLOCK_P: {
QTextBlockFormat blockFmt;
int margin = m_doc->defaultFont().pointSize() / 2;
Expand All @@ -157,10 +158,10 @@ int QTextMarkdownImporter::cbEnterBlock(MD_BLOCKTYPE type, void *det)
MD_BLOCK_H_DETAIL *detail = static_cast<MD_BLOCK_H_DETAIL *>(det);
QTextBlockFormat blockFmt;
QTextCharFormat charFmt;
int sizeAdjustment = 4 - detail->level; // H1 to H6: +3 to -2
int sizeAdjustment = 4 - int(detail->level); // H1 to H6: +3 to -2
charFmt.setProperty(QTextFormat::FontSizeAdjustment, sizeAdjustment);
charFmt.setFontWeight(QFont::Bold);
blockFmt.setHeadingLevel(detail->level);
blockFmt.setHeadingLevel(int(detail->level));
m_cursor->insertBlock(blockFmt, charFmt);
} break;
case MD_BLOCK_LI: {
Expand Down Expand Up @@ -258,10 +259,10 @@ int QTextMarkdownImporter::cbEnterBlock(MD_BLOCKTYPE type, void *det)
return 0; // no error
}

int QTextMarkdownImporter::cbLeaveBlock(MD_BLOCKTYPE type, void *detail)
int QTextMarkdownImporter::cbLeaveBlock(int blockType, void *detail)
{
Q_UNUSED(detail)
switch (type) {
switch (blockType) {
case MD_BLOCK_UL:
case MD_BLOCK_OL:
m_listStack.pop();
Expand Down Expand Up @@ -304,10 +305,10 @@ int QTextMarkdownImporter::cbLeaveBlock(MD_BLOCKTYPE type, void *detail)
return 0; // no error
}

int QTextMarkdownImporter::cbEnterSpan(MD_SPANTYPE type, void *det)
int QTextMarkdownImporter::cbEnterSpan(int spanType, void *det)
{
QTextCharFormat charFmt;
switch (type) {
switch (spanType) {
case MD_SPAN_EM:
charFmt.setFontItalic(true);
break;
Expand All @@ -316,8 +317,8 @@ int QTextMarkdownImporter::cbEnterSpan(MD_SPANTYPE type, void *det)
break;
case MD_SPAN_A: {
MD_SPAN_A_DETAIL *detail = static_cast<MD_SPAN_A_DETAIL *>(det);
QString url = QString::fromLatin1(detail->href.text, detail->href.size);
QString title = QString::fromLatin1(detail->title.text, detail->title.size);
QString url = QString::fromLatin1(detail->href.text, int(detail->href.size));
QString title = QString::fromLatin1(detail->title.text, int(detail->title.size));
charFmt.setAnchorHref(url);
charFmt.setAnchorNames(QStringList(title));
charFmt.setForeground(m_palette.link());
Expand All @@ -326,8 +327,8 @@ int QTextMarkdownImporter::cbEnterSpan(MD_SPANTYPE type, void *det)
case MD_SPAN_IMG: {
m_imageSpan = true;
MD_SPAN_IMG_DETAIL *detail = static_cast<MD_SPAN_IMG_DETAIL *>(det);
QString src = QString::fromUtf8(detail->src.text, detail->src.size);
QString title = QString::fromUtf8(detail->title.text, detail->title.size);
QString src = QString::fromUtf8(detail->src.text, int(detail->src.size));
QString title = QString::fromUtf8(detail->title.text, int(detail->title.size));
QTextImageFormat img;
img.setName(src);
qCDebug(lcMD) << "image" << src << "title" << title << "relative to" << m_doc->baseUrl();
Expand All @@ -346,7 +347,7 @@ int QTextMarkdownImporter::cbEnterSpan(MD_SPANTYPE type, void *det)
return 0; // no error
}

int QTextMarkdownImporter::cbLeaveSpan(MD_SPANTYPE type, void *detail)
int QTextMarkdownImporter::cbLeaveSpan(int spanType, void *detail)
{
Q_UNUSED(detail)
QTextCharFormat charFmt;
Expand All @@ -356,20 +357,20 @@ int QTextMarkdownImporter::cbLeaveSpan(MD_SPANTYPE type, void *detail)
charFmt = m_spanFormatStack.top();
}
m_cursor->setCharFormat(charFmt);
if (type == MD_SPAN_IMG)
if (spanType == int(MD_SPAN_IMG))
m_imageSpan = false;
return 0; // no error
}

int QTextMarkdownImporter::cbText(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size)
int QTextMarkdownImporter::cbText(int textType, const char *text, unsigned size)
{
if (m_imageSpan)
return 0; // it's the alt-text
static const QRegularExpression openingBracket(QStringLiteral("<[a-zA-Z]"));
static const QRegularExpression closingBracket(QStringLiteral("(/>|</)"));
QString s = QString::fromUtf8(text, size);
QString s = QString::fromUtf8(text, int(size));

switch (type) {
switch (textType) {
case MD_TEXT_NORMAL:
if (m_htmlTagDepth) {
m_htmlAccumulator += s;
Expand Down
45 changes: 22 additions & 23 deletions src/gui/text/qtextmarkdownimporter_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@
#include <QtGui/qtextlist.h>
#include <QtCore/qstack.h>

#include "../../3rdparty/md4c/md4c.h"

QT_BEGIN_NAMESPACE

class QTextCursor;
Expand All @@ -69,22 +67,23 @@ class Q_GUI_EXPORT QTextMarkdownImporter
{
public:
enum Feature {
FeatureCollapseWhitespace = MD_FLAG_COLLAPSEWHITESPACE,
FeaturePermissiveATXHeaders = MD_FLAG_PERMISSIVEATXHEADERS,
FeaturePermissiveURLAutoLinks = MD_FLAG_PERMISSIVEURLAUTOLINKS,
FeaturePermissiveMailAutoLinks = MD_FLAG_PERMISSIVEEMAILAUTOLINKS,
FeatureNoIndentedCodeBlocks = MD_FLAG_NOINDENTEDCODEBLOCKS,
FeatureNoHTMLBlocks = MD_FLAG_NOHTMLBLOCKS,
FeatureNoHTMLSpans = MD_FLAG_NOHTMLSPANS,
FeatureTables = MD_FLAG_TABLES,
FeatureStrikeThrough = MD_FLAG_STRIKETHROUGH,
FeaturePermissiveWWWAutoLinks = MD_FLAG_PERMISSIVEWWWAUTOLINKS,
FeatureTasklists = MD_FLAG_TASKLISTS,
// Must be kept in sync with MD_FLAG_* in md4c.h
FeatureCollapseWhitespace = 0x0001, // MD_FLAG_COLLAPSEWHITESPACE
FeaturePermissiveATXHeaders = 0x0002, // MD_FLAG_PERMISSIVEATXHEADERS
FeaturePermissiveURLAutoLinks = 0x0004, // MD_FLAG_PERMISSIVEURLAUTOLINKS
FeaturePermissiveMailAutoLinks = 0x0008, // MD_FLAG_PERMISSIVEEMAILAUTOLINKS
FeatureNoIndentedCodeBlocks = 0x0010, // MD_FLAG_NOINDENTEDCODEBLOCKS
FeatureNoHTMLBlocks = 0x0020, // MD_FLAG_NOHTMLBLOCKS
FeatureNoHTMLSpans = 0x0040, // MD_FLAG_NOHTMLSPANS
FeatureTables = 0x0100, // MD_FLAG_TABLES
FeatureStrikeThrough = 0x0200, // MD_FLAG_STRIKETHROUGH
FeaturePermissiveWWWAutoLinks = 0x0400, // MD_FLAG_PERMISSIVEWWWAUTOLINKS
FeatureTasklists = 0x0800, // MD_FLAG_TASKLISTS
// composite flags
FeaturePermissiveAutoLinks = MD_FLAG_PERMISSIVEAUTOLINKS,
FeatureNoHTML = MD_FLAG_NOHTML,
DialectCommonMark = MD_DIALECT_COMMONMARK,
DialectGitHub = MD_DIALECT_GITHUB
FeaturePermissiveAutoLinks = FeaturePermissiveMailAutoLinks | FeaturePermissiveURLAutoLinks | FeaturePermissiveWWWAutoLinks, // MD_FLAG_PERMISSIVEAUTOLINKS
FeatureNoHTML = FeatureNoHTMLBlocks | FeatureNoHTMLSpans, // MD_FLAG_NOHTML
DialectCommonMark = 0, // MD_DIALECT_COMMONMARK
DialectGitHub = FeaturePermissiveAutoLinks | FeatureTables | FeatureStrikeThrough | FeatureTasklists // MD_DIALECT_GITHUB
};
Q_DECLARE_FLAGS(Features, Feature)

Expand All @@ -94,11 +93,11 @@ class Q_GUI_EXPORT QTextMarkdownImporter

public:
// MD4C callbacks
int cbEnterBlock(MD_BLOCKTYPE type, void* detail);
int cbLeaveBlock(MD_BLOCKTYPE type, void* detail);
int cbEnterSpan(MD_SPANTYPE type, void* detail);
int cbLeaveSpan(MD_SPANTYPE type, void* detail);
int cbText(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size);
int cbEnterBlock(int blockType, void* detail);
int cbLeaveBlock(int blockType, void* detail);
int cbEnterSpan(int spanType, void* detail);
int cbLeaveSpan(int spanType, void* detail);
int cbText(int textType, const char* text, unsigned size);

private:
QTextDocument *m_doc = nullptr;
Expand All @@ -115,7 +114,7 @@ class Q_GUI_EXPORT QTextMarkdownImporter
int m_tableRowCount = 0;
int m_tableCol = -1; // because relative cell movements (e.g. m_cursor->movePosition(QTextCursor::NextCell)) don't work
Features m_features;
MD_BLOCKTYPE m_blockType = MD_BLOCK_DOC;
int m_blockType = 0;
bool m_emptyList = false; // true when the last thing we did was insertList
bool m_imageSpan = false;
};
Expand Down

0 comments on commit 6a58a68

Please sign in to comment.