From ce81283b2633d945d86046d0772a17989c3c7045 Mon Sep 17 00:00:00 2001 From: Petra Baranski Date: Sat, 26 Aug 2023 13:03:35 +0200 Subject: [PATCH] feat: enable optional inline parsing in headlines --- include/maddy/headlineparser.h | 9 +++++++-- include/maddy/parser.h | 20 ++++++++++++++++---- include/maddy/parserconfig.h | 11 +++++++++++ tests/maddy/test_maddy_parser.cpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/include/maddy/headlineparser.h b/include/maddy/headlineparser.h index 9eafec5..7b25d55 100644 --- a/include/maddy/headlineparser.h +++ b/include/maddy/headlineparser.h @@ -57,9 +57,11 @@ public: */ HeadlineParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback + std::function(const std::string& line)> getBlockParserForLineCallback, + bool isInlineParserAllowed = true ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) + , isInlineParserAllowed(isInlineParserAllowed) {} /** @@ -103,7 +105,7 @@ protected: bool isLineParserAllowed() const override { - return false; + return this->isInlineParserAllowed; } void @@ -131,6 +133,9 @@ protected: line = std::regex_replace(line, hlRegex[i], hlReplacement[i]); } } + +private: + bool isInlineParserAllowed; }; // class HeadlineParser // ----------------------------------------------------------------------------- diff --git a/include/maddy/parser.h b/include/maddy/parser.h index 7d9b48b..76bee7b 100644 --- a/include/maddy/parser.h +++ b/include/maddy/parser.h @@ -287,10 +287,22 @@ private: maddy::HeadlineParser::IsStartingLine(line) ) { - parser = std::make_shared( - nullptr, - nullptr - ); + if (!this->config || this->config->isHeadlineInlineParsingEnabled) + { + parser = std::make_shared( + [this](std::string& line){ this->runLineParser(line); }, + nullptr, + true + ); + } + else + { + parser = std::make_shared( + nullptr, + nullptr, + false + ); + } } else if ( ( diff --git a/include/maddy/parserconfig.h b/include/maddy/parserconfig.h index cb1171a..a5469a2 100644 --- a/include/maddy/parserconfig.h +++ b/include/maddy/parserconfig.h @@ -70,11 +70,22 @@ struct ParserConfig */ bool isHTMLWrappedInParagraph; + /** + * en-/disable headline inline-parsing + * + * default: enabled + */ + bool isHeadlineInlineParsingEnabled; + + /** + * enabled parsers bitfield + */ uint32_t enabledParsers; ParserConfig() : isEmphasizedParserEnabled(true) , isHTMLWrappedInParagraph(true) + , isHeadlineInlineParsingEnabled(true) , enabledParsers(maddy::types::DEFAULT) {} }; // class ParserConfig diff --git a/tests/maddy/test_maddy_parser.cpp b/tests/maddy/test_maddy_parser.cpp index f69f44c..112189e 100644 --- a/tests/maddy/test_maddy_parser.cpp +++ b/tests/maddy/test_maddy_parser.cpp @@ -64,3 +64,34 @@ TEST(MADDY_PARSER, ItShouldParseWithSmallConfig) ASSERT_EQ(testHtml3, output); } + +TEST(MADDY_PARSER, ItShouldParseInlineCodeInHeadlines) +{ + const std::string headlineTest = R"( +# Some **test** markdown +)"; + const std::string expectedHTML = "

Some test markdown

"; + std::stringstream markdown(headlineTest); + + auto parser = std::make_shared(); + + const std::string output = parser->Parse(markdown); + + ASSERT_EQ(expectedHTML, output); +} + +TEST(MADDY_PARSER, ItShouldNotParseInlineCodeInHeadlineIfDisabled) +{ + const std::string headlineTest = R"( +# Some **test** markdown +)"; + const std::string expectedHTML = "

Some **test** markdown

"; + std::stringstream markdown(headlineTest); + auto config = std::make_shared(); + config->isHeadlineInlineParsingEnabled = false; + auto parser = std::make_shared(config); + + const std::string output = parser->Parse(markdown); + + ASSERT_EQ(expectedHTML, output); +}