/* * This project is licensed under the MIT license. For more information see the * LICENSE file. */ #pragma once // ----------------------------------------------------------------------------- #include #include #include #include "maddy/blockparser.h" // ----------------------------------------------------------------------------- namespace maddy { // ----------------------------------------------------------------------------- /** * ChecklistParser * * @class */ class ChecklistParser : public BlockParser { public: /** * ctor * * @method * @param {std::function} parseLineCallback * @param {std::function(const std::string& line)>} getBlockParserForLineCallback */ ChecklistParser( std::function parseLineCallback, std::function(const std::string& line)> getBlockParserForLineCallback ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) , isStarted(false) , isFinished(false) {} /** * IsStartingLine * * An unordered list starts with `* `. * * @method * @param {const std::string&} line * @return {bool} */ static bool IsStartingLine(const std::string& line) { static std::regex re("^- \\[[x| ]\\] .*"); return std::regex_match(line, re); } /** * IsFinished * * @method * @return {bool} */ bool IsFinished() const override { return this->isFinished; } protected: bool isInlineBlockAllowed() const override { return true; } bool isLineParserAllowed() const override { return true; } void parseBlock(std::string& line) override { bool isStartOfNewListItem = IsStartingLine(line); uint32_t indentation = getIndentationWidth(line); static std::regex lineRegex("^(- )"); line = std::regex_replace(line, lineRegex, ""); static std::regex emptyBoxRegex("^\\[ \\]"); static std::string emptyBoxReplacement = ""; line = std::regex_replace(line, emptyBoxRegex, emptyBoxReplacement); static std::regex boxRegex("^\\[x\\]"); static std::string boxReplacement = ""; line = std::regex_replace(line, boxRegex, boxReplacement); if (!this->isStarted) { line = "
") != std::string::npos ) { line = "" + line; this->isFinished = true; return; } if (isStartOfNewListItem) { line = "