chore: Add clang-format

This commit is contained in:
Petra Baranski
2024-11-16 05:50:22 +01:00
committed by GitHub
parent 1f12bc15b6
commit d0dae091e0
48 changed files with 766 additions and 774 deletions

46
.clang-format Normal file
View File

@@ -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

View File

@@ -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

25
.github/workflows/run-checks.yml vendored Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -10,8 +10,8 @@
#include <sstream>
#include <string>
// windows compatibility includes
#include <cctype>
#include <algorithm>
#include <cctype>
// -----------------------------------------------------------------------------
@@ -36,11 +36,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
BlockParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback
)
: result("", std::ios_base::ate | std::ios_base::in | std::ios_base::out)
, childParser(nullptr)
@@ -64,8 +66,7 @@ public:
* @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 @@ public:
* @method
* @return {std::stringstream}
*/
std::stringstream&
GetResult()
{
return this->result;
}
std::stringstream& GetResult() { return this->result; }
/**
* Clear
@@ -129,11 +126,7 @@ public:
* @method
* @return {void}
*/
void
Clear()
{
this->result.str("");
}
void Clear() { this->result.str(""); }
protected:
std::stringstream result;
@@ -143,8 +136,7 @@ protected:
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,13 +144,11 @@ protected:
}
}
uint32_t
getIndentationWidth(const std::string& line) const
uint32_t getIndentationWidth(const std::string& line) const
{
bool hasMetNonSpace = false;
uint32_t indentation = static_cast<uint32_t>(
std::count_if(
uint32_t indentation = static_cast<uint32_t>(std::count_if(
line.begin(),
line.end(),
[&hasMetNonSpace](unsigned char c)
@@ -176,14 +166,12 @@ protected:
hasMetNonSpace = true;
return false;
}
)
);
));
return indentation;
}
std::shared_ptr<BlockParser>
getBlockParserForLine(const std::string& line)
std::shared_ptr<BlockParser> getBlockParserForLine(const std::string& line)
{
if (getBlockParserForLineCallback)
{
@@ -195,7 +183,8 @@ protected:
private:
std::function<void(std::string&)> parseLineCallback;
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback;
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback;
}; // class BlockParser
// -----------------------------------------------------------------------------

View File

@@ -6,8 +6,8 @@
// -----------------------------------------------------------------------------
#include <string>
#include <regex>
#include <string>
#include "maddy/lineparser.h"
@@ -36,8 +36,7 @@ public:
* @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 = "<br>";

View File

@@ -31,11 +31,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
ChecklistParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
, isStarted(false)
@@ -51,8 +53,7 @@ public:
* @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 @@ public:
* @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 @@ protected:
line = std::regex_replace(line, emptyBoxRegex, emptyBoxReplacement);
static std::regex boxRegex(R"(^\[x\])");
static std::string boxReplacement = "<input type=\"checkbox\" checked=\"checked\"/>";
static std::string boxReplacement =
"<input type=\"checkbox\" checked=\"checked\"/>";
line = std::regex_replace(line, boxRegex, boxReplacement);
if (!this->isStarted)
@@ -113,11 +102,9 @@ protected:
return;
}
if (
line.empty() ||
if (line.empty() ||
line.find("</label></li><li><label>") != std::string::npos ||
line.find("</label></li></ul>") != std::string::npos
)
line.find("</label></li></ul>") != std::string::npos)
{
line = "</label></li></ul>" + line;
this->isFinished = true;

View File

@@ -7,8 +7,8 @@
// -----------------------------------------------------------------------------
#include <functional>
#include <string>
#include <regex>
#include <string>
#include "maddy/blockparser.h"
@@ -47,11 +47,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
CodeBlockParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
, isStarted(false)
@@ -71,8 +73,7 @@ public:
* @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 @@ public:
* @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 == "```")
{

View File

@@ -6,8 +6,8 @@
// -----------------------------------------------------------------------------
#include <string>
#include <regex>
#include <string>
#include "maddy/lineparser.h"
@@ -38,10 +38,11 @@ public:
* @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>.*)([^_]*)_(?!.*`.*|.*<\/code>.*))");
static std::regex re(
R"((?!.*`.*|.*<code>.*)_(?!.*`.*|.*<\/code>.*)([^_]*)_(?!.*`.*|.*<\/code>.*))"
);
static std::string replacement = "<em>$1</em>";
line = std::regex_replace(line, re, replacement);

View File

@@ -7,8 +7,8 @@
// -----------------------------------------------------------------------------
#include <functional>
#include <string>
#include <regex>
#include <string>
#include "maddy/blockparser.h"
@@ -53,11 +53,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
HeadlineParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback,
bool isInlineParserAllowed = true
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
@@ -73,8 +75,7 @@ public:
* @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 @@ public:
* @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<std::regex> 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<std::string> hlReplacement = {
"<h1>$1</h1>"
, "<h2>$1</h2>"
, "<h3>$1</h3>"
, "<h4>$1</h4>"
, "<h5>$1</h5>"
, "<h6>$1</h6>"
"<h1>$1</h1>",
"<h2>$1</h2>",
"<h3>$1</h3>",
"<h4>$1</h4>",
"<h5>$1</h5>",
"<h6>$1</h6>"
};
for (uint8_t i = 0; i < 6; ++i)

View File

@@ -7,8 +7,8 @@
// -----------------------------------------------------------------------------
#include <functional>
#include <string>
#include <regex>
#include <string>
#include "maddy/blockparser.h"
@@ -35,11 +35,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
HorizontalLineParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
, lineRegex("^---$")
@@ -54,8 +56,7 @@ public:
* @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 @@ public:
* @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 = "<hr/>";

View File

@@ -30,11 +30,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
HtmlParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
, isStarted(false)
@@ -52,11 +54,7 @@ public:
* @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 @@ public:
* @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)
{

View File

@@ -6,8 +6,8 @@
// -----------------------------------------------------------------------------
#include <string>
#include <regex>
#include <string>
#include "maddy/lineparser.h"
@@ -38,8 +38,7 @@ public:
* @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 = "<img src=\"$2\" alt=\"$1\"/>";

View File

@@ -6,8 +6,8 @@
// -----------------------------------------------------------------------------
#include <string>
#include <regex>
#include <string>
#include "maddy/lineparser.h"
@@ -36,8 +36,7 @@ public:
* @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 = "<code>$1</code>";

View File

@@ -6,8 +6,8 @@
// -----------------------------------------------------------------------------
#include <string>
#include <regex>
#include <string>
#include "maddy/lineparser.h"
@@ -36,10 +36,11 @@ public:
* @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>.*)([^\*]*)\*(?!.*`.*|.*<\/code>.*))");
static std::regex re(
R"((?!.*`.*|.*<code>.*)\*(?!.*`.*|.*<\/code>.*)([^\*]*)\*(?!.*`.*|.*<\/code>.*))"
);
static std::string replacement = "<i>$1</i>";
line = std::regex_replace(line, re, replacement);
}

View File

@@ -7,8 +7,8 @@
// -----------------------------------------------------------------------------
#include <functional>
#include <string>
#include <regex>
#include <string>
#include "maddy/blockparser.h"
@@ -51,11 +51,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
LatexBlockParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
, isStarted(false)
@@ -75,8 +77,7 @@ public:
* @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 @@ public:
* @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 @@ protected:
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;

View File

@@ -6,8 +6,8 @@
// -----------------------------------------------------------------------------
#include <string>
#include <regex>
#include <string>
#include "maddy/lineparser.h"
@@ -38,8 +38,7 @@ public:
* @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 = "<a href=\"$2\">$1</a>";

View File

@@ -31,11 +31,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
OrderedListParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
, isStarted(false)
@@ -51,8 +53,7 @@ public:
* @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 @@ public:
* @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 @@ protected:
return;
}
if (
line.empty() ||
line.find("</li><li>") != std::string::npos ||
if (line.empty() || line.find("</li><li>") != std::string::npos ||
line.find("</li></ol>") != std::string::npos ||
line.find("</li></ul>") != std::string::npos
)
line.find("</li></ul>") != std::string::npos)
{
line = "</li></ol>" + line;
this->isFinished = true;
@@ -129,8 +114,7 @@ private:
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);

View File

@@ -30,11 +30,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
ParagraphParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback,
bool isEnabled
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
@@ -54,11 +56,7 @@ public:
* @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 @@ public:
* @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)
{

View File

@@ -6,8 +6,8 @@
// -----------------------------------------------------------------------------
#include <memory>
#include <functional>
#include <memory>
#include <string>
#include "maddy/parserconfig.h"
@@ -57,7 +57,11 @@ public:
* 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 @@ public:
*
* @method
*/
Parser(std::shared_ptr<ParserConfig> config = nullptr)
: config(config)
Parser(std::shared_ptr<ParserConfig> config = nullptr) : config(config)
{
// deprecated backward compatibility
// will be removed in 1.4.0 latest including the booleans
@@ -80,66 +83,50 @@ public:
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<BreakLineParser>();
}
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<EmphasizedParser>();
}
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<ImageParser>();
}
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<InlineCodeParser>();
}
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<ItalicParser>();
}
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<LinkParser>();
}
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<StrikeThroughParser>();
}
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<StrongParser>();
}
@@ -152,8 +139,7 @@ public:
* @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<BlockParser> currentBlockParser = nullptr;
@@ -204,8 +190,7 @@ private:
std::shared_ptr<StrongParser> 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 @@ private:
}
}
std::shared_ptr<BlockParser>
getBlockParserForLine(const std::string& line) const
std::shared_ptr<BlockParser> getBlockParserForLine(const std::string& line
) const
{
std::shared_ptr<BlockParser> 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<maddy::CodeBlockParser>(
nullptr,
nullptr
);
parser = std::make_shared<maddy::CodeBlockParser>(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<LatexBlockParser>(
nullptr,
nullptr
);
parser = std::make_shared<LatexBlockParser>(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<maddy::HeadlineParser>(
[this](std::string& line){ this->runLineParser(line); },
[this](std::string& line) { this->runLineParser(line); },
nullptr,
true
);
}
else
{
parser = std::make_shared<maddy::HeadlineParser>(
nullptr,
nullptr,
false
);
parser =
std::make_shared<maddy::HeadlineParser>(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<maddy::HorizontalLineParser>(
nullptr,
nullptr
);
parser = std::make_shared<maddy::HorizontalLineParser>(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<maddy::QuoteParser>(
[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<maddy::TableParser>(
[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 &&
else if (this->config &&
(this->config->enabledParsers & maddy::types::HTML_PARSER) != 0 &&
maddy::HtmlParser::IsStartingLine(line)
)
maddy::HtmlParser::IsStartingLine(line))
{
parser = std::make_shared<maddy::HtmlParser>(nullptr, nullptr);
}
else if (
maddy::ParagraphParser::IsStartingLine(line)
)
else if (maddy::ParagraphParser::IsStartingLine(line))
{
parser = std::make_shared<maddy::ParagraphParser>(
[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<BlockParser>
createChecklistParser() const
std::shared_ptr<BlockParser> createChecklistParser() const
{
return std::make_shared<maddy::ChecklistParser>(
[this](std::string& line){ this->runLineParser(line); },
[this](std::string& line) { this->runLineParser(line); },
[this](const std::string& line)
{
std::shared_ptr<BlockParser> 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 @@ private:
);
}
std::shared_ptr<BlockParser>
createOrderedListParser() const
std::shared_ptr<BlockParser> createOrderedListParser() const
{
return std::make_shared<maddy::OrderedListParser>(
[this](std::string& line){ this->runLineParser(line); },
[this](std::string& line) { this->runLineParser(line); },
[this](const std::string& line)
{
std::shared_ptr<BlockParser> 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
else if ((!this->config || (this->config->enabledParsers &
maddy::types::UNORDERED_LIST_PARSER) != 0
) &&
maddy::UnorderedListParser::IsStartingLine(line)
)
maddy::UnorderedListParser::IsStartingLine(line))
{
parser = this->createUnorderedListParser();
}
@@ -455,32 +379,24 @@ private:
);
}
std::shared_ptr<BlockParser>
createUnorderedListParser() const
std::shared_ptr<BlockParser> createUnorderedListParser() const
{
return std::make_shared<maddy::UnorderedListParser>(
[this](std::string& line){ this->runLineParser(line); },
[this](std::string& line) { this->runLineParser(line); },
[this](const std::string& line)
{
std::shared_ptr<BlockParser> 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
else if ((!this->config || (this->config->enabledParsers &
maddy::types::UNORDERED_LIST_PARSER) != 0
) &&
maddy::UnorderedListParser::IsStartingLine(line)
)
maddy::UnorderedListParser::IsStartingLine(line))
{
parser = this->createUnorderedListParser();
}

View File

@@ -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

View File

@@ -31,11 +31,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
QuoteParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
, isStarted(false)
@@ -51,8 +53,7 @@ public:
* @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 @@ public:
* @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 @@ public:
* @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, "");

View File

@@ -6,8 +6,8 @@
// -----------------------------------------------------------------------------
#include <string>
#include <regex>
#include <string>
#include "maddy/lineparser.h"
@@ -36,10 +36,11 @@ public:
* @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>.*)([^\~]*)\~\~(?!.*`.*|.*<\/code>.*))");
static std::regex re(
R"((?!.*`.*|.*<code>.*)\~\~(?!.*`.*|.*<\/code>.*)([^\~]*)\~\~(?!.*`.*|.*<\/code>.*))"
);
static std::string replacement = "<s>$1</s>";
line = std::regex_replace(line, re, replacement);

View File

@@ -6,8 +6,8 @@
// -----------------------------------------------------------------------------
#include <string>
#include <regex>
#include <string>
#include "maddy/lineparser.h"
@@ -38,13 +38,15 @@ public:
* @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<std::regex> res
{
std::regex{R"((?!.*`.*|.*<code>.*)\*\*(?!.*`.*|.*<\/code>.*)([^\*\*]*)\*\*(?!.*`.*|.*<\/code>.*))"},
std::regex{R"((?!.*`.*|.*<code>.*)__(?!.*`.*|.*<\/code>.*)([^__]*)__(?!.*`.*|.*<\/code>.*))"}
static std::vector<std::regex> res{
std::regex{
R"((?!.*`.*|.*<code>.*)\*\*(?!.*`.*|.*<\/code>.*)([^\*\*]*)\*\*(?!.*`.*|.*<\/code>.*))"
},
std::regex{
R"((?!.*`.*|.*<code>.*)__(?!.*`.*|.*<\/code>.*)([^__]*)__(?!.*`.*|.*<\/code>.*))"
}
};
static std::string replacement = "<strong>$1</strong>";
for (const auto& re : res)

View File

@@ -7,8 +7,8 @@
// -----------------------------------------------------------------------------
#include <functional>
#include <string>
#include <regex>
#include <string>
#include "maddy/blockparser.h"
@@ -33,11 +33,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
TableParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
, isStarted(false)
@@ -55,8 +57,7 @@ public:
* @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 @@ public:
* @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 @@ public:
* @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 << "<table>";

View File

@@ -31,11 +31,13 @@ public:
*
* @method
* @param {std::function<void(std::string&)>} parseLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
* @param {std::function<std::shared_ptr<BlockParser>(const std::string&
* line)>} getBlockParserForLineCallback
*/
UnorderedListParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)>
getBlockParserForLineCallback
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
, isStarted(false)
@@ -51,8 +53,7 @@ public:
* @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 @@ public:
* @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 @@ protected:
return;
}
if (
line.empty() ||
line.find("</li><li>") != std::string::npos ||
if (line.empty() || line.find("</li><li>") != std::string::npos ||
line.find("</li></ol>") != std::string::npos ||
line.find("</li></ul>") != std::string::npos
)
line.find("</li></ul>") != std::string::npos)
{
line = "</li></ul>" + line;
this->isFinished = true;

View File

@@ -15,10 +15,10 @@ class MADDY_CHECKLISTPARSER : public ::testing::Test
protected:
std::shared_ptr<maddy::ChecklistParser> clParser;
void
SetUp() override
void SetUp() override
{
std::function<std::shared_ptr<maddy::BlockParser>(const std::string& line)> getBlockParserForLineCallback = [](const std::string& line)
std::function<std::shared_ptr<maddy::BlockParser>(const std::string& line)>
getBlockParserForLineCallback = [](const std::string& line)
{
if (maddy::ChecklistParser::IsStartingLine(line))
{
@@ -32,15 +32,16 @@ protected:
};
this->clParser = std::make_shared<maddy::ChecklistParser>(
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<std::string> markdown = {
"- [ ] a"
, "- [x] b"
, ""
};
std::string expected = "<ul class=\"checklist\"><li><label><input type=\"checkbox\"/> a</label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> b</label></li></ul>";
std::vector<std::string> markdown = {"- [ ] a", "- [x] b", ""};
std::string expected =
"<ul class=\"checklist\"><li><label><input type=\"checkbox\"/> "
"a</label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> "
"b</label></li></ul>";
for (std::string md : markdown)
{
@@ -76,14 +76,15 @@ TEST_F(MADDY_CHECKLISTPARSER, ItReplacesMarkdownWithAnHtmlChecklist)
TEST_F(MADDY_CHECKLISTPARSER, ItReplacesMarkdownWithAnHierachicalHtmlList)
{
std::vector<std::string> markdown = {
"- [ ] a"
, " - [ ] d"
, " - [ ] e"
, "- [ ] b"
, " - [x] c"
, ""
"- [ ] a", " - [ ] d", " - [ ] e", "- [ ] b", " - [x] c", ""
};
std::string expected = "<ul class=\"checklist\"><li><label><input type=\"checkbox\"/> a<ul class=\"checklist\"><li><label><input type=\"checkbox\"/> d</label></li><li><label><input type=\"checkbox\"/> e</label></li></ul></label></li><li><label><input type=\"checkbox\"/> b<ul class=\"checklist\"><li><label><input type=\"checkbox\" checked=\"checked\"/> c</label></li></ul></label></li></ul>";
std::string expected =
"<ul class=\"checklist\"><li><label><input type=\"checkbox\"/> a<ul "
"class=\"checklist\"><li><label><input type=\"checkbox\"/> "
"d</label></li><li><label><input type=\"checkbox\"/> "
"e</label></li></ul></label></li><li><label><input type=\"checkbox\"/> "
"b<ul class=\"checklist\"><li><label><input type=\"checkbox\" "
"checked=\"checked\"/> c</label></li></ul></label></li></ul>";
for (std::string md : markdown)
{

View File

@@ -15,13 +15,9 @@ class MADDY_CODEBLOCKPARSER : public ::testing::Test
protected:
std::shared_ptr<maddy::CodeBlockParser> cbParser;
void
SetUp() override
void SetUp() override
{
this->cbParser = std::make_shared<maddy::CodeBlockParser>(
nullptr,
nullptr
);
this->cbParser = std::make_shared<maddy::CodeBlockParser>(nullptr, nullptr);
}
};
@@ -40,13 +36,11 @@ TEST_F(MADDY_CODEBLOCKPARSER, IsFinishedReturnsFalseInTheBeginning)
TEST_F(MADDY_CODEBLOCKPARSER, ItReplacesMarkdownWithAnHtmlCodeBlock)
{
std::vector<std::string> markdown = {
"```"
, "some code"
, "some other code"
, "```"
"```", "some code", "some other code", "```"
};
std::string expected = "<pre><code>\nsome code\nsome other code\n</code></pre>";
std::string expected =
"<pre><code>\nsome code\nsome other code\n</code></pre>";
for (std::string md : markdown)
{
@@ -63,13 +57,11 @@ TEST_F(MADDY_CODEBLOCKPARSER, ItReplacesMarkdownWithAnHtmlCodeBlock)
TEST_F(MADDY_CODEBLOCKPARSER, ItShouldUseAnythingBehindFirstBackticksAsClass)
{
std::vector<std::string> markdown = {
"```cpp"
, "some code"
, "some other code"
, "```"
"```cpp", "some code", "some other code", "```"
};
std::string expected = "<pre class=\"cpp\"><code>\nsome code\nsome other code\n</code></pre>";
std::string expected =
"<pre class=\"cpp\"><code>\nsome code\nsome other code\n</code></pre>";
for (std::string md : markdown)
{

View File

@@ -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 <em>it</em> out";
std::string expected =
"some text `*bla*` `/**text*/` testing <em>it</em> out";
auto emphasizedParser = std::make_shared<maddy::EmphasizedParser>();
emphasizedParser->Parse(text);

View File

@@ -15,19 +15,17 @@ class MADDY_HEADLINEPARSER : public ::testing::Test
protected:
std::shared_ptr<maddy::HeadlineParser> hlParser;
void
SetUp() override
void SetUp() override
{
this->hlParser = std::make_shared<maddy::HeadlineParser>(
nullptr,
nullptr
);
this->hlParser = std::make_shared<maddy::HeadlineParser>(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<std::string> markdown = {
"# a"
, "## a"
, "### a"
, "#### a"
, "##### a"
, "###### a"
"# a", "## a", "### a", "#### a", "##### a", "###### a"
};
std::vector<std::string> expected = {
"<h1>a</h1>"
, "<h2>a</h2>"
, "<h3>a</h3>"
, "<h4>a</h4>"
, "<h5>a</h5>"
, "<h6>a</h6>"
"<h1>a</h1>",
"<h2>a</h2>",
"<h3>a</h3>",
"<h4>a</h4>",
"<h5>a</h5>",
"<h6>a</h6>"
};
for (uint8_t i = 0; i < 6; ++i)

View File

@@ -15,19 +15,18 @@ class MADDY_HORIZONTALLINEPARSER : public ::testing::Test
protected:
std::shared_ptr<maddy::HorizontalLineParser> hlParser;
void
SetUp() override
void SetUp() override
{
this->hlParser = std::make_shared<maddy::HorizontalLineParser>(
nullptr,
nullptr
);
this->hlParser =
std::make_shared<maddy::HorizontalLineParser>(nullptr, nullptr);
}
};
// -----------------------------------------------------------------------------
TEST_F(MADDY_HORIZONTALLINEPARSER, IsStartingLineReturnsTrueWhenFacedWithThreeDashes)
TEST_F(
MADDY_HORIZONTALLINEPARSER, IsStartingLineReturnsTrueWhenFacedWithThreeDashes
)
{
ASSERT_TRUE(maddy::HorizontalLineParser::IsStartingLine("---"));
}

View File

@@ -15,13 +15,9 @@ class MADDY_HTMLPARSER : public ::testing::Test
protected:
std::shared_ptr<maddy::HtmlParser> pParser;
void
SetUp() override
void SetUp() override
{
this->pParser = std::make_shared<maddy::HtmlParser>(
nullptr,
nullptr
);
this->pParser = std::make_shared<maddy::HtmlParser>(nullptr, nullptr);
}
};
@@ -35,11 +31,7 @@ TEST_F(MADDY_HTMLPARSER, IsFinishedReturnsFalseInTheBeginning)
TEST_F(MADDY_HTMLPARSER, IsStartingLineReturnsFalseWhenFacedWithNoSmallerThan)
{
const std::vector<std::string> 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<std::string> markdown {
"some text in a paragraph"
, ""
, "<div> some HTML</div>"
, ""
, "<div>more"
, "HTML"
, "</div>"
, ""
const std::vector<std::string> markdown{
"some text in a paragraph",
"",
"<div> some HTML</div>",
"",
"<div>more",
"HTML",
"</div>",
""
};
const std::string expected = "some text in a paragraph <div> some HTML</div><div>more HTML </div>";
const std::string expected =
"some text in a paragraph <div> some HTML</div><div>more HTML </div>";
for (std::string md : markdown)
{

View File

@@ -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 <img src=\"http://example.com/a.png\" alt=\"Image Title\"/>";
std::string expected =
"Some text <img src=\"http://example.com/a.png\" alt=\"Image Title\"/>";
auto imageParser = std::make_shared<maddy::ImageParser>();
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 <img src=\"http://example.com/a.png\" alt=\"Image Title\"/> bla <img src=\"http://example.com/a.png\" alt=\"Image Title\"/>";
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 <img src=\"http://example.com/a.png\" alt=\"Image Title\"/> bla "
"<img src=\"http://example.com/a.png\" alt=\"Image Title\"/>";
auto imageParser = std::make_shared<maddy::ImageParser>();
imageParser->Parse(text);

View File

@@ -13,7 +13,8 @@
TEST(MADDY_INLINECODEPARSER, ItReplacesMarkdownWithCodeHTML)
{
std::string text = "some text `bla` text testing `it` out";
std::string expected = "some text <code>bla</code> text testing <code>it</code> out";
std::string expected =
"some text <code>bla</code> text testing <code>it</code> out";
auto emphasizedParser = std::make_shared<maddy::InlineCodeParser>();
emphasizedParser->Parse(text);

View File

@@ -12,7 +12,6 @@
TEST(MADDY_ITALICPARSER, ItReplacesMarkdownWithItalicHTML)
{
std::string text = "some text *bla* text testing *it* out";
std::string expected = "some text <i>bla</i> text testing <i>it</i> out";
auto italicParser = std::make_shared<maddy::ItalicParser>();

View File

@@ -15,13 +15,10 @@ class MADDY_LATEXBLOCKPARSER : public ::testing::Test
protected:
std::shared_ptr<maddy::LatexBlockParser> lbParser;
void
SetUp() override
void SetUp() override
{
this->lbParser = std::make_shared<maddy::LatexBlockParser>(
nullptr,
nullptr
);
this->lbParser =
std::make_shared<maddy::LatexBlockParser>(nullptr, nullptr);
}
};
@@ -60,9 +57,7 @@ TEST_F(MADDY_LATEXBLOCKPARSER, ItReplacesOneLineMarkdownWithALatexBlock)
TEST_F(MADDY_LATEXBLOCKPARSER, ItReplacesABlockMarkdownWithALatexBlock)
{
std::vector<std::string> 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<std::string> 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";

View File

@@ -13,7 +13,8 @@
TEST(MADDY_LINKPARSER, ItReplacesMarkdownWithALink)
{
std::string text = "Some text [Link Title](http://example.com)";
std::string expected = "Some text <a href=\"http://example.com\">Link Title</a>";
std::string expected =
"Some text <a href=\"http://example.com\">Link Title</a>";
auto linkParser = std::make_shared<maddy::LinkParser>();
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 <a href=\"http://example.com\">Link Title</a> bla <a href=\"http://example.com\">Link Title</a>";
std::string text =
"Some text [Link Title](http://example.com) bla [Link "
"Title](http://example.com)";
std::string expected =
"Some text <a href=\"http://example.com\">Link Title</a> bla <a "
"href=\"http://example.com\">Link Title</a>";
auto linkParser = std::make_shared<maddy::LinkParser>();
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

View File

@@ -15,10 +15,10 @@ class MADDY_ORDEREDLISTPARSER : public ::testing::Test
protected:
std::shared_ptr<maddy::OrderedListParser> olParser;
void
SetUp() override
void SetUp() override
{
std::function<std::shared_ptr<maddy::BlockParser>(const std::string& line)> getBlockParserForLineCallback = [](const std::string& line)
std::function<std::shared_ptr<maddy::BlockParser>(const std::string& line)>
getBlockParserForLineCallback = [](const std::string& line)
{
if (maddy::OrderedListParser::IsStartingLine(line))
{
@@ -32,15 +32,16 @@ protected:
};
this->olParser = std::make_shared<maddy::OrderedListParser>(
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<std::string> markdown = {
"1. a"
, "* b"
, ""
};
std::vector<std::string> markdown = {"1. a", "* b", ""};
std::string expected = "<ol><li>a</li><li>b</li></ol>";
for (std::string md : markdown)
@@ -75,14 +72,11 @@ TEST_F(MADDY_ORDEREDLISTPARSER, ItReplacesMarkdownWithAnHtmlOrderedList)
TEST_F(MADDY_ORDEREDLISTPARSER, ItReplacesMarkdownWithAnHierachicalHtmlList)
{
std::vector<std::string> markdown = {
"1. a"
, " 1. d"
, " * e"
, "* b"
, " 1. c"
, ""
"1. a", " 1. d", " * e", "* b", " 1. c", ""
};
std::string expected = "<ol><li>a<ol><li>d</li><li>e</li></ol></li><li>b<ol><li>c</li></ol></li></ol>";
std::string expected =
"<ol><li>a<ol><li>d</li><li>e</li></ol></li><li>b<ol><li>c</li></ol></li></"
"ol>";
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<std::string> markdown = {
"1. a"
, "94. b"
, "103. c"
, ""
};
std::vector<std::string> markdown = {"1. a", "94. b", "103. c", ""};
std::string expected = "<ol><li>a</li><li>b</li><li>c</li></ol>";
for (std::string md : markdown)

View File

@@ -15,14 +15,10 @@ class MADDY_PARAGRAPHPARSER : public ::testing::Test
protected:
std::shared_ptr<maddy::ParagraphParser> pParser;
void
SetUp() override
void SetUp() override
{
this->pParser = std::make_shared<maddy::ParagraphParser>(
nullptr,
nullptr,
true
);
this->pParser =
std::make_shared<maddy::ParagraphParser>(nullptr, nullptr, true);
}
};
@@ -40,11 +36,7 @@ TEST_F(MADDY_PARAGRAPHPARSER, IsFinishedReturnsFalseInTheBeginning)
TEST_F(MADDY_PARAGRAPHPARSER, ItReplacesMarkdownWithAnHtmlLine)
{
std::vector<std::string> markdown = {
"Some text"
, "and some other text"
, ""
};
std::vector<std::string> markdown = {"Some text", "and some other text", ""};
std::string expected = "<p>Some text and some other text </p>";
for (std::string md : markdown)

View File

@@ -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<maddy::ParserConfig>();
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<maddy::Parser>(config);
@@ -70,7 +71,8 @@ TEST(MADDY_PARSER, ItShouldParseInlineCodeInHeadlines)
const std::string headlineTest = R"(
# Some **test** markdown
)";
const std::string expectedHTML = "<h1>Some <strong>test</strong> markdown</h1>";
const std::string expectedHTML =
"<h1>Some <strong>test</strong> markdown</h1>";
std::stringstream markdown(headlineTest);
auto parser = std::make_shared<maddy::Parser>();

View File

@@ -6,7 +6,8 @@
#include <string>
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 = "<h1>This is a test</h1><p>This should result in a praragraph it's that simple. </p><ul><li>an <i>unordered</i> list<ul><li>with some <strong>hierarchy</strong><ol><li>and an <em>ordered</em></li><li>list</li><li>directly</li></ol></li><li>inside</li></ul></li></ul><pre><code>\nvar c = 'blub';\n</code></pre><blockquote><p>A Quote </p><p>With some <s>text</s> blocks inside </p><ul><li>even a list </li><li>should be </li><li>possible </li></ul></blockquote><p>And well <code>inline code</code> should also work. </p><h2>Another Headline</h2><p>And not to forget <a href=\"http://progsource.de\">link to progsource</a> should work. And well - let's see how an image would be shown: </p><p><img src=\"http://progsource.de/img/progsource.png\" alt=\"an image\"/> </p><hr/><p><a name=\"to top\"></a> </p><h3>and more headlines</h3><ul class=\"checklist\"><li><label><input type=\"checkbox\"/> how</label></li><li><label><input type=\"checkbox\"/> about<ul class=\"checklist\"><li><label><input type=\"checkbox\"/> a</label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> nice</label></li></ul></label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> check</label></li><li><label><input type=\"checkbox\"/> list</label></li></ul><h4>even a table</h4><table><thead><tr><th>Left header</th><th>middle header</th><th>last header</th></tr></thead><tbody><tr><td>cell 1</td><td>cell <strong>2</strong></td><td>cell 3</td></tr><tr><td>cell 4</td><td>cell 5</td><td>cell 6</td></tr></tbody><tfoot><tr><td>foot a</td><td>foot b</td><td>foot c</td></tr></tfoot></table><h5>h5</h5><h6>h6</h6>";
const std::string testHtml2 = "<h1>This is a test</h1><p>This should result in a praragraph it's that simple. </p><ul><li>an <i>unordered</i> list<ul><li>with some <strong>hierarchy</strong><ol><li>and an _ordered_</li><li>list</li><li>directly</li></ol></li><li>inside</li></ul></li></ul><pre><code>\nvar c = 'blub';\n</code></pre><blockquote><p>A Quote </p><p>With some <s>text</s> blocks inside </p><ul><li>even a list </li><li>should be </li><li>possible </li></ul></blockquote><p>And well <code>inline code</code> should also work. </p><h2>Another Headline</h2><p>And not to forget <a href=\"http://progsource.de\">link to progsource</a> should work. And well - let's see how an image would be shown: </p><p><img src=\"http://progsource.de/img/progsource.png\" alt=\"an image\"/> </p><hr/><a name=\"to top\"></a><h3>and more headlines</h3><ul class=\"checklist\"><li><label><input type=\"checkbox\"/> how</label></li><li><label><input type=\"checkbox\"/> about<ul class=\"checklist\"><li><label><input type=\"checkbox\"/> a</label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> nice</label></li></ul></label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> check</label></li><li><label><input type=\"checkbox\"/> list</label></li></ul><h4>even a table</h4><table><thead><tr><th>Left header</th><th>middle header</th><th>last header</th></tr></thead><tbody><tr><td>cell 1</td><td>cell <strong>2</strong></td><td>cell 3</td></tr><tr><td>cell 4</td><td>cell 5</td><td>cell 6</td></tr></tbody><tfoot><tr><td>foot a</td><td>foot b</td><td>foot c</td></tr></tfoot></table><h5>h5</h5><h6>h6</h6>";
const std::string testHtml3 = "# This is a test <br/>This should result in a praragraph it's that simple. <br/>* an *unordered* list * with some <strong>hierarchy</strong> 1. and an <em>ordered</em> * list * directly * inside <br/>``` var c = 'blub'; ``` <br/>> A Quote > > With some ~~text~~ blocks inside > > * even a list > * should be > * possible > <br/>And well `inline code` should also work. <br/>## Another Headline <br/>And not to forget [link to progsource](http://progsource.de) should work. And well - let's see how an image would be shown: <br/>![an image](http://progsource.de/img/progsource.png) <br/>--- <br/><a name=\"to top\"></a> <br/>### and more headlines <br/>- [ ] how - [ ] about - [ ] a - [x] nice - [x] check - [ ] list <br/>#### even a table <br/>|table> Left header|middle header|last header - | - | - cell 1|cell <strong>2</strong>|cell 3 cell 4|cell 5|cell 6 - | - | - foot a|foot b|foot c |<table <br/>##### h5 ###### h6 <br/>";
const std::string testHtml =
"<h1>This is a test</h1><p>This should result in a praragraph it's that "
"simple. </p><ul><li>an <i>unordered</i> list<ul><li>with some "
"<strong>hierarchy</strong><ol><li>and an "
"<em>ordered</em></li><li>list</li><li>directly</li></ol></li><li>inside</"
"li></ul></li></ul><pre><code>\nvar c = "
"'blub';\n</code></pre><blockquote><p>A Quote </p><p>With some <s>text</s> "
"blocks inside </p><ul><li>even a list </li><li>should be </li><li>possible "
"</li></ul></blockquote><p>And well <code>inline code</code> should also "
"work. </p><h2>Another Headline</h2><p>And not to forget <a "
"href=\"http://progsource.de\">link to progsource</a> should work. And well "
"- let's see how an image would be shown: </p><p><img "
"src=\"http://progsource.de/img/progsource.png\" alt=\"an image\"/> "
"</p><hr/><p><a name=\"to top\"></a> </p><h3>and more headlines</h3><ul "
"class=\"checklist\"><li><label><input type=\"checkbox\"/> "
"how</label></li><li><label><input type=\"checkbox\"/> about<ul "
"class=\"checklist\"><li><label><input type=\"checkbox\"/> "
"a</label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> "
"nice</label></li></ul></label></li><li><label><input type=\"checkbox\" "
"checked=\"checked\"/> check</label></li><li><label><input "
"type=\"checkbox\"/> list</label></li></ul><h4>even a "
"table</h4><table><thead><tr><th>Left header</th><th>middle "
"header</th><th>last header</th></tr></thead><tbody><tr><td>cell "
"1</td><td>cell <strong>2</strong></td><td>cell 3</td></tr><tr><td>cell "
"4</td><td>cell 5</td><td>cell 6</td></tr></tbody><tfoot><tr><td>foot "
"a</td><td>foot b</td><td>foot "
"c</td></tr></tfoot></table><h5>h5</h5><h6>h6</h6>";
const std::string testHtml2 =
"<h1>This is a test</h1><p>This should result in a praragraph it's that "
"simple. </p><ul><li>an <i>unordered</i> list<ul><li>with some "
"<strong>hierarchy</strong><ol><li>and an "
"_ordered_</li><li>list</li><li>directly</li></ol></li><li>inside</li></ul></"
"li></ul><pre><code>\nvar c = 'blub';\n</code></pre><blockquote><p>A Quote "
"</p><p>With some <s>text</s> blocks inside </p><ul><li>even a list "
"</li><li>should be </li><li>possible </li></ul></blockquote><p>And well "
"<code>inline code</code> should also work. </p><h2>Another "
"Headline</h2><p>And not to forget <a href=\"http://progsource.de\">link to "
"progsource</a> should work. And well - let's see how an image would be "
"shown: </p><p><img src=\"http://progsource.de/img/progsource.png\" alt=\"an "
"image\"/> </p><hr/><a name=\"to top\"></a><h3>and more headlines</h3><ul "
"class=\"checklist\"><li><label><input type=\"checkbox\"/> "
"how</label></li><li><label><input type=\"checkbox\"/> about<ul "
"class=\"checklist\"><li><label><input type=\"checkbox\"/> "
"a</label></li><li><label><input type=\"checkbox\" checked=\"checked\"/> "
"nice</label></li></ul></label></li><li><label><input type=\"checkbox\" "
"checked=\"checked\"/> check</label></li><li><label><input "
"type=\"checkbox\"/> list</label></li></ul><h4>even a "
"table</h4><table><thead><tr><th>Left header</th><th>middle "
"header</th><th>last header</th></tr></thead><tbody><tr><td>cell "
"1</td><td>cell <strong>2</strong></td><td>cell 3</td></tr><tr><td>cell "
"4</td><td>cell 5</td><td>cell 6</td></tr></tbody><tfoot><tr><td>foot "
"a</td><td>foot b</td><td>foot "
"c</td></tr></tfoot></table><h5>h5</h5><h6>h6</h6>";
const std::string testHtml3 =
"# This is a test <br/>This should result in a praragraph it's that simple. "
"<br/>* an *unordered* list * with some <strong>hierarchy</strong> 1. "
"and an <em>ordered</em> * list * directly * inside <br/>``` var c "
"= 'blub'; ``` <br/>> A Quote > > With some ~~text~~ blocks inside > > * "
"even a list > * should be > * possible > <br/>And well `inline code` should "
"also work. <br/>## Another Headline <br/>And not to forget [link to "
"progsource](http://progsource.de) should work. And well - let's see how an "
"image would be shown: <br/>![an "
"image](http://progsource.de/img/progsource.png) <br/>--- <br/><a name=\"to "
"top\"></a> <br/>### and more headlines <br/>- [ ] how - [ ] about - [ ] a "
" - [x] nice - [x] check - [ ] list <br/>#### even a table <br/>|table> "
"Left header|middle header|last header - | - | - cell 1|cell "
"<strong>2</strong>|cell 3 cell 4|cell 5|cell 6 - | - | - foot a|foot b|foot "
"c |<table <br/>##### h5 ###### h6 <br/>";

View File

@@ -15,19 +15,17 @@ class MADDY_QUOTEPARSER : public ::testing::Test
protected:
std::shared_ptr<maddy::QuoteParser> quoteParser;
void
SetUp() override
void SetUp() override
{
this->quoteParser = std::make_shared<maddy::QuoteParser>(
nullptr,
nullptr
);
this->quoteParser = std::make_shared<maddy::QuoteParser>(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<std::string> markdown = {
"> a"
, "> b"
, ">"
, "> c"
, ""
};
std::vector<std::string> markdown = {"> a", "> b", ">", "> c", ""};
std::string expected = "<blockquote>a b c </blockquote>";
for (std::string md : markdown)

View File

@@ -23,8 +23,10 @@ TEST(MADDY_STRIKETHROUGHPARSER, ItReplacesMarkdownWithStrikeThroughHTML)
TEST(MADDY_STRIKETHROUGHPARSER, ItDoesNotParseInsideInlineCode)
{
std::string text = "some text `~~bla~~` ` ~~text~~ ` testing <code>~~it~~</code> out";
std::string expected = "some text `~~bla~~` ` ~~text~~ ` testing <code>~~it~~</code> out";
std::string text =
"some text `~~bla~~` ` ~~text~~ ` testing <code>~~it~~</code> out";
std::string expected =
"some text `~~bla~~` ` ~~text~~ ` testing <code>~~it~~</code> out";
auto strikeThroughParser = std::make_shared<maddy::StrikeThroughParser>();
strikeThroughParser->Parse(text);

View File

@@ -18,16 +18,11 @@ TEST(MADDY_STRONGPARSER, ItReplacesMarkdownWithStrongHTML)
std::string expected;
};
std::vector<testIt> tests
{
{
"some text **bla** text testing **it** out",
"some text <strong>bla</strong> text testing <strong>it</strong> out"
},
{
"some text __bla__ text testing __it__ out",
"some text <strong>bla</strong> text testing <strong>it</strong> out"
},
std::vector<testIt> tests{
{"some text **bla** text testing **it** out",
"some text <strong>bla</strong> text testing <strong>it</strong> out"},
{"some text __bla__ text testing __it__ out",
"some text <strong>bla</strong> text testing <strong>it</strong> out"},
};
auto strongParser = std::make_shared<maddy::StrongParser>();
@@ -47,16 +42,11 @@ TEST(MADDY_STRONGPARSER, ItReplacesEmphasizedMarkdownNotWithStrongHTML)
std::string expected;
};
std::vector<testIt> tests
{
{
"some text *bla* text testing **it** out",
"some text *bla* text testing <strong>it</strong> out"
},
{
"some text _bla_ text testing __it__ out",
"some text _bla_ text testing <strong>it</strong> out"
},
std::vector<testIt> tests{
{"some text *bla* text testing **it** out",
"some text *bla* text testing <strong>it</strong> out"},
{"some text _bla_ text testing __it__ out",
"some text _bla_ text testing <strong>it</strong> out"},
};
auto strongParser = std::make_shared<maddy::StrongParser>();
@@ -76,16 +66,13 @@ TEST(MADDY_STRONGPARSER, ItDoesNotParseInsideInlineCode)
std::string expected;
};
std::vector<testIt> tests
{
std::vector<testIt> 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 <strong>it</strong> out"
},
{"some text _bla_ text testing __it__ out",
"some text _bla_ text testing <strong>it</strong> out"},
};
auto strongParser = std::make_shared<maddy::StrongParser>();

View File

@@ -15,19 +15,17 @@ class MADDY_TABLEPARSER : public ::testing::Test
protected:
std::shared_ptr<maddy::TableParser> tableParser;
void
SetUp() override
void SetUp() override
{
this->tableParser = std::make_shared<maddy::TableParser>(
nullptr,
nullptr
);
this->tableParser = std::make_shared<maddy::TableParser>(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<std::string> 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"
, "|<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",
"|<table"
};
std::string expected = "<table><thead><tr><th>Left header</th><th>middle header</th><th>last header</th></tr></thead><tbody><tr><td>cell 1</td><td>cell 2</td><td>cell 3</td></tr><tr><td>cell 4</td><td>cell 5</td><td>cell 6</td></tr></tbody><tfoot><tr><td>foot a</td><td>foot b</td><td>foot c</td></tr></tfoot></table>";
std::string expected =
"<table><thead><tr><th>Left header</th><th>middle header</th><th>last "
"header</th></tr></thead><tbody><tr><td>cell 1</td><td>cell 2</td><td>cell "
"3</td></tr><tr><td>cell 4</td><td>cell 5</td><td>cell "
"6</td></tr></tbody><tfoot><tr><td>foot a</td><td>foot b</td><td>foot "
"c</td></tr></tfoot></table>";
for (std::string md : markdown)
{

View File

@@ -15,10 +15,10 @@ class MADDY_UNORDEREDLISTPARSER : public ::testing::Test
protected:
std::shared_ptr<maddy::UnorderedListParser> ulParser;
void
SetUp() override
void SetUp() override
{
std::function<std::shared_ptr<maddy::BlockParser>(const std::string& line)> getBlockParserForLineCallback = [](const std::string& line)
std::function<std::shared_ptr<maddy::BlockParser>(const std::string& line)>
getBlockParserForLineCallback = [](const std::string& line)
{
if (maddy::UnorderedListParser::IsStartingLine(line))
{
@@ -32,15 +32,17 @@ protected:
};
this->ulParser = std::make_shared<maddy::UnorderedListParser>(
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<std::string> markdown = {
"* a"
, "* b"
, "- c"
, "- d"
, "+ e"
, "+ f"
, "* g"
, ""
"* a", "* b", "- c", "- d", "+ e", "+ f", "* g", ""
};
std::string expected = "<ul><li>a</li><li>b</li><li>c</li><li>d</li><li>e</li><li>f</li><li>g</li></ul>";
std::string expected =
"<ul><li>a</li><li>b</li><li>c</li><li>d</li><li>e</li><li>f</li><li>g</"
"li></ul>";
for (std::string md : markdown)
{
@@ -80,17 +77,11 @@ TEST_F(MADDY_UNORDEREDLISTPARSER, ItReplacesMarkdownWithAnHtmlUnorderedList)
TEST_F(MADDY_UNORDEREDLISTPARSER, ItReplacesMarkdownWithAnHierachicalHtmlList)
{
std::vector<std::string> markdown = {
"* a"
, " * d"
, " * e"
, "* b"
, " * c"
, " + x"
, " + y"
, " - z"
, ""
"* a", " * d", " * e", "* b", " * c", " + x", " + y", " - z", ""
};
std::string expected = "<ul><li>a<ul><li>d</li><li>e</li></ul></li><li>b<ul><li>c</li><li>x</li><li>y</li><li>z</li></ul></li></ul>";
std::string expected =
"<ul><li>a<ul><li>d</li><li>e</li></ul></li><li>b<ul><li>c</li><li>x</"
"li><li>y</li><li>z</li></ul></li></ul>";
for (std::string md : markdown)
{

View File

@@ -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();

143
tools/format.py Normal file
View File

@@ -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 <dry_run|format>"
)
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()