mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 12:10:42 +02:00
added format_file_conditionally()
This commit is contained in:
@@ -3,12 +3,43 @@
|
||||
#include <iostream>
|
||||
#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)
|
||||
// Replace TEMPLATE with REPLACMENT in-place
|
||||
static bool replace_once_global(std::string &STR, const std::string &TEMPLATE,
|
||||
const std::string &REPLACMENT) {
|
||||
size_t start_pos = STR.find(TEMPLATE);
|
||||
if (start_pos == std::string::npos) {
|
||||
return false;
|
||||
str.replace(start_pos, from.length(), to);
|
||||
}
|
||||
STR.replace(start_pos, TEMPLATE.length(), REPLACMENT);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Replace TEMPLATE with REPLACMENT between starting from `start`
|
||||
static bool replace_once_between(std::string &STR, const std::string &TEMPLATE,
|
||||
const std::string &REPLACMENT,
|
||||
const size_t &START) {
|
||||
size_t start_pos = STR.find(TEMPLATE, START);
|
||||
if (start_pos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
STR.replace(start_pos, TEMPLATE.length(), REPLACMENT);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Remove everything from START to STOP
|
||||
static bool remove_from_to(std::string &STR, const std::string &START,
|
||||
const std::string &STOP) {
|
||||
size_t start_pos = STR.find(START);
|
||||
if (start_pos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -24,7 +55,36 @@ std::string format_file(const std::string &TEMPLATE_FILE,
|
||||
}
|
||||
|
||||
while (is_template) {
|
||||
is_template = replace_once(result, TEMPLATE, INPUT);
|
||||
is_template = replace_once_global(result, TEMPLATE, INPUT);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string format_file_condtionally(const std::string &TEMPLATE_FILE,
|
||||
const std::string &TEMPLATE,
|
||||
const std::string &INPUT) {
|
||||
|
||||
size_t template_pos = TEMPLATE_FILE.find(TEMPLATE);
|
||||
bool is_template = template_pos != 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;
|
||||
}
|
||||
|
||||
// if template is empty then we remove everything in between
|
||||
if (TEMPLATE == "") {
|
||||
while (is_template) {
|
||||
is_template = remove_from_to(result, TEMPLATE, "{{endif}");
|
||||
}
|
||||
}
|
||||
|
||||
while (is_template) {
|
||||
is_template =
|
||||
replace_once_between(result, TEMPLATE, INPUT, result.find(TEMPLATE));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -7,3 +7,13 @@
|
||||
// @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_condtionally(const std::string &TEMPLATE_FILE,
|
||||
const std::string &TEMPLATE,
|
||||
const std::string &INPUT);
|
||||
|
||||
Reference in New Issue
Block a user