From 7d3c9ec7368a5d25235e818c2c147ba7e89f6567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gassmann?= Date: Sun, 27 Feb 2022 14:06:15 +0100 Subject: [+] Added base project --- inc/maddy/horizontallineparser.h | 106 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 inc/maddy/horizontallineparser.h (limited to 'inc/maddy/horizontallineparser.h') diff --git a/inc/maddy/horizontallineparser.h b/inc/maddy/horizontallineparser.h new file mode 100644 index 0000000..add6bf1 --- /dev/null +++ b/inc/maddy/horizontallineparser.h @@ -0,0 +1,106 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include +#include + +#include "maddy/blockparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * HorizontalLineParser + * + * From Markdown: `---` + * + * To HTML: `
` + * + * @class + */ +class HorizontalLineParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} parseLineCallback + * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + */ + HorizontalLineParser( + std::function parseLineCallback, + std::function(const std::string& line)> getBlockParserForLineCallback + ) + : BlockParser(parseLineCallback, getBlockParserForLineCallback) + , lineRegex("^---$") + {} + + /** + * IsStartingLine + * + * If the line has exact three dashes `---`, then it is a horizontal line. + * + * @method + * @param {const std::string&} line + * @return {bool} + */ + static bool + IsStartingLine(const std::string& line) + { + static std::regex re("^---$"); + return std::regex_match(line, re); + } + + /** + * IsFinished + * + * The horizontal line is always only one line long, so this method always + * returns true. + * + * @method + * @return {bool} + */ + bool + IsFinished() const override + { + return true; + } + +protected: + bool + isInlineBlockAllowed() const override + { + return false; + } + + bool + isLineParserAllowed() const override + { + return false; + } + + void + parseBlock(std::string& line) override + { + static std::string replacement = "
"; + + line = std::regex_replace(line, lineRegex, replacement); + } + +private: + std::regex lineRegex; +}; // class HorizontalLineParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy -- cgit v1.2.3