1
0
peilaus alkaen https://github.com/quitesimpleorg/qsmaddy.git synced 2024-11-10 19:04:38 +01:00
qsmaddy/include/maddy/linkparser.h
Albert S e0e0db80d5 Linkparser: Use callbacks, so app can influence how the HTML gets rendered exactly
In particular, this allows making dynamic decisions: Some links may
for example require a different CSS class or so, therefore a static
hardcoded template is not enough for those cases.
2021-04-18 13:10:50 +02:00

67 rivejä
1.4 KiB
C++

/*
* This project is licensed under the MIT license. For more information see the
* LICENSE file.
*/
#pragma once
// -----------------------------------------------------------------------------
#include <string>
#include <regex>
#include "maddy/lineparser.h"
#include "maddy/callbackreplacer.h"
// -----------------------------------------------------------------------------
namespace maddy {
// -----------------------------------------------------------------------------
/**
* LinkParser
*
* Has to be used after the `ImageParser`.
*
* @class
*/
class LinkParser : public LineParser
{
private:
std::function<std::string(std::smatch &)> callback = [](std::smatch & match){
std::string inner = match.str(1);
std::string link = match.str(2);
return "<a href=\"" + link + "\">" + inner + "</a>";
};
std::regex re = std::regex("\\[([^\\]]*)\\]\\(([^\\]]*)\\)");
public:
/**
* Parse
*
* From Markdown: `[text](http://example.com)`
*
* To HTML: `<a href="http://example.com">text</a>`
*
* @method
* @param {std::string&} line The line to interpret
* @return {void}
*/
void
Parse(std::string& line) override
{
line = regex_callback_replacer(re,line,callback);
}
void setCallback(std::function<std::string(std::smatch &)> callback)
{
this->callback = callback;
}
}; // class LinkParser
// -----------------------------------------------------------------------------
} // namespace maddy