mirror of
https://github.com/progsource/maddy.git
synced 2026-03-25 07:50:39 +01:00
chore: Add clang-format
This commit is contained in:
@@ -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,38 +144,34 @@ 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(
|
||||
line.begin(),
|
||||
line.end(),
|
||||
[&hasMetNonSpace](unsigned char c)
|
||||
uint32_t indentation = static_cast<uint32_t>(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<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
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -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>";
|
||||
|
||||
@@ -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(
|
||||
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() ||
|
||||
line.find("</label></li><li><label>") != std::string::npos ||
|
||||
line.find("</label></li></ul>") != std::string::npos
|
||||
)
|
||||
if (line.empty() ||
|
||||
line.find("</label></li><li><label>") != std::string::npos ||
|
||||
line.find("</label></li></ul>") != std::string::npos)
|
||||
{
|
||||
line = "</label></li></ul>" + line;
|
||||
this->isFinished = true;
|
||||
|
||||
@@ -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(
|
||||
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 == "```")
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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/>";
|
||||
|
||||
|
||||
@@ -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(
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -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\"/>";
|
||||
|
||||
@@ -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>";
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
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;
|
||||
|
||||
@@ -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>";
|
||||
|
||||
@@ -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(
|
||||
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 ||
|
||||
line.find("</li></ol>") != std::string::npos ||
|
||||
line.find("</li></ul>") != 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 = "</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);
|
||||
|
||||
@@ -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(
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "maddy/parserconfig.h"
|
||||
@@ -56,8 +56,12 @@ 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 &&
|
||||
(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<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
|
||||
) &&
|
||||
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 @@ 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
|
||||
) &&
|
||||
maddy::UnorderedListParser::IsStartingLine(line)
|
||||
)
|
||||
else if ((!this->config || (this->config->enabledParsers &
|
||||
maddy::types::UNORDERED_LIST_PARSER) != 0
|
||||
) &&
|
||||
maddy::UnorderedListParser::IsStartingLine(line))
|
||||
{
|
||||
parser = this->createUnorderedListParser();
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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(
|
||||
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, "");
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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(
|
||||
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>";
|
||||
|
||||
|
||||
@@ -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(
|
||||
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 ||
|
||||
line.find("</li></ol>") != std::string::npos ||
|
||||
line.find("</li></ul>") != 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 = "</li></ul>" + line;
|
||||
this->isFinished = true;
|
||||
|
||||
Reference in New Issue
Block a user