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/codeblockparser.h | 137 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 inc/maddy/codeblockparser.h (limited to 'inc/maddy/codeblockparser.h') diff --git a/inc/maddy/codeblockparser.h b/inc/maddy/codeblockparser.h new file mode 100644 index 0000000..2cb4a97 --- /dev/null +++ b/inc/maddy/codeblockparser.h @@ -0,0 +1,137 @@ +/* + * 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 { + +// ----------------------------------------------------------------------------- + +/** + * CodeBlockParser + * + * From Markdown: 3 times surrounded code (without space in the beginning) + * + * ``` + * ``` + * some code + * ``` + * ``` + * + * To HTML: + * + * ``` + *

+ * some code
+ * 
+ * ``` + * + * @class + */ +class CodeBlockParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} parseLineCallback + * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + */ + CodeBlockParser( + std::function parseLineCallback, + std::function(const std::string& line)> getBlockParserForLineCallback + ) + : BlockParser(parseLineCallback, getBlockParserForLineCallback) + , isStarted(false) + , isFinished(false) + {} + + /** + * IsStartingLine + * + * If the line starts with three code signs, then it is a code block. + * + * ``` + * ``` + * ``` + * + * @method + * @param {const std::string&} line + * @return {bool} + */ + static bool + IsStartingLine(const std::string& line) + { + static std::regex re("^(?:`){3}$"); + return std::regex_match(line, re); + } + + /** + * IsFinished + * + * @method + * @return {bool} + */ + bool + IsFinished() const override + { + return this->isFinished; + } + +protected: + bool + isInlineBlockAllowed() const override + { + return false; + } + + bool + isLineParserAllowed() const override + { + return false; + } + + void + parseBlock(std::string& line) override + { + if (line == "```") + { + if (!this->isStarted) + { + line = "
\n";
+        this->isStarted = true;
+        this->isFinished = false;
+        return;
+      }
+      else
+      {
+        line = "
"; + this->isFinished = true; + this->isStarted = false; + return; + } + } + + line += "\n"; + } + +private: + bool isStarted; + bool isFinished; +}; // class CodeBlockParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy -- cgit v1.2.3