mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 20:20:41 +02:00
Merge branch 'master' into feature/blog-template
This commit is contained in:
@@ -11,10 +11,12 @@ As this is still a WIP, here is a short list of what works and what still needs
|
|||||||
- [x] metadata parsing
|
- [x] metadata parsing
|
||||||
- [x] basic tests
|
- [x] basic tests
|
||||||
- [x] parsing markdown
|
- [x] parsing markdown
|
||||||
- [ ] generating html documents
|
- [x] generating html documents
|
||||||
- [ ] some nicer css styles
|
- [ ] some nicer css styles
|
||||||
- [ ] more sensible templates
|
- [ ] more sensible templates
|
||||||
- [x] change yaml style metadata to toml
|
- [x] change yaml style metadata to toml
|
||||||
|
- [ ] cli
|
||||||
|
- [ ] perhaps someday gui?
|
||||||
|
|
||||||
# What is this?
|
# What is this?
|
||||||
This is a small blog app. Basically a static site generator.
|
This is a small blog app. Basically a static site generator.
|
||||||
|
|||||||
43
src/builder/builder.cpp
Normal file
43
src/builder/builder.cpp
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#include "builder/builder.hpp"
|
||||||
|
#include "config/config.hpp"
|
||||||
|
#include "format/format.hpp"
|
||||||
|
|
||||||
|
std::string build_index(const std::string &TEMPLATE, const Page &PAGE,
|
||||||
|
const Information &INFORMATION) {
|
||||||
|
std::string result = TEMPLATE;
|
||||||
|
result = format_file(result, "{{lang}}", INFORMATION.config.general.lang);
|
||||||
|
result = format_file(result, "{{head}}", PAGE.head);
|
||||||
|
result = format_file(result, "{{header}}", PAGE.header);
|
||||||
|
result = format_file(result, "{{main}}", PAGE.main);
|
||||||
|
result = format_file(result, "{{footer}}", PAGE.footer);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string build_header(const std::string &TEMPLATE,
|
||||||
|
const Information &INFORMATION) {
|
||||||
|
std::string result =
|
||||||
|
format_file(TEMPLATE, "{{title}}", INFORMATION.metadata.title);
|
||||||
|
;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string build_head(const std::string &TEMPLATE,
|
||||||
|
const Information &INFORMATION) {
|
||||||
|
std::string result =
|
||||||
|
format_file(TEMPLATE, "{{title}}", INFORMATION.metadata.title);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string build_footer(const std::string &TEMPLATE,
|
||||||
|
const Information &INFORMATION) {
|
||||||
|
std::string result = TEMPLATE;
|
||||||
|
result =
|
||||||
|
format_file_conditionally(result, "{{if email}}", "{{endif}}",
|
||||||
|
"{{email}}", INFORMATION.config.contact.email);
|
||||||
|
|
||||||
|
result = format_file_conditionally(result, "{{if signal}}", "{{endif}}",
|
||||||
|
"{{signal}}",
|
||||||
|
INFORMATION.config.contact.signal);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
22
src/builder/builder.hpp
Normal file
22
src/builder/builder.hpp
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include "config/config.hpp"
|
||||||
|
#include "markdown/markdown.hpp"
|
||||||
|
|
||||||
|
struct Page {
|
||||||
|
std::string head;
|
||||||
|
std::string header;
|
||||||
|
std::string main;
|
||||||
|
std::string footer;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Information {
|
||||||
|
Config config;
|
||||||
|
Metadata metadata;
|
||||||
|
};
|
||||||
|
|
||||||
|
// @brief Function that builds index.html. It takes all the information from
|
||||||
|
// `Page` struct that should be built before calling it.
|
||||||
|
// @param TEMPLATE Template file that will be used to build index
|
||||||
|
// @param PAGE Page struct with information on html tags
|
||||||
|
// @return Built index.html in string
|
||||||
|
std::string build_index(const std::string &TEMPLATE, const Page &PAGE,
|
||||||
|
const Information &INFORMATION);
|
||||||
@@ -1,31 +1,7 @@
|
|||||||
#include "format/format.hpp"
|
#include "format/format.hpp"
|
||||||
#include "markdown/markdown.hpp"
|
#include "markdown/markdown.hpp"
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
// 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, 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
|
// Remove everything from START to STOP
|
||||||
static bool remove_from_to(std::string &STR, const std::string &START,
|
static bool remove_from_to(std::string &STR, const std::string &START,
|
||||||
const std::string &STOP) {
|
const std::string &STOP) {
|
||||||
@@ -45,46 +21,51 @@ static bool remove_from_to(std::string &STR, const std::string &START,
|
|||||||
|
|
||||||
std::string format_file(const std::string &TEMPLATE_FILE,
|
std::string format_file(const std::string &TEMPLATE_FILE,
|
||||||
const std::string &TEMPLATE, const std::string &INPUT) {
|
const std::string &TEMPLATE, const std::string &INPUT) {
|
||||||
bool is_template = TEMPLATE_FILE.find(TEMPLATE) != std::string::npos;
|
|
||||||
std::string result = TEMPLATE_FILE;
|
std::string result = TEMPLATE_FILE;
|
||||||
|
size_t current_pos = 0;
|
||||||
|
|
||||||
if (!is_template) {
|
while (true) {
|
||||||
std::clog << "There is no template in given file. Is this correct?"
|
size_t found_pos = result.find(TEMPLATE, current_pos);
|
||||||
<< std::endl;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (is_template) {
|
if (found_pos == std::string::npos) {
|
||||||
is_template = replace_once_global(result, TEMPLATE, INPUT);
|
break;
|
||||||
}
|
|
||||||
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) {
|
result.replace(found_pos, TEMPLATE.length(), INPUT);
|
||||||
is_template =
|
current_pos = found_pos + INPUT.length();
|
||||||
replace_once_between(result, TEMPLATE, INPUT, result.find(TEMPLATE));
|
|
||||||
}
|
}
|
||||||
return result;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ std::string format_file(const std::string &TEMPLATE_FILE,
|
|||||||
// @param TEMPLATE_FILE File that you want to format in string.
|
// @param TEMPLATE_FILE File that you want to format in string.
|
||||||
// @param TEMPLATE Full name of the if statement. For example `{{if title}}`
|
// @param TEMPLATE Full name of the if statement. For example `{{if title}}`
|
||||||
// @return Formatted file in std::string
|
// @return Formatted file in std::string
|
||||||
std::string format_file_condtionally(const std::string &TEMPLATE_FILE,
|
std::string format_file_conditionally(const std::string &TEMPLATE_FILE,
|
||||||
const std::string &TEMPLATE,
|
const std::string &START_TAG,
|
||||||
const std::string &INPUT);
|
const std::string &END_TAG,
|
||||||
|
const std::string &TEMPLATE,
|
||||||
|
const std::string &INPUT);
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ SOURCES = main.cpp \
|
|||||||
config/config.cpp \
|
config/config.cpp \
|
||||||
format/format.cpp \
|
format/format.cpp \
|
||||||
markdown/markdown.cpp \
|
markdown/markdown.cpp \
|
||||||
io/io.cpp
|
io/io.cpp \
|
||||||
|
builder/builder.cpp
|
||||||
|
|
||||||
OBJECTS = $(SOURCES:%.cpp=$(BUILD_DIR)/%.o)
|
OBJECTS = $(SOURCES:%.cpp=$(BUILD_DIR)/%.o)
|
||||||
|
|
||||||
@@ -17,7 +18,8 @@ TEST_SOURCES = tests.cpp \
|
|||||||
config/config.cpp \
|
config/config.cpp \
|
||||||
format/format.cpp \
|
format/format.cpp \
|
||||||
markdown/markdown.cpp \
|
markdown/markdown.cpp \
|
||||||
io/io.cpp
|
io/io.cpp \
|
||||||
|
builder/builder.cpp
|
||||||
|
|
||||||
TEST_OBJECTS = $(TEST_SOURCES:%.cpp=$(BUILD_DIR)/%.o)
|
TEST_OBJECTS = $(TEST_SOURCES:%.cpp=$(BUILD_DIR)/%.o)
|
||||||
|
|
||||||
@@ -31,7 +33,9 @@ all: directories $(TARGET)
|
|||||||
directories:
|
directories:
|
||||||
@mkdir -p $(BUILD_DIR)/config
|
@mkdir -p $(BUILD_DIR)/config
|
||||||
@mkdir -p $(BUILD_DIR)/format
|
@mkdir -p $(BUILD_DIR)/format
|
||||||
|
@mkdir -p $(BUILD_DIR)/io
|
||||||
@mkdir -p $(BUILD_DIR)/markdown
|
@mkdir -p $(BUILD_DIR)/markdown
|
||||||
|
@mkdir -p $(BUILD_DIR)/builder
|
||||||
|
|
||||||
$(TARGET): $(OBJECTS)
|
$(TARGET): $(OBJECTS)
|
||||||
$(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS)
|
$(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|||||||
110
src/tests.cpp
110
src/tests.cpp
@@ -1,3 +1,4 @@
|
|||||||
|
#include "builder/builder.hpp"
|
||||||
#include "config/config.hpp"
|
#include "config/config.hpp"
|
||||||
#include "format/format.hpp"
|
#include "format/format.hpp"
|
||||||
#include "io/io.hpp"
|
#include "io/io.hpp"
|
||||||
@@ -43,6 +44,46 @@ constexpr const char *formated_html = R"(<!DOCTYPE html>
|
|||||||
</body>
|
</body>
|
||||||
</html>)";
|
</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 +129,21 @@ This [should be clickable](https://example.com)
|
|||||||
- nine
|
- nine
|
||||||
- six
|
- six
|
||||||
- nine)";
|
- nine)";
|
||||||
|
|
||||||
|
constexpr const char *template_index = R"(<!DOCTYPE html>
|
||||||
|
<html lang="{{lang}}">
|
||||||
|
<head>
|
||||||
|
{{head}}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>{{header}}</header>
|
||||||
|
<main>
|
||||||
|
{{main}}
|
||||||
|
</main>
|
||||||
|
{{footer}}
|
||||||
|
</body>
|
||||||
|
</html>)";
|
||||||
|
|
||||||
} // namespace TestFiles
|
} // namespace TestFiles
|
||||||
|
|
||||||
// IO tests
|
// IO tests
|
||||||
@@ -118,7 +174,7 @@ TEST_CASE("get_metadata function data correctness test") {
|
|||||||
Metadata result = get_metadata(TestFiles::post_md).value();
|
Metadata result = get_metadata(TestFiles::post_md).value();
|
||||||
|
|
||||||
std::string expected_author = "joseph";
|
std::string expected_author = "joseph";
|
||||||
struct tm datetime = {0};
|
struct tm datetime = {};
|
||||||
datetime.tm_year = 2026 - 1900;
|
datetime.tm_year = 2026 - 1900;
|
||||||
datetime.tm_mon = 1 - 1;
|
datetime.tm_mon = 1 - 1;
|
||||||
datetime.tm_mday = 1;
|
datetime.tm_mday = 1;
|
||||||
@@ -183,3 +239,55 @@ TEST_CASE("Test format_file function with correct data") {
|
|||||||
"{{replace_me}}",
|
"{{replace_me}}",
|
||||||
"I'm formatted!"));
|
"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}}", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Builder tests
|
||||||
|
TEST_CASE("build_index correctly assembles the page", "[builder]") {
|
||||||
|
Config config;
|
||||||
|
config.general.lang = "pl";
|
||||||
|
Metadata metadata;
|
||||||
|
|
||||||
|
Information info;
|
||||||
|
info.config = config;
|
||||||
|
info.metadata = metadata;
|
||||||
|
|
||||||
|
Page page;
|
||||||
|
page.head = "<meta charset='utf-8'><title>Test</title>";
|
||||||
|
page.header = "<nav>Menu</nav>";
|
||||||
|
page.main = "<article>Treść wpisu</article>";
|
||||||
|
page.footer = "<footer>Stopka</footer>";
|
||||||
|
|
||||||
|
std::string expected_output = R"(<!DOCTYPE html>
|
||||||
|
<html lang="pl">
|
||||||
|
<head>
|
||||||
|
<meta charset='utf-8'><title>Test</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header><nav>Menu</nav></header>
|
||||||
|
<main>
|
||||||
|
<article>Treść wpisu</article>
|
||||||
|
</main>
|
||||||
|
<footer>Stopka</footer>
|
||||||
|
</body>
|
||||||
|
</html>)";
|
||||||
|
|
||||||
|
std::string result = build_index(TestFiles::template_index, page, info);
|
||||||
|
REQUIRE(result == expected_output);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="{{lang}}">
|
<html lang="{{lang}}">
|
||||||
|
<head>
|
||||||
{{head}}
|
{{head}}
|
||||||
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<header>
|
||||||
{{header}}
|
{{header}}
|
||||||
|
</header>
|
||||||
<main>
|
<main>
|
||||||
<ol id="posts">
|
{{main}}
|
||||||
{{posts}}
|
|
||||||
</ol>
|
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<footer>
|
||||||
|
{{footer}}
|
||||||
|
</footer>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user