working commit

This commit is contained in:
2024-01-26 00:16:45 +01:00
parent 4ca287f5e5
commit e60dab78ea
7 changed files with 412 additions and 0 deletions

3
include/BotToken.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
const static char BOT_TOKEN[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

18
include/Format.h Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#include <string>
#include <memory>
#include <stdexcept>
template <typename... Args>
std::string Format(const std::string &format, Args... args)
{
int size = snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
if (size <= 0)
{
throw std::runtime_error("Error during formatting.");
}
std::unique_ptr<char[]> buf(new char[size]);
snprintf(buf.get(), static_cast<std::size_t>(size), format.c_str(), args...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}

112
include/Lazy.h Normal file
View File

@@ -0,0 +1,112 @@
#pragma once
#include <string>
#include <vector>
#include <memory>
enum class LazyType
{
Title,
Text,
Image,
};
class LazyElement
{
public:
virtual LazyType GetType() const = 0;
};
class LazyTitle : public LazyElement
{
private:
std::string m_Title;
std::uint8_t m_TitleLevel;
public:
LazyTitle(const std::string &title, const std::uint8_t level) : m_Title(title), m_TitleLevel(level) {}
virtual LazyType GetType() const override
{
return LazyType::Title;
};
const std::string &GetTitle() const
{
return m_Title;
}
int GetTitleLevel() const
{
return m_TitleLevel;
}
};
class LazyText : public LazyElement
{
private:
std::string m_Text;
public:
LazyText(const std::string &text) : m_Text(text) {}
virtual LazyType GetType() const override
{
return LazyType::Text;
};
const std::string &GetText() const
{
return m_Text;
}
};
class LazyImage : public LazyElement
{
private:
std::string m_ImageLink;
std::string m_ImageAlt;
public:
LazyImage(const std::string &link, const std::string &alt) : m_ImageLink(link), m_ImageAlt(alt) {}
virtual LazyType GetType() const override
{
return LazyType::Image;
};
const std::string &GetAlt() const
{
return m_ImageAlt;
}
const std::string &GetLink() const
{
return m_ImageLink;
}
};
typedef std::vector<std::unique_ptr<LazyElement>> ElementList;
class LazyFile
{
private:
ElementList m_Elements;
public:
void AddImage(const std::string &link, const std::string &alt) {
m_Elements.push_back(std::make_unique<LazyImage>(link, alt));
}
void AddTitle(const std::string &title, const std::uint8_t level) {
m_Elements.push_back(std::make_unique<LazyTitle>(title, level));
}
void AddText(const std::string& text) {
m_Elements.push_back(std::make_unique<LazyText>(text));
}
const ElementList& GetElements() const {
return m_Elements;
}
};
std::string RenderHtml(const LazyFile& lazy);
LazyFile ReadLazyFile(const std::string& filePath);