mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-05-06 17:51:36 +02:00
Added a new Utils class containing helper methods.
This commit is contained in:
parent
67a3ae801a
commit
6a01c57c36
127
core/utils.cpp
Normal file
127
core/utils.cpp
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
void Utils::newline_to_br(std::string *str) {
|
||||||
|
str_replace(str, "\r\n", "<br>");
|
||||||
|
str_replace(str, "\n", "<br>");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Utils::bbcode_evaluate_simple(std::string *str) {
|
||||||
|
bbcpp::BBDocumentPtr doc = bbcode((*str));
|
||||||
|
|
||||||
|
bbcpp::BBNodeList n = doc->getChildren();
|
||||||
|
|
||||||
|
str->clear();
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < n.size(); ++i) {
|
||||||
|
eval_node(str, n[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bbcpp::BBDocumentPtr Utils::bbcode(const std::string &str) {
|
||||||
|
bbcpp::BBDocumentPtr doc = bbcpp::BBDocument::create();
|
||||||
|
|
||||||
|
doc->load(str);
|
||||||
|
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Utils::str_replace(std::string *str, const std::string &from, const std::string &to) {
|
||||||
|
if (from.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
size_t start_pos = 0;
|
||||||
|
while ((start_pos = str->find(from, start_pos)) != std::string::npos) {
|
||||||
|
str->replace(start_pos, from.length(), to);
|
||||||
|
start_pos += to.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Utils::eval_node(std::string *str, bbcpp::BBNodePtr node) {
|
||||||
|
switch (node->getNodeType()) {
|
||||||
|
case bbcpp::BBNode::NodeType::TEXT: {
|
||||||
|
bbcpp::BBTextPtr t = node->downCast<bbcpp::BBTextPtr>();
|
||||||
|
|
||||||
|
(*str) += t->getText();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case bbcpp::BBNode::NodeType::ELEMENT: {
|
||||||
|
bbcpp::BBElementPtr e = node->downCast<bbcpp::BBElementPtr>();
|
||||||
|
|
||||||
|
eval_element(str, e);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case bbcpp::BBNode::NodeType::ATTRIBUTE:
|
||||||
|
break;
|
||||||
|
case bbcpp::BBNode::NodeType::DOCUMENT:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
bbcpp::BBNodeList n = node->getChildren();
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < n.size(); ++i) {
|
||||||
|
eval_node(str, n[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Utils::eval_element(std::string *str, bbcpp::BBElementPtr element) {
|
||||||
|
switch (element->getElementType()) {
|
||||||
|
case bbcpp::BBElement::ElementType::SIMPLE: {
|
||||||
|
|
||||||
|
if (element->getNodeName() == "b") {
|
||||||
|
(*str) += "<span style=\"font-weight: bold;\">";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element->getNodeName() == "i") {
|
||||||
|
(*str) += "<span style=\"font-style: italic;\">";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element->getNodeName() == "u") {
|
||||||
|
(*str) += "<span style=\"text-decoration: underline;\">";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element->getNodeName() == "s") {
|
||||||
|
(*str) += "<span style=\"text-decoration: line-through;\">";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case bbcpp::BBElement::ElementType::VALUE: {
|
||||||
|
//NYI
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case bbcpp::BBElement::ElementType::PARAMETER: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case bbcpp::BBElement::ElementType::CLOSING: {
|
||||||
|
if (element->getNodeName() == "b") {
|
||||||
|
(*str) += "</span>";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element->getNodeName() == "i") {
|
||||||
|
(*str) += "</span>";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element->getNodeName() == "u") {
|
||||||
|
(*str) += "</span>";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element->getNodeName() == "s") {
|
||||||
|
(*str) += "</span>";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
core/utils.h
Normal file
23
core/utils.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef UTILS_H
|
||||||
|
#define UTILS_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <bbcpp/BBDocument.h>
|
||||||
|
|
||||||
|
class Utils {
|
||||||
|
public:
|
||||||
|
static void newline_to_br(std::string *str);
|
||||||
|
//htmlspecialchars
|
||||||
|
|
||||||
|
static void bbcode_evaluate_simple(std::string *str);
|
||||||
|
static bbcpp::BBDocumentPtr bbcode(const std::string &str);
|
||||||
|
|
||||||
|
static void str_replace(std::string *str, const std::string& from, const std::string& to);
|
||||||
|
protected:
|
||||||
|
static void eval_node(std::string *str, bbcpp::BBNodePtr node);
|
||||||
|
static void eval_element(std::string *str, bbcpp::BBElementPtr node);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user