added format_file() function

This commit is contained in:
2026-01-12 10:41:50 +01:00
parent 84148f92df
commit 8796199757
3 changed files with 49 additions and 5 deletions

View File

@@ -1,4 +1,6 @@
#include "format/format.hpp"
#include "markdown/markdown.hpp" #include "markdown/markdown.hpp"
#include <iostream>
#include <string> #include <string>
static bool replace_once(std::string &str, const std::string &from, static bool replace_once(std::string &str, const std::string &from,
@@ -9,3 +11,20 @@ static bool replace_once(std::string &str, const std::string &from,
str.replace(start_pos, from.length(), to); str.replace(start_pos, from.length(), to);
return true; return true;
} }
std::string format_file(const std::string &TEMPLATE_FILE,
const std::string &TEMPLATE, const std::string &INPUT) {
bool is_template = TEMPLATE_FILE.find(TEMPLATE) != std::string::npos;
std::string result = TEMPLATE_FILE;
if (!is_template) {
std::clog << "There is no template in given file. Is this correct?"
<< std::endl;
return result;
}
while (is_template) {
is_template = replace_once(result, TEMPLATE, INPUT);
}
return result;
}

View File

@@ -1,7 +1,9 @@
#include <string> #include <string>
// Change template from file into html // @brief Format file.
// Return html in string // @param TEMPLATE_FILE File that you want to format in string.
std::string inject_html_into_template(const std::string &FILE_NAME, // @param TEMPLATE Template that you want to replace. For example `{{title}}`
const std::string &TEMPLATE, // @param INPUT Text that you want to replace TEMPLATE with.
const std::string &HTML); // @return Formatted file in std::string
std::string format_file(const std::string &TEMPLATE_FILE,
const std::string &TEMPLATE, const std::string &INPUT);

View File

@@ -31,6 +31,18 @@ constexpr const char *formatter_html = R"(<!DOCTYPE html>
</body> </body>
</html>)"; </html>)";
constexpr const char *formated_html = R"(<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
I'm formatted!
</body>
</html>)";
constexpr const char *no_metadata_post_md = R"(# This markdown has no metadata. constexpr const char *no_metadata_post_md = R"(# This markdown has no metadata.
That's bad. Don't do it. It's for tests only.)"; That's bad. Don't do it. It's for tests only.)";
@@ -88,6 +100,8 @@ TEST_CASE("Test reading a file that doesn't exist") {
REQUIRE_THROWS(read_file("this/path/doesnt/exist")); REQUIRE_THROWS(read_file("this/path/doesnt/exist"));
} }
// Config tests
TEST_CASE("Checking config file read", "[config]") { TEST_CASE("Checking config file read", "[config]") {
Config config = get_config(TestFiles::config_toml); Config config = get_config(TestFiles::config_toml);
@@ -98,6 +112,8 @@ TEST_CASE("Checking config file read", "[config]") {
REQUIRE(config.general.title == "Blog"); REQUIRE(config.general.title == "Blog");
} }
// Markdown tests
TEST_CASE("get_metadata function data correctness test") { TEST_CASE("get_metadata function data correctness test") {
Metadata result = get_metadata(TestFiles::post_md).value(); Metadata result = get_metadata(TestFiles::post_md).value();
@@ -160,3 +176,10 @@ int main() {
REQUIRE(get_html(TestFiles::post_md) == EXPECTED_OUTPUT); REQUIRE(get_html(TestFiles::post_md) == EXPECTED_OUTPUT);
} }
// Format tests
TEST_CASE("Test format_file function with correct data") {
REQUIRE(TestFiles::formated_html == format_file(TestFiles::formatter_html,
"{{replace_me}}",
"I'm formatted!"));
}