147 linhas
4.4 KiB
C++
147 linhas
4.4 KiB
C++
/* Copyright (c) 2018 Albert S.
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
#include <regex>
|
|
#include <iostream>
|
|
#include <regex>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <iterator>
|
|
#include "parser.h"
|
|
#include "utils.h"
|
|
#include "htmllink.h"
|
|
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;
|
|
Headline h;
|
|
h.level = utils::toUInt(smatch.str(1));
|
|
h.title = smatch.str(2);
|
|
result.push_back(h);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::vector<std::string> Parser::extractCategories(std::string content) const
|
|
{
|
|
std::vector<std::string> result;
|
|
std::string reg = R"(\[category\](.*?)\[/category\])";
|
|
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;
|
|
result.emplace_back(smatch.str(1));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::string Parser::extractCommand(std::string cmdname, std::string content) const
|
|
{
|
|
std::string cmd = "[cmd:" + cmdname + "]";
|
|
std::string cmdend = "[/cmd:" + cmdname + "]";
|
|
|
|
std::string_view view = content;
|
|
size_t pos = 0;
|
|
if((pos = view.find(cmd)) != std::string::npos)
|
|
{
|
|
view.remove_prefix(pos);
|
|
view.remove_prefix(cmd.size());
|
|
if((pos = view.find(cmdend)) != std::string::npos)
|
|
{
|
|
auto result = view.substr(0, pos);
|
|
return std::string{result};
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
std::string Parser::processLink(const PageDao &pageDao, UrlProvider &urlProvider, std::smatch &match) const
|
|
{
|
|
std::string linktag = match.str(1);
|
|
std::string inside = match.str(2);
|
|
|
|
std::vector<std::string> splitted = utils::split(inside, '|');
|
|
HtmlLink htmllink;
|
|
if(splitted.size() == 2)
|
|
{
|
|
htmllink.innervalue = splitted[1];
|
|
htmllink.href = splitted[0];
|
|
}
|
|
else
|
|
{
|
|
htmllink.innervalue = inside;
|
|
htmllink.href = inside;
|
|
}
|
|
|
|
if(linktag == "wikilink")
|
|
{
|
|
if(pageDao.exists(htmllink.href))
|
|
{
|
|
htmllink.cssclass = "exists";
|
|
}
|
|
else
|
|
{
|
|
htmllink.cssclass = "notexists";
|
|
}
|
|
|
|
htmllink.href = urlProvider.page(htmllink.href);
|
|
}
|
|
|
|
return htmllink.render();
|
|
}
|
|
|
|
std::string Parser::parse(const PageDao &pagedao, UrlProvider &provider, std::string content) 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|category)*?\]((\s|\S)*?)\[/\1])");
|
|
result = utils::regex_callback_replacer(tagfinder, content, [&](std::smatch &match) {
|
|
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);
|
|
if(std::find(std::begin(justreplace), std::end(justreplace), tag) != std::end(justreplace))
|
|
{
|
|
return "<" + tag + ">" + content + "</" + tag + ">";
|
|
}
|
|
if(tag == "link" || tag == "wikilink")
|
|
{
|
|
return this->processLink(pagedao, provider,
|
|
match); // TODO: recreate this so we don't check inside the function stuff again
|
|
}
|
|
if(tag[0] == 'h')
|
|
{
|
|
return "<" + tag + " id='" + content + "'>" + content + "</" + tag + ">";
|
|
}
|
|
return std::string("");
|
|
});
|
|
result = utils::strreplace(result, "\r\n", "<br>");
|
|
return result;
|
|
}
|