مقایسه کامیتها
6 کامیتها
WIP/cpp20
...
534d9f74e7
مولف | SHA1 | تاریخ | |
---|---|---|---|
534d9f74e7 | |||
5f674cfe6f | |||
ea7476a882 | |||
ab9e5fb0bd | |||
7ca04fbf45 | |||
8b44dd4e94 |
3
.gitmodules
فروخته شده
3
.gitmodules
فروخته شده
@ -7,3 +7,6 @@
|
||||
[submodule "submodules/qssb.h"]
|
||||
path = submodules/qssb.h
|
||||
url = https://gitea.quitesimple.org/crtxcr/qssb.h.git
|
||||
[submodule "submodules/qsmaddy"]
|
||||
path = submodules/qsmaddy
|
||||
url = https://gitea.quitesimple.org/crtxcr/qsmaddy
|
||||
|
2
Makefile
2
Makefile
@ -3,7 +3,7 @@
|
||||
CXXFLAGS=-std=c++17 -O0 -g -no-pie -pipe -MMD -Wall -Wextra
|
||||
RELEASE_CXXFLAGS=-std=c++17 -O3 -pipe -MMD -Wall -Wextra
|
||||
LDFLAGS=-lsqlite3 -lpthread -lcrypto -lstdc++fs -lseccomp
|
||||
INCLUDEFLAGS=-I submodules/sqlitemoderncpp/hdr -I submodules/cpp-httplib -I submodules/qssb.h
|
||||
INCLUDEFLAGS=-I submodules/sqlitemoderncpp/hdr -I submodules/cpp-httplib -I submodules/qssb.h -I submodules/qsmaddy/include/
|
||||
|
||||
CXX=g++
|
||||
|
||||
|
11
parser.cpp
11
parser.cpp
@ -30,19 +30,12 @@ SOFTWARE.
|
||||
std::vector<Headline> Parser::extractHeadlines(std::string content) const
|
||||
{
|
||||
std::vector<Headline> result;
|
||||
std::string reg = R"(\[h(1|2|3)\](.*?)\[/h\1\])";
|
||||
std::regex headerfinder(reg);
|
||||
auto begin = std::sregex_iterator(content.begin(), content.end(), headerfinder);
|
||||
auto end = std::sregex_iterator();
|
||||
|
||||
for(auto it = begin; it != end; it++)
|
||||
{
|
||||
auto smatch = *it;
|
||||
utils::regex_callback_extractor(std::regex(R"(\[h(1|2|3)\](.*?)\[/h\1\])"), content, [&](std::smatch &smatch) {
|
||||
Headline h;
|
||||
h.level = utils::toUInt(smatch.str(1));
|
||||
h.title = smatch.str(2);
|
||||
result.push_back(h);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
|
61
parsermarkdown.cpp
Normal file
61
parsermarkdown.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include "parsermarkdown.h"
|
||||
#include "logger.h"
|
||||
#include "htmllink.h"
|
||||
|
||||
std::string ParserMarkdown::processLink(const PageDao &pageDao, UrlProvider &urlProvider, std::smatch &match) const
|
||||
{
|
||||
std::string inner = match.str(1);
|
||||
std::string link = match.str(2);
|
||||
HtmlLink htmllink;
|
||||
htmllink.href = link;
|
||||
htmllink.innervalue = inner;
|
||||
|
||||
if(link.find("http://") == 0 || link.find("https://") == 0)
|
||||
{
|
||||
return htmllink.render();
|
||||
}
|
||||
|
||||
if(pageDao.exists(link))
|
||||
{
|
||||
htmllink.cssclass = "exists";
|
||||
}
|
||||
else
|
||||
{
|
||||
htmllink.cssclass = "notexists";
|
||||
}
|
||||
|
||||
htmllink.href = urlProvider.page(htmllink.href);
|
||||
|
||||
return htmllink.render();
|
||||
}
|
||||
|
||||
ParserMarkdown::ParserMarkdown()
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<Headline> ParserMarkdown::extractHeadlines(std::string content) const
|
||||
{
|
||||
std::vector<Headline> result;
|
||||
utils::regex_callback_extractor(std::regex(R"((#{1,6}) (.*))"), content, [&](std::smatch &smatch) {
|
||||
Headline h;
|
||||
h.level = smatch.str(1).length();
|
||||
h.title = smatch.str(2);
|
||||
result.push_back(h);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string ParserMarkdown::parse(const PageDao &pagedao, UrlProvider &provider, std::string content) const
|
||||
{
|
||||
std::shared_ptr<maddy::ParserConfig> config = std::make_shared<maddy::ParserConfig>();
|
||||
auto maddy = std::make_shared<maddy::Parser>(config);
|
||||
|
||||
auto linkParser = std::make_shared<maddy::LinkParser>();
|
||||
linkParser->setCallback([&](std::smatch &match) { return processLink(pagedao, provider, match); });
|
||||
maddy->setLinkParser(linkParser);
|
||||
// TODO: hack because the parser breaks if there is an \r
|
||||
content = utils::strreplace(content, "\r", "");
|
||||
std::stringstream s{content};
|
||||
std::string result = maddy->Parse(s);
|
||||
return result;
|
||||
}
|
17
parsermarkdown.h
Normal file
17
parsermarkdown.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef PARSER_MARKDOWN_H
|
||||
#define PARSER_MARKDOWN_H
|
||||
|
||||
#include "parser.h"
|
||||
#include "maddy/parser.h"
|
||||
class ParserMarkdown : public Parser
|
||||
{
|
||||
private:
|
||||
std::string processLink(const PageDao &pageDao, UrlProvider &urlProvider, std::smatch &match) const;
|
||||
|
||||
public:
|
||||
ParserMarkdown();
|
||||
std::vector<Headline> extractHeadlines(std::string content) const;
|
||||
std::string parse(const PageDao &pagedao, UrlProvider &provider, std::string content) const;
|
||||
};
|
||||
|
||||
#endif // PARSER_MARKDOWN_H
|
1
submodules/qsmaddy
Submodule
1
submodules/qsmaddy
Submodule
Submodule submodules/qsmaddy added at 167ce3943d
11
utils.cpp
11
utils.cpp
@ -175,3 +175,14 @@ std::string utils::toISODate(time_t t)
|
||||
}
|
||||
return std::string{result};
|
||||
}
|
||||
|
||||
void utils::regex_callback_extractor(std::regex regex, const std::string &input, std::function<void (std::smatch &)> callback)
|
||||
{
|
||||
auto begin = std::sregex_iterator(input.begin(), input.end(), regex);
|
||||
auto end = std::sregex_iterator();
|
||||
for(auto it = begin; it != end; it++)
|
||||
{
|
||||
std::smatch smatch = *it;
|
||||
callback(smatch);
|
||||
}
|
||||
}
|
||||
|
1
utils.h
1
utils.h
@ -60,6 +60,7 @@ template <class T, class U> std::vector<U> getAll(std::multimap<T, U> map, T key
|
||||
std::string regex_callback_replacer(std::regex regex, const std::string &input,
|
||||
std::function<std::string(std::smatch &)> callback);
|
||||
|
||||
void regex_callback_extractor(std::regex regex, const std::string &input, std::function<void(std::smatch &)> callback);
|
||||
std::string readCompleteFile(std::string_view filepath);
|
||||
|
||||
inline std::string nz(const char *s)
|
||||
|
مرجع در شماره جدید
Block a user