Compare commits

..

11 Commits

13 changed files with 286 additions and 14 deletions

View File

@@ -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.

27
flake.lock generated Normal file
View File

@@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1771848320,
"narHash": "sha256-0MAd+0mun3K/Ns8JATeHT1sX28faLII5hVLq0L3BdZU=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "2fc6539b481e1d2569f25f8799236694180c0993",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

26
flake.nix Normal file
View File

@@ -0,0 +1,26 @@
{
description = "Static site generator in c++";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs =
{ self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in
{
devShells.${system}.default = pkgs.mkShell {
packages = with pkgs; [
gcc
gnumake
md4c
catch2_3
clang-tools
gdb
];
};
};
}

58
src/builder/builder.cpp Normal file
View File

@@ -0,0 +1,58 @@
#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, "{{config.lang}}", INFORMATION.config.general.lang);
result = format_file(result, "{{page.head}}", PAGE.head);
result = format_file(result, "{{page.header}}", PAGE.header);
result = format_file(result, "{{page.main}}", PAGE.main);
result = format_file(result, "{{page.footer}}", PAGE.footer);
return result;
}
std::string build_blog_entry(const std::string &TEMPLATE, const Page &BLOG,
const Information &INFORMATION) {
std::string result = TEMPLATE;
result =
format_file(result, "{{config.lang}}", INFORMATION.config.general.lang);
result = format_file(result, "{{page.head}}", BLOG.head);
result = format_file(result, "{{page.header}}", BLOG.header);
result =
format_file(result, "{{metadata.title}}", INFORMATION.metadata.title);
result = format_file(result, "{{page.content}}", BLOG.main);
result = format_file(result, "{{page.footer}}", BLOG.footer);
return result;
}
std::string build_header(const std::string &TEMPLATE,
const Information &INFORMATION) {
std::string result =
format_file(TEMPLATE, "{{metadata.title}}", INFORMATION.metadata.title);
;
return result;
}
std::string build_head(const std::string &TEMPLATE,
const Information &INFORMATION) {
std::string result =
format_file(TEMPLATE, "{{metadata.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;
}

31
src/builder/builder.hpp Normal file
View File

@@ -0,0 +1,31 @@
#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);
// @brief Function that builds particular blog entry. It takes all it's
// information flog `Page struct` that should be built before calling it.
// @param TEMPLATE Template file for blog
// @param PAGE Page struct with information on blog entry content
// @param INFORMATION Information metadata about config and metadata of the file
// @return Built blog_entry.html in string
std::string build_blog_entry(const std::string &TEMPLATE, const Page &BLOG,
const Information &INFORMATION);

View File

@@ -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)

View File

@@ -1,3 +1,4 @@
#pragma once
#include <ctime>
#include <optional>
#include <string>

View File

@@ -1,3 +1,4 @@
#include "builder/builder.hpp"
#include "config/config.hpp"
#include "format/format.hpp"
#include "io/io.hpp"
@@ -128,6 +129,35 @@ This [should be clickable](https://example.com)
- nine
- six
- nine)";
constexpr const char *template_index = R"(<!DOCTYPE html>
<html lang="{{config.lang}}">
<head>
{{page.head}}
</head>
<body>
<header>{{page.header}}</header>
<main>
{{page.main}}
</main>
{{page.footer}}
</body>
</html>)";
constexpr const char *template_blog_entry = R"(<!DOCTYPE html>
<html lang="{{config.lang}}">
<head>
{{page.head}}
</head>
<body>
<header>{{page.header}}</header>
<main>
<article>{{page.content}}</article>
</main>
{{page.footer}}
</body>
</html>)";
} // namespace TestFiles
// IO tests
@@ -158,7 +188,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 +271,71 @@ 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 = "<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);
}
TEST_CASE("test build_blog_entry 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 = "Hej ho!";
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>Hej ho!</article>
</main>
<footer>Stopka</footer>
</body>
</html>)";
std::string result =
build_blog_entry(TestFiles::template_blog_entry, page, info);
REQUIRE(result == expected_output);
}

21
templates/blog_entry.html Normal file
View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="{{config.lang}}">
<head>
{{page.head}}
</head>
<body>
<header>
<h1>{{metadata.title}}</h1>
</header>
<main>
<article>
{{page.content}}
</article>
</main>
<footer>
{{page.footer}}
</footer>
</body>
</html>

View File

@@ -1,5 +1,5 @@
<footer>
<p>Strona napisana przez: {{author}}</p>
<p>Blog owned by: {{config.author}}</p>
<ul>
{{if email}}
<li><a href="mailto:{{email}}">{{email}}</a></li>

View File

@@ -1,6 +1,6 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{title}}</title>
<title>{{metadata.title}}</title>
<link rel="stylesheet" href="/style/style.css">
</head>

View File

@@ -1,3 +1,3 @@
<header>
<h1>{{title}}</h1>
<h1>{{metadata.title}}</h1>
</header>

View File

@@ -1,13 +1,18 @@
<!DOCTYPE html>
<html lang="{{lang}}">
{{head}}
<html lang="{{config.lang}}">
<head>
{{page.head}}
</head>
<body>
{{header}}
<header>
{{page.header}}
</header>
<main>
<ol id="posts">
{{posts}}
</ol>
{{page.main}}
</main>
<footer>
{{page.footer}}
</footer>
</body>
</html>