diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..412fcbd --- /dev/null +++ b/.clang-format @@ -0,0 +1,46 @@ +BasedOnStyle: Google +AccessModifierOffset: -2 +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeComma +ConstructorInitializerIndentWidth: 2 +BracedInitializerIndentWidth: 2 +ContinuationIndentWidth: 2 +Cpp11BracedListStyle: true +IndentAccessModifiers: false +InsertNewlineAtEOF: true +LambdaBodyIndentation: Signature +AlignAfterOpenBracket: BlockIndent +AllowShortIfStatementsOnASingleLine: Never +BinPackArguments: false +BinPackParameters: false +AllowShortLambdasOnASingleLine: All +SpacesBeforeTrailingComments: 1 +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: true + AfterClass: true + AfterControlStatement: Always + AfterEnum: true + AfterFunction: true + AfterNamespace: false + AfterStruct: true + AfterUnion: true + AfterExternBlock: false + BeforeCatch: true + BeforeElse: true + BeforeLambdaBody: true + IndentBraces: false + BeforeWhile: true + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false +IncludeCategories: + - Regex: '^<.*>' + Priority: 1 + - Regex: '^"maddy/.*' + Priority: 3 + - Regex: '.*' + Priority: 2 +SortIncludes: true +IncludeBlocks: Regroup +InsertTrailingCommas: Wrapped diff --git a/.editorconfig b/.editorconfig index 9035bef..5f79c80 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,5 +8,8 @@ indent_style = space insert_final_newline = true trim_trailing_whitespace = true -[*.{h,hh,hpp,c,cc,cpp,cxx,yml}] +[*.{h,hh,hpp,c,cc,cpp,cxx,yml,py,md}] +indent_size = 2 + +[CMakeLists.txt] indent_size = 2 diff --git a/.github/workflows/run-checks.yml b/.github/workflows/run-checks.yml new file mode 100644 index 0000000..b5749d6 --- /dev/null +++ b/.github/workflows/run-checks.yml @@ -0,0 +1,25 @@ +name: run-checks + +on: + push: + branches: + - master + pull_request: + +jobs: + run-clang-format: + runs-on: ubuntu-24.04 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.8' + + - name: Run format script with dry_run + run: | + clang-format --version + python tools/format.py dry_run diff --git a/CHANGELOG.md b/CHANGELOG.md index 11098cc..619f4ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ maddy uses [semver versioning](https://semver.org/). ## Upcoming * ![**CHANGED**](https://img.shields.io/badge/-CHANGED-%23e90) Updated google test to v1.15.0. +* ![**ADDED**](https://img.shields.io/badge/-ADDED-%23099) clang-format ## version 1.3.0 2023-08-26 diff --git a/include/maddy/blockparser.h b/include/maddy/blockparser.h index 9df4fa6..f93dd2c 100644 --- a/include/maddy/blockparser.h +++ b/include/maddy/blockparser.h @@ -10,8 +10,8 @@ #include #include // windows compatibility includes -#include #include +#include // ----------------------------------------------------------------------------- @@ -36,11 +36,13 @@ class BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ BlockParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback + std::function(const std::string& line)> + getBlockParserForLineCallback ) : result("", std::ios_base::ate | std::ios_base::in | std::ios_base::out) , childParser(nullptr) @@ -64,8 +66,7 @@ class BlockParser * @param {std::string&} line * @return {void} */ - virtual void - AddLine(std::string& line) + virtual void AddLine(std::string& line) { this->parseBlock(line); @@ -113,11 +114,7 @@ class BlockParser * @method * @return {std::stringstream} */ - std::stringstream& - GetResult() - { - return this->result; - } + std::stringstream& GetResult() { return this->result; } /** * Clear @@ -129,11 +126,7 @@ class BlockParser * @method * @return {void} */ - void - Clear() - { - this->result.str(""); - } + void Clear() { this->result.str(""); } protected: std::stringstream result; @@ -143,8 +136,7 @@ class BlockParser virtual bool isLineParserAllowed() const = 0; virtual void parseBlock(std::string& line) = 0; - void - parseLine(std::string& line) + void parseLine(std::string& line) { if (parseLineCallback) { @@ -152,38 +144,34 @@ class BlockParser } } - uint32_t - getIndentationWidth(const std::string& line) const + uint32_t getIndentationWidth(const std::string& line) const { bool hasMetNonSpace = false; - uint32_t indentation = static_cast( - std::count_if( - line.begin(), - line.end(), - [&hasMetNonSpace](unsigned char c) + uint32_t indentation = static_cast(std::count_if( + line.begin(), + line.end(), + [&hasMetNonSpace](unsigned char c) + { + if (hasMetNonSpace) { - if (hasMetNonSpace) - { - return false; - } - - if (std::isspace(c)) - { - return true; - } - - hasMetNonSpace = true; return false; } - ) - ); + + if (std::isspace(c)) + { + return true; + } + + hasMetNonSpace = true; + return false; + } + )); return indentation; } - std::shared_ptr - getBlockParserForLine(const std::string& line) + std::shared_ptr getBlockParserForLine(const std::string& line) { if (getBlockParserForLineCallback) { @@ -195,7 +183,8 @@ class BlockParser private: std::function parseLineCallback; - std::function(const std::string& line)> getBlockParserForLineCallback; + std::function(const std::string& line)> + getBlockParserForLineCallback; }; // class BlockParser // ----------------------------------------------------------------------------- diff --git a/include/maddy/breaklineparser.h b/include/maddy/breaklineparser.h index e3ee6fc..e60dda2 100644 --- a/include/maddy/breaklineparser.h +++ b/include/maddy/breaklineparser.h @@ -6,8 +6,8 @@ // ----------------------------------------------------------------------------- -#include #include +#include #include "maddy/lineparser.h" @@ -36,8 +36,7 @@ class BreakLineParser : public LineParser * @param {std::string&} line The line to interpret * @return {void} */ - void - Parse(std::string& line) override + void Parse(std::string& line) override { static std::regex re(R"((\r\n|\r))"); static std::string replacement = "
"; diff --git a/include/maddy/checklistparser.h b/include/maddy/checklistparser.h index dab5ffc..36a154b 100644 --- a/include/maddy/checklistparser.h +++ b/include/maddy/checklistparser.h @@ -31,11 +31,13 @@ class ChecklistParser : public BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ - ChecklistParser( + ChecklistParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback + std::function(const std::string& line)> + getBlockParserForLineCallback ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) , isStarted(false) @@ -51,8 +53,7 @@ class ChecklistParser : public BlockParser * @param {const std::string&} line * @return {bool} */ - static bool - IsStartingLine(const std::string& line) + static bool IsStartingLine(const std::string& line) { static std::regex re(R"(^- \[[x| ]\] .*)"); return std::regex_match(line, re); @@ -64,27 +65,14 @@ class ChecklistParser : public BlockParser * @method * @return {bool} */ - bool - IsFinished() const override - { - return this->isFinished; - } + bool IsFinished() const override { return this->isFinished; } protected: - bool - isInlineBlockAllowed() const override - { - return true; - } + bool isInlineBlockAllowed() const override { return true; } - bool - isLineParserAllowed() const override - { - return true; - } + bool isLineParserAllowed() const override { return true; } - void - parseBlock(std::string& line) override + void parseBlock(std::string& line) override { bool isStartOfNewListItem = IsStartingLine(line); uint32_t indentation = getIndentationWidth(line); @@ -97,7 +85,8 @@ class ChecklistParser : public BlockParser line = std::regex_replace(line, emptyBoxRegex, emptyBoxReplacement); static std::regex boxRegex(R"(^\[x\])"); - static std::string boxReplacement = ""; + static std::string boxReplacement = + ""; line = std::regex_replace(line, boxRegex, boxReplacement); if (!this->isStarted) @@ -113,11 +102,9 @@ class ChecklistParser : public BlockParser return; } - if ( - line.empty() || - line.find("
  • ") != std::string::npos - ) + if (line.empty() || + line.find("
  • ") != std::string::npos) { line = "" + line; this->isFinished = true; diff --git a/include/maddy/codeblockparser.h b/include/maddy/codeblockparser.h index 9f69db9..f0fbb29 100644 --- a/include/maddy/codeblockparser.h +++ b/include/maddy/codeblockparser.h @@ -7,8 +7,8 @@ // ----------------------------------------------------------------------------- #include -#include #include +#include #include "maddy/blockparser.h" @@ -47,11 +47,13 @@ class CodeBlockParser : public BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ - CodeBlockParser( + CodeBlockParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback + std::function(const std::string& line)> + getBlockParserForLineCallback ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) , isStarted(false) @@ -71,8 +73,7 @@ class CodeBlockParser : public BlockParser * @param {const std::string&} line * @return {bool} */ - static bool - IsStartingLine(const std::string& line) + static bool IsStartingLine(const std::string& line) { static std::regex re("^(?:`){3}(.*)$"); return std::regex_match(line, re); @@ -84,27 +85,14 @@ class CodeBlockParser : public BlockParser * @method * @return {bool} */ - bool - IsFinished() const override - { - return this->isFinished; - } + bool IsFinished() const override { return this->isFinished; } protected: - bool - isInlineBlockAllowed() const override - { - return false; - } + bool isInlineBlockAllowed() const override { return false; } - bool - isLineParserAllowed() const override - { - return false; - } + bool isLineParserAllowed() const override { return false; } - void - parseBlock(std::string& line) override + void parseBlock(std::string& line) override { if (line == "```") { diff --git a/include/maddy/emphasizedparser.h b/include/maddy/emphasizedparser.h index c1b21ed..1705e6f 100644 --- a/include/maddy/emphasizedparser.h +++ b/include/maddy/emphasizedparser.h @@ -6,8 +6,8 @@ // ----------------------------------------------------------------------------- -#include #include +#include #include "maddy/lineparser.h" @@ -38,10 +38,11 @@ class EmphasizedParser : public LineParser * @param {std::string&} line The line to interpret * @return {void} */ - void - Parse(std::string& line) override + void Parse(std::string& line) override { - static std::regex re(R"((?!.*`.*|.*.*)_(?!.*`.*|.*<\/code>.*)([^_]*)_(?!.*`.*|.*<\/code>.*))"); + static std::regex re( + R"((?!.*`.*|.*.*)_(?!.*`.*|.*<\/code>.*)([^_]*)_(?!.*`.*|.*<\/code>.*))" + ); static std::string replacement = "$1"; line = std::regex_replace(line, re, replacement); diff --git a/include/maddy/headlineparser.h b/include/maddy/headlineparser.h index 7b25d55..7c516fc 100644 --- a/include/maddy/headlineparser.h +++ b/include/maddy/headlineparser.h @@ -7,8 +7,8 @@ // ----------------------------------------------------------------------------- #include -#include #include +#include #include "maddy/blockparser.h" @@ -53,11 +53,13 @@ class HeadlineParser : public BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ HeadlineParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback, + std::function(const std::string& line)> + getBlockParserForLineCallback, bool isInlineParserAllowed = true ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) @@ -73,8 +75,7 @@ class HeadlineParser : public BlockParser * @param {const std::string&} line * @return {bool} */ - static bool - IsStartingLine(const std::string& line) + static bool IsStartingLine(const std::string& line) { static std::regex re("^(?:#){1,6} (.*)"); return std::regex_match(line, re); @@ -89,43 +90,33 @@ class HeadlineParser : public BlockParser * @method * @return {bool} */ - bool - IsFinished() const override - { - return true; - } + bool IsFinished() const override { return true; } protected: - bool - isInlineBlockAllowed() const override - { - return false; - } + bool isInlineBlockAllowed() const override { return false; } - bool - isLineParserAllowed() const override + bool isLineParserAllowed() const override { return this->isInlineParserAllowed; } - void - parseBlock(std::string& line) override + void parseBlock(std::string& line) override { static std::vector hlRegex = { - std::regex("^# (.*)") - , std::regex("^(?:#){2} (.*)") - , std::regex("^(?:#){3} (.*)") - , std::regex("^(?:#){4} (.*)") - , std::regex("^(?:#){5} (.*)") - , std::regex("^(?:#){6} (.*)") + std::regex("^# (.*)"), + std::regex("^(?:#){2} (.*)"), + std::regex("^(?:#){3} (.*)"), + std::regex("^(?:#){4} (.*)"), + std::regex("^(?:#){5} (.*)"), + std::regex("^(?:#){6} (.*)") }; static std::vector hlReplacement = { - "

    $1

    " - , "

    $1

    " - , "

    $1

    " - , "

    $1

    " - , "
    $1
    " - , "
    $1
    " + "

    $1

    ", + "

    $1

    ", + "

    $1

    ", + "

    $1

    ", + "
    $1
    ", + "
    $1
    " }; for (uint8_t i = 0; i < 6; ++i) diff --git a/include/maddy/horizontallineparser.h b/include/maddy/horizontallineparser.h index add6bf1..ae06773 100644 --- a/include/maddy/horizontallineparser.h +++ b/include/maddy/horizontallineparser.h @@ -7,8 +7,8 @@ // ----------------------------------------------------------------------------- #include -#include #include +#include #include "maddy/blockparser.h" @@ -35,11 +35,13 @@ class HorizontalLineParser : public BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ HorizontalLineParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback + std::function(const std::string& line)> + getBlockParserForLineCallback ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) , lineRegex("^---$") @@ -54,8 +56,7 @@ class HorizontalLineParser : public BlockParser * @param {const std::string&} line * @return {bool} */ - static bool - IsStartingLine(const std::string& line) + static bool IsStartingLine(const std::string& line) { static std::regex re("^---$"); return std::regex_match(line, re); @@ -70,27 +71,14 @@ class HorizontalLineParser : public BlockParser * @method * @return {bool} */ - bool - IsFinished() const override - { - return true; - } + bool IsFinished() const override { return true; } protected: - bool - isInlineBlockAllowed() const override - { - return false; - } + bool isInlineBlockAllowed() const override { return false; } - bool - isLineParserAllowed() const override - { - return false; - } + bool isLineParserAllowed() const override { return false; } - void - parseBlock(std::string& line) override + void parseBlock(std::string& line) override { static std::string replacement = "
    "; diff --git a/include/maddy/htmlparser.h b/include/maddy/htmlparser.h index b6845a7..8518de9 100644 --- a/include/maddy/htmlparser.h +++ b/include/maddy/htmlparser.h @@ -30,11 +30,13 @@ class HtmlParser : public BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ - HtmlParser( + HtmlParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback + std::function(const std::string& line)> + getBlockParserForLineCallback ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) , isStarted(false) @@ -52,11 +54,7 @@ class HtmlParser : public BlockParser * @param {const std::string&} line * @return {bool} */ - static bool - IsStartingLine(const std::string& line) - { - return line[0] == '<'; - } + static bool IsStartingLine(const std::string& line) { return line[0] == '<'; } /** * IsFinished @@ -66,27 +64,14 @@ class HtmlParser : public BlockParser * @method * @return {bool} */ - bool - IsFinished() const override - { - return this->isFinished; - } + bool IsFinished() const override { return this->isFinished; } protected: - bool - isInlineBlockAllowed() const override - { - return false; - } + bool isInlineBlockAllowed() const override { return false; } - bool - isLineParserAllowed() const override - { - return false; - } + bool isLineParserAllowed() const override { return false; } - void - parseBlock(std::string& line) override + void parseBlock(std::string& line) override { if (!this->isStarted) { diff --git a/include/maddy/imageparser.h b/include/maddy/imageparser.h index 65bca7c..6223d0b 100644 --- a/include/maddy/imageparser.h +++ b/include/maddy/imageparser.h @@ -6,8 +6,8 @@ // ----------------------------------------------------------------------------- -#include #include +#include #include "maddy/lineparser.h" @@ -38,8 +38,7 @@ class ImageParser : public LineParser * @param {std::string&} line The line to interpret * @return {void} */ - void - Parse(std::string& line) override + void Parse(std::string& line) override { static std::regex re(R"(\!\[([^\]]*)\]\(([^\]]*)\))"); static std::string replacement = "\"$1\"/"; diff --git a/include/maddy/inlinecodeparser.h b/include/maddy/inlinecodeparser.h index 3699501..c393e3f 100644 --- a/include/maddy/inlinecodeparser.h +++ b/include/maddy/inlinecodeparser.h @@ -6,8 +6,8 @@ // ----------------------------------------------------------------------------- -#include #include +#include #include "maddy/lineparser.h" @@ -36,8 +36,7 @@ class InlineCodeParser : public LineParser * @param {std::string&} line The line to interpret * @return {void} */ - void - Parse(std::string& line) override + void Parse(std::string& line) override { static std::regex re("`([^`]*)`"); static std::string replacement = "$1"; diff --git a/include/maddy/italicparser.h b/include/maddy/italicparser.h index ac3df0f..3f7075d 100644 --- a/include/maddy/italicparser.h +++ b/include/maddy/italicparser.h @@ -6,8 +6,8 @@ // ----------------------------------------------------------------------------- -#include #include +#include #include "maddy/lineparser.h" @@ -36,10 +36,11 @@ class ItalicParser : public LineParser * @param {std::string&} line The line to interpret * @return {void} */ - void - Parse(std::string& line) override + void Parse(std::string& line) override { - static std::regex re(R"((?!.*`.*|.*.*)\*(?!.*`.*|.*<\/code>.*)([^\*]*)\*(?!.*`.*|.*<\/code>.*))"); + static std::regex re( + R"((?!.*`.*|.*.*)\*(?!.*`.*|.*<\/code>.*)([^\*]*)\*(?!.*`.*|.*<\/code>.*))" + ); static std::string replacement = "$1"; line = std::regex_replace(line, re, replacement); } diff --git a/include/maddy/latexblockparser.h b/include/maddy/latexblockparser.h index f668523..7e36d13 100644 --- a/include/maddy/latexblockparser.h +++ b/include/maddy/latexblockparser.h @@ -7,8 +7,8 @@ // ----------------------------------------------------------------------------- #include -#include #include +#include #include "maddy/blockparser.h" @@ -51,11 +51,13 @@ class LatexBlockParser : public BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ - LatexBlockParser( + LatexBlockParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback + std::function(const std::string& line)> + getBlockParserForLineCallback ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) , isStarted(false) @@ -75,8 +77,7 @@ class LatexBlockParser : public BlockParser * @param {const std::string&} line * @return {bool} */ - static bool - IsStartingLine(const std::string& line) + static bool IsStartingLine(const std::string& line) { static std::regex re(R"(^(?:\$){2}(.*)$)"); return std::regex_match(line, re); @@ -88,27 +89,14 @@ class LatexBlockParser : public BlockParser * @method * @return {bool} */ - bool - IsFinished() const override - { - return this->isFinished; - } + bool IsFinished() const override { return this->isFinished; } protected: - bool - isInlineBlockAllowed() const override - { - return false; - } + bool isInlineBlockAllowed() const override { return false; } - bool - isLineParserAllowed() const override - { - return false; - } + bool isLineParserAllowed() const override { return false; } - void - parseBlock(std::string& line) override + void parseBlock(std::string& line) override { if (!this->isStarted && line.substr(0, 2) == "$$") { @@ -116,7 +104,8 @@ class LatexBlockParser : public BlockParser this->isFinished = false; } - if (this->isStarted && !this->isFinished && line.size() > 1 && line.substr(line.size() - 2, 2) == "$$") + if (this->isStarted && !this->isFinished && line.size() > 1 && + line.substr(line.size() - 2, 2) == "$$") { this->isFinished = true; this->isStarted = false; diff --git a/include/maddy/linkparser.h b/include/maddy/linkparser.h index e634d43..d6c72d0 100644 --- a/include/maddy/linkparser.h +++ b/include/maddy/linkparser.h @@ -6,8 +6,8 @@ // ----------------------------------------------------------------------------- -#include #include +#include #include "maddy/lineparser.h" @@ -38,8 +38,7 @@ class LinkParser : public LineParser * @param {std::string&} line The line to interpret * @return {void} */ - void - Parse(std::string& line) override + void Parse(std::string& line) override { static std::regex re(R"(\[([^\]]*)\]\(([^\]]*)\))"); static std::string replacement = "$1"; diff --git a/include/maddy/orderedlistparser.h b/include/maddy/orderedlistparser.h index 27a2aca..37e24db 100644 --- a/include/maddy/orderedlistparser.h +++ b/include/maddy/orderedlistparser.h @@ -31,11 +31,13 @@ class OrderedListParser : public BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ - OrderedListParser( + OrderedListParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback + std::function(const std::string& line)> + getBlockParserForLineCallback ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) , isStarted(false) @@ -51,8 +53,7 @@ class OrderedListParser : public BlockParser * @param {const std::string&} line * @return {bool} */ - static bool - IsStartingLine(const std::string& line) + static bool IsStartingLine(const std::string& line) { static std::regex re("^1\\. .*"); return std::regex_match(line, re); @@ -64,27 +65,14 @@ class OrderedListParser : public BlockParser * @method * @return {bool} */ - bool - IsFinished() const override - { - return this->isFinished; - } + bool IsFinished() const override { return this->isFinished; } protected: - bool - isInlineBlockAllowed() const override - { - return true; - } + bool isInlineBlockAllowed() const override { return true; } - bool - isLineParserAllowed() const override - { - return true; - } + bool isLineParserAllowed() const override { return true; } - void - parseBlock(std::string& line) override + void parseBlock(std::string& line) override { bool isStartOfNewListItem = this->isStartOfNewListItem(line); uint32_t indentation = getIndentationWidth(line); @@ -107,12 +95,9 @@ class OrderedListParser : public BlockParser return; } - if ( - line.empty() || - line.find("
  • ") != std::string::npos || - line.find("
  • ") != std::string::npos || - line.find("") != std::string::npos - ) + if (line.empty() || line.find("
  • ") != std::string::npos || + line.find("
  • ") != std::string::npos || + line.find("") != std::string::npos) { line = "" + line; this->isFinished = true; @@ -129,8 +114,7 @@ class OrderedListParser : public BlockParser bool isStarted; bool isFinished; - bool - isStartOfNewListItem(const std::string& line) const + bool isStartOfNewListItem(const std::string& line) const { static std::regex re(R"(^(?:[1-9]+[0-9]*\. |\* ).*)"); return std::regex_match(line, re); diff --git a/include/maddy/paragraphparser.h b/include/maddy/paragraphparser.h index 40d19cc..4c477b8 100644 --- a/include/maddy/paragraphparser.h +++ b/include/maddy/paragraphparser.h @@ -30,11 +30,13 @@ class ParagraphParser : public BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ - ParagraphParser( + ParagraphParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback, + std::function(const std::string& line)> + getBlockParserForLineCallback, bool isEnabled ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) @@ -54,11 +56,7 @@ class ParagraphParser : public BlockParser * @param {const std::string&} line * @return {bool} */ - static bool - IsStartingLine(const std::string& line) - { - return !line.empty(); - } + static bool IsStartingLine(const std::string& line) { return !line.empty(); } /** * IsFinished @@ -68,27 +66,14 @@ class ParagraphParser : public BlockParser * @method * @return {bool} */ - bool - IsFinished() const override - { - return this->isFinished; - } + bool IsFinished() const override { return this->isFinished; } protected: - bool - isInlineBlockAllowed() const override - { - return false; - } + bool isInlineBlockAllowed() const override { return false; } - bool - isLineParserAllowed() const override - { - return true; - } + bool isLineParserAllowed() const override { return true; } - void - parseBlock(std::string& line) override + void parseBlock(std::string& line) override { if (this->isEnabled && !this->isStarted) { diff --git a/include/maddy/parser.h b/include/maddy/parser.h index aeb349d..9f4a4cb 100644 --- a/include/maddy/parser.h +++ b/include/maddy/parser.h @@ -6,8 +6,8 @@ // ----------------------------------------------------------------------------- -#include #include +#include #include #include "maddy/parserconfig.h" @@ -56,8 +56,12 @@ class Parser * * Check https://github.com/progsource/maddy/blob/master/CHANGELOG.md * for the changelog. - */ - static const std::string& version() { static const std::string v = "1.3.0"; return v; } + */ + static const std::string& version() + { + static const std::string v = "1.3.0"; + return v; + } /** * ctor @@ -66,8 +70,7 @@ class Parser * * @method */ - Parser(std::shared_ptr config = nullptr) - : config(config) + Parser(std::shared_ptr config = nullptr) : config(config) { // deprecated backward compatibility // will be removed in 1.4.0 latest including the booleans @@ -80,66 +83,50 @@ class Parser this->config->enabledParsers |= maddy::types::HTML_PARSER; } - if ( - !this->config || - (this->config->enabledParsers & maddy::types::BREAKLINE_PARSER) != 0 - ) + if (!this->config || + (this->config->enabledParsers & maddy::types::BREAKLINE_PARSER) != 0) { this->breakLineParser = std::make_shared(); } - if ( - !this->config || - (this->config->enabledParsers & maddy::types::EMPHASIZED_PARSER) != 0 - ) + if (!this->config || + (this->config->enabledParsers & maddy::types::EMPHASIZED_PARSER) != 0) { this->emphasizedParser = std::make_shared(); } - if ( - !this->config || - (this->config->enabledParsers & maddy::types::IMAGE_PARSER) != 0 - ) + if (!this->config || + (this->config->enabledParsers & maddy::types::IMAGE_PARSER) != 0) { this->imageParser = std::make_shared(); } - if ( - !this->config || - (this->config->enabledParsers & maddy::types::INLINE_CODE_PARSER) != 0 - ) + if (!this->config || + (this->config->enabledParsers & maddy::types::INLINE_CODE_PARSER) != 0) { this->inlineCodeParser = std::make_shared(); } - if ( - !this->config || - (this->config->enabledParsers & maddy::types::ITALIC_PARSER) != 0 - ) + if (!this->config || + (this->config->enabledParsers & maddy::types::ITALIC_PARSER) != 0) { this->italicParser = std::make_shared(); } - if ( - !this->config || - (this->config->enabledParsers & maddy::types::LINK_PARSER) != 0 - ) + if (!this->config || + (this->config->enabledParsers & maddy::types::LINK_PARSER) != 0) { this->linkParser = std::make_shared(); } - if ( - !this->config || - (this->config->enabledParsers & maddy::types::STRIKETHROUGH_PARSER) != 0 - ) + if (!this->config || (this->config->enabledParsers & + maddy::types::STRIKETHROUGH_PARSER) != 0) { this->strikeThroughParser = std::make_shared(); } - if ( - !this->config || - (this->config->enabledParsers & maddy::types::STRONG_PARSER) != 0 - ) + if (!this->config || + (this->config->enabledParsers & maddy::types::STRONG_PARSER) != 0) { this->strongParser = std::make_shared(); } @@ -152,8 +139,7 @@ class Parser * @param {const std::istream&} markdown * @return {std::string} HTML */ - std::string - Parse(std::istream& markdown) const + std::string Parse(std::istream& markdown) const { std::string result = ""; std::shared_ptr currentBlockParser = nullptr; @@ -204,8 +190,7 @@ class Parser std::shared_ptr strongParser; // block parser have to run before - void - runLineParser(std::string& line) const + void runLineParser(std::string& line) const { // Attention! ImageParser has to be before LinkParser if (this->imageParser) @@ -250,167 +235,114 @@ class Parser } } - std::shared_ptr - getBlockParserForLine(const std::string& line) const + std::shared_ptr getBlockParserForLine(const std::string& line + ) const { std::shared_ptr parser; - if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::CODE_BLOCK_PARSER) != 0 - ) && - maddy::CodeBlockParser::IsStartingLine(line) - ) + if ((!this->config || (this->config->enabledParsers & + maddy::types::CODE_BLOCK_PARSER) != 0) && + maddy::CodeBlockParser::IsStartingLine(line)) { - parser = std::make_shared( - nullptr, - nullptr - ); + parser = std::make_shared(nullptr, nullptr); } - else if ( - this->config && - (this->config->enabledParsers & maddy::types::LATEX_BLOCK_PARSER) != 0 && - maddy::LatexBlockParser::IsStartingLine(line) - ) + else if (this->config && + (this->config->enabledParsers & maddy::types::LATEX_BLOCK_PARSER + ) != 0 && + maddy::LatexBlockParser::IsStartingLine(line)) { - parser = std::make_shared( - nullptr, - nullptr - ); + parser = std::make_shared(nullptr, nullptr); } - else if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::HEADLINE_PARSER) != 0 - ) && - maddy::HeadlineParser::IsStartingLine(line) - ) + else if ((!this->config || (this->config->enabledParsers & + maddy::types::HEADLINE_PARSER) != 0) && + maddy::HeadlineParser::IsStartingLine(line)) { if (!this->config || this->config->isHeadlineInlineParsingEnabled) { parser = std::make_shared( - [this](std::string& line){ this->runLineParser(line); }, + [this](std::string& line) { this->runLineParser(line); }, nullptr, true ); } else { - parser = std::make_shared( - nullptr, - nullptr, - false - ); + parser = + std::make_shared(nullptr, nullptr, false); } } - else if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::HORIZONTAL_LINE_PARSER) != 0 - ) && - maddy::HorizontalLineParser::IsStartingLine(line) - ) + else if ((!this->config || (this->config->enabledParsers & + maddy::types::HORIZONTAL_LINE_PARSER) != 0) && + maddy::HorizontalLineParser::IsStartingLine(line)) { - parser = std::make_shared( - nullptr, - nullptr - ); + parser = std::make_shared(nullptr, nullptr); } - else if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::QUOTE_PARSER) != 0 - ) && - maddy::QuoteParser::IsStartingLine(line) - ) + else if ((!this->config || (this->config->enabledParsers & + maddy::types::QUOTE_PARSER) != 0) && + maddy::QuoteParser::IsStartingLine(line)) { parser = std::make_shared( - [this](std::string& line){ this->runLineParser(line); }, - [this](const std::string& line){ return this->getBlockParserForLine(line); } + [this](std::string& line) { this->runLineParser(line); }, + [this](const std::string& line) + { return this->getBlockParserForLine(line); } ); } - else if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::TABLE_PARSER) != 0 - ) && - maddy::TableParser::IsStartingLine(line) - ) + else if ((!this->config || (this->config->enabledParsers & + maddy::types::TABLE_PARSER) != 0) && + maddy::TableParser::IsStartingLine(line)) { parser = std::make_shared( - [this](std::string& line){ this->runLineParser(line); }, - nullptr + [this](std::string& line) { this->runLineParser(line); }, nullptr ); } - else if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::CHECKLIST_PARSER) != 0 - ) && - maddy::ChecklistParser::IsStartingLine(line) - ) + else if ((!this->config || (this->config->enabledParsers & + maddy::types::CHECKLIST_PARSER) != 0) && + maddy::ChecklistParser::IsStartingLine(line)) { parser = this->createChecklistParser(); } - else if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::ORDERED_LIST_PARSER) != 0 - ) && - maddy::OrderedListParser::IsStartingLine(line) - ) + else if ((!this->config || (this->config->enabledParsers & + maddy::types::ORDERED_LIST_PARSER) != 0) && + maddy::OrderedListParser::IsStartingLine(line)) { parser = this->createOrderedListParser(); } - else if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::UNORDERED_LIST_PARSER) != 0 - ) && - maddy::UnorderedListParser::IsStartingLine(line) - ) + else if ((!this->config || (this->config->enabledParsers & + maddy::types::UNORDERED_LIST_PARSER) != 0) && + maddy::UnorderedListParser::IsStartingLine(line)) { parser = this->createUnorderedListParser(); } - else if ( - this->config && - (this->config->enabledParsers & maddy::types::HTML_PARSER) != 0 && - maddy::HtmlParser::IsStartingLine(line) - ) + else if (this->config && + (this->config->enabledParsers & maddy::types::HTML_PARSER) != 0 && + maddy::HtmlParser::IsStartingLine(line)) { parser = std::make_shared(nullptr, nullptr); } - else if ( - maddy::ParagraphParser::IsStartingLine(line) - ) + else if (maddy::ParagraphParser::IsStartingLine(line)) { parser = std::make_shared( - [this](std::string& line){ this->runLineParser(line); }, + [this](std::string& line) { this->runLineParser(line); }, nullptr, - (!this->config || (this->config->enabledParsers & maddy::types::PARAGRAPH_PARSER) != 0) + (!this->config || + (this->config->enabledParsers & maddy::types::PARAGRAPH_PARSER) != 0) ); } return parser; } - std::shared_ptr - createChecklistParser() const + std::shared_ptr createChecklistParser() const { return std::make_shared( - [this](std::string& line){ this->runLineParser(line); }, + [this](std::string& line) { this->runLineParser(line); }, [this](const std::string& line) { std::shared_ptr parser; - if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::CHECKLIST_PARSER) != 0 - ) && - maddy::ChecklistParser::IsStartingLine(line) - ) + if ((!this->config || (this->config->enabledParsers & + maddy::types::CHECKLIST_PARSER) != 0) && + maddy::ChecklistParser::IsStartingLine(line)) { parser = this->createChecklistParser(); } @@ -420,32 +352,24 @@ class Parser ); } - std::shared_ptr - createOrderedListParser() const + std::shared_ptr createOrderedListParser() const { return std::make_shared( - [this](std::string& line){ this->runLineParser(line); }, + [this](std::string& line) { this->runLineParser(line); }, [this](const std::string& line) { std::shared_ptr parser; - if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::ORDERED_LIST_PARSER) != 0 - ) && - maddy::OrderedListParser::IsStartingLine(line) - ) + if ((!this->config || (this->config->enabledParsers & + maddy::types::ORDERED_LIST_PARSER) != 0) && + maddy::OrderedListParser::IsStartingLine(line)) { parser = this->createOrderedListParser(); } - else if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::UNORDERED_LIST_PARSER) != 0 - ) && - maddy::UnorderedListParser::IsStartingLine(line) - ) + else if ((!this->config || (this->config->enabledParsers & + maddy::types::UNORDERED_LIST_PARSER) != 0 + ) && + maddy::UnorderedListParser::IsStartingLine(line)) { parser = this->createUnorderedListParser(); } @@ -455,32 +379,24 @@ class Parser ); } - std::shared_ptr - createUnorderedListParser() const + std::shared_ptr createUnorderedListParser() const { return std::make_shared( - [this](std::string& line){ this->runLineParser(line); }, + [this](std::string& line) { this->runLineParser(line); }, [this](const std::string& line) { std::shared_ptr parser; - if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::ORDERED_LIST_PARSER) != 0 - ) && - maddy::OrderedListParser::IsStartingLine(line) - ) + if ((!this->config || (this->config->enabledParsers & + maddy::types::ORDERED_LIST_PARSER) != 0) && + maddy::OrderedListParser::IsStartingLine(line)) { parser = this->createOrderedListParser(); } - else if ( - ( - !this->config || - (this->config->enabledParsers & maddy::types::UNORDERED_LIST_PARSER) != 0 - ) && - maddy::UnorderedListParser::IsStartingLine(line) - ) + else if ((!this->config || (this->config->enabledParsers & + maddy::types::UNORDERED_LIST_PARSER) != 0 + ) && + maddy::UnorderedListParser::IsStartingLine(line)) { parser = this->createUnorderedListParser(); } diff --git a/include/maddy/parserconfig.h b/include/maddy/parserconfig.h index a5469a2..04acdc7 100644 --- a/include/maddy/parserconfig.h +++ b/include/maddy/parserconfig.h @@ -14,6 +14,7 @@ namespace maddy { namespace types { +// clang-format off /** * PARSER_TYPE * @@ -46,6 +47,7 @@ enum PARSER_TYPE : uint32_t DEFAULT = 0b0111111111110111111, ALL = 0b1111111111111111111, }; +// clang-format on } // namespace types @@ -60,26 +62,26 @@ struct ParserConfig * @deprecated will be removed in 1.4.0 latest * * this flag = false == `enabledParsers &= ~maddy::types::EMPHASIZED_PARSER` - */ + */ bool isEmphasizedParserEnabled; /** * @deprecated will be removed in 1.4.0 latest * * this flag = false == `enabledParsers |= maddy::types::HTML_PARSER` - */ + */ bool isHTMLWrappedInParagraph; /** * en-/disable headline inline-parsing * * default: enabled - */ + */ bool isHeadlineInlineParsingEnabled; /** * enabled parsers bitfield - */ + */ uint32_t enabledParsers; ParserConfig() diff --git a/include/maddy/quoteparser.h b/include/maddy/quoteparser.h index c0415dd..13151ef 100644 --- a/include/maddy/quoteparser.h +++ b/include/maddy/quoteparser.h @@ -31,11 +31,13 @@ class QuoteParser : public BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ - QuoteParser( + QuoteParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback + std::function(const std::string& line)> + getBlockParserForLineCallback ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) , isStarted(false) @@ -51,8 +53,7 @@ class QuoteParser : public BlockParser * @param {const std::string&} line * @return {bool} */ - static bool - IsStartingLine(const std::string& line) + static bool IsStartingLine(const std::string& line) { static std::regex re(R"(^\>.*)"); return std::regex_match(line, re); @@ -67,8 +68,7 @@ class QuoteParser : public BlockParser * @param {std::string&} line * @return {void} */ - void - AddLine(std::string& line) override + void AddLine(std::string& line) override { if (!this->isStarted) { @@ -122,27 +122,14 @@ class QuoteParser : public BlockParser * @method * @return {bool} */ - bool - IsFinished() const override - { - return this->isFinished; - } + bool IsFinished() const override { return this->isFinished; } protected: - bool - isInlineBlockAllowed() const override - { - return true; - } + bool isInlineBlockAllowed() const override { return true; } - bool - isLineParserAllowed() const override - { - return true; - } + bool isLineParserAllowed() const override { return true; } - void - parseBlock(std::string& line) override + void parseBlock(std::string& line) override { static std::regex lineRegexWithSpace(R"(^\> )"); line = std::regex_replace(line, lineRegexWithSpace, ""); diff --git a/include/maddy/strikethroughparser.h b/include/maddy/strikethroughparser.h index 0c5a8ca..effa5d8 100644 --- a/include/maddy/strikethroughparser.h +++ b/include/maddy/strikethroughparser.h @@ -6,8 +6,8 @@ // ----------------------------------------------------------------------------- -#include #include +#include #include "maddy/lineparser.h" @@ -36,10 +36,11 @@ class StrikeThroughParser : public LineParser * @param {std::string&} line The line to interpret * @return {void} */ - void - Parse(std::string& line) override + void Parse(std::string& line) override { - static std::regex re(R"((?!.*`.*|.*.*)\~\~(?!.*`.*|.*<\/code>.*)([^\~]*)\~\~(?!.*`.*|.*<\/code>.*))"); + static std::regex re( + R"((?!.*`.*|.*.*)\~\~(?!.*`.*|.*<\/code>.*)([^\~]*)\~\~(?!.*`.*|.*<\/code>.*))" + ); static std::string replacement = "$1"; line = std::regex_replace(line, re, replacement); diff --git a/include/maddy/strongparser.h b/include/maddy/strongparser.h index 6e5c41c..348f2d4 100644 --- a/include/maddy/strongparser.h +++ b/include/maddy/strongparser.h @@ -6,8 +6,8 @@ // ----------------------------------------------------------------------------- -#include #include +#include #include "maddy/lineparser.h" @@ -38,13 +38,15 @@ class StrongParser : public LineParser * @param {std::string&} line The line to interpret * @return {void} */ - void - Parse(std::string& line) override + void Parse(std::string& line) override { - static std::vector res - { - std::regex{R"((?!.*`.*|.*.*)\*\*(?!.*`.*|.*<\/code>.*)([^\*\*]*)\*\*(?!.*`.*|.*<\/code>.*))"}, - std::regex{R"((?!.*`.*|.*.*)__(?!.*`.*|.*<\/code>.*)([^__]*)__(?!.*`.*|.*<\/code>.*))"} + static std::vector res{ + std::regex{ + R"((?!.*`.*|.*.*)\*\*(?!.*`.*|.*<\/code>.*)([^\*\*]*)\*\*(?!.*`.*|.*<\/code>.*))" + }, + std::regex{ + R"((?!.*`.*|.*.*)__(?!.*`.*|.*<\/code>.*)([^__]*)__(?!.*`.*|.*<\/code>.*))" + } }; static std::string replacement = "$1"; for (const auto& re : res) diff --git a/include/maddy/tableparser.h b/include/maddy/tableparser.h index c230cc6..9f1051f 100644 --- a/include/maddy/tableparser.h +++ b/include/maddy/tableparser.h @@ -7,8 +7,8 @@ // ----------------------------------------------------------------------------- #include -#include #include +#include #include "maddy/blockparser.h" @@ -33,11 +33,13 @@ class TableParser : public BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ - TableParser( + TableParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback + std::function(const std::string& line)> + getBlockParserForLineCallback ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) , isStarted(false) @@ -55,8 +57,7 @@ class TableParser : public BlockParser * @param {const std::string&} line * @return {bool} */ - static bool - IsStartingLine(const std::string& line) + static bool IsStartingLine(const std::string& line) { static std::string matchString("|table>"); return line == matchString; @@ -71,8 +72,7 @@ class TableParser : public BlockParser * @param {std::string&} line * @return {void} */ - void - AddLine(std::string& line) override + void AddLine(std::string& line) override { if (!this->isStarted && line == "|table>") { @@ -124,27 +124,14 @@ class TableParser : public BlockParser * @method * @return {bool} */ - bool - IsFinished() const override - { - return this->isFinished; - } + bool IsFinished() const override { return this->isFinished; } protected: - bool - isInlineBlockAllowed() const override - { - return false; - } + bool isInlineBlockAllowed() const override { return false; } - bool - isLineParserAllowed() const override - { - return true; - } + bool isLineParserAllowed() const override { return true; } - void - parseBlock(std::string&) override + void parseBlock(std::string&) override { result << ""; diff --git a/include/maddy/unorderedlistparser.h b/include/maddy/unorderedlistparser.h index 0c1aa5d..53859bf 100644 --- a/include/maddy/unorderedlistparser.h +++ b/include/maddy/unorderedlistparser.h @@ -31,11 +31,13 @@ class UnorderedListParser : public BlockParser * * @method * @param {std::function} parseLineCallback - * @param {std::function(const std::string& line)>} getBlockParserForLineCallback + * @param {std::function(const std::string& + * line)>} getBlockParserForLineCallback */ - UnorderedListParser( + UnorderedListParser( std::function parseLineCallback, - std::function(const std::string& line)> getBlockParserForLineCallback + std::function(const std::string& line)> + getBlockParserForLineCallback ) : BlockParser(parseLineCallback, getBlockParserForLineCallback) , isStarted(false) @@ -51,8 +53,7 @@ class UnorderedListParser : public BlockParser * @param {const std::string&} line * @return {bool} */ - static bool - IsStartingLine(const std::string& line) + static bool IsStartingLine(const std::string& line) { static std::regex re("^[+*-] .*"); return std::regex_match(line, re); @@ -64,27 +65,14 @@ class UnorderedListParser : public BlockParser * @method * @return {bool} */ - bool - IsFinished() const override - { - return this->isFinished; - } + bool IsFinished() const override { return this->isFinished; } protected: - bool - isInlineBlockAllowed() const override - { - return true; - } + bool isInlineBlockAllowed() const override { return true; } - bool - isLineParserAllowed() const override - { - return true; - } + bool isLineParserAllowed() const override { return true; } - void - parseBlock(std::string& line) override + void parseBlock(std::string& line) override { bool isStartOfNewListItem = IsStartingLine(line); uint32_t indentation = getIndentationWidth(line); @@ -105,12 +93,9 @@ class UnorderedListParser : public BlockParser return; } - if ( - line.empty() || - line.find("
  • ") != std::string::npos || - line.find("
  • ") != std::string::npos || - line.find("") != std::string::npos - ) + if (line.empty() || line.find("
  • ") != std::string::npos || + line.find("
  • ") != std::string::npos || + line.find("") != std::string::npos) { line = "" + line; this->isFinished = true; diff --git a/tests/maddy/test_maddy_checklistparser.cpp b/tests/maddy/test_maddy_checklistparser.cpp index 927652c..a525d2c 100644 --- a/tests/maddy/test_maddy_checklistparser.cpp +++ b/tests/maddy/test_maddy_checklistparser.cpp @@ -15,10 +15,10 @@ class MADDY_CHECKLISTPARSER : public ::testing::Test protected: std::shared_ptr clParser; - void - SetUp() override + void SetUp() override { - std::function(const std::string& line)> getBlockParserForLineCallback = [](const std::string& line) + std::function(const std::string& line)> + getBlockParserForLineCallback = [](const std::string& line) { if (maddy::ChecklistParser::IsStartingLine(line)) { @@ -32,15 +32,16 @@ class MADDY_CHECKLISTPARSER : public ::testing::Test }; this->clParser = std::make_shared( - nullptr, - getBlockParserForLineCallback + nullptr, getBlockParserForLineCallback ); } }; // ----------------------------------------------------------------------------- -TEST_F(MADDY_CHECKLISTPARSER, IsStartingLineReturnsTrueWhenFacedWithBeginningOfList) +TEST_F( + MADDY_CHECKLISTPARSER, IsStartingLineReturnsTrueWhenFacedWithBeginningOfList +) { ASSERT_TRUE(maddy::ChecklistParser::IsStartingLine("- [ ] a")); ASSERT_TRUE(maddy::ChecklistParser::IsStartingLine("- [x] b")); @@ -53,12 +54,11 @@ TEST_F(MADDY_CHECKLISTPARSER, IsFinishedAlwaysReturnsFalseInTheBeginning) TEST_F(MADDY_CHECKLISTPARSER, ItReplacesMarkdownWithAnHtmlChecklist) { - std::vector markdown = { - "- [ ] a" - , "- [x] b" - , "" - }; - std::string expected = "
    "; + std::vector markdown = {"- [ ] a", "- [x] b", ""}; + std::string expected = + "
    "; for (std::string md : markdown) { @@ -76,14 +76,15 @@ TEST_F(MADDY_CHECKLISTPARSER, ItReplacesMarkdownWithAnHtmlChecklist) TEST_F(MADDY_CHECKLISTPARSER, ItReplacesMarkdownWithAnHierachicalHtmlList) { std::vector markdown = { - "- [ ] a" - , " - [ ] d" - , " - [ ] e" - , "- [ ] b" - , " - [x] c" - , "" + "- [ ] a", " - [ ] d", " - [ ] e", "- [ ] b", " - [x] c", "" }; - std::string expected = "
    "; + std::string expected = + "
    "; for (std::string md : markdown) { diff --git a/tests/maddy/test_maddy_codeblockparser.cpp b/tests/maddy/test_maddy_codeblockparser.cpp index 319af98..5306658 100644 --- a/tests/maddy/test_maddy_codeblockparser.cpp +++ b/tests/maddy/test_maddy_codeblockparser.cpp @@ -15,13 +15,9 @@ class MADDY_CODEBLOCKPARSER : public ::testing::Test protected: std::shared_ptr cbParser; - void - SetUp() override + void SetUp() override { - this->cbParser = std::make_shared( - nullptr, - nullptr - ); + this->cbParser = std::make_shared(nullptr, nullptr); } }; @@ -40,13 +36,11 @@ TEST_F(MADDY_CODEBLOCKPARSER, IsFinishedReturnsFalseInTheBeginning) TEST_F(MADDY_CODEBLOCKPARSER, ItReplacesMarkdownWithAnHtmlCodeBlock) { std::vector markdown = { - "```" - , "some code" - , "some other code" - , "```" + "```", "some code", "some other code", "```" }; - std::string expected = "
    \nsome code\nsome other code\n
    "; + std::string expected = + "
    \nsome code\nsome other code\n
    "; for (std::string md : markdown) { @@ -63,13 +57,11 @@ TEST_F(MADDY_CODEBLOCKPARSER, ItReplacesMarkdownWithAnHtmlCodeBlock) TEST_F(MADDY_CODEBLOCKPARSER, ItShouldUseAnythingBehindFirstBackticksAsClass) { std::vector markdown = { - "```cpp" - , "some code" - , "some other code" - , "```" + "```cpp", "some code", "some other code", "```" }; - std::string expected = "
    \nsome code\nsome other code\n
    "; + std::string expected = + "
    \nsome code\nsome other code\n
    "; for (std::string md : markdown) { diff --git a/tests/maddy/test_maddy_emphasizedparser.cpp b/tests/maddy/test_maddy_emphasizedparser.cpp index c4cd56c..6442779 100644 --- a/tests/maddy/test_maddy_emphasizedparser.cpp +++ b/tests/maddy/test_maddy_emphasizedparser.cpp @@ -24,7 +24,8 @@ TEST(MADDY_EMPHASIZEDPARSER, ItReplacesMarkdownWithEmphasizedHTML) TEST(MADDY_EMPHASIZEDPARSER, ItDoesNotParseInsideInlineCode) { std::string text = "some text `*bla*` `/**text*/` testing _it_ out"; - std::string expected = "some text `*bla*` `/**text*/` testing it out"; + std::string expected = + "some text `*bla*` `/**text*/` testing it out"; auto emphasizedParser = std::make_shared(); emphasizedParser->Parse(text); diff --git a/tests/maddy/test_maddy_headlineparser.cpp b/tests/maddy/test_maddy_headlineparser.cpp index 33b2144..8a9acd0 100644 --- a/tests/maddy/test_maddy_headlineparser.cpp +++ b/tests/maddy/test_maddy_headlineparser.cpp @@ -15,19 +15,17 @@ class MADDY_HEADLINEPARSER : public ::testing::Test protected: std::shared_ptr hlParser; - void - SetUp() override + void SetUp() override { - this->hlParser = std::make_shared( - nullptr, - nullptr - ); + this->hlParser = std::make_shared(nullptr, nullptr); } }; // ----------------------------------------------------------------------------- -TEST_F(MADDY_HEADLINEPARSER, IsStartingLineReturnsTrueWhenFacedWithOneToSixHashes) +TEST_F( + MADDY_HEADLINEPARSER, IsStartingLineReturnsTrueWhenFacedWithOneToSixHashes +) { ASSERT_TRUE(maddy::HeadlineParser::IsStartingLine("# a")); ASSERT_TRUE(maddy::HeadlineParser::IsStartingLine("## a")); @@ -45,21 +43,16 @@ TEST_F(MADDY_HEADLINEPARSER, IsFinishedAlwaysReturnsTrue) TEST_F(MADDY_HEADLINEPARSER, ItReplacesMarkdownWithAnHtmlHeadline) { std::vector markdown = { - "# a" - , "## a" - , "### a" - , "#### a" - , "##### a" - , "###### a" + "# a", "## a", "### a", "#### a", "##### a", "###### a" }; std::vector expected = { - "

    a

    " - , "

    a

    " - , "

    a

    " - , "

    a

    " - , "
    a
    " - , "
    a
    " + "

    a

    ", + "

    a

    ", + "

    a

    ", + "

    a

    ", + "
    a
    ", + "
    a
    " }; for (uint8_t i = 0; i < 6; ++i) diff --git a/tests/maddy/test_maddy_horizontallineparser.cpp b/tests/maddy/test_maddy_horizontallineparser.cpp index c250285..76d1e22 100644 --- a/tests/maddy/test_maddy_horizontallineparser.cpp +++ b/tests/maddy/test_maddy_horizontallineparser.cpp @@ -15,19 +15,18 @@ class MADDY_HORIZONTALLINEPARSER : public ::testing::Test protected: std::shared_ptr hlParser; - void - SetUp() override + void SetUp() override { - this->hlParser = std::make_shared( - nullptr, - nullptr - ); + this->hlParser = + std::make_shared(nullptr, nullptr); } }; // ----------------------------------------------------------------------------- -TEST_F(MADDY_HORIZONTALLINEPARSER, IsStartingLineReturnsTrueWhenFacedWithThreeDashes) +TEST_F( + MADDY_HORIZONTALLINEPARSER, IsStartingLineReturnsTrueWhenFacedWithThreeDashes +) { ASSERT_TRUE(maddy::HorizontalLineParser::IsStartingLine("---")); } diff --git a/tests/maddy/test_maddy_htmlparser.cpp b/tests/maddy/test_maddy_htmlparser.cpp index 465ce62..c465bb0 100644 --- a/tests/maddy/test_maddy_htmlparser.cpp +++ b/tests/maddy/test_maddy_htmlparser.cpp @@ -15,13 +15,9 @@ class MADDY_HTMLPARSER : public ::testing::Test protected: std::shared_ptr pParser; - void - SetUp() override + void SetUp() override { - this->pParser = std::make_shared( - nullptr, - nullptr - ); + this->pParser = std::make_shared(nullptr, nullptr); } }; @@ -35,11 +31,7 @@ TEST_F(MADDY_HTMLPARSER, IsFinishedReturnsFalseInTheBeginning) TEST_F(MADDY_HTMLPARSER, IsStartingLineReturnsFalseWhenFacedWithNoSmallerThan) { const std::vector markdown = { - "> quote" - , "some text" - , "* list" - , "1. numbered list" - , "|table>" + "> quote", "some text", "* list", "1. numbered list", "|table>" }; for (size_t i = 0; i < markdown.size(); ++i) @@ -57,17 +49,18 @@ TEST_F(MADDY_HTMLPARSER, IsStartingLineReturnsTrueWhenFacedWithSmallerThan) TEST_F(MADDY_HTMLPARSER, ItReplacesNoHtml) { - const std::vector markdown { - "some text in a paragraph" - , "" - , "
    some HTML
    " - , "" - , "
    more" - , "HTML" - , "
    " - , "" + const std::vector markdown{ + "some text in a paragraph", + "", + "
    some HTML
    ", + "", + "
    more", + "HTML", + "
    ", + "" }; - const std::string expected = "some text in a paragraph
    some HTML
    more HTML
    "; + const std::string expected = + "some text in a paragraph
    some HTML
    more HTML
    "; for (std::string md : markdown) { diff --git a/tests/maddy/test_maddy_imageparser.cpp b/tests/maddy/test_maddy_imageparser.cpp index b48b2fa..7824d7d 100644 --- a/tests/maddy/test_maddy_imageparser.cpp +++ b/tests/maddy/test_maddy_imageparser.cpp @@ -13,7 +13,8 @@ TEST(MADDY_IMAGEPARSER, ItReplacesMarkdownWithAnImage) { std::string text = "Some text ![Image Title](http://example.com/a.png)"; - std::string expected = "Some text \"Image"; + std::string expected = + "Some text \"Image"; auto imageParser = std::make_shared(); imageParser->Parse(text); @@ -23,8 +24,12 @@ TEST(MADDY_IMAGEPARSER, ItReplacesMarkdownWithAnImage) TEST(MADDY_IMAGEPARSER, ItReplacesMarkdownWithImages) { - std::string text = "Some text ![Image Title](http://example.com/a.png) bla ![Image Title](http://example.com/a.png)"; - std::string expected = "Some text \"Image bla \"Image"; + std::string text = + "Some text ![Image Title](http://example.com/a.png) bla ![Image " + "Title](http://example.com/a.png)"; + std::string expected = + "Some text \"Image bla " + "\"Image"; auto imageParser = std::make_shared(); imageParser->Parse(text); diff --git a/tests/maddy/test_maddy_inlinecodeparser.cpp b/tests/maddy/test_maddy_inlinecodeparser.cpp index ad34e71..14bfa56 100644 --- a/tests/maddy/test_maddy_inlinecodeparser.cpp +++ b/tests/maddy/test_maddy_inlinecodeparser.cpp @@ -13,7 +13,8 @@ TEST(MADDY_INLINECODEPARSER, ItReplacesMarkdownWithCodeHTML) { std::string text = "some text `bla` text testing `it` out"; - std::string expected = "some text bla text testing it out"; + std::string expected = + "some text bla text testing it out"; auto emphasizedParser = std::make_shared(); emphasizedParser->Parse(text); diff --git a/tests/maddy/test_maddy_italicparser.cpp b/tests/maddy/test_maddy_italicparser.cpp index c87f266..03c5aac 100644 --- a/tests/maddy/test_maddy_italicparser.cpp +++ b/tests/maddy/test_maddy_italicparser.cpp @@ -12,7 +12,6 @@ TEST(MADDY_ITALICPARSER, ItReplacesMarkdownWithItalicHTML) { - std::string text = "some text *bla* text testing *it* out"; std::string expected = "some text bla text testing it out"; auto italicParser = std::make_shared(); diff --git a/tests/maddy/test_maddy_latexblockparser.cpp b/tests/maddy/test_maddy_latexblockparser.cpp index 701b1a3..a53e82b 100644 --- a/tests/maddy/test_maddy_latexblockparser.cpp +++ b/tests/maddy/test_maddy_latexblockparser.cpp @@ -15,13 +15,10 @@ class MADDY_LATEXBLOCKPARSER : public ::testing::Test protected: std::shared_ptr lbParser; - void - SetUp() override + void SetUp() override { - this->lbParser = std::make_shared( - nullptr, - nullptr - ); + this->lbParser = + std::make_shared(nullptr, nullptr); } }; @@ -60,9 +57,7 @@ TEST_F(MADDY_LATEXBLOCKPARSER, ItReplacesOneLineMarkdownWithALatexBlock) TEST_F(MADDY_LATEXBLOCKPARSER, ItReplacesABlockMarkdownWithALatexBlock) { std::vector markdown = { - "$$", - "x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.", - "$$" + "$$", "x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.", "$$" }; std::string expected = "$$\nx = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.\n$$\n"; @@ -82,9 +77,7 @@ TEST_F(MADDY_LATEXBLOCKPARSER, ItReplacesABlockMarkdownWithALatexBlock) TEST_F(MADDY_LATEXBLOCKPARSER, ItReplacesAMultilineBlockMarkdownWithALatexBlock) { std::vector markdown = { - "$$", - "x = {-b \\pm \\sqrt{b^2-4ac}", - "\\over 2a}.$$" + "$$", "x = {-b \\pm \\sqrt{b^2-4ac}", "\\over 2a}.$$" }; std::string expected = "$$\nx = {-b \\pm \\sqrt{b^2-4ac}\n\\over 2a}.$$\n"; diff --git a/tests/maddy/test_maddy_linkparser.cpp b/tests/maddy/test_maddy_linkparser.cpp index aae3ed8..b4adc6d 100644 --- a/tests/maddy/test_maddy_linkparser.cpp +++ b/tests/maddy/test_maddy_linkparser.cpp @@ -13,7 +13,8 @@ TEST(MADDY_LINKPARSER, ItReplacesMarkdownWithALink) { std::string text = "Some text [Link Title](http://example.com)"; - std::string expected = "Some text Link Title"; + std::string expected = + "Some text Link Title"; auto linkParser = std::make_shared(); linkParser->Parse(text); @@ -23,8 +24,12 @@ TEST(MADDY_LINKPARSER, ItReplacesMarkdownWithALink) TEST(MADDY_LINKPARSER, ItReplacesMarkdownWithLinks) { - std::string text = "Some text [Link Title](http://example.com) bla [Link Title](http://example.com)"; - std::string expected = "Some text Link Title bla Link Title"; + std::string text = + "Some text [Link Title](http://example.com) bla [Link " + "Title](http://example.com)"; + std::string expected = + "Some text Link Title bla Link Title"; auto linkParser = std::make_shared(); linkParser->Parse(text); @@ -34,7 +39,8 @@ TEST(MADDY_LINKPARSER, ItReplacesMarkdownWithLinks) // ----------------------------------------------------------------------------- -class DISABLED_MADDY_LINKPARSER : public ::testing::Test { }; +class DISABLED_MADDY_LINKPARSER : public ::testing::Test +{}; // This test is disabled for now, so make sure, to first run the ImageParser // before using the LinkParser diff --git a/tests/maddy/test_maddy_orderedlistparser.cpp b/tests/maddy/test_maddy_orderedlistparser.cpp index ecb73c5..eaf3ca7 100644 --- a/tests/maddy/test_maddy_orderedlistparser.cpp +++ b/tests/maddy/test_maddy_orderedlistparser.cpp @@ -15,10 +15,10 @@ class MADDY_ORDEREDLISTPARSER : public ::testing::Test protected: std::shared_ptr olParser; - void - SetUp() override + void SetUp() override { - std::function(const std::string& line)> getBlockParserForLineCallback = [](const std::string& line) + std::function(const std::string& line)> + getBlockParserForLineCallback = [](const std::string& line) { if (maddy::OrderedListParser::IsStartingLine(line)) { @@ -32,15 +32,16 @@ class MADDY_ORDEREDLISTPARSER : public ::testing::Test }; this->olParser = std::make_shared( - nullptr, - getBlockParserForLineCallback + nullptr, getBlockParserForLineCallback ); } }; // ----------------------------------------------------------------------------- -TEST_F(MADDY_ORDEREDLISTPARSER, IsStartingLineReturnsTrueWhenFacedWithBeginningOfList) +TEST_F( + MADDY_ORDEREDLISTPARSER, IsStartingLineReturnsTrueWhenFacedWithBeginningOfList +) { ASSERT_TRUE(maddy::OrderedListParser::IsStartingLine("1. a")); } @@ -52,11 +53,7 @@ TEST_F(MADDY_ORDEREDLISTPARSER, IsFinishedAlwaysReturnsFalseInTheBeginning) TEST_F(MADDY_ORDEREDLISTPARSER, ItReplacesMarkdownWithAnHtmlOrderedList) { - std::vector markdown = { - "1. a" - , "* b" - , "" - }; + std::vector markdown = {"1. a", "* b", ""}; std::string expected = "
    1. a
    2. b
    "; for (std::string md : markdown) @@ -75,14 +72,11 @@ TEST_F(MADDY_ORDEREDLISTPARSER, ItReplacesMarkdownWithAnHtmlOrderedList) TEST_F(MADDY_ORDEREDLISTPARSER, ItReplacesMarkdownWithAnHierachicalHtmlList) { std::vector markdown = { - "1. a" - , " 1. d" - , " * e" - , "* b" - , " 1. c" - , "" + "1. a", " 1. d", " * e", "* b", " 1. c", "" }; - std::string expected = "
    1. a
      1. d
      2. e
    2. b
      1. c
    "; + std::string expected = + "
    1. a
      1. d
      2. e
    2. b
      1. c
    3. "; for (std::string md : markdown) { @@ -97,14 +91,11 @@ TEST_F(MADDY_ORDEREDLISTPARSER, ItReplacesMarkdownWithAnHierachicalHtmlList) ASSERT_EQ(expected, outputString); } -TEST_F(MADDY_ORDEREDLISTPARSER, ItReplacesNumberedMarkdownListWithAnHtmlOrderedList) +TEST_F( + MADDY_ORDEREDLISTPARSER, ItReplacesNumberedMarkdownListWithAnHtmlOrderedList +) { - std::vector markdown = { - "1. a" - , "94. b" - , "103. c" - , "" - }; + std::vector markdown = {"1. a", "94. b", "103. c", ""}; std::string expected = "
      1. a
      2. b
      3. c
      "; for (std::string md : markdown) diff --git a/tests/maddy/test_maddy_paragraphparser.cpp b/tests/maddy/test_maddy_paragraphparser.cpp index 27e2fb4..a1adb16 100644 --- a/tests/maddy/test_maddy_paragraphparser.cpp +++ b/tests/maddy/test_maddy_paragraphparser.cpp @@ -15,14 +15,10 @@ class MADDY_PARAGRAPHPARSER : public ::testing::Test protected: std::shared_ptr pParser; - void - SetUp() override + void SetUp() override { - this->pParser = std::make_shared( - nullptr, - nullptr, - true - ); + this->pParser = + std::make_shared(nullptr, nullptr, true); } }; @@ -40,11 +36,7 @@ TEST_F(MADDY_PARAGRAPHPARSER, IsFinishedReturnsFalseInTheBeginning) TEST_F(MADDY_PARAGRAPHPARSER, ItReplacesMarkdownWithAnHtmlLine) { - std::vector markdown = { - "Some text" - , "and some other text" - , "" - }; + std::vector markdown = {"Some text", "and some other text", ""}; std::string expected = "

      Some text and some other text

      "; for (std::string md : markdown) diff --git a/tests/maddy/test_maddy_parser.cpp b/tests/maddy/test_maddy_parser.cpp index 112189e..f6855fb 100644 --- a/tests/maddy/test_maddy_parser.cpp +++ b/tests/maddy/test_maddy_parser.cpp @@ -3,10 +3,11 @@ * LICENSE file. */ +#include "maddy/test_maddy_parser.h" + #include "gmock/gmock.h" #include "maddy/parser.h" -#include "maddy/test_maddy_parser.h" // ----------------------------------------------------------------------------- @@ -53,8 +54,8 @@ TEST(MADDY_PARSER, ItShouldParseWithBitwiseConfig) TEST(MADDY_PARSER, ItShouldParseWithSmallConfig) { auto config = std::make_shared(); - config->enabledParsers = maddy::types::EMPHASIZED_PARSER | - maddy::types::STRONG_PARSER; + config->enabledParsers = + maddy::types::EMPHASIZED_PARSER | maddy::types::STRONG_PARSER; auto parser = std::make_shared(config); @@ -70,7 +71,8 @@ TEST(MADDY_PARSER, ItShouldParseInlineCodeInHeadlines) const std::string headlineTest = R"( # Some **test** markdown )"; - const std::string expectedHTML = "

      Some test markdown

      "; + const std::string expectedHTML = + "

      Some test markdown

      "; std::stringstream markdown(headlineTest); auto parser = std::make_shared(); diff --git a/tests/maddy/test_maddy_parser.h b/tests/maddy/test_maddy_parser.h index aab0617..576f30a 100644 --- a/tests/maddy/test_maddy_parser.h +++ b/tests/maddy/test_maddy_parser.h @@ -6,7 +6,8 @@ #include -const std::string testMarkdown = "# This is a test\n\ +const std::string testMarkdown = + "# This is a test\n\ \n\ This should result in a praragraph\n\ it's that simple.\n\ @@ -69,6 +70,71 @@ foot a|foot b|foot c\n\ \n\ "; -const std::string testHtml = "

      This is a test

      This should result in a praragraph it's that simple.

      • an unordered list
        • with some hierarchy
          1. and an ordered
          2. list
          3. directly
        • inside
      \nvar c = 'blub';\n

      A Quote

      With some text blocks inside

      • even a list
      • should be
      • possible

      And well inline code should also work.

      Another Headline

      And not to forget link to progsource should work. And well - let's see how an image would be shown:

      \"an


      and more headlines

      even a table

    Left headermiddle headerlast header
    cell 1cell 2cell 3
    cell 4cell 5cell 6
    foot afoot bfoot c
    h5
    h6
    "; -const std::string testHtml2 = "

    This is a test

    This should result in a praragraph it's that simple.

    • an unordered list
      • with some hierarchy
        1. and an _ordered_
        2. list
        3. directly
      • inside
    \nvar c = 'blub';\n

    A Quote

    With some text blocks inside

    • even a list
    • should be
    • possible

    And well inline code should also work.

    Another Headline

    And not to forget link to progsource should work. And well - let's see how an image would be shown:

    \"an


    and more headlines

    even a table

    Left headermiddle headerlast header
    cell 1cell 2cell 3
    cell 4cell 5cell 6
    foot afoot bfoot c
    h5
    h6
    "; -const std::string testHtml3 = "# This is a test
    This should result in a praragraph it's that simple.
    * an *unordered* list * with some hierarchy 1. and an ordered * list * directly * inside
    ``` var c = 'blub'; ```
    > A Quote > > With some ~~text~~ blocks inside > > * even a list > * should be > * possible >
    And well `inline code` should also work.
    ## Another Headline
    And not to forget [link to progsource](http://progsource.de) should work. And well - let's see how an image would be shown:
    ![an image](http://progsource.de/img/progsource.png)
    ---

    ### and more headlines
    - [ ] how - [ ] about - [ ] a - [x] nice - [x] check - [ ] list
    #### even a table
    |table> Left header|middle header|last header - | - | - cell 1|cell 2|cell 3 cell 4|cell 5|cell 6 - | - | - foot a|foot b|foot c |##### h5 ###### h6
    "; +const std::string testHtml = + "

    This is a test

    This should result in a praragraph it's that " + "simple.

    • an unordered list
      • with some " + "hierarchy
        1. and an " + "ordered
        2. list
        3. directly
      • inside
    \nvar c = "
    +  "'blub';\n

    A Quote

    With some text " + "blocks inside

    • even a list
    • should be
    • possible " + "

    And well inline code should also " + "work.

    Another Headline

    And not to forget link to progsource should work. And well " + "- let's see how an image would be shown:

    \"an " + "


    and more headlines

    even a " + "table

    Left headermiddle " + "headerlast header
    cell " + "1cell 2cell 3
    cell " + "4cell 5cell 6
    foot " + "afoot bfoot " + "c
    h5
    h6
    "; +const std::string testHtml2 = + "

    This is a test

    This should result in a praragraph it's that " + "simple.

    • an unordered list
      • with some " + "hierarchy
        1. and an " + "_ordered_
        2. list
        3. directly
      • inside
    \nvar c = 'blub';\n

    A Quote " + "

    With some text blocks inside

    • even a list " + "
    • should be
    • possible

    And well " + "inline code should also work.

    Another " + "Headline

    And not to forget link to " + "progsource should work. And well - let's see how an image would be " + "shown:

    \"an


    and more headlines

    even a " + "table

    Left headermiddle " + "headerlast header
    cell " + "1cell 2cell 3
    cell " + "4cell 5cell 6
    foot " + "afoot bfoot " + "c
    h5
    h6
    "; +const std::string testHtml3 = + "# This is a test
    This should result in a praragraph it's that simple. " + "
    * an *unordered* list * with some hierarchy 1. " + "and an ordered * list * directly * inside
    ``` var c " + "= 'blub'; ```
    > A Quote > > With some ~~text~~ blocks inside > > * " + "even a list > * should be > * possible >
    And well `inline code` should " + "also work.
    ## Another Headline
    And not to forget [link to " + "progsource](http://progsource.de) should work. And well - let's see how an " + "image would be shown:
    ![an " + "image](http://progsource.de/img/progsource.png)
    ---

    ### and more headlines
    - [ ] how - [ ] about - [ ] a " + " - [x] nice - [x] check - [ ] list
    #### even a table
    |table> " + "Left header|middle header|last header - | - | - cell 1|cell " + "2|cell 3 cell 4|cell 5|cell 6 - | - | - foot a|foot b|foot " + "c |##### h5 ###### h6
    "; diff --git a/tests/maddy/test_maddy_quoteparser.cpp b/tests/maddy/test_maddy_quoteparser.cpp index 432a31d..f49f7d1 100644 --- a/tests/maddy/test_maddy_quoteparser.cpp +++ b/tests/maddy/test_maddy_quoteparser.cpp @@ -15,19 +15,17 @@ class MADDY_QUOTEPARSER : public ::testing::Test protected: std::shared_ptr quoteParser; - void - SetUp() override + void SetUp() override { - this->quoteParser = std::make_shared( - nullptr, - nullptr - ); + this->quoteParser = std::make_shared(nullptr, nullptr); } }; // ----------------------------------------------------------------------------- -TEST_F(MADDY_QUOTEPARSER, IsStartingLineReturnsTrueWhenFacedWithBeginningOfAQuote) +TEST_F( + MADDY_QUOTEPARSER, IsStartingLineReturnsTrueWhenFacedWithBeginningOfAQuote +) { ASSERT_TRUE(maddy::QuoteParser::IsStartingLine("> a")); } @@ -39,13 +37,7 @@ TEST_F(MADDY_QUOTEPARSER, IsFinishedAlwaysReturnsFalseInTheBeginning) TEST_F(MADDY_QUOTEPARSER, ItReplacesMarkdownWithAnHtmlBlockQuote) { - std::vector markdown = { - "> a" - , "> b" - , ">" - , "> c" - , "" - }; + std::vector markdown = {"> a", "> b", ">", "> c", ""}; std::string expected = "
    a b c
    "; for (std::string md : markdown) diff --git a/tests/maddy/test_maddy_strikethroughparser.cpp b/tests/maddy/test_maddy_strikethroughparser.cpp index b6efd04..a31108c 100644 --- a/tests/maddy/test_maddy_strikethroughparser.cpp +++ b/tests/maddy/test_maddy_strikethroughparser.cpp @@ -23,8 +23,10 @@ TEST(MADDY_STRIKETHROUGHPARSER, ItReplacesMarkdownWithStrikeThroughHTML) TEST(MADDY_STRIKETHROUGHPARSER, ItDoesNotParseInsideInlineCode) { - std::string text = "some text `~~bla~~` ` ~~text~~ ` testing ~~it~~ out"; - std::string expected = "some text `~~bla~~` ` ~~text~~ ` testing ~~it~~ out"; + std::string text = + "some text `~~bla~~` ` ~~text~~ ` testing ~~it~~ out"; + std::string expected = + "some text `~~bla~~` ` ~~text~~ ` testing ~~it~~ out"; auto strikeThroughParser = std::make_shared(); strikeThroughParser->Parse(text); diff --git a/tests/maddy/test_maddy_strongparser.cpp b/tests/maddy/test_maddy_strongparser.cpp index e054aa7..f006e26 100644 --- a/tests/maddy/test_maddy_strongparser.cpp +++ b/tests/maddy/test_maddy_strongparser.cpp @@ -18,16 +18,11 @@ TEST(MADDY_STRONGPARSER, ItReplacesMarkdownWithStrongHTML) std::string expected; }; - std::vector tests - { - { - "some text **bla** text testing **it** out", - "some text bla text testing it out" - }, - { - "some text __bla__ text testing __it__ out", - "some text bla text testing it out" - }, + std::vector tests{ + {"some text **bla** text testing **it** out", + "some text bla text testing it out"}, + {"some text __bla__ text testing __it__ out", + "some text bla text testing it out"}, }; auto strongParser = std::make_shared(); @@ -47,16 +42,11 @@ TEST(MADDY_STRONGPARSER, ItReplacesEmphasizedMarkdownNotWithStrongHTML) std::string expected; }; - std::vector tests - { - { - "some text *bla* text testing **it** out", - "some text *bla* text testing it out" - }, - { - "some text _bla_ text testing __it__ out", - "some text _bla_ text testing it out" - }, + std::vector tests{ + {"some text *bla* text testing **it** out", + "some text *bla* text testing it out"}, + {"some text _bla_ text testing __it__ out", + "some text _bla_ text testing it out"}, }; auto strongParser = std::make_shared(); @@ -76,16 +66,13 @@ TEST(MADDY_STRONGPARSER, ItDoesNotParseInsideInlineCode) std::string expected; }; - std::vector tests - { + std::vector tests{ { "some text **bla** `/**text**/` testing `**it**` out", "some text **bla** `/**text**/` testing `**it**` out", }, - { - "some text _bla_ text testing __it__ out", - "some text _bla_ text testing it out" - }, + {"some text _bla_ text testing __it__ out", + "some text _bla_ text testing it out"}, }; auto strongParser = std::make_shared(); diff --git a/tests/maddy/test_maddy_tableparser.cpp b/tests/maddy/test_maddy_tableparser.cpp index addb1ae..5d50d92 100644 --- a/tests/maddy/test_maddy_tableparser.cpp +++ b/tests/maddy/test_maddy_tableparser.cpp @@ -15,19 +15,17 @@ class MADDY_TABLEPARSER : public ::testing::Test protected: std::shared_ptr tableParser; - void - SetUp() override + void SetUp() override { - this->tableParser = std::make_shared( - nullptr, - nullptr - ); + this->tableParser = std::make_shared(nullptr, nullptr); } }; // ----------------------------------------------------------------------------- -TEST_F(MADDY_TABLEPARSER, IsStartingLineReturnsTrueWhenFacedWithTheBeginningOfATable) +TEST_F( + MADDY_TABLEPARSER, IsStartingLineReturnsTrueWhenFacedWithTheBeginningOfATable +) { ASSERT_EQ(true, maddy::TableParser::IsStartingLine("|table>")); } @@ -40,16 +38,21 @@ TEST_F(MADDY_TABLEPARSER, IsFinishedReturnsFalseInTheBeginning) TEST_F(MADDY_TABLEPARSER, ItReplacesMarkdownWithAnHtmlTable) { std::vector markdown = { - "|table>" - , "Left header|middle header|last header" - , "- | - | -" - , "cell 1|cell 2|cell 3" - , "cell 4|cell 5|cell 6" - , "- | - | -" - , "foot a|foot b|foot c" - , "|", + "Left header|middle header|last header", + "- | - | -", + "cell 1|cell 2|cell 3", + "cell 4|cell 5|cell 6", + "- | - | -", + "foot a|foot b|foot c", + "|
    Left headermiddle headerlast " + "header
    cell 1cell 2cell " + "3
    cell 4cell 5cell " + "6
    foot afoot bfoot " + "c
    "; for (std::string md : markdown) { diff --git a/tests/maddy/test_maddy_unorderedlistparser.cpp b/tests/maddy/test_maddy_unorderedlistparser.cpp index eed8427..5f9a551 100644 --- a/tests/maddy/test_maddy_unorderedlistparser.cpp +++ b/tests/maddy/test_maddy_unorderedlistparser.cpp @@ -15,10 +15,10 @@ class MADDY_UNORDEREDLISTPARSER : public ::testing::Test protected: std::shared_ptr ulParser; - void - SetUp() override + void SetUp() override { - std::function(const std::string& line)> getBlockParserForLineCallback = [](const std::string& line) + std::function(const std::string& line)> + getBlockParserForLineCallback = [](const std::string& line) { if (maddy::UnorderedListParser::IsStartingLine(line)) { @@ -32,15 +32,17 @@ class MADDY_UNORDEREDLISTPARSER : public ::testing::Test }; this->ulParser = std::make_shared( - nullptr, - getBlockParserForLineCallback + nullptr, getBlockParserForLineCallback ); } }; // ----------------------------------------------------------------------------- -TEST_F(MADDY_UNORDEREDLISTPARSER, IsStartingLineReturnsTrueWhenFacedWithBeginningOfList) +TEST_F( + MADDY_UNORDEREDLISTPARSER, + IsStartingLineReturnsTrueWhenFacedWithBeginningOfList +) { ASSERT_TRUE(maddy::UnorderedListParser::IsStartingLine("* a")); } @@ -53,16 +55,11 @@ TEST_F(MADDY_UNORDEREDLISTPARSER, IsFinishedAlwaysReturnsFalseInTheBeginning) TEST_F(MADDY_UNORDEREDLISTPARSER, ItReplacesMarkdownWithAnHtmlUnorderedList) { std::vector markdown = { - "* a" - , "* b" - , "- c" - , "- d" - , "+ e" - , "+ f" - , "* g" - , "" + "* a", "* b", "- c", "- d", "+ e", "+ f", "* g", "" }; - std::string expected = "
    • a
    • b
    • c
    • d
    • e
    • f
    • g
    "; + std::string expected = + "
    • a
    • b
    • c
    • d
    • e
    • f
    • g
    "; for (std::string md : markdown) { @@ -80,17 +77,11 @@ TEST_F(MADDY_UNORDEREDLISTPARSER, ItReplacesMarkdownWithAnHtmlUnorderedList) TEST_F(MADDY_UNORDEREDLISTPARSER, ItReplacesMarkdownWithAnHierachicalHtmlList) { std::vector markdown = { - "* a" - , " * d" - , " * e" - , "* b" - , " * c" - , " + x" - , " + y" - , " - z" - , "" + "* a", " * d", " * e", "* b", " * c", " + x", " + y", " - z", "" }; - std::string expected = "
    • a
      • d
      • e
    • b
      • c
      • x
      • y
      • z
    "; + std::string expected = + "
    • a
      • d
      • e
    • b
      • c
      • x
      • y
      • z
    "; for (std::string md : markdown) { diff --git a/tests/main.cpp b/tests/main.cpp index d61102e..2bf4c98 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -7,7 +7,8 @@ // ----------------------------------------------------------------------------- -int main (int argc, char** argv) { +int main(int argc, char** argv) +{ ::testing::GTEST_FLAG(throw_on_failure) = true; ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); diff --git a/tools/format.py b/tools/format.py new file mode 100644 index 0000000..7ee9403 --- /dev/null +++ b/tools/format.py @@ -0,0 +1,143 @@ +import glob +import os +import shlex +import subprocess +import sys + +# required clang-format version +REQUIRED_VERSION = "18.1.3" + +def check_clang_format_version(): + """ + Check if the installed clang-format version matches the required version. + + Raises: + SystemExit: If the version does not match the required version or if + there is an error checking the version. + """ + try: + result = subprocess.run( + ['clang-format', '--version'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + check=True + ) + version_out = result.stdout.strip().split() + + version_line = '0.0.0' + is_next = False + + for vo in version_out: + if vo == 'version': + is_next = True + continue + if is_next: + version_line = vo + break + + if version_line != REQUIRED_VERSION: + print(f"Error: Required clang-format version is " + f"{REQUIRED_VERSION}, but found {version_line}.") + sys.exit(1) + else: + print('clang-format version equals the required version.') + except subprocess.CalledProcessError as e: + print(f"Error checking clang-format version: {e.stderr}") + sys.exit(1) + +def format_files(dry_run): + """ + Format .h and .cpp files in specified directories using clang-format. + + Args: + dry_run (bool): If True, performs a dry run to check for formatting + issues without modifying files. If False, formats + the files in place. + + Raises: + subprocess.CalledProcessError: If there is an error running clang-format + during the actual formatting process. + """ + patterns = [ + "include/**/*.h", + "tests/**/*.h", + "tests/**/*.cpp", + ] + + files_to_format = [] + for pattern in patterns: + matched_files = glob.glob(pattern, recursive=True) + files_to_format.extend(matched_files) + + if not files_to_format: + print("No files to format.") + return + + # Create a space-separated string of files + patterns_arg = ' '.join(file.replace('\\', '/') for file in files_to_format) + + cwd = os.getcwd() + + if dry_run: + # Check for changes without modifying the files + command = f'clang-format --style=file --dry-run --Werror {patterns_arg}' + result = subprocess.run( + shlex.split(command), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + cwd=cwd + ) + if result.returncode != 0: + print("Files that need formatting:") + print(result.stdout) + print("Error output:") + print(result.stderr) + sys.exit(1) + else: + print("no changes found") + else: + # Format the files in place + command = f'clang-format --style=file -i {patterns_arg}' + result = subprocess.run( + shlex.split(command), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + cwd=cwd + ) + if result.returncode != 0: + print("Error formatting files:") + print(result.stderr) + sys.exit(1) + else: + print('formatting done') + +def main(): + """ + Main function to handle command-line arguments and invoke formatting. + + Checks for the required command-line argument, verifies the clang-format + version, and calls the format_files function to format the code. + """ + if len(sys.argv) != 2: + print( + "Usage: python check_clang_format.py " + ) + sys.exit(1) + else: + print(f"Running format with {sys.argv[1]}") + + dry_run = False + if sys.argv[1] == "dry_run": + dry_run = True + elif sys.argv[1] != "format": + print("Invalid argument. Use 'dry_run' or 'format'.") + sys.exit(1) + + check_clang_format_version() + format_files(dry_run) + +if __name__ == "__main__": + main()