Add htmlp

This commit is contained in:
2025-10-15 17:42:58 -05:00
parent 931cf49320
commit 3575afab91
2 changed files with 57 additions and 0 deletions

View File

@@ -156,6 +156,7 @@ struct CaseInsensitiveLess {
static std::string UrlPathDecode(std::string v);
static std::string UrlPathEncode(std::string v, bool ignoreSpace=false);
static std::string HtmlEncode(std::string v);
static std::string HtmlP(std::string text);
static std::string HtmlDecodeOnlyEntityNumber(std::string v);
static std::vector<std::string> SplitString(std::string text, std::string delimiter,std::size_t maxCnt = std::string::npos);
static std::string Replace(std::string str, std::string find, std::string replace);

View File

@@ -582,6 +582,62 @@ namespace Tesses::Framework::Http {
}
return buff;
}
std::string HttpUtils::HtmlP(std::string text)
{
std::string newText = "";
std::string builder = "";
auto flush = [&]()->void {
if(!builder.empty())
{
if(builder.find("http://") == 0 || builder.find("https://") == 0 || builder.find("ftp://") == 0 || builder.find("ftps://") == 0 || builder.find("magnet:") == 0 || builder.find("btmh:") == 0)
{
newText += "<a href=\"" + HttpUtils::HtmlEncode(builder) + "\">" + HttpUtils::HtmlEncode(builder) + "</a>";
}
else if(builder.find("mailto:") == 0)
{
newText += "<a href=\"" + HttpUtils::HtmlEncode(builder) + "\">" + HttpUtils::HtmlEncode(builder.substr(7)) + "</a>";
}
else if(builder.find("tel:") == 0)
{
newText += "<a href=\"" + HttpUtils::HtmlEncode(builder) + "\">" + HttpUtils::HtmlEncode(builder.substr(4)) + "</a>";
}
else {
newText += HttpUtils::HtmlEncode(builder);
}
builder = "";
}
};
for(auto item : text)
{
switch(item)
{
case ' ':
flush();
newText += " ";
break;
case '\n':
flush();
newText += "<br>";
break;
case '\t':
flush();
newText += "&tab;";
break;
case '\r':
flush();
break;
default:
builder += item;
break;
}
}
flush();
return newText;
}
std::string HttpUtils::HtmlEncode(std::string html)
{
std::string myHtml = {};