mirror of
https://github.com/quitesimpleorg/qsmaddy.git
synced 2024-12-04 04:12:35 +01:00
breaklineparser: First version
Some markdown provides such as GitHub, uses \r\n to create <br> Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
This commit is contained in:
parent
0fb6de703d
commit
77782635d4
51
include/maddy/breaklineparser.h
Normal file
51
include/maddy/breaklineparser.h
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. For more information see the
|
||||||
|
* LICENSE file.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
|
#include "maddy/lineparser.h"
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace maddy {
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BreakLineParser
|
||||||
|
*
|
||||||
|
* @class
|
||||||
|
*/
|
||||||
|
class BreakLineParser : public LineParser
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Parse
|
||||||
|
*
|
||||||
|
* From Markdown: `text\r\n text`
|
||||||
|
*
|
||||||
|
* To HTML: `text<br> 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 = "<br>";
|
||||||
|
|
||||||
|
line = std::regex_replace(line, re, replacement);
|
||||||
|
}
|
||||||
|
}; // class BreakLineParser
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
} // namespace maddy
|
@ -22,6 +22,7 @@
|
|||||||
#include "maddy/unorderedlistparser.h"
|
#include "maddy/unorderedlistparser.h"
|
||||||
|
|
||||||
// LineParser
|
// LineParser
|
||||||
|
#include "maddy/breaklineparser.h"
|
||||||
#include "maddy/emphasizedparser.h"
|
#include "maddy/emphasizedparser.h"
|
||||||
#include "maddy/imageparser.h"
|
#include "maddy/imageparser.h"
|
||||||
#include "maddy/inlinecodeparser.h"
|
#include "maddy/inlinecodeparser.h"
|
||||||
@ -54,7 +55,8 @@ public:
|
|||||||
* @method
|
* @method
|
||||||
*/
|
*/
|
||||||
Parser()
|
Parser()
|
||||||
: emphasizedParser(std::make_shared<EmphasizedParser>())
|
: breakLineParser(std::make_shared<BreakLineParser>())
|
||||||
|
, emphasizedParser(std::make_shared<EmphasizedParser>())
|
||||||
, imageParser(std::make_shared<ImageParser>())
|
, imageParser(std::make_shared<ImageParser>())
|
||||||
, inlineCodeParser(std::make_shared<InlineCodeParser>())
|
, inlineCodeParser(std::make_shared<InlineCodeParser>())
|
||||||
, italicParser(std::make_shared<ItalicParser>())
|
, italicParser(std::make_shared<ItalicParser>())
|
||||||
@ -111,6 +113,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::shared_ptr<BreakLineParser> breakLineParser;
|
||||||
std::shared_ptr<EmphasizedParser> emphasizedParser;
|
std::shared_ptr<EmphasizedParser> emphasizedParser;
|
||||||
std::shared_ptr<ImageParser> imageParser;
|
std::shared_ptr<ImageParser> imageParser;
|
||||||
std::shared_ptr<InlineCodeParser> inlineCodeParser;
|
std::shared_ptr<InlineCodeParser> inlineCodeParser;
|
||||||
@ -136,6 +139,8 @@ private:
|
|||||||
this->inlineCodeParser->Parse(line);
|
this->inlineCodeParser->Parse(line);
|
||||||
|
|
||||||
this->italicParser->Parse(line);
|
this->italicParser->Parse(line);
|
||||||
|
|
||||||
|
this->breakLineParser->Parse(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<BlockParser>
|
std::shared_ptr<BlockParser>
|
||||||
|
35
tests/maddy/test_maddy_breaklineparser.cpp
Normal file
35
tests/maddy/test_maddy_breaklineparser.cpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. For more information see the
|
||||||
|
* LICENSE file.
|
||||||
|
*/
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#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<br>"
|
||||||
|
"test text to check<br>"
|
||||||
|
"check testing to text.<br>"
|
||||||
|
"Check test to test text<br>"
|
||||||
|
"This is a test<br>"
|
||||||
|
"No more test to check";
|
||||||
|
auto breakLineParser = std::make_shared<maddy::BreakLineParser>();
|
||||||
|
|
||||||
|
breakLineParser->Parse(text);
|
||||||
|
|
||||||
|
ASSERT_EQ(text, expected);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user