From 572fdbb5ccf255ffa39265d353af1ffcdd17b435 Mon Sep 17 00:00:00 2001 From: Simon Pribylski Date: Mon, 1 Jan 2024 15:31:33 +0100 Subject: [PATCH] initial commit --- .gitignore | 11 +++++ include/Format.h | 18 +++++++ include/Lazy.h | 112 +++++++++++++++++++++++++++++++++++++++++++ src/Lazy.cpp | 121 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 20 ++++++++ xmake.lua | 7 +++ 6 files changed, 289 insertions(+) create mode 100644 .gitignore create mode 100644 include/Format.h create mode 100644 include/Lazy.h create mode 100644 src/Lazy.cpp create mode 100644 src/main.cpp create mode 100644 xmake.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0edc472 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + +.vscode + +assets +html \ No newline at end of file diff --git a/include/Format.h b/include/Format.h new file mode 100644 index 0000000..0795820 --- /dev/null +++ b/include/Format.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include + +template +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 buf(new char[size]); + snprintf(buf.get(), static_cast(size), format.c_str(), args...); + return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside +} \ No newline at end of file diff --git a/include/Lazy.h b/include/Lazy.h new file mode 100644 index 0000000..7682586 --- /dev/null +++ b/include/Lazy.h @@ -0,0 +1,112 @@ +#pragma once + +#include +#include +#include + +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> 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(link, alt)); + } + + void AddTitle(const std::string &title, const std::uint8_t level) { + m_Elements.push_back(std::make_unique(title, level)); + } + + void AddText(const std::string& text) { + m_Elements.push_back(std::make_unique(text)); + } + + const ElementList& GetElements() const { + return m_Elements; + } +}; + +std::string RenderHtml(const LazyFile& lazy); +LazyFile ReadLazyFile(const std::string& filePath); \ No newline at end of file diff --git a/src/Lazy.cpp b/src/Lazy.cpp new file mode 100644 index 0000000..285bdf7 --- /dev/null +++ b/src/Lazy.cpp @@ -0,0 +1,121 @@ +#include "Lazy.h" +#include "Format.h" +#include +#include +#include + +static const std::string HtmlTemplate = R"( + + + + + + + + + +
+%s +
+ +)"; + +static std::string RenderTitle(const LazyTitle *element) +{ + return Format(" %s ", element->GetTitleLevel(), element->GetTitle().c_str(), element->GetTitleLevel()); +} + +static std::string RenderText(const LazyText *element) +{ + return Format("

%s

", element->GetText().c_str()); +} + +static std::string RenderImage(const LazyImage *element) +{ + return Format("

\"%s\"

", element->GetLink().c_str(), element->GetAlt().c_str()); +} + +static std::string RenderElement(const LazyElement *element) +{ + switch (element->GetType()) + { + case LazyType::Image: + return RenderImage(dynamic_cast(element)); + + case LazyType::Text: + return RenderText(dynamic_cast(element)); + + case LazyType::Title: + return RenderTitle(dynamic_cast(element)); + + default: + break; + } +} + +std::string RenderHtml(const LazyFile &lazy) +{ + std::string result; + for (const auto &element : lazy.GetElements()) + { + result += RenderElement(element.get()); + result += "\n"; + } + return Format(HtmlTemplate, result.c_str()); +} + +static void ParseLine(LazyFile &file, const std::string &line) +{ + // Checks for empty line + if (line.empty()) + return; + + // Checks for title + static const std::string Title = "######"; + for (int i = 6; i > 0; i--) + { + if (line.substr(0, i) == Title.substr(0, i)) + { + file.AddTitle(line.substr(i), static_cast(i)); + return; + } + } + + // Checks for image + if (line[0] == '!') { + std::stringstream ss {line.substr(1)}; + std::string link, alt; + ss >> link >> alt; + file.AddImage(link, alt); + return; + } + + // Add Raw text + file.AddText(line); +} + +LazyFile ReadLazyFile(const std::string &filePath) +{ + std::ifstream input{filePath}; + if (!input) + { + std::cerr << "Can't read file !\n"; + } + + LazyFile file; + + std::string line; + while (getline(input, line)) + { + ParseLine(file, line); + } + + return file; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..75cd7bb --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include + +#include "Format.h" +#include "Lazy.h" + +static void WriteFile(const std::string &filePath, const std::string &content) +{ + std::ofstream output{filePath}; + output << content; +} + +int main(int argc, char **argv) +{ + WriteFile("index.html", RenderHtml(ReadLazyFile("assets/Chapitre 1 Analyse.lazy"))); + return 0; +} diff --git a/xmake.lua b/xmake.lua new file mode 100644 index 0000000..68ad1d3 --- /dev/null +++ b/xmake.lua @@ -0,0 +1,7 @@ +add_rules("mode.debug", "mode.release") + +target("LazyStudy") + set_kind("binary") + add_files("src/*.cpp") + add_includedirs("include") + set_rundir("$(projectdir)") \ No newline at end of file