From 763a9410424fb43e29384255459fd9cdc70ef314 Mon Sep 17 00:00:00 2001 From: Szymon P Date: Thu, 8 Jan 2026 11:36:05 +0100 Subject: [PATCH 1/4] created formatter --- src/formatter.cpp | 31 +++++++++++++++++++++++++++++++ src/formatter.hpp | 0 2 files changed, 31 insertions(+) create mode 100644 src/formatter.cpp create mode 100644 src/formatter.hpp diff --git a/src/formatter.cpp b/src/formatter.cpp new file mode 100644 index 0000000..a96845e --- /dev/null +++ b/src/formatter.cpp @@ -0,0 +1,31 @@ +#include "markdown.hpp" +#include +#include +#include + +bool replace(std::string &str, const std::string &from, const std::string &to) { + size_t start_pos = str.find(from); + if (start_pos == std::string::npos) + return false; + str.replace(start_pos, from.length(), to); + return true; +} + +// 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) { + std::string result; + std::ifstream file(FILE_NAME); + if (!file.is_open()) { + return ""; + } + std::ostringstream ss; + ss << file.rdbuf(); + result = ss.str(); + if (replace(result, TEMPLATE, HTML)) { + return result; + } + return ""; +} diff --git a/src/formatter.hpp b/src/formatter.hpp new file mode 100644 index 0000000..e69de29 From 1754c9fbab4ef77c6f8fbc988144ffbecdf9fdfc Mon Sep 17 00:00:00 2001 From: Szymon P Date: Thu, 8 Jan 2026 11:38:07 +0100 Subject: [PATCH 2/4] added formatter to makefile --- src/makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/makefile b/src/makefile index 934f001..f14ae3e 100644 --- a/src/makefile +++ b/src/makefile @@ -21,11 +21,14 @@ utils.o: utils.cpp utils.hpp markdown.o: markdown.cpp markdown.hpp $(CXX) $(CXXFLAGS) -c markdown.cpp +formatter.o: formatter.cpp formatter.hpp + $(CXX) $(CXXFLAGS) -c formatter.hpp + tests.o: $(TEST_DIR)/tests.cpp config.hpp $(CXX) $(CXXFLAGS) -c $(TEST_DIR)/tests.cpp -tests: tests.o config.o utils.o markdown.o - g++ -std=c++17 -Wall tests.o config.o utils.o markdown.o -o $(TEST_DIR)/tests $(TEST_LIBS) +tests: tests.o config.o utils.o markdown.o formatter.o + g++ -std=c++17 -Wall tests.o config.o utils.o markdown.o formatter.o -o $(TEST_DIR)/tests $(TEST_LIBS) check: tests $(TEST_DIR)/tests From cf3b5025097946a9a5fa39730ae0907308573335 Mon Sep 17 00:00:00 2001 From: Szymon P Date: Thu, 8 Jan 2026 12:32:04 +0100 Subject: [PATCH 3/4] added formatter inject_html test --- src/formatter.cpp | 5 ++--- src/formatter.hpp | 7 +++++++ src/makefile | 2 +- src/tests/formatter.html | 11 +++++++++++ src/tests/tests.cpp | 20 ++++++++++++++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/tests/formatter.html diff --git a/src/formatter.cpp b/src/formatter.cpp index a96845e..202013d 100644 --- a/src/formatter.cpp +++ b/src/formatter.cpp @@ -3,7 +3,8 @@ #include #include -bool replace(std::string &str, const std::string &from, const std::string &to) { +static bool replace(std::string &str, const std::string &from, + const std::string &to) { size_t start_pos = str.find(from); if (start_pos == std::string::npos) return false; @@ -11,8 +12,6 @@ bool replace(std::string &str, const std::string &from, const std::string &to) { return true; } -// 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) { diff --git a/src/formatter.hpp b/src/formatter.hpp index e69de29..ee70c95 100644 --- a/src/formatter.hpp +++ b/src/formatter.hpp @@ -0,0 +1,7 @@ +#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); diff --git a/src/makefile b/src/makefile index f14ae3e..ad44c4f 100644 --- a/src/makefile +++ b/src/makefile @@ -22,7 +22,7 @@ markdown.o: markdown.cpp markdown.hpp $(CXX) $(CXXFLAGS) -c markdown.cpp formatter.o: formatter.cpp formatter.hpp - $(CXX) $(CXXFLAGS) -c formatter.hpp + $(CXX) $(CXXFLAGS) -c formatter.cpp tests.o: $(TEST_DIR)/tests.cpp config.hpp $(CXX) $(CXXFLAGS) -c $(TEST_DIR)/tests.cpp diff --git a/src/tests/formatter.html b/src/tests/formatter.html new file mode 100644 index 0000000..973e582 --- /dev/null +++ b/src/tests/formatter.html @@ -0,0 +1,11 @@ + + + + + + Document + + + {{replace_me}} + + diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index c8e269e..2daa9b1 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -1,4 +1,5 @@ #include "../config.hpp" +#include "../formatter.hpp" #include "../markdown.hpp" #include "../utils.hpp" #include @@ -83,3 +84,22 @@ int main() { REQUIRE(get_html(INPUT) == EXPECTED_OUTPUT); } + +TEST_CASE("formatter injection test") { + const std::string FILE_NAME = "./tests/formatter.html"; + const std::string EXPECTED_OUTPUT = R"( + + + + + Document + + + I'm replaced! + + +)"; + const std::string output = + inject_html_into_template(FILE_NAME, "{{replace_me}}", "I'm replaced!"); + REQUIRE(EXPECTED_OUTPUT == output); +} From 2c8b9f2cfc287752add4a51b62a6da460c82ce59 Mon Sep 17 00:00:00 2001 From: Szymon P Date: Sun, 11 Jan 2026 20:50:49 +0100 Subject: [PATCH 4/4] rename replace to replace_once --- src/formatter.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/formatter.cpp b/src/formatter.cpp index 202013d..c71408f 100644 --- a/src/formatter.cpp +++ b/src/formatter.cpp @@ -3,8 +3,8 @@ #include #include -static bool replace(std::string &str, const std::string &from, - const std::string &to) { +static bool replace_once(std::string &str, const std::string &from, + const std::string &to) { size_t start_pos = str.find(from); if (start_pos == std::string::npos) return false; @@ -23,7 +23,7 @@ std::string inject_html_into_template(const std::string &FILE_NAME, std::ostringstream ss; ss << file.rdbuf(); result = ss.str(); - if (replace(result, TEMPLATE, HTML)) { + if (replace_once(result, TEMPLATE, HTML)) { return result; } return "";