Parser: Add callback support for unknown "tags"

Cette révision appartient à :
Albert S. 2022-03-27 08:31:59 +02:00
Parent 44c27ed8b4
révision a4a45d9add
3 fichiers modifiés avec 23 ajouts et 7 suppressions

Voir le fichier

@ -2,15 +2,27 @@
#define IPARSER_H
#include <vector>
#include <string_view>
#include <functional>
#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<Headline> 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<std::string(std::string_view, std::string_view)> &callback) const = 0;
virtual std::vector<std::string> extractCategories(const std::string &content) const = 0;
virtual ~IParser(){};

Voir le fichier

@ -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<std::string(std::string_view, std::string_view)> &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 + "</" + tag + ">";
@ -144,7 +145,7 @@ std::string Parser::parse(const PageDao &pagedao, UrlProvider &provider, const s
{
return "<" + tag + " id='" + content + "'>" + content + "</" + tag + ">";
}
return std::string("");
return callback(tag, content);
});
result = utils::strreplace(result, "\r\n", "<br>");
return result;

Voir le fichier

@ -11,9 +11,12 @@ class Parser : public IParser
std::string extractCommand(std::string cmdname, const std::string &content) const;
std::vector<Headline> extractHeadlines(const std::string &content) const override;
std::vector<std::string> 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<std::string(std::string_view, std::string_view)> &callback) const override;
using IParser::IParser;
~Parser(){};
};
#endif // PARSER_H