mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 12:10:42 +02:00
added format_file() function
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#include "format/format.hpp"
|
||||
#include "markdown/markdown.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#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);
|
||||
// @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);
|
||||
|
||||
@@ -31,6 +31,18 @@ constexpr const char *formatter_html = R"(<!DOCTYPE html>
|
||||
</body>
|
||||
</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.
|
||||
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!"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user