Mercurial > thymian
comparison 3rdparty/maddy/htmlparser.h @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 /* | |
| 2 * This project is licensed under the MIT license. For more information see the | |
| 3 * LICENSE file. | |
| 4 */ | |
| 5 #pragma once | |
| 6 | |
| 7 // ----------------------------------------------------------------------------- | |
| 8 | |
| 9 #include <functional> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "maddy/blockparser.h" | |
| 13 | |
| 14 // ----------------------------------------------------------------------------- | |
| 15 | |
| 16 namespace maddy { | |
| 17 | |
| 18 // ----------------------------------------------------------------------------- | |
| 19 | |
| 20 /** | |
| 21 * HtmlParser | |
| 22 * | |
| 23 * @class | |
| 24 */ | |
| 25 class HtmlParser : public BlockParser | |
| 26 { | |
| 27 public: | |
| 28 /** | |
| 29 * ctor | |
| 30 * | |
| 31 * @method | |
| 32 * @param {std::function<void(std::string&)>} parseLineCallback | |
| 33 * @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback | |
| 34 */ | |
| 35 HtmlParser( | |
| 36 std::function<void(std::string&)> parseLineCallback, | |
| 37 std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback | |
| 38 ) | |
| 39 : BlockParser(parseLineCallback, getBlockParserForLineCallback) | |
| 40 , isStarted(false) | |
| 41 , isFinished(false) | |
| 42 , isGreaterThanFound(false) | |
| 43 {} | |
| 44 | |
| 45 /** | |
| 46 * IsStartingLine | |
| 47 * | |
| 48 * If the line is starting with `<`, HTML is expected to follow. | |
| 49 * Nothing after that will be parsed, it only is copied. | |
| 50 * | |
| 51 * @method | |
| 52 * @param {const std::string&} line | |
| 53 * @return {bool} | |
| 54 */ | |
| 55 static bool | |
| 56 IsStartingLine(const std::string& line) | |
| 57 { | |
| 58 return line[0] == '<'; | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * IsFinished | |
| 63 * | |
| 64 * `>` followed by an empty line will end the HTML block. | |
| 65 * | |
| 66 * @method | |
| 67 * @return {bool} | |
| 68 */ | |
| 69 bool | |
| 70 IsFinished() const override | |
| 71 { | |
| 72 return this->isFinished; | |
| 73 } | |
| 74 | |
| 75 protected: | |
| 76 bool | |
| 77 isInlineBlockAllowed() const override | |
| 78 { | |
| 79 return false; | |
| 80 } | |
| 81 | |
| 82 bool | |
| 83 isLineParserAllowed() const override | |
| 84 { | |
| 85 return false; | |
| 86 } | |
| 87 | |
| 88 void | |
| 89 parseBlock(std::string& line) override | |
| 90 { | |
| 91 if (!this->isStarted) | |
| 92 { | |
| 93 this->isStarted = true; | |
| 94 } | |
| 95 | |
| 96 if (!line.empty() && line[line.size() - 1] == '>') | |
| 97 { | |
| 98 this->isGreaterThanFound = true; | |
| 99 return; | |
| 100 } | |
| 101 | |
| 102 if (line.empty() && this->isGreaterThanFound) | |
| 103 { | |
| 104 this->isFinished = true; | |
| 105 return; | |
| 106 } | |
| 107 | |
| 108 if (!line.empty() && this->isGreaterThanFound) | |
| 109 { | |
| 110 this->isGreaterThanFound = false; | |
| 111 } | |
| 112 | |
| 113 if (!line.empty()) | |
| 114 { | |
| 115 line += " "; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 private: | |
| 120 bool isStarted; | |
| 121 bool isFinished; | |
| 122 bool isGreaterThanFound; | |
| 123 }; // class HtmlParser | |
| 124 | |
| 125 // ----------------------------------------------------------------------------- | |
| 126 | |
| 127 } // namespace maddy |
