comparison 3rdparty/maddy/orderedlistparser.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 * OrderedListParser
23 *
24 * @class
25 */
26 class OrderedListParser : 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 OrderedListParser(
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 ordered list starts with `1. `.
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("^1\\. .*");
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 = this->isStartOfNewListItem(line);
90 uint32_t indentation = getIndentationWidth(line);
91
92 static std::regex orderedlineRegex("^[1-9]+[0-9]*\\. ");
93 line = std::regex_replace(line, orderedlineRegex, "");
94 static std::regex unorderedlineRegex("^\\* ");
95 line = std::regex_replace(line, unorderedlineRegex, "");
96
97 if (!this->isStarted)
98 {
99 line = "<ol><li>" + line;
100 this->isStarted = true;
101 return;
102 }
103
104 if (indentation >= 2)
105 {
106 line = line.substr(2);
107 return;
108 }
109
110 if (
111 line.empty() ||
112 line.find("</li><li>") != std::string::npos ||
113 line.find("</li></ol>") != std::string::npos ||
114 line.find("</li></ul>") != std::string::npos
115 )
116 {
117 line = "</li></ol>" + line;
118 this->isFinished = true;
119 return;
120 }
121
122 if (isStartOfNewListItem)
123 {
124 line = "</li><li>" + line;
125 }
126 }
127
128 private:
129 bool isStarted;
130 bool isFinished;
131
132 bool
133 isStartOfNewListItem(const std::string& line) const
134 {
135 static std::regex re("^(?:[1-9]+[0-9]*\\. |\\* ).*");
136 return std::regex_match(line, re);
137 }
138 }; // class OrderedListParser
139
140 // -----------------------------------------------------------------------------
141
142 } // namespace maddy