BareGit

Refactor block parsing into explicit phases

- Add reusable line cursor and pure marker recognizers.
- Separate container, leaf, and paragraph processing.
- Register the Markdown HTML suite with the test target.
Author: MetroWind <chris.corsair@gmail.com>
Date: Sun Aug 2 10:56:06 2026 -0700
Commit: 31e42d5f8ed10f3cf19028d9773bdd0cdcccdf7f

Changes

diff --git a/CMakeLists.txt b/CMakeLists.txt
index a73bc67..6609e9f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -69,11 +69,15 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
         tests/test_nodes.cpp 
         tests/test_macrodown.cpp 
         tests/test_custom_markup.cpp
+        tests/test_md_html_suite.cpp
     )
     target_link_libraries(macrodown_test PRIVATE macrodown_lib GTest::gtest_main)
     target_include_directories(macrodown_test PRIVATE include)
+    target_compile_definitions(
+        macrodown_test PRIVATE MACRODOWN_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
+    )
 
     include(GoogleTest)
     gtest_discover_tests(macrodown_test)
 
-endif()
\ No newline at end of file
+endif()
diff --git a/include/block_parser.h b/include/block_parser.h
index 1c0cae6..8bee5ac 100644
--- a/include/block_parser.h
+++ b/include/block_parser.h
@@ -17,25 +17,21 @@ public:
     static std::unique_ptr<Block> parse(const std::string& input);
 
 private:
-    struct BlockStackItem
-    {
-        Block* block;
-        // Parsing state specific to this block type could go here
-    };
-
     std::unique_ptr<Block> root;
-    std::vector<BlockStackItem> open_blocks;
+    std::vector<Block*> open_blocks;
 
     BlockParser();
-    void process_line(const std::string& line);
-    void close_unmatched_blocks(size_t last_matched_index);
-    void add_text_to_current(const std::string& text);
-
-    // Checkers
-    bool is_container(BlockType type);
+    void processLine(const std::string& line);
+    size_t matchOpenBlocks(const std::string& line, size_t& offset);
+    void openContainers(const std::string& line, size_t& offset);
+    bool continueLeaf(const std::string& line, size_t offset);
+    bool openLeaf(const std::string& line, size_t offset);
+    void appendParagraphText(const std::string& line, size_t offset);
+    void closeBlocks(size_t last_matched_index);
+    Block* appendBlock(Block* parent, BlockType type, bool is_open = true);
     bool matches(Block* block, const std::string& line, size_t& offset);
 };
 
 } // namespace macrodown
 
-#endif // MACRODOWN_BLOCK_PARSER_H
\ No newline at end of file
+#endif // MACRODOWN_BLOCK_PARSER_H
diff --git a/src/block_parser.cpp b/src/block_parser.cpp
index 9c41be6..d9a5855 100644
--- a/src/block_parser.cpp
+++ b/src/block_parser.cpp
@@ -1,8 +1,10 @@
 #include "block_parser.h"
 
 #include <algorithm>
-#include <iostream>
+#include <cctype>
+#include <optional>
 #include <sstream>
+#include <string_view>
 
 namespace macrodown
 {
@@ -10,22 +12,314 @@ namespace macrodown
 namespace
 {
 
-// Helper: check if line is blank (only spaces)
-bool is_blank(const std::string& line)
+class LineCursor
 {
-    return std::all_of(line.begin(), line.end(),
-                       [](unsigned char c) { return std::isspace(c); });
+public:
+    LineCursor(const std::string& line, size_t offset = 0) :
+        line(line), position(offset)
+    {}
+
+    bool blank() const
+    {
+        return std::all_of(
+            line.begin(), line.end(),
+            [](unsigned char character) { return std::isspace(character); });
+    }
+
+    size_t offset() const
+    {
+        return position;
+    }
+
+    size_t indentation() const
+    {
+        size_t count = 0;
+        while(position + count < line.size() &&
+              line[position + count] == ' ')
+        {
+            count++;
+        }
+        return count;
+    }
+
+    bool end(size_t at) const
+    {
+        return at >= line.size();
+    }
+
+    char character(size_t at) const
+    {
+        return line[at];
+    }
+
+    size_t size() const
+    {
+        return line.size();
+    }
+
+    size_t runLength(size_t at, char expected) const
+    {
+        size_t length = 0;
+        while(at + length < line.size() &&
+              line[at + length] == expected)
+        {
+            length++;
+        }
+        return length;
+    }
+
+    size_t skipSpaces(size_t at) const
+    {
+        return at + runLength(at, ' ');
+    }
+
+    std::string_view remaining(size_t at) const
+    {
+        return std::string_view(line).substr(at);
+    }
+
+private:
+    const std::string& line;
+    size_t position;
+};
+
+struct BlockQuoteMarker
+{
+    size_t content_offset;
+};
+
+struct ListMarker
+{
+    char delimiter;
+    bool is_ordered;
+    int start;
+    size_t item_indent;
+    size_t content_offset;
+};
+
+struct ListMarkerPrefix
+{
+    char delimiter;
+    bool is_ordered;
+    int start;
+    size_t length;
+};
+
+struct FenceMarker
+{
+    char character;
+    size_t length;
+    std::string info_string;
+};
+
+struct HeadingMarker
+{
+    size_t level;
+    size_t content_offset;
+};
+
+std::optional<BlockQuoteMarker> parseBlockQuoteMarker(LineCursor cursor)
+{
+    const size_t indent = cursor.indentation();
+    const size_t marker_offset = cursor.offset() + indent;
+    if(indent >= 4 || cursor.end(marker_offset) ||
+       cursor.character(marker_offset) != '>')
+    {
+        return std::nullopt;
+    }
+
+    size_t content_offset = marker_offset + 1;
+    if(!cursor.end(content_offset) && cursor.character(content_offset) == ' ')
+    {
+        content_offset++;
+    }
+    return BlockQuoteMarker{content_offset};
 }
 
-// Helper: count indentation
-size_t count_indent(const std::string& line, size_t offset)
+bool isThematicBreak(LineCursor cursor)
 {
-    size_t count = 0;
-    while(offset + count < line.size() && line[offset + count] == ' ')
+    const size_t indent = cursor.indentation();
+    size_t position = cursor.offset() + indent;
+    if(indent >= 4 || cursor.end(position))
     {
-        count++;
+        return false;
+    }
+
+    const char marker = cursor.character(position);
+    if(marker != '-' && marker != '_' && marker != '*')
+    {
+        return false;
+    }
+
+    size_t marker_count = 0;
+    while(!cursor.end(position))
+    {
+        if(cursor.character(position) == marker)
+        {
+            marker_count++;
+        }
+        else if(cursor.character(position) != ' ')
+        {
+            return false;
+        }
+        position++;
+    }
+    return marker_count >= 3;
+}
+
+std::optional<HeadingMarker> parseAtxHeading(LineCursor cursor)
+{
+    const size_t indent = cursor.indentation();
+    const size_t marker_offset = cursor.offset() + indent;
+    if(indent >= 4 || cursor.end(marker_offset) ||
+       cursor.character(marker_offset) != '#')
+    {
+        return std::nullopt;
+    }
+
+    const size_t level =
+        std::min(cursor.runLength(marker_offset, '#'), size_t{6});
+    if(marker_offset + level < cursor.size() &&
+       cursor.character(marker_offset + level) != ' ')
+    {
+        return std::nullopt;
+    }
+
+    const size_t content_offset = cursor.skipSpaces(marker_offset + level);
+    return HeadingMarker{level, content_offset};
+}
+
+std::optional<ListMarkerPrefix>
+parseListMarkerPrefix(LineCursor cursor, size_t position)
+{
+    const char first = cursor.character(position);
+    if(first == '-' || first == '+' || first == '*')
+    {
+        if(position + 1 < cursor.size() &&
+           cursor.character(position + 1) != ' ')
+        {
+            return std::nullopt;
+        }
+        if(isThematicBreak(cursor))
+        {
+            return std::nullopt;
+        }
+        return ListMarkerPrefix{first, false, 1, 1};
+    }
+
+    if(!std::isdigit(static_cast<unsigned char>(first)))
+    {
+        return std::nullopt;
+    }
+
+    const size_t number_offset = position;
+    int digit_count = 0;
+    int start = 0;
+    while(!cursor.end(position) && digit_count < 9 &&
+          std::isdigit(static_cast<unsigned char>(cursor.character(position))))
+    {
+        start = start * 10 + (cursor.character(position) - '0');
+        position++;
+        digit_count++;
+    }
+    if(digit_count == 0 || cursor.end(position) ||
+       (cursor.character(position) != '.' &&
+        cursor.character(position) != ')'))
+    {
+        return std::nullopt;
     }
-    return count;
+    const char delimiter = cursor.character(position);
+    if(position + 1 < cursor.size() &&
+       cursor.character(position + 1) != ' ')
+    {
+        return std::nullopt;
+    }
+    return ListMarkerPrefix{delimiter, true, start,
+                            position + 1 - number_offset};
+}
+
+std::optional<ListMarker> parseListMarker(LineCursor cursor)
+{
+    const size_t indent = cursor.indentation();
+    const size_t marker_offset = cursor.offset() + indent;
+    if(indent >= 4 || cursor.end(marker_offset))
+    {
+        return std::nullopt;
+    }
+
+    const auto prefix = parseListMarkerPrefix(cursor, marker_offset);
+    if(!prefix)
+    {
+        return std::nullopt;
+    }
+
+    const size_t padding_offset = marker_offset + prefix->length;
+    const size_t spaces = cursor.runLength(padding_offset, ' ');
+
+    size_t item_indent = indent + prefix->length + spaces;
+    size_t content_offset = padding_offset + spaces;
+    if(spaces > 4)
+    {
+        item_indent = indent + prefix->length + 1;
+        content_offset = padding_offset + 1;
+    }
+    else if(spaces == 0 && padding_offset < cursor.size())
+    {
+        item_indent = indent + prefix->length;
+        content_offset = padding_offset;
+    }
+
+    return ListMarker{prefix->delimiter, prefix->is_ordered, prefix->start,
+                      item_indent, content_offset};
+}
+
+std::optional<FenceMarker> parseOpeningFence(LineCursor cursor)
+{
+    const size_t indent = cursor.indentation();
+    const size_t marker_offset = cursor.offset() + indent;
+    if(indent >= 4 || cursor.end(marker_offset))
+    {
+        return std::nullopt;
+    }
+
+    const char marker = cursor.character(marker_offset);
+    if(marker != '`' && marker != '~')
+    {
+        return std::nullopt;
+    }
+
+    const size_t length = cursor.runLength(marker_offset, marker);
+    if(length < 3)
+    {
+        return std::nullopt;
+    }
+
+    const size_t info_offset = cursor.skipSpaces(marker_offset + length);
+    std::string info_string(cursor.remaining(info_offset));
+    if(marker == '`' && info_string.find('`') != std::string::npos)
+    {
+        return std::nullopt;
+    }
+    return FenceMarker{marker, length, std::move(info_string)};
+}
+
+bool isClosingFence(LineCursor cursor, const Block& block)
+{
+    const size_t indent = cursor.indentation();
+    size_t position = cursor.offset() + indent;
+    if(indent >= 4)
+    {
+        return false;
+    }
+
+    const size_t length = cursor.runLength(position, block.fence_char);
+    if(length < block.fence_length)
+    {
+        return false;
+    }
+
+    position = cursor.skipSpaces(position + length);
+    return cursor.end(position);
 }
 
 } // namespace
@@ -33,7 +327,7 @@ size_t count_indent(const std::string& line, size_t offset)
 BlockParser::BlockParser()
 {
     root = std::make_unique<Block>(BlockType::Document);
-    open_blocks.push_back({root.get()});
+    open_blocks.push_back(root.get());
 }
 
 std::unique_ptr<Block> BlockParser::parse(const std::string& input)
@@ -49,11 +343,11 @@ std::unique_ptr<Block> BlockParser::parse(const std::string& input)
         {
             line.pop_back();
         }
-        parser.process_line(line);
+        parser.processLine(line);
     }
 
     // Close all remaining blocks
-    parser.close_unmatched_blocks(
+    parser.closeBlocks(
         0); // 0 means keep only root, but since we are done, close everything
     // Actually we don't "close" root in the stack sense, but we mark open=false
     parser.root->open = false;
@@ -61,35 +355,22 @@ std::unique_ptr<Block> BlockParser::parse(const std::string& input)
     return std::move(parser.root);
 }
 
-bool BlockParser::is_container(BlockType type)
-{
-    return type == BlockType::Document || type == BlockType::Quote ||
-           type == BlockType::List || type == BlockType::ListItem;
-}
-
-// Determines if an open block matches the current line
-// Updates offset to consume markers
 bool BlockParser::matches(Block* block, const std::string& line, size_t& offset)
 {
+    LineCursor cursor(line, offset);
+
     if(block->type == BlockType::Document)
     {
-        return true; // Document always matches
+        return true;
     }
 
     if(block->type == BlockType::Quote)
     {
-        size_t indent = count_indent(line, offset);
-        if(indent < 4)
+        const auto marker = parseBlockQuoteMarker(cursor);
+        if(marker)
         {
-            if(offset + indent < line.size() && line[offset + indent] == '>')
-            {
-                offset += indent + 1; // Consume indent and '>'
-                if(offset < line.size() && line[offset] == ' ')
-                {
-                    offset++; // Optional space
-                }
-                return true;
-            }
+            offset = marker->content_offset;
+            return true;
         }
         return false;
     }
@@ -101,11 +382,11 @@ bool BlockParser::matches(Block* block, const std::string& line, size_t& offset)
 
     if(block->type == BlockType::ListItem)
     {
-        if(is_blank(line))
+        if(cursor.blank())
         {
             return true;
         }
-        size_t indent = count_indent(line, offset);
+        const size_t indent = cursor.indentation();
         if(indent >= block->indent)
         {
             offset += block->indent;
@@ -116,65 +397,14 @@ bool BlockParser::matches(Block* block, const std::string& line, size_t& offset)
 
     if(block->type == BlockType::Paragraph)
     {
-        if(is_blank(line))
+        if(cursor.blank())
         {
             return false;
         }
-
-        // Paragraphs match unless interrupted by a new block type
-        size_t indent = count_indent(line, offset);
-        if(indent < 4)
+        if(parseBlockQuoteMarker(cursor) || isThematicBreak(cursor) ||
+           parseAtxHeading(cursor))
         {
-            // Check for BlockQuote
-            if(offset + indent < line.size() && line[offset + indent] == '>')
-            {
-                return false;
-            }
-
-            // Check for ThematicBreak
-            size_t check_pos = offset + indent;
-            if(check_pos < line.size())
-            {
-                char c = line[check_pos];
-                if(c == '-' || c == '_' || c == '*')
-                {
-                    int count = 0;
-                    size_t p = check_pos;
-                    while(p < line.size())
-                    {
-                        if(line[p] == c)
-                        {
-                            count++;
-                        }
-                        else if(line[p] != ' ')
-                        {
-                            break;
-                        }
-                        p++;
-                    }
-                    if(count >= 3 && p == line.size())
-                    {
-                        return false;
-                    }
-                }
-            }
-
-            // Check for ATX Heading
-            check_pos = offset + indent;
-            if(check_pos < line.size() && line[check_pos] == '#')
-            {
-                size_t hash_count = 0;
-                while(check_pos + hash_count < line.size() &&
-                      line[check_pos + hash_count] == '#' && hash_count < 6)
-                {
-                    hash_count++;
-                }
-                if(hash_count > 0 && (check_pos + hash_count == line.size() ||
-                                      line[check_pos + hash_count] == ' '))
-                {
-                    return false;
-                }
-            }
+            return false;
         }
         return true;
     }
@@ -186,13 +416,13 @@ bool BlockParser::matches(Block* block, const std::string& line, size_t& offset)
 
     if(block->type == BlockType::IndentedCode)
     {
-        size_t indent = count_indent(line, offset);
+        const size_t indent = cursor.indentation();
         if(indent >= 4)
         {
             offset += 4;
             return true;
         }
-        else if(is_blank(line))
+        if(cursor.blank())
         {
             return true;
         }
@@ -202,216 +432,87 @@ bool BlockParser::matches(Block* block, const std::string& line, size_t& offset)
     return false;
 }
 
-void BlockParser::process_line(const std::string& line)
+size_t BlockParser::matchOpenBlocks(const std::string& line, size_t& offset)
 {
-    size_t offset = 0;
-    size_t matches_count = 0;
-
-    // 1. Find matches in open blocks
-    matches_count = 0; // Root always matches
+    size_t last_matched_index = 0;
     for(size_t i = 1; i < open_blocks.size(); ++i)
     {
-        if(matches(open_blocks[i].block, line, offset))
+        if(matches(open_blocks[i], line, offset))
         {
-            matches_count = i;
+            last_matched_index = i;
         }
         else
         {
             break;
         }
     }
+    return last_matched_index;
+}
 
-    // 2. Close unmatched blocks
-    close_unmatched_blocks(matches_count);
-
-    // 3. Open new blocks
+void BlockParser::openContainers(const std::string& line, size_t& offset)
+{
     while(true)
     {
-        Block* tip = open_blocks.back().block;
+        Block* tip = open_blocks.back();
         if(tip->type == BlockType::FencedCode ||
            tip->type == BlockType::IndentedCode)
         {
             break;
         }
 
-        size_t indent = count_indent(line, offset);
-
-        // Check for BlockQuote
-        if(indent < 4 && offset + indent < line.size() &&
-           line[offset + indent] == '>')
+        LineCursor cursor(line, offset);
+        if(const auto quote = parseBlockQuoteMarker(cursor))
         {
-            offset += indent + 1;
-            if(offset < line.size() && line[offset] == ' ')
-            {
-                offset++;
-            }
-
-            auto new_block = std::make_unique<Block>(BlockType::Quote);
-            Block* ptr = new_block.get();
-            open_blocks.back().block->children.push_back(std::move(new_block));
-            open_blocks.push_back({ptr});
+            offset = quote->content_offset;
+            appendBlock(tip, BlockType::Quote);
             continue;
         }
 
-        // Check for List Items
-        bool is_list_item = false;
-        char list_char = 0;
-        bool is_ordered = false;
-        int list_start = 1;
-        size_t marker_length = 0;
+        const auto marker = parseListMarker(cursor);
+        if(!marker)
+        {
+            break;
+        }
 
-        if(indent < 4)
+        offset = marker->content_offset;
+        tip = open_blocks.back();
+        if(tip->type == BlockType::Paragraph)
         {
-            size_t p = offset + indent;
-            if(p < line.size())
-            {
-                char c = line[p];
-                if(c == '-' || c == '+' || c == '*')
-                {
-                    if(p + 1 == line.size() || line[p + 1] == ' ')
-                    {
-                        // Avoid thematic break collision (like ---)
-                        int count = 0;
-                        size_t tb_p = p;
-                        while(tb_p < line.size())
-                        {
-                            if(line[tb_p] == c)
-                            {
-                                count++;
-                            }
-                            else if(line[tb_p] != ' ')
-                            {
-                                break;
-                            }
-                            tb_p++;
-                        }
-                        if(count < 3 || tb_p != line.size())
-                        {
-                            is_list_item = true;
-                            list_char = c;
-                            is_ordered = false;
-                            marker_length = 1;
-                        }
-                    }
-                }
-                else if(std::isdigit(c))
-                {
-                    size_t start_p = p;
-                    int num = 0;
-                    int digits = 0;
-                    while(p < line.size() && std::isdigit(line[p]) &&
-                          digits < 9)
-                    {
-                        num = num * 10 + (line[p] - '0');
-                        p++;
-                        digits++;
-                    }
-                    if(digits > 0 && p < line.size() &&
-                       (line[p] == '.' || line[p] == ')'))
-                    {
-                        char delim = line[p];
-                        if(p + 1 == line.size() || line[p + 1] == ' ')
-                        {
-                            is_list_item = true;
-                            list_char = delim;
-                            is_ordered = true;
-                            list_start = num;
-                            marker_length = (p + 1) - start_p;
-                        }
-                    }
-                }
-            }
+            closeBlocks(open_blocks.size() - 2);
+            tip = open_blocks.back();
         }
 
-        if(is_list_item)
+        if(tip->type != BlockType::List ||
+           tip->list_char != marker->delimiter ||
+           tip->is_ordered != marker->is_ordered)
         {
-            size_t p = offset + indent + marker_length;
-            size_t spaces = 0;
-            while(p + spaces < line.size() && line[p + spaces] == ' ')
-            {
-                spaces++;
-            }
-            size_t item_indent = indent + marker_length + spaces;
-            if(spaces > 4)
-            {
-                item_indent = indent + marker_length + 1;
-                offset = offset + indent + marker_length + 1;
-            }
-            else if(spaces == 0 && p < line.size())
-            {
-                item_indent = indent + marker_length; // blank line
-                offset = p;
-            }
-            else
-            {
-                offset = p + spaces;
-            }
-
-            Block* tip = open_blocks.back().block;
-            if(tip->type == BlockType::Paragraph)
-            {
-                close_unmatched_blocks(open_blocks.size() - 2);
-                tip = open_blocks.back().block;
-            }
-
-            if(tip->type != BlockType::List || tip->list_char != list_char ||
-               tip->is_ordered != is_ordered)
-            {
-                auto list_block = std::make_unique<Block>(BlockType::List);
-                list_block->list_char = list_char;
-                list_block->is_ordered = is_ordered;
-                list_block->list_start = list_start;
-                Block* l_ptr = list_block.get();
-                tip->children.push_back(std::move(list_block));
-                open_blocks.push_back({l_ptr});
-                tip = l_ptr;
-            }
-
-            auto li_block = std::make_unique<Block>(BlockType::ListItem);
-            li_block->indent = item_indent;
-            Block* li_ptr = li_block.get();
-            tip->children.push_back(std::move(li_block));
-            open_blocks.push_back({li_ptr});
-            continue;
+            tip = appendBlock(tip, BlockType::List);
+            tip->list_char = marker->delimiter;
+            tip->is_ordered = marker->is_ordered;
+            tip->list_start = marker->start;
         }
 
-        break;
+        Block* item = appendBlock(tip, BlockType::ListItem);
+        item->indent = marker->item_indent;
     }
+}
 
-    Block* tip = open_blocks.back().block;
-
+bool BlockParser::continueLeaf(const std::string& line, size_t offset)
+{
+    Block* tip = open_blocks.back();
     if(tip->type == BlockType::FencedCode)
     {
-        size_t indent = count_indent(line, offset);
-        if(indent < 4)
+        if(isClosingFence(LineCursor(line, offset), *tip))
         {
-            size_t check_pos = offset + indent;
-            size_t fence_len = 0;
-            while(check_pos + fence_len < line.size() &&
-                  line[check_pos + fence_len] == tip->fence_char)
-            {
-                fence_len++;
-            }
-            if(fence_len >= tip->fence_length)
-            {
-                size_t trail = check_pos + fence_len;
-                while(trail < line.size() && line[trail] == ' ')
-                {
-                    trail++;
-                }
-                if(trail == line.size())
-                {
-                    close_unmatched_blocks(open_blocks.size() - 2);
-                    return;
-                }
-            }
+            closeBlocks(open_blocks.size() - 2);
+            return true;
         }
         if(!tip->literal_content.empty())
         {
             tip->literal_content += "\n";
         }
         tip->literal_content += line.substr(offset);
-        return;
+        return true;
     }
 
     if(tip->type == BlockType::IndentedCode)
@@ -421,204 +522,131 @@ void BlockParser::process_line(const std::string& line)
             tip->literal_content += "\n";
         }
         tip->literal_content += line.substr(offset);
-        return;
+        return true;
     }
+    return false;
+}
 
-    // 4. Handle Leaf Blocks
-    size_t indent = count_indent(line, offset);
+bool BlockParser::openLeaf(const std::string& line, size_t offset)
+{
+    LineCursor cursor(line, offset);
+    Block* tip = open_blocks.back();
 
-    // Check for Thematic Break
-    if(indent < 4)
+    if(isThematicBreak(cursor))
     {
-        size_t check_pos = offset + indent;
-        if(check_pos < line.size())
+        if(tip->type == BlockType::Paragraph)
         {
-            char c = line[check_pos];
-            if(c == '-' || c == '_' || c == '*')
-            {
-                int count = 0;
-                size_t p = check_pos;
-                while(p < line.size())
-                {
-                    if(line[p] == c)
-                    {
-                        count++;
-                    }
-                    else if(line[p] != ' ')
-                    {
-                        break;
-                    }
-                    p++;
-                }
-                if(count >= 3 && p == line.size())
-                {
-                    if(tip->type == BlockType::Paragraph)
-                    {
-                        close_unmatched_blocks(open_blocks.size() - 2);
-                        tip = open_blocks.back().block;
-                    }
-                    auto hr = std::make_unique<Block>(BlockType::ThematicBreak);
-                    hr->open = false;
-                    tip->children.push_back(std::move(hr));
-                    return;
-                }
-            }
+            closeBlocks(open_blocks.size() - 2);
+            tip = open_blocks.back();
         }
+        appendBlock(tip, BlockType::ThematicBreak, false);
+        return true;
     }
 
-    // Check for FencedCode Opening
-    if(indent < 4)
+    if(const auto fence = parseOpeningFence(cursor))
     {
-        size_t check_pos = offset + indent;
-        if(check_pos < line.size() &&
-           (line[check_pos] == '`' || line[check_pos] == '~'))
+        if(tip->type == BlockType::Paragraph)
         {
-            char fence_char = line[check_pos];
-            size_t fence_len = 0;
-            while(check_pos + fence_len < line.size() &&
-                  line[check_pos + fence_len] == fence_char)
-            {
-                fence_len++;
-            }
-
-            if(fence_len >= 3)
-            {
-                size_t info_start = check_pos + fence_len;
-                while(info_start < line.size() && line[info_start] == ' ')
-                {
-                    info_start++;
-                }
-                std::string info_string = line.substr(info_start);
-                bool valid = true;
-                if(fence_char == '`' &&
-                   info_string.find('`') != std::string::npos)
-                {
-                    valid = false;
-                }
-
-                if(valid)
-                {
-                    if(tip->type == BlockType::Paragraph)
-                    {
-                        close_unmatched_blocks(open_blocks.size() - 2);
-                        tip = open_blocks.back().block;
-                    }
-
-                    auto code_block =
-                        std::make_unique<Block>(BlockType::FencedCode);
-                    code_block->fence_char = fence_char;
-                    code_block->fence_length = fence_len;
-                    code_block->info_string = info_string;
-
-                    Block* ptr = code_block.get();
-                    tip->children.push_back(std::move(code_block));
-                    open_blocks.push_back({ptr});
-                    return;
-                }
-            }
+            closeBlocks(open_blocks.size() - 2);
+            tip = open_blocks.back();
         }
+
+        Block* block = appendBlock(tip, BlockType::FencedCode);
+        block->fence_char = fence->character;
+        block->fence_length = fence->length;
+        block->info_string = fence->info_string;
+        return true;
     }
 
-    // Check for IndentedCode Opening
-    if(indent >= 4)
+    if(cursor.indentation() >= 4 && tip->type != BlockType::Paragraph &&
+       !cursor.blank())
     {
-        if(tip->type != BlockType::Paragraph && !is_blank(line))
-        {
-            auto code_block = std::make_unique<Block>(BlockType::IndentedCode);
-            code_block->literal_content = line.substr(offset + 4);
-            Block* ptr = code_block.get();
-            tip->children.push_back(std::move(code_block));
-            open_blocks.push_back({ptr});
-            return;
-        }
+        Block* block = appendBlock(tip, BlockType::IndentedCode);
+        block->literal_content = line.substr(offset + 4);
+        return true;
     }
 
-    // Check for ATX Heading
-    if(indent < 4)
+    if(const auto heading = parseAtxHeading(cursor))
     {
-        size_t check_pos = offset + indent;
-        size_t hash_count = 0;
-        while(check_pos + hash_count < line.size() &&
-              line[check_pos + hash_count] == '#' && hash_count < 6)
+        if(tip->type == BlockType::Paragraph)
         {
-            hash_count++;
+            closeBlocks(open_blocks.size() - 2);
+            tip = open_blocks.back();
         }
 
-        if(hash_count > 0 && (check_pos + hash_count == line.size() ||
-                              line[check_pos + hash_count] == ' '))
-        {
-            if(tip->type == BlockType::Paragraph)
-            {
-                close_unmatched_blocks(open_blocks.size() - 2);
-                tip = open_blocks.back().block;
-            }
-
-            auto heading = std::make_unique<Block>(BlockType::Heading);
-            heading->level = hash_count;
-            size_t content_start = check_pos + hash_count;
-            while(content_start < line.size() && line[content_start] == ' ')
-            {
-                content_start++;
-            }
-            heading->literal_content = line.substr(content_start);
-            heading->open = false;
-
-            tip->children.push_back(std::move(heading));
-            return;
-        }
+        Block* block = appendBlock(tip, BlockType::Heading, false);
+        block->level = heading->level;
+        block->literal_content = line.substr(heading->content_offset);
+        return true;
     }
 
-    // Paragraph continuation check
-    if(tip->type == BlockType::Paragraph)
+    if(tip->type == BlockType::Paragraph && cursor.blank())
     {
-        if(is_blank(line))
-        {
-            close_unmatched_blocks(open_blocks.size() - 2); // Close paragraph
-            return;
-        }
+        closeBlocks(open_blocks.size() - 2);
+        return true;
     }
 
-    // 5. Finalize: Text or Paragraph
-    if(is_blank(line))
-    {
-        return;
-    }
+    return cursor.blank();
+}
 
-    // If the current tip is a List, we must close it because a List can only
-    // contain ListItems. A paragraph encountered here indicates the list has
-    // ended.
+void BlockParser::appendParagraphText(const std::string& line, size_t offset)
+{
+    Block* tip = open_blocks.back();
+    LineCursor cursor(line, offset);
     if(tip->type == BlockType::List)
     {
-        close_unmatched_blocks(open_blocks.size() - 2);
-        tip = open_blocks.back().block;
+        closeBlocks(open_blocks.size() - 2);
+        tip = open_blocks.back();
     }
 
+    const size_t content_offset = offset + cursor.indentation();
     if(tip->type == BlockType::Document || tip->type == BlockType::Quote ||
        tip->type == BlockType::ListItem)
     {
-        auto p = std::make_unique<Block>(BlockType::Paragraph);
-        Block* p_ptr = p.get();
-        tip->children.push_back(std::move(p));
-        open_blocks.push_back({p_ptr});
-
-        size_t content_start = offset + count_indent(line, offset);
-        p_ptr->literal_content = line.substr(content_start);
+        Block* paragraph = appendBlock(tip, BlockType::Paragraph);
+        paragraph->literal_content = line.substr(content_offset);
     }
     else if(tip->type == BlockType::Paragraph)
     {
-        size_t content_start = offset + count_indent(line, offset);
-        tip->literal_content += "\n" + line.substr(content_start);
+        tip->literal_content += "\n" + line.substr(content_offset);
+    }
+}
+
+void BlockParser::processLine(const std::string& line)
+{
+    size_t offset = 0;
+    const size_t last_matched_index = matchOpenBlocks(line, offset);
+    closeBlocks(last_matched_index);
+    openContainers(line, offset);
+
+    if(continueLeaf(line, offset) || openLeaf(line, offset))
+    {
+        return;
+    }
+    appendParagraphText(line, offset);
+}
+
+Block* BlockParser::appendBlock(Block* parent, BlockType type, bool is_open)
+{
+    auto block = std::make_unique<Block>(type);
+    block->open = is_open;
+    Block* pointer = block.get();
+    parent->children.push_back(std::move(block));
+    if(is_open)
+    {
+        open_blocks.push_back(pointer);
     }
+    return pointer;
 }
 
-void BlockParser::close_unmatched_blocks(size_t last_matched_index)
+void BlockParser::closeBlocks(size_t last_matched_index)
 {
     while(open_blocks.size() > last_matched_index + 1)
     {
-        Block* block = open_blocks.back().block;
+        Block* block = open_blocks.back();
         block->open = false;
         open_blocks.pop_back();
     }
 }
 
-} // namespace macrodown
\ No newline at end of file
+} // namespace macrodown