From d377d1f3a921488525f6e75c850632f559707747 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Mon, 21 Oct 2019 15:14:53 +0200 Subject: Enforce QTextDocument::MarkdownFeature compatibility at compile time We use md4c for parsing markdown. It provides flags to control the feature set that will be supported when parsing particular documents. QTextMarkdownImporter::Feature is a fine-grained set of flags that exactly match the md4c feature flags that we support in Qt so far. QTextMarkdownImporter is a private exported class (new in 5.14). We don't expect the corresponding flags in md4c to change in incompatible ways in the future: the md4c authors have as much respect for avoiding compatibility issues as we do, and likely will only add features, not remove them. We now enforce QTextMarkdownImporter::Features compatibility with QTextDocument::MarkdownFeatures by setting them directly. We check QTextMarkdownImporter::Features compatibility with md4c's #define'd feature flags using static asserts, so that any hypothetical incompatibility would be detected at compile time. The enum conversion from QTextDocument::MarkdownFeatures to QTextMarkdownImporter::Features is moved to a new QTextMarkdownImporter constructor; thus the conversions from QTextDocument::MarkdownFeatures to QTextMarkdownImporter::Features, and then to unsigned (in QTextMarkdownImporter::import()) are adjacent in the same private class implementation. If incompatibility ever occurred, we would need to replace one or both of those with another suitable conversion function. Change-Id: I0bf8a21eb7559df1d38406b948ef657f9060c67b Reviewed-by: Vitaly Fanaskov --- src/gui/text/qtextmarkdownimporter_p.h | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'src/gui/text/qtextmarkdownimporter_p.h') diff --git a/src/gui/text/qtextmarkdownimporter_p.h b/src/gui/text/qtextmarkdownimporter_p.h index 35655aff8a..f450da5eb3 100644 --- a/src/gui/text/qtextmarkdownimporter_p.h +++ b/src/gui/text/qtextmarkdownimporter_p.h @@ -54,6 +54,7 @@ #include #include #include +#include #include #include @@ -67,27 +68,28 @@ class Q_GUI_EXPORT QTextMarkdownImporter { public: enum Feature { - // 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 + FeatureCollapseWhitespace = 0x0001, + FeaturePermissiveATXHeaders = 0x0002, + FeaturePermissiveURLAutoLinks = 0x0004, + FeaturePermissiveMailAutoLinks = 0x0008, + FeatureNoIndentedCodeBlocks = 0x0010, + FeatureNoHTMLBlocks = 0x0020, + FeatureNoHTMLSpans = 0x0040, + FeatureTables = 0x0100, + FeatureStrikeThrough = 0x0200, + FeaturePermissiveWWWAutoLinks = 0x0400, + FeatureTasklists = 0x0800, // composite flags - 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 + FeaturePermissiveAutoLinks = FeaturePermissiveMailAutoLinks + | FeaturePermissiveURLAutoLinks | FeaturePermissiveWWWAutoLinks, + FeatureNoHTML = QTextDocument::MarkdownNoHTML, + DialectCommonMark = QTextDocument::MarkdownDialectCommonMark, + DialectGitHub = QTextDocument::MarkdownDialectGitHub }; Q_DECLARE_FLAGS(Features, Feature) QTextMarkdownImporter(Features features); + QTextMarkdownImporter(QTextDocument::MarkdownFeatures features); void import(QTextDocument *doc, const QString &markdown); -- cgit v1.2.3