diff --git a/iparser.h b/iparser.h index aa79ec5..c619338 100644 --- a/iparser.h +++ b/iparser.h @@ -2,15 +2,27 @@ #define IPARSER_H #include #include +#include #include "headline.h" #include "database/pagedao.h" #include "urlprovider.h" class IParser { + protected: + static std::string empty(std::string_view key, std::string_view content) + { + return ""; + } + public: virtual std::string extractCommand(std::string cmdname, const std::string &content) const = 0; virtual std::vector extractHeadlines(const std::string &content) const = 0; - virtual std::string parse(const PageDao &pagedao, UrlProvider &provider, const std::string &content) const = 0; + virtual inline std::string parse(const PageDao &pagedao, UrlProvider &provider, const std::string &content) const + { + return parse(pagedao, provider, content, empty); + } + virtual std::string parse(const PageDao &pagedao, UrlProvider &provider, const std::string &content, + const std::function &callback) const = 0; virtual std::vector extractCategories(const std::string &content) const = 0; virtual ~IParser(){}; diff --git a/parser.cpp b/parser.cpp index 17e7db8..3f971c8 100644 --- a/parser.cpp +++ b/parser.cpp @@ -116,12 +116,13 @@ std::string Parser::processLink(const PageDao &pageDao, UrlProvider &urlProvider return htmllink.render(); } -std::string Parser::parse(const PageDao &pagedao, UrlProvider &provider, const std::string &content) const +std::string Parser::parse(const PageDao &pagedao, UrlProvider &provider, const std::string &content, + const std::function &callback) const { std::string result; // we don't care about commands, but we nevertheless replace them with empty strings std::regex tagfinder( - R"(\[(b|i|u|li||ul|ol|link|wikilink|h\d|cmd:rename|cmd:redirect|cmd:pagetitle|category)*?\]((\s|\S)*?)\[/\1])"); + R"(\[(b|i|u|li||ul|ol|link|wikilink|h\d|cmd:rename|cmd:redirect|cmd:pagetitle|category|dynamic:postlist)*?\]((\s|\S)*?)\[/\1])"); result = utils::regex_callback_replacer( tagfinder, content, [&](std::smatch &match) @@ -129,7 +130,7 @@ std::string Parser::parse(const PageDao &pagedao, UrlProvider &provider, const s std::string tag = match.str(1); std::string content = match.str(2); std::string justreplace[] = {"b", "i", "u", "ul", "li", "ol"}; - content = parse(pagedao, provider, content); + content = parse(pagedao, provider, content, callback); if(std::find(std::begin(justreplace), std::end(justreplace), tag) != std::end(justreplace)) { return "<" + tag + ">" + content + ""; @@ -144,7 +145,7 @@ std::string Parser::parse(const PageDao &pagedao, UrlProvider &provider, const s { return "<" + tag + " id='" + content + "'>" + content + ""; } - return std::string(""); + return callback(tag, content); }); result = utils::strreplace(result, "\r\n", "
"); return result; diff --git a/parser.h b/parser.h index a9bb95c..2c5a82a 100644 --- a/parser.h +++ b/parser.h @@ -11,9 +11,12 @@ class Parser : public IParser std::string extractCommand(std::string cmdname, const std::string &content) const; std::vector extractHeadlines(const std::string &content) const override; std::vector extractCategories(const std::string &content) const override; - std::string parse(const PageDao &pagedao, UrlProvider &provider, const std::string &content) const override; + using IParser::parse; + virtual std::string parse( + const PageDao &pagedao, UrlProvider &provider, const std::string &content, + const std::function &callback) const override; + using IParser::IParser; - ~Parser(){}; }; #endif // PARSER_H