mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 12:10:42 +02:00
Merge pull request #5 from szymon-jozef/feature/html-substitution
feature/html substitution
This commit is contained in:
30
src/formatter.cpp
Normal file
30
src/formatter.cpp
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#include "markdown.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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 "";
|
||||||
|
}
|
||||||
7
src/formatter.hpp
Normal file
7
src/formatter.hpp
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
// 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);
|
||||||
@@ -21,11 +21,14 @@ utils.o: utils.cpp utils.hpp
|
|||||||
markdown.o: markdown.cpp markdown.hpp
|
markdown.o: markdown.cpp markdown.hpp
|
||||||
$(CXX) $(CXXFLAGS) -c markdown.cpp
|
$(CXX) $(CXXFLAGS) -c markdown.cpp
|
||||||
|
|
||||||
|
formatter.o: formatter.cpp formatter.hpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c formatter.cpp
|
||||||
|
|
||||||
tests.o: $(TEST_DIR)/tests.cpp config.hpp
|
tests.o: $(TEST_DIR)/tests.cpp config.hpp
|
||||||
$(CXX) $(CXXFLAGS) -c $(TEST_DIR)/tests.cpp
|
$(CXX) $(CXXFLAGS) -c $(TEST_DIR)/tests.cpp
|
||||||
|
|
||||||
tests: tests.o config.o utils.o markdown.o
|
tests: tests.o config.o utils.o markdown.o formatter.o
|
||||||
g++ -std=c++17 -Wall tests.o config.o utils.o markdown.o -o $(TEST_DIR)/tests $(TEST_LIBS)
|
g++ -std=c++17 -Wall tests.o config.o utils.o markdown.o formatter.o -o $(TEST_DIR)/tests $(TEST_LIBS)
|
||||||
|
|
||||||
check: tests
|
check: tests
|
||||||
$(TEST_DIR)/tests
|
$(TEST_DIR)/tests
|
||||||
|
|||||||
11
src/tests/formatter.html
Normal file
11
src/tests/formatter.html
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<!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>
|
||||||
|
{{replace_me}}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "../config.hpp"
|
#include "../config.hpp"
|
||||||
|
#include "../formatter.hpp"
|
||||||
#include "../markdown.hpp"
|
#include "../markdown.hpp"
|
||||||
#include "../utils.hpp"
|
#include "../utils.hpp"
|
||||||
#include <catch2/catch_all.hpp>
|
#include <catch2/catch_all.hpp>
|
||||||
@@ -83,3 +84,22 @@ int main() {
|
|||||||
|
|
||||||
REQUIRE(get_html(INPUT) == EXPECTED_OUTPUT);
|
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"(<!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 replaced!
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
)";
|
||||||
|
const std::string output =
|
||||||
|
inject_html_into_template(FILE_NAME, "{{replace_me}}", "I'm replaced!");
|
||||||
|
REQUIRE(EXPECTED_OUTPUT == output);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user