|
9 | 9 | #include <tao/pegtl/contrib/unescape.hpp> |
10 | 10 |
|
11 | 11 | #include <functional> |
| 12 | +#include <iterator> |
12 | 13 | #include <memory> |
13 | 14 | #include <numeric> |
| 15 | +#include <optional> |
14 | 16 | #include <tuple> |
| 17 | +#include <utility> |
15 | 18 |
|
16 | 19 | using namespace std::literals; |
17 | 20 |
|
@@ -47,7 +50,90 @@ std::string_view ast_node::unescaped_view() const |
47 | 50 | { |
48 | 51 | if (!_unescaped) |
49 | 52 | { |
50 | | - if (children.size() > 1) |
| 53 | + if (is_type<block_quote_content_lines>()) |
| 54 | + { |
| 55 | + // Trim leading and trailing empty lines |
| 56 | + const auto isNonEmptyLine = [](const std::unique_ptr<ast_node>& child) noexcept { |
| 57 | + return child->is_type<block_quote_line>(); |
| 58 | + }; |
| 59 | + const auto itrEndRev = std::make_reverse_iterator( |
| 60 | + std::find_if(children.cbegin(), children.cend(), isNonEmptyLine)); |
| 61 | + const auto itrRev = std::find_if(children.crbegin(), itrEndRev, isNonEmptyLine); |
| 62 | + std::vector<std::optional<std::pair<std::string_view, std::string_view>>> lines( |
| 63 | + std::distance(itrRev, itrEndRev)); |
| 64 | + |
| 65 | + std::transform(itrRev, |
| 66 | + itrEndRev, |
| 67 | + lines.rbegin(), |
| 68 | + [](const std::unique_ptr<ast_node>& child) noexcept { |
| 69 | + return (child->is_type<block_quote_line>() && !child->children.empty() |
| 70 | + && child->children.front()->is_type<block_quote_empty_line>() |
| 71 | + && child->children.back()->is_type<block_quote_line_content>()) |
| 72 | + ? std::make_optional(std::make_pair(child->children.front()->string_view(), |
| 73 | + child->children.back()->unescaped_view())) |
| 74 | + : std::nullopt; |
| 75 | + }); |
| 76 | + |
| 77 | + // Calculate the common indent |
| 78 | + const auto commonIndent = std::accumulate(lines.cbegin(), |
| 79 | + lines.cend(), |
| 80 | + std::optional<size_t> {}, |
| 81 | + [](auto value, const auto& line) noexcept { |
| 82 | + if (line) |
| 83 | + { |
| 84 | + const auto indent = line->first.size(); |
| 85 | + |
| 86 | + if (!value || indent < *value) |
| 87 | + { |
| 88 | + value = indent; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return value; |
| 93 | + }); |
| 94 | + |
| 95 | + const auto trimIndent = commonIndent ? *commonIndent : 0; |
| 96 | + std::string joined; |
| 97 | + |
| 98 | + if (!lines.empty()) |
| 99 | + { |
| 100 | + joined.reserve(std::accumulate(lines.cbegin(), |
| 101 | + lines.cend(), |
| 102 | + size_t {}, |
| 103 | + [trimIndent](auto value, const auto& line) noexcept { |
| 104 | + if (line) |
| 105 | + { |
| 106 | + value += line->first.size() - trimIndent; |
| 107 | + value += line->second.size(); |
| 108 | + } |
| 109 | + |
| 110 | + return value; |
| 111 | + }) |
| 112 | + + lines.size() - 1); |
| 113 | + |
| 114 | + bool firstLine = true; |
| 115 | + |
| 116 | + for (const auto& line : lines) |
| 117 | + { |
| 118 | + if (!firstLine) |
| 119 | + { |
| 120 | + joined.append(1, '\n'); |
| 121 | + } |
| 122 | + |
| 123 | + if (line) |
| 124 | + { |
| 125 | + joined.append(line->first.substr(trimIndent)); |
| 126 | + joined.append(line->second); |
| 127 | + } |
| 128 | + |
| 129 | + firstLine = false; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + const_cast<ast_node*>(this)->_unescaped = |
| 134 | + std::make_unique<unescaped_t>(std::move(joined)); |
| 135 | + } |
| 136 | + else if (children.size() > 1) |
51 | 137 | { |
52 | 138 | std::string joined; |
53 | 139 |
|
@@ -224,6 +310,26 @@ struct ast_selector<block_escape_sequence> : std::true_type |
224 | 310 | } |
225 | 311 | }; |
226 | 312 |
|
| 313 | +template <> |
| 314 | +struct ast_selector<block_quote_content_lines> : std::true_type |
| 315 | +{ |
| 316 | +}; |
| 317 | + |
| 318 | +template <> |
| 319 | +struct ast_selector<block_quote_empty_line> : std::true_type |
| 320 | +{ |
| 321 | +}; |
| 322 | + |
| 323 | +template <> |
| 324 | +struct ast_selector<block_quote_line> : std::true_type |
| 325 | +{ |
| 326 | +}; |
| 327 | + |
| 328 | +template <> |
| 329 | +struct ast_selector<block_quote_line_content> : std::true_type |
| 330 | +{ |
| 331 | +}; |
| 332 | + |
227 | 333 | template <> |
228 | 334 | struct ast_selector<block_quote_character> : std::true_type |
229 | 335 | { |
|
0 commit comments