Mercurial > thymian
comparison 3rdparty/maddy/checklistparser.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 * ChecklistParser | |
| 23 * | |
| 24 * @class | |
| 25 */ | |
| 26 class ChecklistParser : 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 ChecklistParser( | |
| 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 * An unordered list 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("^- \\[[x| ]\\] .*"); | |
| 58 return std::regex_match(line, re); | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * IsFinished | |
| 63 * | |
| 64 * @method | |
| 65 * @return {bool} | |
| 66 */ | |
| 67 bool | |
| 68 IsFinished() const override | |
| 69 { | |
| 70 return this->isFinished; | |
| 71 } | |
| 72 | |
| 73 protected: | |
| 74 bool | |
| 75 isInlineBlockAllowed() const override | |
| 76 { | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 bool | |
| 81 isLineParserAllowed() const override | |
| 82 { | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 void | |
| 87 parseBlock(std::string& line) override | |
| 88 { | |
| 89 bool isStartOfNewListItem = IsStartingLine(line); | |
| 90 uint32_t indentation = getIndentationWidth(line); | |
| 91 | |
| 92 static std::regex lineRegex("^(- )"); | |
| 93 line = std::regex_replace(line, lineRegex, ""); | |
| 94 | |
| 95 static std::regex emptyBoxRegex("^\\[ \\]"); | |
| 96 static std::string emptyBoxReplacement = "<input type=\"checkbox\"/>"; | |
| 97 line = std::regex_replace(line, emptyBoxRegex, emptyBoxReplacement); | |
| 98 | |
| 99 static std::regex boxRegex("^\\[x\\]"); | |
| 100 static std::string boxReplacement = "<input type=\"checkbox\" checked=\"checked\"/>"; | |
| 101 line = std::regex_replace(line, boxRegex, boxReplacement); | |
| 102 | |
| 103 if (!this->isStarted) | |
| 104 { | |
| 105 line = "<ul class=\"checklist\"><li><label>" + line; | |
| 106 this->isStarted = true; | |
| 107 return; | |
| 108 } | |
| 109 | |
| 110 if (indentation >= 2) | |
| 111 { | |
| 112 line = line.substr(2); | |
| 113 return; | |
| 114 } | |
| 115 | |
| 116 if ( | |
| 117 line.empty() || | |
| 118 line.find("</label></li><li><label>") != std::string::npos || | |
| 119 line.find("</label></li></ul>") != std::string::npos | |
| 120 ) | |
| 121 { | |
| 122 line = "</label></li></ul>" + line; | |
| 123 this->isFinished = true; | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 if (isStartOfNewListItem) | |
| 128 { | |
| 129 line = "</label></li><li><label>" + line; | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 private: | |
| 134 bool isStarted; | |
| 135 bool isFinished; | |
| 136 }; // class ChecklistParser | |
| 137 | |
| 138 // ----------------------------------------------------------------------------- | |
| 139 | |
| 140 } // namespace maddy |
