added build_index() function

This commit is contained in:
2026-01-12 23:04:37 +01:00
parent e542115cb8
commit a89e4bb60f
6 changed files with 92 additions and 8 deletions

View File

@@ -11,10 +11,12 @@ As this is still a WIP, here is a short list of what works and what still needs
- [x] metadata parsing
- [x] basic tests
- [x] parsing markdown
- [ ] generating html documents
- [x] generating html documents
- [ ] some nicer css styles
- [ ] more sensible templates
- [x] change yaml style metadata to toml
- [ ] cli
- [ ] perhaps someday gui?
# What is this?
This is a small blog app. Basically a static site generator.

View File

@@ -2,6 +2,17 @@
#include "config/config.hpp"
#include "format/format.hpp"
std::string build_index(const std::string &TEMPLATE, const Page &PAGE,
const Information &INFORMATION) {
std::string result = TEMPLATE;
result = format_file(result, "{{lang}}", INFORMATION.config.general.lang);
result = format_file(result, "{{head}}", PAGE.head);
result = format_file(result, "{{header}}", PAGE.header);
result = format_file(result, "{{main}}", PAGE.main);
result = format_file(result, "{{footer}}", PAGE.footer);
return result;
}
std::string build_header(const std::string &TEMPLATE,
const Information &INFORMATION) {
std::string result =
@@ -22,10 +33,10 @@ std::string build_footer(const std::string &TEMPLATE,
const Information &INFORMATION) {
std::string result = TEMPLATE;
result =
format_file_conditionally(TEMPLATE, "{{if email}}", "{{endif}}",
format_file_conditionally(result, "{{if email}}", "{{endif}}",
"{{email}}", INFORMATION.config.contact.email);
result = format_file_conditionally(TEMPLATE, "{{if signal}}", "{{endif}}",
result = format_file_conditionally(result, "{{if signal}}", "{{endif}}",
"{{signal}}",
INFORMATION.config.contact.signal);
return result;

View File

@@ -1,7 +1,22 @@
#include "config/config.hpp"
#include "markdown/markdown.hpp"
struct Page {
std::string head;
std::string header;
std::string main;
std::string footer;
};
struct Information {
Config config;
Metadata metadata;
};
// @brief Function that builds index.html. It takes all the information from
// `Page` struct that should be built before calling it.
// @param TEMPLATE Template file that will be used to build index
// @param PAGE Page struct with information on html tags
// @return Built index.html in string
std::string build_index(const std::string &TEMPLATE, const Page &PAGE,
const Information &INFORMATION);

View File

@@ -1,3 +1,4 @@
#pragma once
#include <ctime>
#include <optional>
#include <string>

View File

@@ -1,3 +1,4 @@
#include "builder/builder.hpp"
#include "config/config.hpp"
#include "format/format.hpp"
#include "io/io.hpp"
@@ -128,6 +129,21 @@ This [should be clickable](https://example.com)
- nine
- six
- nine)";
constexpr const char *template_index = R"(<!DOCTYPE html>
<html lang="{{lang}}">
<head>
{{head}}
</head>
<body>
<header>{{header}}</header>
<main>
{{main}}
</main>
{{footer}}
</body>
</html>)";
} // namespace TestFiles
// IO tests
@@ -158,7 +174,7 @@ TEST_CASE("get_metadata function data correctness test") {
Metadata result = get_metadata(TestFiles::post_md).value();
std::string expected_author = "joseph";
struct tm datetime = {0};
struct tm datetime = {};
datetime.tm_year = 2026 - 1900;
datetime.tm_mon = 1 - 1;
datetime.tm_mday = 1;
@@ -241,3 +257,37 @@ TEST_CASE("Test format_file_conditionally with empty tag") {
format_file_conditionally(TestFiles::formatter_html_conditionally,
"{{if tag}}", "{{endif}}", "{{tag}}", ""));
}
// Builder tests
TEST_CASE("build_index correctly assembles the page", "[builder]") {
Config config;
config.general.lang = "pl";
Metadata metadata;
Information info;
info.config = config;
info.metadata = metadata;
Page page;
page.head = "<meta charset='utf-8'><title>Test</title>";
page.header = "<nav>Menu</nav>";
page.main = "<article>Treść wpisu</article>";
page.footer = "<footer>Stopka</footer>";
std::string expected_output = R"(<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset='utf-8'><title>Test</title>
</head>
<body>
<header><nav>Menu</nav></header>
<main>
<article>Treść wpisu</article>
</main>
<footer>Stopka</footer>
</body>
</html>)";
std::string result = build_index(TestFiles::template_index, page, info);
REQUIRE(result == expected_output);
}

View File

@@ -1,13 +1,18 @@
<!DOCTYPE html>
<html lang="{{lang}}">
<head>
{{head}}
</head>
<body>
<header>
{{header}}
</header>
<main>
<ol id="posts">
{{posts}}
</ol>
{{main}}
</main>
<footer>
{{footer}}
</footer>
</body>
</html>