comparison 3rdparty/maddy/headlineparser.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 * HeadlineParser
23 *
24 * From Markdown:
25 *
26 * ```
27 * # Headline 1
28 * ## Headline 2
29 * ### Headline 3
30 * #### Headline 4
31 * ##### Headline 5
32 * ###### Headline 6
33 * ```
34 *
35 * To HTML:
36 *
37 * ```
38 * <h1>Headline 1</h1>
39 * <h2>Headline 2</h2>
40 * <h3>Headline 3</h3>
41 * <h4>Headline 4</h4>
42 * <h5>Headline 5</h5>
43 * <h6>Headline 6</h6>
44 * ```
45 *
46 * @class
47 */
48 class HeadlineParser : public BlockParser
49 {
50 public:
51 /**
52 * ctor
53 *
54 * @method
55 * @param {std::function<void(std::string&)>} parseLineCallback
56 * @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
57 */
58 HeadlineParser(
59 std::function<void(std::string&)> parseLineCallback,
60 std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
61 )
62 : BlockParser(parseLineCallback, getBlockParserForLineCallback)
63 {}
64
65 /**
66 * IsStartingLine
67 *
68 * If the line starts with 1 - 6 `#`, then it is a headline.
69 *
70 * @method
71 * @param {const std::string&} line
72 * @return {bool}
73 */
74 static bool
75 IsStartingLine(const std::string& line)
76 {
77 static std::regex re("^(?:#){1,6} (.*)");
78 return std::regex_match(line, re);
79 }
80
81 /**
82 * IsFinished
83 *
84 * The headline is always only one line long, so this method always returns
85 * true.
86 *
87 * @method
88 * @return {bool}
89 */
90 bool
91 IsFinished() const override
92 {
93 return true;
94 }
95
96 protected:
97 bool
98 isInlineBlockAllowed() const override
99 {
100 return false;
101 }
102
103 bool
104 isLineParserAllowed() const override
105 {
106 return false;
107 }
108
109 void
110 parseBlock(std::string& line) override
111 {
112 static std::vector<std::regex> hlRegex = {
113 std::regex("^# (.*)")
114 , std::regex("^(?:#){2} (.*)")
115 , std::regex("^(?:#){3} (.*)")
116 , std::regex("^(?:#){4} (.*)")
117 , std::regex("^(?:#){5} (.*)")
118 , std::regex("^(?:#){6} (.*)")
119 };
120 static std::vector<std::string> hlReplacement = {
121 "<h1>$1</h1>"
122 , "<h2>$1</h2>"
123 , "<h3>$1</h3>"
124 , "<h4>$1</h4>"
125 , "<h5>$1</h5>"
126 , "<h6>$1</h6>"
127 };
128
129 for (uint8_t i = 0; i < 6; ++i)
130 {
131 line = std::regex_replace(line, hlRegex[i], hlReplacement[i]);
132 }
133 }
134 }; // class HeadlineParser
135
136 // -----------------------------------------------------------------------------
137
138 } // namespace maddy