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/htmlparser.h | 127 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 inc/maddy/htmlparser.h (limited to 'inc/maddy/htmlparser.h') diff --git a/inc/maddy/htmlparser.h b/inc/maddy/htmlparser.h new file mode 100644 index 0000000..b6845a7 --- /dev/null +++ b/inc/maddy/htmlparser.h @@ -0,0 +1,127 @@ +/* + * This project is licensed under the MIT license. For more information see the + * LICENSE file. + */ +#pragma once + +// ----------------------------------------------------------------------------- + +#include +#include + +#include "maddy/blockparser.h" + +// ----------------------------------------------------------------------------- + +namespace maddy { + +// ----------------------------------------------------------------------------- + +/** + * HtmlParser + * + * @class + */ +class HtmlParser : public BlockParser +{ +public: + /** + * ctor + * + * @method + * @param {std::function} parseLineCallback + * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + */ + HtmlParser( + std::function parseLineCallback, + std::function(const std::string& line)> getBlockParserForLineCallback + ) + : BlockParser(parseLineCallback, getBlockParserForLineCallback) + , isStarted(false) + , isFinished(false) + , isGreaterThanFound(false) + {} + + /** + * IsStartingLine + * + * If the line is starting with `<`, HTML is expected to follow. + * Nothing after that will be parsed, it only is copied. + * + * @method + * @param {const std::string&} line + * @return {bool} + */ + static bool + IsStartingLine(const std::string& line) + { + return line[0] == '<'; + } + + /** + * IsFinished + * + * `>` followed by an empty line will end the HTML block. + * + * @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 (!this->isStarted) + { + this->isStarted = true; + } + + if (!line.empty() && line[line.size() - 1] == '>') + { + this->isGreaterThanFound = true; + return; + } + + if (line.empty() && this->isGreaterThanFound) + { + this->isFinished = true; + return; + } + + if (!line.empty() && this->isGreaterThanFound) + { + this->isGreaterThanFound = false; + } + + if (!line.empty()) + { + line += " "; + } + } + +private: + bool isStarted; + bool isFinished; + bool isGreaterThanFound; +}; // class HtmlParser + +// ----------------------------------------------------------------------------- + +} // namespace maddy -- cgit v1.2.3