diff --git a/README.md b/README.md index 5200a3e..15a31ee 100644 --- a/README.md +++ b/README.md @@ -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] basic tests - [x] parsing markdown -- [ ] generating html documents +- [x] generating html documents - [ ] some nicer css styles - [ ] more sensible templates - [x] change yaml style metadata to toml +- [ ] cli +- [ ] perhaps someday gui? # What is this? This is a small blog app. Basically a static site generator. diff --git a/src/builder/builder.cpp b/src/builder/builder.cpp new file mode 100644 index 0000000..8216505 --- /dev/null +++ b/src/builder/builder.cpp @@ -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; +} diff --git a/src/builder/builder.hpp b/src/builder/builder.hpp new file mode 100644 index 0000000..08c2373 --- /dev/null +++ b/src/builder/builder.hpp @@ -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); diff --git a/src/makefile b/src/makefile index 0d73592..5d76770 100644 --- a/src/makefile +++ b/src/makefile @@ -9,7 +9,8 @@ SOURCES = main.cpp \ config/config.cpp \ format/format.cpp \ markdown/markdown.cpp \ - io/io.cpp + io/io.cpp \ + builder/builder.cpp OBJECTS = $(SOURCES:%.cpp=$(BUILD_DIR)/%.o) @@ -17,7 +18,8 @@ TEST_SOURCES = tests.cpp \ config/config.cpp \ format/format.cpp \ markdown/markdown.cpp \ - io/io.cpp + io/io.cpp \ + builder/builder.cpp TEST_OBJECTS = $(TEST_SOURCES:%.cpp=$(BUILD_DIR)/%.o) @@ -33,6 +35,7 @@ directories: @mkdir -p $(BUILD_DIR)/format @mkdir -p $(BUILD_DIR)/io @mkdir -p $(BUILD_DIR)/markdown + @mkdir -p $(BUILD_DIR)/builder $(TARGET): $(OBJECTS) $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS) diff --git a/src/markdown/markdown.hpp b/src/markdown/markdown.hpp index cff04b1..d55cbe1 100644 --- a/src/markdown/markdown.hpp +++ b/src/markdown/markdown.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/src/tests.cpp b/src/tests.cpp index c2bc76d..4d61b81 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -1,3 +1,4 @@ +#include "builder/builder.hpp" #include "config/config.hpp" #include "format/format.hpp" #include "io/io.hpp" @@ -128,6 +129,21 @@ This [should be clickable](https://example.com) - nine - six - nine)"; + +constexpr const char *template_index = R"( + + + {{head}} + + +
{{header}}
+
+ {{main}} +
+ {{footer}} + +)"; + } // namespace TestFiles // IO tests @@ -158,7 +174,7 @@ TEST_CASE("get_metadata function data correctness test") { Metadata result = get_metadata(TestFiles::post_md).value(); std::string expected_author = "joseph"; - struct tm datetime = {0}; + struct tm datetime = {}; datetime.tm_year = 2026 - 1900; datetime.tm_mon = 1 - 1; datetime.tm_mday = 1; @@ -241,3 +257,37 @@ TEST_CASE("Test format_file_conditionally with empty tag") { 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 = "Test"; + page.header = ""; + page.main = "
Treść wpisu
"; + page.footer = "
Stopka
"; + + std::string expected_output = R"( + + + Test + + +
+
+
Treść wpisu
+
+
Stopka
+ +)"; + + std::string result = build_index(TestFiles::template_index, page, info); + REQUIRE(result == expected_output); +} diff --git a/templates/index.html b/templates/index.html index aed8988..ae4a9bd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,13 +1,18 @@ + {{head}} + +
{{header}} - +
-
    - {{posts}} -
+ {{main}}
+ +