comparison 3rdparty/maddy/unorderedlistparser.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 * UnorderedListParser
23 *
24 * @class
25 */
26 class UnorderedListParser : 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 UnorderedListParser(
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("^[+*-] .*");
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 if (!this->isStarted)
96 {
97 line = "<ul><li>" + line;
98 this->isStarted = true;
99 return;
100 }
101
102 if (indentation >= 2)
103 {
104 line = line.substr(2);
105 return;
106 }
107
108 if (
109 line.empty() ||
110 line.find("</li><li>") != std::string::npos ||
111 line.find("</li></ol>") != std::string::npos ||
112 line.find("</li></ul>") != std::string::npos
113 )
114 {
115 line = "</li></ul>" + line;
116 this->isFinished = true;
117 return;
118 }
119
120 if (isStartOfNewListItem)
121 {
122 line = "</li><li>" + line;
123 }
124 }
125
126 private:
127 bool isStarted;
128 bool isFinished;
129 }; // class UnorderedListParser
130
131 // -----------------------------------------------------------------------------
132
133 } // namespace maddy