initial commit

This commit is contained in:
2024-01-01 15:31:33 +01:00
commit 572fdbb5cc
6 changed files with 289 additions and 0 deletions

11
.gitignore vendored Normal file
View File

@@ -0,0 +1,11 @@
# Xmake cache
.xmake/
build/
# MacOS Cache
.DS_Store
.vscode
assets
html

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);

121
src/Lazy.cpp Normal file
View File

@@ -0,0 +1,121 @@
#include "Lazy.h"
#include "Format.h"
#include <fstream>
#include <iostream>
#include <sstream>
static const std::string HtmlTemplate = R"(
<!DOCTYPE html>
<head>
<meta charset="UTF-8"/>
<link rel="icon" href="http://thesims.freeboxos.fr:30000/dupaigne.png">
<style>
div {
margin: auto;
}
p, h1, h2 {
text-align: center;
}
</style>
</head>
<body>
<div>
%s
</div>
</body>
)";
static std::string RenderTitle(const LazyTitle *element)
{
return Format("<h%i> %s </h%i>", element->GetTitleLevel(), element->GetTitle().c_str(), element->GetTitleLevel());
}
static std::string RenderText(const LazyText *element)
{
return Format("<p> %s </p>", element->GetText().c_str());
}
static std::string RenderImage(const LazyImage *element)
{
return Format("<p><img src=\"%s\" alt=\"%s\" width=\"75%\" /></p>", 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<const LazyImage *>(element));
case LazyType::Text:
return RenderText(dynamic_cast<const LazyText *>(element));
case LazyType::Title:
return RenderTitle(dynamic_cast<const LazyTitle *>(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<std::uint8_t>(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;
}

20
src/main.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include <iostream>
#include <fstream>
#include <memory>
#include <vector>
#include <cstdint>
#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;
}

7
xmake.lua Normal file
View File

@@ -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)")