2022-06-25 01:55:54 +02:00
|
|
|
#include "bbcode_parser.h"
|
2022-07-03 18:40:05 +02:00
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
#include "core/error_macros.h"
|
2022-07-19 14:27:01 +02:00
|
|
|
#include "core/log/logger.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
bool BBCodeParserAttribute::match_attrib(const String &attrib) {
|
|
|
|
return attribute == attrib;
|
|
|
|
}
|
|
|
|
bool BBCodeParserAttribute::match_data(const String &d) {
|
|
|
|
return data == d;
|
|
|
|
}
|
|
|
|
bool BBCodeParserAttribute::match_data(const Vector<String> &d) {
|
|
|
|
// todo
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool BBCodeParserAttribute::contains_data(const String &d) {
|
|
|
|
return data.find(d) != -1;
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
String BBCodeParserAttribute::convert_to_string() const {
|
2022-06-25 01:55:54 +02:00
|
|
|
if (single) {
|
|
|
|
return attribute;
|
|
|
|
}
|
|
|
|
|
2022-07-03 18:40:05 +02:00
|
|
|
if (data.find_char('"' == -1)) {
|
2022-06-25 01:55:54 +02:00
|
|
|
return attribute + "=\"" + data + "\"";
|
|
|
|
} else {
|
|
|
|
return attribute + "=\'" + data + "\'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
void BBCodeParserAttribute::print() const {
|
|
|
|
PLOG_MSG(convert_to_string());
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BBCodeParserAttribute::BBCodeParserAttribute() {
|
|
|
|
single = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BBCodeParserAttribute::~BBCodeParserAttribute() {
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserTag> BBCodeParserTag::get_first(const String &t) {
|
2022-06-25 01:55:54 +02:00
|
|
|
if (tag == t) {
|
2022-07-19 14:27:01 +02:00
|
|
|
return Ref<BBCodeParserTag>(this);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < tags.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserTag> ht = tags.write[i]->get_first(t);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
if (ht.is_valid()) {
|
2022-06-25 01:55:54 +02:00
|
|
|
return ht;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
return Ref<BBCodeParserTag>();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserTag> BBCodeParserTag::get_first(const String &t, const String &attrib, const String &val) {
|
2022-06-25 01:55:54 +02:00
|
|
|
if (tag == t) {
|
|
|
|
if (has_attribute(attrib, val)) {
|
2022-07-19 14:27:01 +02:00
|
|
|
return Ref<BBCodeParserTag>(this);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < tags.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserTag> ht = tags.write[i]->get_first(t, attrib, val);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
if (ht.is_valid()) {
|
2022-06-25 01:55:54 +02:00
|
|
|
return ht;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
return Ref<BBCodeParserTag>();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String BBCodeParserTag::get_attribute_value(const String &attrib) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserAttribute> a = get_attribute(attrib);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
if (a.is_valid()) {
|
2022-06-25 01:55:54 +02:00
|
|
|
return a->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserAttribute> BBCodeParserTag::get_attribute(const String &attrib) {
|
2022-06-25 01:55:54 +02:00
|
|
|
for (int i = 0; i < attributes.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserAttribute> a = attributes[i];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (a->match_attrib(attrib)) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
return Ref<BBCodeParserAttribute>();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BBCodeParserTag::has_attribute(const String &attrib) {
|
|
|
|
for (int i = 0; i < attributes.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserAttribute> a = attributes[i];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (a->match_attrib(attrib)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserAttribute> BBCodeParserTag::get_attribute(const String &attrib, const String &contains_val) {
|
2022-06-25 01:55:54 +02:00
|
|
|
for (int i = 0; i < attributes.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserAttribute> a = attributes[i];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (a->match_attrib(attrib) && a->contains_data(contains_val)) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
return Ref<BBCodeParserAttribute>();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BBCodeParserTag::has_attribute(const String &attrib, const String &contains_val) {
|
|
|
|
for (int i = 0; i < attributes.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserAttribute> a = attributes[i];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (a->match_attrib(attrib) && a->contains_data(contains_val)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BBCodeParserTag::process() {
|
|
|
|
if (type != BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.size() < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_COND(data[0] != '[');
|
|
|
|
ERR_FAIL_COND(data[data.size() - 1] != ']');
|
|
|
|
|
|
|
|
int start_index = 1;
|
|
|
|
if (data[1] == '/') {
|
|
|
|
++start_index;
|
|
|
|
|
|
|
|
type = BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_CLOSING_TAG;
|
|
|
|
} else {
|
|
|
|
String tag_text;
|
|
|
|
|
|
|
|
if (data[data.size() - 2] == '/') {
|
|
|
|
// will catch all that looks like <br/>
|
|
|
|
// tags that look like <br> will be caught later in a post process, in a way
|
|
|
|
// which also tries to catch erroneously not closed tags that supposed to be closed
|
|
|
|
type = BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_SELF_CLOSING_TAG;
|
|
|
|
|
|
|
|
tag_text = data.substr(1, data.size() - 3);
|
|
|
|
} else {
|
|
|
|
type = BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_OPENING_TAG;
|
|
|
|
|
|
|
|
tag_text = data.substr(1, data.size() - 2);
|
|
|
|
}
|
|
|
|
|
2022-07-03 18:40:05 +02:00
|
|
|
int fspc_index = tag_text.find_char(' ');
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (fspc_index == -1) {
|
|
|
|
// no args
|
2022-07-03 18:40:05 +02:00
|
|
|
|
|
|
|
int feq_ind = tag_text.find_char('=');
|
2022-06-25 01:55:54 +02:00
|
|
|
if (feq_ind == -1) {
|
|
|
|
tag = tag_text;
|
|
|
|
return;
|
2022-07-03 18:40:05 +02:00
|
|
|
}
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
//Tag is like: [color=white]
|
|
|
|
//tag will be like color
|
|
|
|
tag = tag_text.substr(0, feq_ind);
|
|
|
|
|
2022-07-03 18:40:05 +02:00
|
|
|
//Add color=white as argument
|
2022-06-25 01:55:54 +02:00
|
|
|
parse_args(tag_text);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// grab the tag itself
|
|
|
|
tag = tag_text.substr(0, fspc_index);
|
|
|
|
|
|
|
|
if (fspc_index + 1 == tag_text.size()) {
|
|
|
|
// no args, but had a space like <br />
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String args = tag_text.substr(fspc_index + 1, tag_text.size() - fspc_index - 1);
|
|
|
|
parse_args(args);
|
|
|
|
}
|
|
|
|
|
2022-07-03 18:40:05 +02:00
|
|
|
int tag_end_index = data.find_char(' ', start_index);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (tag_end_index == -1) {
|
|
|
|
// simple tag
|
|
|
|
tag = data.substr(start_index, data.size() - start_index - 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BBCodeParserTag::parse_args(const String &args) {
|
|
|
|
attributes.clear();
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
while (i < args.size()) {
|
|
|
|
if (args[i] == ' ') {
|
|
|
|
//"trim"
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-03 18:40:05 +02:00
|
|
|
int equals_index = args.find_char('=', i);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserAttribute> a;
|
|
|
|
a.instance();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (equals_index == -1) {
|
|
|
|
a->attribute = args.substr(i, args.size() - i);
|
|
|
|
a->single = true;
|
|
|
|
attributes.push_back(a);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
a->attribute = args.substr(i, equals_index - i);
|
|
|
|
|
|
|
|
// todo
|
|
|
|
// a.trim();
|
|
|
|
|
|
|
|
int next_char_index = equals_index + 1;
|
|
|
|
|
|
|
|
if (next_char_index >= args.size()) {
|
|
|
|
// an attribute looks like this "... attrib="
|
|
|
|
attributes.push_back(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip spaces
|
|
|
|
while (args[next_char_index] == ' ') {
|
|
|
|
++next_char_index;
|
|
|
|
|
|
|
|
if (next_char_index >= args.size()) {
|
|
|
|
// an attribute looks like this "... attrib= "
|
|
|
|
attributes.push_back(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char c = args[next_char_index];
|
|
|
|
char find_char = ' ';
|
|
|
|
|
|
|
|
if (c == '"' || c == '\'') {
|
|
|
|
++next_char_index;
|
|
|
|
find_char = c;
|
|
|
|
}
|
|
|
|
|
2022-07-03 18:40:05 +02:00
|
|
|
int end_index = args.find_char(find_char, next_char_index);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (end_index == -1) {
|
|
|
|
// missing closing ' or " if c is ' or "
|
|
|
|
// else missing parameter
|
|
|
|
|
|
|
|
a->data = args.substr(next_char_index, args.size() - next_char_index);
|
|
|
|
attributes.push_back(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
a->data = args.substr(next_char_index, end_index - next_char_index);
|
|
|
|
attributes.push_back(a);
|
|
|
|
|
|
|
|
i = end_index + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
String BBCodeParserTag::convert_to_string(const int level) const {
|
2022-06-25 01:55:54 +02:00
|
|
|
String s;
|
|
|
|
|
2022-07-03 18:40:05 +02:00
|
|
|
s += String(" ").repeat(level);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (type == BBCODE_PARSER_TAG_TYPE_CONTENT) {
|
|
|
|
s += data + "\n";
|
|
|
|
|
|
|
|
if (tags.size() != 0) {
|
2022-07-03 18:40:05 +02:00
|
|
|
s += String(" ").repeat(level);
|
2022-06-25 01:55:54 +02:00
|
|
|
s += "(!CONTENT TAG HAS TAGS!)\n";
|
|
|
|
|
|
|
|
for (int i = 0; i < tags.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
s += tags[i]->convert_to_string(level + 1) + "\n";
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type == BBCODE_PARSER_TAG_TYPE_OPENING_TAG) {
|
|
|
|
int ln = level + 1;
|
|
|
|
|
|
|
|
s += "[" + tag;
|
|
|
|
|
|
|
|
for (int i = 0; i < attributes.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
s += " " + attributes[i]->convert_to_string();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s += "]\n";
|
|
|
|
|
|
|
|
for (int i = 0; i < tags.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
s += tags[i]->convert_to_string(ln);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-03 18:40:05 +02:00
|
|
|
s += String(" ").repeat(level);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
s += "[/" + tag + "]\n";
|
|
|
|
} else if (type == BBCODE_PARSER_TAG_TYPE_CLOSING_TAG) {
|
|
|
|
// BBCodeParserTag should handle this automatically
|
|
|
|
// it's here for debugging purposes though
|
|
|
|
s += "[/" + tag + "(!)]";
|
|
|
|
|
|
|
|
if (tags.size() != 0) {
|
2022-07-03 18:40:05 +02:00
|
|
|
s += String(" ").repeat(level);
|
2022-06-25 01:55:54 +02:00
|
|
|
s += "(!CLOSING TAG HAS TAGS!)\n";
|
|
|
|
|
|
|
|
for (int i = 0; i < tags.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
s += tags[i]->convert_to_string(level + 1) + "\n";
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type == BBCODE_PARSER_TAG_TYPE_SELF_CLOSING_TAG) {
|
|
|
|
s += "[" + tag;
|
|
|
|
|
|
|
|
for (int i = 0; i < attributes.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
s += " " + attributes[i]->convert_to_string();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s += "/]\n";
|
|
|
|
|
|
|
|
if (tags.size() != 0) {
|
2022-07-03 18:40:05 +02:00
|
|
|
s += String(" ").repeat(level);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < tags.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
s += tags[i]->convert_to_string(level + 1) + "\n";
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type == BBCODE_PARSER_TAG_TYPE_NONE) {
|
|
|
|
for (int i = 0; i < tags.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
s += tags[i]->convert_to_string(level) + "\n";
|
2022-07-03 18:40:05 +02:00
|
|
|
s += String(" ").repeat(level);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2022-07-19 14:27:01 +02:00
|
|
|
void BBCodeParserTag::print() const {
|
|
|
|
PLOG_MSG(convert_to_string());
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BBCodeParserTag::BBCodeParserTag() {
|
|
|
|
type = BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BBCodeParserTag::~BBCodeParserTag() {
|
2022-07-19 14:27:01 +02:00
|
|
|
tags.clear();
|
|
|
|
attributes.clear();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BBCodeParser::parse(const String &data) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Vector<Ref<BBCodeParserTag>> tags;
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
// split into tags
|
|
|
|
for (int i = 0; i < data.size(); ++i) {
|
|
|
|
if (data[i] == '[') {
|
|
|
|
// tag
|
|
|
|
for (int j = i + 1; j < data.size(); ++j) {
|
|
|
|
if (data[j] == ']') {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserTag> t = memnew(BBCodeParserTag);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
t->data = data.substr(i, j - i + 1);
|
|
|
|
t->process();
|
|
|
|
|
|
|
|
tags.push_back(t);
|
|
|
|
|
|
|
|
i = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// content
|
|
|
|
|
|
|
|
for (int j = i + 1; j < data.size(); ++j) {
|
|
|
|
if (data[j] == '[') {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserTag> t = memnew(BBCodeParserTag);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
t->data = data.substr(i, j - i);
|
|
|
|
t->type = BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_CONTENT;
|
|
|
|
|
|
|
|
tags.push_back(t);
|
|
|
|
|
|
|
|
i = j - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
root.instance();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
// process tags into hierarchical order
|
2022-07-19 14:27:01 +02:00
|
|
|
Vector<Ref<BBCodeParserTag>> tag_stack;
|
2022-06-25 01:55:54 +02:00
|
|
|
for (int i = 0; i < tags.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserTag> t = tags[i];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
ERR_CONTINUE(!t.is_valid());
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_NONE) {
|
2022-07-03 18:40:05 +02:00
|
|
|
ERR_PRINT("BBCodeParser::parse: t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_NONE!");
|
2022-07-19 14:27:01 +02:00
|
|
|
//memdelete(t);
|
|
|
|
tags.write[i].unref();
|
2022-06-25 01:55:54 +02:00
|
|
|
continue;
|
|
|
|
} else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_OPENING_TAG) {
|
|
|
|
tag_stack.push_back(t);
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
tags.write[i].unref();
|
2022-06-25 01:55:54 +02:00
|
|
|
continue;
|
|
|
|
} else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_SELF_CLOSING_TAG) {
|
|
|
|
if (tag_stack.size() == 0) {
|
|
|
|
root->tags.push_back(t);
|
|
|
|
} else {
|
2022-07-19 14:27:01 +02:00
|
|
|
tag_stack.write[tag_stack.size() - 1]->tags.push_back(t);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
tags.write[i].unref();
|
2022-06-25 01:55:54 +02:00
|
|
|
continue;
|
|
|
|
} else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_CONTENT) {
|
|
|
|
if (tag_stack.size() == 0) {
|
|
|
|
root->tags.push_back(t);
|
|
|
|
} else {
|
2022-07-19 14:27:01 +02:00
|
|
|
tag_stack.write[tag_stack.size() - 1]->tags.push_back(t);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
tags.write[i].unref();
|
2022-06-25 01:55:54 +02:00
|
|
|
continue;
|
|
|
|
} else if (t->type == BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_CLOSING_TAG) {
|
|
|
|
if (tag_stack.size() == 0) {
|
2022-07-19 14:27:01 +02:00
|
|
|
tags.write[i].unref();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
// ill-formed html
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find it's pair
|
|
|
|
int tag_index = 0;
|
|
|
|
for (int j = tag_stack.size() - 1; j > 0; --j) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserTag> ts = tag_stack[j];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
// we sould only have opening tags on the stack
|
|
|
|
if (ts->tag == t->tag) {
|
|
|
|
// found
|
|
|
|
tag_index = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserTag> opening_tag = tag_stack[tag_index];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
// mark everything else that we found before finding the opening tag as self closing, and add them to out opening tag
|
|
|
|
// If the html is ill formed, it just grabs everything from the tag stack
|
|
|
|
for (int j = tag_index + 1; j < tag_stack.size(); ++j) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserTag> ts = tag_stack[j];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
ts->type = BBCodeParserTag::BBCODE_PARSER_TAG_TYPE_SELF_CLOSING_TAG;
|
|
|
|
opening_tag->tags.push_back(ts);
|
|
|
|
}
|
|
|
|
|
|
|
|
tag_stack.resize(tag_index);
|
|
|
|
|
|
|
|
if (tag_stack.size() == 0) {
|
|
|
|
root->tags.push_back(opening_tag);
|
|
|
|
} else {
|
2022-07-19 14:27:01 +02:00
|
|
|
tag_stack.write[tag_stack.size() - 1]->tags.push_back(opening_tag);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
tags.write[i].unref();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add everything remaining on the stack to root
|
|
|
|
for (int i = 0; i < tag_stack.size(); ++i) {
|
|
|
|
root->tags.push_back(tag_stack[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < tags.size(); ++i) {
|
2022-07-19 14:27:01 +02:00
|
|
|
Ref<BBCodeParserTag> t = tags[i];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
if (t.is_valid()) {
|
2022-07-03 18:40:05 +02:00
|
|
|
ERR_PRINT("BBCodeParser::parse(const String &data): tag was not processed!\n");
|
2022-06-25 01:55:54 +02:00
|
|
|
t->print();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
String BBCodeParser::convert_to_string() const {
|
|
|
|
if (!root.is_valid()) {
|
2022-06-25 01:55:54 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:27:01 +02:00
|
|
|
return root->convert_to_string();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
2022-07-19 14:27:01 +02:00
|
|
|
void BBCodeParser::print() const {
|
|
|
|
if (root.is_valid()) {
|
2022-06-25 01:55:54 +02:00
|
|
|
root->print();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BBCodeParser::BBCodeParser() {
|
|
|
|
}
|
|
|
|
|
|
|
|
BBCodeParser::~BBCodeParser() {
|
2022-07-19 14:27:01 +02:00
|
|
|
root.unref();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|