From 8796199757156e5fb98d7a27e31145f1c3bb62dd Mon Sep 17 00:00:00 2001 From: Szymon P Date: Mon, 12 Jan 2026 10:41:50 +0100 Subject: [PATCH] added format_file() function --- src/format/format.cpp | 19 +++++++++++++++++++ src/format/format.hpp | 12 +++++++----- src/tests.cpp | 23 +++++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/format/format.cpp b/src/format/format.cpp index 68516de..c7f867f 100644 --- a/src/format/format.cpp +++ b/src/format/format.cpp @@ -1,4 +1,6 @@ +#include "format/format.hpp" #include "markdown/markdown.hpp" +#include #include 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); 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; +} diff --git a/src/format/format.hpp b/src/format/format.hpp index ee70c95..efa9f2b 100644 --- a/src/format/format.hpp +++ b/src/format/format.hpp @@ -1,7 +1,9 @@ #include -// Change template from file into html -// Return html in string -std::string inject_html_into_template(const std::string &FILE_NAME, - const std::string &TEMPLATE, - const std::string &HTML); +// @brief Format file. +// @param TEMPLATE_FILE File that you want to format in string. +// @param TEMPLATE Template that you want to replace. For example `{{title}}` +// @param INPUT Text that you want to replace TEMPLATE with. +// @return Formatted file in std::string +std::string format_file(const std::string &TEMPLATE_FILE, + const std::string &TEMPLATE, const std::string &INPUT); diff --git a/src/tests.cpp b/src/tests.cpp index e13575e..8f82fde 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -31,6 +31,18 @@ constexpr const char *formatter_html = R"( )"; +constexpr const char *formated_html = R"( + + + + + Document + + + I'm formatted! + +)"; + 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.)"; @@ -88,6 +100,8 @@ TEST_CASE("Test reading a file that doesn't exist") { REQUIRE_THROWS(read_file("this/path/doesnt/exist")); } +// Config tests + TEST_CASE("Checking config file read", "[config]") { Config config = get_config(TestFiles::config_toml); @@ -98,6 +112,8 @@ TEST_CASE("Checking config file read", "[config]") { REQUIRE(config.general.title == "Blog"); } +// Markdown tests + TEST_CASE("get_metadata function data correctness test") { Metadata result = get_metadata(TestFiles::post_md).value(); @@ -160,3 +176,10 @@ int main() { 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!")); +}