Mercurial > thymian
comparison 3rdparty/maddy/quoteparser.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 <regex> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "maddy/blockparser.h" | |
| 14 | |
| 15 // ----------------------------------------------------------------------------- | |
| 16 | |
| 17 namespace maddy { | |
| 18 | |
| 19 // ----------------------------------------------------------------------------- | |
| 20 | |
| 21 /** | |
| 22 * QuoteParser | |
| 23 * | |
| 24 * @class | |
| 25 */ | |
| 26 class QuoteParser : public BlockParser | |
| 27 { | |
| 28 public: | |
| 29 /** | |
| 30 * ctor | |
| 31 * | |
| 32 * @method | |
| 33 * @param {std::function<void(std::string&)>} parseLineCallback | |
| 34 * @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback | |
| 35 */ | |
| 36 QuoteParser( | |
| 37 std::function<void(std::string&)> parseLineCallback, | |
| 38 std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback | |
| 39 ) | |
| 40 : BlockParser(parseLineCallback, getBlockParserForLineCallback) | |
| 41 , isStarted(false) | |
| 42 , isFinished(false) | |
| 43 {} | |
| 44 | |
| 45 /** | |
| 46 * IsStartingLine | |
| 47 * | |
| 48 * A quote starts with `> `. | |
| 49 * | |
| 50 * @method | |
| 51 * @param {const std::string&} line | |
| 52 * @return {bool} | |
| 53 */ | |
| 54 static bool | |
| 55 IsStartingLine(const std::string& line) | |
| 56 { | |
| 57 static std::regex re("^\\>.*"); | |
| 58 return std::regex_match(line, re); | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * AddLine | |
| 63 * | |
| 64 * Adding a line which has to be parsed. | |
| 65 * | |
| 66 * @method | |
| 67 * @param {std::string&} line | |
| 68 * @return {void} | |
| 69 */ | |
| 70 void | |
| 71 AddLine(std::string& line) override | |
| 72 { | |
| 73 if (!this->isStarted) | |
| 74 { | |
| 75 this->result << "<blockquote>"; | |
| 76 this->isStarted = true; | |
| 77 } | |
| 78 | |
| 79 bool finish = false; | |
| 80 if (line.empty()) | |
| 81 { | |
| 82 finish = true; | |
| 83 } | |
| 84 | |
| 85 this->parseBlock(line); | |
| 86 | |
| 87 if (this->isInlineBlockAllowed() && !this->childParser) | |
| 88 { | |
| 89 this->childParser = this->getBlockParserForLine(line); | |
| 90 } | |
| 91 | |
| 92 if (this->childParser) | |
| 93 { | |
| 94 this->childParser->AddLine(line); | |
| 95 | |
| 96 if (this->childParser->IsFinished()) | |
| 97 { | |
| 98 this->result << this->childParser->GetResult().str(); | |
| 99 this->childParser = nullptr; | |
| 100 } | |
| 101 | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 if (this->isLineParserAllowed()) | |
| 106 { | |
| 107 this->parseLine(line); | |
| 108 } | |
| 109 | |
| 110 if (finish) | |
| 111 { | |
| 112 this->result << "</blockquote>"; | |
| 113 this->isFinished = true; | |
| 114 } | |
| 115 | |
| 116 this->result << line; | |
| 117 } | |
| 118 | |
| 119 /** | |
| 120 * IsFinished | |
| 121 * | |
| 122 * @method | |
| 123 * @return {bool} | |
| 124 */ | |
| 125 bool | |
| 126 IsFinished() const override | |
| 127 { | |
| 128 return this->isFinished; | |
| 129 } | |
| 130 | |
| 131 protected: | |
| 132 bool | |
| 133 isInlineBlockAllowed() const override | |
| 134 { | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 bool | |
| 139 isLineParserAllowed() const override | |
| 140 { | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 void | |
| 145 parseBlock(std::string& line) override | |
| 146 { | |
| 147 static std::regex lineRegexWithSpace("^\\> "); | |
| 148 line = std::regex_replace(line, lineRegexWithSpace, ""); | |
| 149 static std::regex lineRegexWithoutSpace("^\\>"); | |
| 150 line = std::regex_replace(line, lineRegexWithoutSpace, ""); | |
| 151 | |
| 152 if (!line.empty()) | |
| 153 { | |
| 154 line += " "; | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 private: | |
| 159 bool isStarted; | |
| 160 bool isFinished; | |
| 161 }; // class QuoteParser | |
| 162 | |
| 163 // ----------------------------------------------------------------------------- | |
| 164 | |
| 165 } // namespace maddy |
