diff --git a/src/formatter.cpp b/src/formatter.cpp new file mode 100644 index 0000000..c71408f --- /dev/null +++ b/src/formatter.cpp @@ -0,0 +1,30 @@ +#include "markdown.hpp" +#include +#include +#include + +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; + str.replace(start_pos, from.length(), to); + return true; +} + +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_once(result, TEMPLATE, HTML)) { + return result; + } + return ""; +} diff --git a/src/formatter.hpp b/src/formatter.hpp new file mode 100644 index 0000000..ee70c95 --- /dev/null +++ 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 934f001..ad44c4f 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.cpp + 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 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); +}