Mercurial > thymian
comparison 3rdparty/maddy/horizontallineparser.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 #include <regex> | |
| 12 | |
| 13 #include "maddy/blockparser.h" | |
| 14 | |
| 15 // ----------------------------------------------------------------------------- | |
| 16 | |
| 17 namespace maddy { | |
| 18 | |
| 19 // ----------------------------------------------------------------------------- | |
| 20 | |
| 21 /** | |
| 22 * HorizontalLineParser | |
| 23 * | |
| 24 * From Markdown: `---` | |
| 25 * | |
| 26 * To HTML: `<hr/>` | |
| 27 * | |
| 28 * @class | |
| 29 */ | |
| 30 class HorizontalLineParser : public BlockParser | |
| 31 { | |
| 32 public: | |
| 33 /** | |
| 34 * ctor | |
| 35 * | |
| 36 * @method | |
| 37 * @param {std::function<void(std::string&)>} parseLineCallback | |
| 38 * @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback | |
| 39 */ | |
| 40 HorizontalLineParser( | |
| 41 std::function<void(std::string&)> parseLineCallback, | |
| 42 std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback | |
| 43 ) | |
| 44 : BlockParser(parseLineCallback, getBlockParserForLineCallback) | |
| 45 , lineRegex("^---$") | |
| 46 {} | |
| 47 | |
| 48 /** | |
| 49 * IsStartingLine | |
| 50 * | |
| 51 * If the line has exact three dashes `---`, then it is a horizontal line. | |
| 52 * | |
| 53 * @method | |
| 54 * @param {const std::string&} line | |
| 55 * @return {bool} | |
| 56 */ | |
| 57 static bool | |
| 58 IsStartingLine(const std::string& line) | |
| 59 { | |
| 60 static std::regex re("^---$"); | |
| 61 return std::regex_match(line, re); | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * IsFinished | |
| 66 * | |
| 67 * The horizontal line is always only one line long, so this method always | |
| 68 * returns true. | |
| 69 * | |
| 70 * @method | |
| 71 * @return {bool} | |
| 72 */ | |
| 73 bool | |
| 74 IsFinished() const override | |
| 75 { | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 protected: | |
| 80 bool | |
| 81 isInlineBlockAllowed() const override | |
| 82 { | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 bool | |
| 87 isLineParserAllowed() const override | |
| 88 { | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 void | |
| 93 parseBlock(std::string& line) override | |
| 94 { | |
| 95 static std::string replacement = "<hr/>"; | |
| 96 | |
| 97 line = std::regex_replace(line, lineRegex, replacement); | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 std::regex lineRegex; | |
| 102 }; // class HorizontalLineParser | |
| 103 | |
| 104 // ----------------------------------------------------------------------------- | |
| 105 | |
| 106 } // namespace maddy |
