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/headlineparser.h | 138 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 inc/maddy/headlineparser.h (limited to 'inc/maddy/headlineparser.h') diff --git a/inc/maddy/headlineparser.h b/inc/maddy/headlineparser.h new file mode 100644 index 0000000..9eafec5 --- /dev/null +++ b/inc/maddy/headlineparser.h @@ -0,0 +1,138 @@ +/* + * 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 { + +// ----------------------------------------------------------------------------- + +/** + * HeadlineParser + * + * From Markdown: + * + * ``` + * # Headline 1 + * ## Headline 2 + * ### Headline 3 + * #### Headline 4 + * ##### Headline 5 + * ###### Headline 6 + * ``` + * + * To HTML: + * + * ``` + *

Headline 1

+ *

Headline 2

+ *

Headline 3

+ *

Headline 4

+ *
Headline 5
+ *
Headline 6
+ * ``` + * + * @class + */ +class HeadlineParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} parseLineCallback + * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + */ + HeadlineParser( + std::function parseLineCallback, + std::function(const std::string& line)> getBlockParserForLineCallback + ) + : BlockParser(parseLineCallback, getBlockParserForLineCallback) + {} + + /** + * IsStartingLine + * + * If the line starts with 1 - 6 `#`, then it is a headline. + * + * @method + * @param {const std::string&} line + * @return {bool} + */ + static bool + IsStartingLine(const std::string& line) + { + static std::regex re("^(?:#){1,6} (.*)"); + return std::regex_match(line, re); + } + + /** + * IsFinished + * + * The headline 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::vector hlRegex = { + std::regex("^# (.*)") + , std::regex("^(?:#){2} (.*)") + , std::regex("^(?:#){3} (.*)") + , std::regex("^(?:#){4} (.*)") + , std::regex("^(?:#){5} (.*)") + , std::regex("^(?:#){6} (.*)") + }; + static std::vector hlReplacement = { + "

$1

" + , "

$1

" + , "

$1

" + , "

$1

" + , "
$1
" + , "
$1
" + }; + + for (uint8_t i = 0; i < 6; ++i) + { + line = std::regex_replace(line, hlRegex[i], hlReplacement[i]); + } + } +}; // class HeadlineParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy -- cgit v1.2.3