Initial commit
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include "bbcodes.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace bbcode;
|
||||
|
||||
struct BBStack {
|
||||
shared_ptr<BBEntry> head = make_shared<BBText>();
|
||||
shared_ptr<BBEntry> tail = head;
|
||||
|
||||
string key;
|
||||
string content;
|
||||
};
|
||||
|
||||
shared_ptr<BBEntry> bbcode::parse(string message) {
|
||||
string current_text;
|
||||
string current_key;
|
||||
|
||||
deque<shared_ptr<BBStack>> stack;
|
||||
stack.push_back(make_shared<BBStack>());
|
||||
|
||||
bool escaped = false;
|
||||
bool key_begin = false;
|
||||
bool key_end = false;
|
||||
for(size_t i = 0; i < message.length(); i++) {
|
||||
if(message[i] == '[' && (i == 0 || message[i - 1] != '\\')) {
|
||||
if(!current_text.empty()) {
|
||||
stack.back()->tail->next(make_shared<BBText>(current_text));
|
||||
stack.back()->tail = stack.back()->tail->next();
|
||||
}
|
||||
if(i + 2 < message.length() && message[i + 1] == '/') { //Should be a key end
|
||||
key_end = true;
|
||||
i += 1;
|
||||
continue;
|
||||
} else {
|
||||
key_begin = true;
|
||||
cout << "Message: " << current_text << endl;
|
||||
stack.push_back(make_shared<BBStack>());
|
||||
continue;
|
||||
}
|
||||
} else if(message[i] == ']') {
|
||||
if(key_begin) {
|
||||
cout << "Got key begin '" << current_key << "'" << endl;
|
||||
key_begin = false;
|
||||
stack.back()->key = current_key;
|
||||
current_key = "";
|
||||
continue;
|
||||
} else if(key_end) {
|
||||
cout << "Got key end '" << current_key << "'" << endl;
|
||||
key_end = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if(key_begin || key_end) current_key += message[i];
|
||||
else current_text += message[i];
|
||||
}
|
||||
|
||||
return stack.front()->head;
|
||||
}
|
||||
|
||||
bool bbcode::sloppy::has_tag(std::string message, std::deque<std::string> tag) {
|
||||
std::transform(message.begin(), message.end(), message.begin(), ::tolower);
|
||||
for(auto& entry : tag)
|
||||
std::transform(entry.begin(), entry.end(), entry.begin(), ::tolower);
|
||||
|
||||
std::deque<std::string> begins;
|
||||
size_t index = 0, found, length = 0;
|
||||
do {
|
||||
found = string::npos;
|
||||
for(auto it = tag.begin(); it != tag.end() && found == string::npos; it++) {
|
||||
found = message.find(*it, index);
|
||||
length = it->length();
|
||||
};
|
||||
if(found > 0 && found + length < message.length()) {
|
||||
if(message[found + length] == ']' || (message[found + length] == '=' && message.find(']', found + length) != string::npos)) {
|
||||
if(message[found - 1] == '/') {
|
||||
auto found_tag = message.substr(found, length);
|
||||
for(const auto& entry : begins)
|
||||
if(entry == found_tag) return true;
|
||||
} else if(message[found - 1] == '[' && (found < 2 || message[found - 2] != '\\'))
|
||||
begins.push_back(message.substr(found, length));
|
||||
if(message[found + length] != ']')
|
||||
found = message.find(']', found + length);
|
||||
}
|
||||
}
|
||||
index = found + 1;
|
||||
} while(index != 0);
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
|
||||
namespace bbcode {
|
||||
enum BBType {
|
||||
TEXT,
|
||||
URL,
|
||||
IMG,
|
||||
LIST,
|
||||
UNKNOWN
|
||||
};
|
||||
|
||||
class BBEntry {
|
||||
public:
|
||||
virtual ~BBEntry() = default;
|
||||
virtual BBType type() const = 0;
|
||||
virtual std::string build() const = 0;
|
||||
|
||||
inline std::shared_ptr<BBEntry> next() const { return this->_next; }
|
||||
inline std::shared_ptr<BBEntry> previus() const { return this->_previus; }
|
||||
inline std::shared_ptr<BBEntry> parent() const { return this->_parent.lock(); }
|
||||
|
||||
inline void next(std::shared_ptr<BBEntry> next) { this->_next = std::move(next); }
|
||||
inline void previus(std::shared_ptr<BBEntry> previus) { this->_previus = std::move(previus); }
|
||||
inline void parent(std::shared_ptr<BBEntry> parent) { this->_parent = std::move(parent); }
|
||||
protected:
|
||||
std::weak_ptr<BBEntry> _parent;
|
||||
std::shared_ptr<BBEntry> _previus;
|
||||
std::shared_ptr<BBEntry> _next;
|
||||
};
|
||||
|
||||
class BBText : public BBEntry {
|
||||
public:
|
||||
BBText(const std::string& text = "") { this->_text = text; }
|
||||
|
||||
inline std::string text() const { return this->_text; }
|
||||
inline void text(const std::string& text) { this->_text = text; }
|
||||
|
||||
BBType type() const override { return BBType::TEXT; }
|
||||
|
||||
std::string build() const override { return _text; }
|
||||
|
||||
private:
|
||||
std::string _text;
|
||||
};
|
||||
|
||||
extern std::shared_ptr<BBEntry> parse(std::string);
|
||||
|
||||
namespace sloppy {
|
||||
extern bool has_tag(std::string message, std::deque<std::string> tag);
|
||||
|
||||
inline bool has_url(const std::string& message) { return has_tag(message, {"url"}); }
|
||||
inline bool has_image(const std::string& message) { return has_tag(message, {"img"}); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user