diff --git a/docs/definitions.md b/docs/definitions.md index 0b97ae1..4bbb593 100644 --- a/docs/definitions.md +++ b/docs/definitions.md @@ -269,6 +269,17 @@ results in
``` +## break line + +``` +New\r\nLine +``` +results in +```html +New
+Line +``` + ## Images ``` diff --git a/include/maddy/breaklineparser.h b/include/maddy/breaklineparser.h new file mode 100644 index 0000000..e3ee6fc --- /dev/null +++ b/include/maddy/breaklineparser.h @@ -0,0 +1,51 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include + +#include "maddy/lineparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * BreakLineParser + * + * @class + */ +class BreakLineParser : public LineParser +{ +public: + /** + * Parse + * + * From Markdown: `text\r\n text` + * + * To HTML: `text
text` + * + * @method + * @param {std::string&} line The line to interpret + * @return {void} + */ + void + Parse(std::string& line) override + { + static std::regex re(R"((\r\n|\r))"); + static std::string replacement = "
"; + + line = std::regex_replace(line, re, replacement); + } +}; // class BreakLineParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy diff --git a/include/maddy/parser.h b/include/maddy/parser.h index c11401e..5ff5ec1 100644 --- a/include/maddy/parser.h +++ b/include/maddy/parser.h @@ -22,6 +22,7 @@ #include "maddy/unorderedlistparser.h" // LineParser +#include "maddy/breaklineparser.h" #include "maddy/emphasizedparser.h" #include "maddy/imageparser.h" #include "maddy/inlinecodeparser.h" @@ -54,7 +55,8 @@ public: * @method */ Parser() - : emphasizedParser(std::make_shared()) + : breakLineParser(std::make_shared()) + , emphasizedParser(std::make_shared()) , imageParser(std::make_shared()) , inlineCodeParser(std::make_shared()) , italicParser(std::make_shared()) @@ -111,6 +113,7 @@ public: } private: + std::shared_ptr breakLineParser; std::shared_ptr emphasizedParser; std::shared_ptr imageParser; std::shared_ptr inlineCodeParser; @@ -136,6 +139,8 @@ private: this->inlineCodeParser->Parse(line); this->italicParser->Parse(line); + + this->breakLineParser->Parse(line); } std::shared_ptr diff --git a/tests/maddy/test_maddy_breaklineparser.cpp b/tests/maddy/test_maddy_breaklineparser.cpp new file mode 100644 index 0000000..2e2406b --- /dev/null +++ b/tests/maddy/test_maddy_breaklineparser.cpp @@ -0,0 +1,35 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#include + +#include "gmock/gmock.h" + +#include "maddy/breaklineparser.h" + +// ----------------------------------------------------------------------------- + +TEST(MADDY_BREAKLINEPARSER, ItReplacesMarkdownWithBreakLineHTML) +{ + std::string text = + "Test the text\r\n" + "test text to check\r\n" + "check testing to text.\r" + "Check test to test text\r" + "This is a test\r\n" + "No more test to check"; + + std::string expected = + "Test the text
" + "test text to check
" + "check testing to text.
" + "Check test to test text
" + "This is a test
" + "No more test to check"; + auto breakLineParser = std::make_shared(); + + breakLineParser->Parse(text); + + ASSERT_EQ(text, expected); +}