comparison 3rdparty/maddy/paragraphparser.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
12 #include "maddy/blockparser.h"
13
14 // -----------------------------------------------------------------------------
15
16 namespace maddy {
17
18 // -----------------------------------------------------------------------------
19
20 /**
21 * ParagraphParser
22 *
23 * @class
24 */
25 class ParagraphParser : public BlockParser
26 {
27 public:
28 /**
29 * ctor
30 *
31 * @method
32 * @param {std::function<void(std::string&)>} parseLineCallback
33 * @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
34 */
35 ParagraphParser(
36 std::function<void(std::string&)> parseLineCallback,
37 std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
38 )
39 : BlockParser(parseLineCallback, getBlockParserForLineCallback)
40 , isStarted(false)
41 , isFinished(false)
42 {}
43
44 /**
45 * IsStartingLine
46 *
47 * If the line is not empty, it will be a paragraph.
48 *
49 * This block parser has to always run as the last one!
50 *
51 * @method
52 * @param {const std::string&} line
53 * @return {bool}
54 */
55 static bool
56 IsStartingLine(const std::string& line)
57 {
58 return !line.empty();
59 }
60
61 /**
62 * IsFinished
63 *
64 * An empty line will end the paragraph.
65 *
66 * @method
67 * @return {bool}
68 */
69 bool
70 IsFinished() const override
71 {
72 return this->isFinished;
73 }
74
75 protected:
76 bool
77 isInlineBlockAllowed() const override
78 {
79 return false;
80 }
81
82 bool
83 isLineParserAllowed() const override
84 {
85 return true;
86 }
87
88 void
89 parseBlock(std::string& line) override
90 {
91 if (!this->isStarted)
92 {
93 line = "<p>" + line + " ";
94 this->isStarted = true;
95 return;
96 }
97
98 if (line.empty())
99 {
100 line += "</p>";
101 this->isFinished = true;
102 return;
103 }
104
105 line += " ";
106 }
107
108 private:
109 bool isStarted;
110 bool isFinished;
111 }; // class ParagraphParser
112
113 // -----------------------------------------------------------------------------
114
115 } // namespace maddy