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

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