mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 12:10:42 +02:00
Merge pull request #8 from szymon-jozef/feature/template-replace
feature/template replace
This commit is contained in:
@@ -1,11 +1,71 @@
|
|||||||
|
#include "format/format.hpp"
|
||||||
#include "markdown/markdown.hpp"
|
#include "markdown/markdown.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
static bool replace_once(std::string &str, const std::string &from,
|
// Remove everything from START to STOP
|
||||||
const std::string &to) {
|
static bool remove_from_to(std::string &STR, const std::string &START,
|
||||||
size_t start_pos = str.find(from);
|
const std::string &STOP) {
|
||||||
if (start_pos == std::string::npos)
|
size_t start_pos = STR.find(START);
|
||||||
|
if (start_pos == std::string::npos) {
|
||||||
return false;
|
return false;
|
||||||
str.replace(start_pos, from.length(), to);
|
}
|
||||||
|
|
||||||
|
size_t stop_pos = STR.find(STOP, start_pos);
|
||||||
|
if (stop_pos == std::string::npos) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
STR.replace(start_pos, stop_pos - start_pos + STOP.length(), "");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string format_file(const std::string &TEMPLATE_FILE,
|
||||||
|
const std::string &TEMPLATE, const std::string &INPUT) {
|
||||||
|
std::string result = TEMPLATE_FILE;
|
||||||
|
size_t current_pos = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
size_t found_pos = result.find(TEMPLATE, current_pos);
|
||||||
|
|
||||||
|
if (found_pos == std::string::npos) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.replace(found_pos, TEMPLATE.length(), INPUT);
|
||||||
|
current_pos = found_pos + INPUT.length();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string format_file_conditionally(const std::string &TEMPLATE_FILE,
|
||||||
|
const std::string &START_TAG,
|
||||||
|
const std::string &END_TAG,
|
||||||
|
const std::string &TEMPLATE,
|
||||||
|
const std::string &INPUT) {
|
||||||
|
std::string result = TEMPLATE_FILE;
|
||||||
|
|
||||||
|
if (INPUT.empty()) {
|
||||||
|
while (true) {
|
||||||
|
if (!remove_from_to(result, START_TAG, END_TAG)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// remove START tag
|
||||||
|
size_t pos = 0;
|
||||||
|
while ((pos = result.find(START_TAG, pos)) != std::string::npos) {
|
||||||
|
result.replace(pos, START_TAG.length(), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove END tag
|
||||||
|
pos = 0;
|
||||||
|
while ((pos = result.find(END_TAG, pos)) != std::string::npos) {
|
||||||
|
result.replace(pos, END_TAG.length(), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
// replace
|
||||||
|
result = format_file(result, TEMPLATE, INPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,21 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
// Change template from file into html
|
// @brief Format file.
|
||||||
// Return html in string
|
// @param TEMPLATE_FILE File that you want to format in string.
|
||||||
std::string inject_html_into_template(const std::string &FILE_NAME,
|
// @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);
|
||||||
|
|
||||||
|
// @brief Format file conditionally. Replace everything between `{{if
|
||||||
|
// _something_}}` to `{{endif}}` If INPUT is empty then everything between
|
||||||
|
// this will be removed.
|
||||||
|
// @param TEMPLATE_FILE File that you want to format in string.
|
||||||
|
// @param TEMPLATE Full name of the if statement. For example `{{if title}}`
|
||||||
|
// @return Formatted file in std::string
|
||||||
|
std::string format_file_conditionally(const std::string &TEMPLATE_FILE,
|
||||||
|
const std::string &START_TAG,
|
||||||
|
const std::string &END_TAG,
|
||||||
const std::string &TEMPLATE,
|
const std::string &TEMPLATE,
|
||||||
const std::string &HTML);
|
const std::string &INPUT);
|
||||||
|
|||||||
@@ -31,6 +31,58 @@ constexpr const char *formatter_html = R"(<!DOCTYPE html>
|
|||||||
</body>
|
</body>
|
||||||
</html>)";
|
</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 *formatter_html_conditionally = 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>
|
||||||
|
{{if tag}}
|
||||||
|
<p>{{tag}}</p>
|
||||||
|
{{endif}}
|
||||||
|
</body>
|
||||||
|
</html>)";
|
||||||
|
|
||||||
|
constexpr const char *formated_html_conditionally = 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>
|
||||||
|
|
||||||
|
<p>tag</p>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>)";
|
||||||
|
|
||||||
|
constexpr const char *formated_html_conditionally_empty = 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>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>)";
|
||||||
|
|
||||||
constexpr const char *no_metadata_post_md = R"(# This markdown has no metadata.
|
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.)";
|
That's bad. Don't do it. It's for tests only.)";
|
||||||
|
|
||||||
@@ -88,6 +140,8 @@ TEST_CASE("Test reading a file that doesn't exist") {
|
|||||||
REQUIRE_THROWS(read_file("this/path/doesnt/exist"));
|
REQUIRE_THROWS(read_file("this/path/doesnt/exist"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Config tests
|
||||||
|
|
||||||
TEST_CASE("Checking config file read", "[config]") {
|
TEST_CASE("Checking config file read", "[config]") {
|
||||||
Config config = get_config(TestFiles::config_toml);
|
Config config = get_config(TestFiles::config_toml);
|
||||||
|
|
||||||
@@ -98,6 +152,8 @@ TEST_CASE("Checking config file read", "[config]") {
|
|||||||
REQUIRE(config.general.title == "Blog");
|
REQUIRE(config.general.title == "Blog");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Markdown tests
|
||||||
|
|
||||||
TEST_CASE("get_metadata function data correctness test") {
|
TEST_CASE("get_metadata function data correctness test") {
|
||||||
Metadata result = get_metadata(TestFiles::post_md).value();
|
Metadata result = get_metadata(TestFiles::post_md).value();
|
||||||
|
|
||||||
@@ -160,3 +216,28 @@ int main() {
|
|||||||
|
|
||||||
REQUIRE(get_html(TestFiles::post_md) == EXPECTED_OUTPUT);
|
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!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test format_file function with no templates") {
|
||||||
|
REQUIRE(TestFiles::formated_html ==
|
||||||
|
format_file(TestFiles::formated_html, "{{replace_me}}", "nothing"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test format_file_conditionally with tag") {
|
||||||
|
REQUIRE(TestFiles::formated_html_conditionally ==
|
||||||
|
format_file_conditionally(TestFiles::formatter_html_conditionally,
|
||||||
|
"{{if tag}}", "{{endif}}", "{{tag}}",
|
||||||
|
"tag"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test format_file_conditionally with empty tag") {
|
||||||
|
REQUIRE(TestFiles::formated_html_conditionally_empty ==
|
||||||
|
format_file_conditionally(TestFiles::formatter_html_conditionally,
|
||||||
|
"{{if tag}}", "{{endif}}", "{{tag}}", ""));
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user