Parser: Add callback support for unknown "tags"
This commit is contained in:
parent
44c27ed8b4
commit
a4a45d9add
14
iparser.h
14
iparser.h
@ -2,15 +2,27 @@
|
|||||||
#define IPARSER_H
|
#define IPARSER_H
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
#include <functional>
|
||||||
#include "headline.h"
|
#include "headline.h"
|
||||||
#include "database/pagedao.h"
|
#include "database/pagedao.h"
|
||||||
#include "urlprovider.h"
|
#include "urlprovider.h"
|
||||||
class IParser
|
class IParser
|
||||||
{
|
{
|
||||||
|
protected:
|
||||||
|
static std::string empty(std::string_view key, std::string_view content)
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual std::string extractCommand(std::string cmdname, const std::string &content) const = 0;
|
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::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 std::vector<std::string> extractCategories(const std::string &content) const = 0;
|
||||||
|
|
||||||
virtual ~IParser(){};
|
virtual ~IParser(){};
|
||||||
|
@ -116,12 +116,13 @@ std::string Parser::processLink(const PageDao &pageDao, UrlProvider &urlProvider
|
|||||||
return htmllink.render();
|
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;
|
std::string result;
|
||||||
// we don't care about commands, but we nevertheless replace them with empty strings
|
// we don't care about commands, but we nevertheless replace them with empty strings
|
||||||
std::regex tagfinder(
|
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(
|
result = utils::regex_callback_replacer(
|
||||||
tagfinder, content,
|
tagfinder, content,
|
||||||
[&](std::smatch &match)
|
[&](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 tag = match.str(1);
|
||||||
std::string content = match.str(2);
|
std::string content = match.str(2);
|
||||||
std::string justreplace[] = {"b", "i", "u", "ul", "li", "ol"};
|
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))
|
if(std::find(std::begin(justreplace), std::end(justreplace), tag) != std::end(justreplace))
|
||||||
{
|
{
|
||||||
return "<" + tag + ">" + content + "</" + tag + ">";
|
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 "<" + tag + " id='" + content + "'>" + content + "</" + tag + ">";
|
||||||
}
|
}
|
||||||
return std::string("");
|
return callback(tag, content);
|
||||||
});
|
});
|
||||||
result = utils::strreplace(result, "\r\n", "<br>");
|
result = utils::strreplace(result, "\r\n", "<br>");
|
||||||
return result;
|
return result;
|
||||||
|
7
parser.h
7
parser.h
@ -11,9 +11,12 @@ class Parser : public IParser
|
|||||||
std::string extractCommand(std::string cmdname, const std::string &content) const;
|
std::string extractCommand(std::string cmdname, const std::string &content) const;
|
||||||
std::vector<Headline> extractHeadlines(const std::string &content) const override;
|
std::vector<Headline> extractHeadlines(const std::string &content) const override;
|
||||||
std::vector<std::string> extractCategories(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;
|
using IParser::IParser;
|
||||||
~Parser(){};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PARSER_H
|
#endif // PARSER_H
|
||||||
|
Caricamento…
Fai riferimento in un nuovo problema
Block a user