Compare commits

...

53 Commits

Author SHA1 Message Date
Szymon
84148f92df Update c-cpp.yml 2026-01-11 23:07:05 +01:00
Szymon
2013826acb Update c-cpp.yml 2026-01-11 23:04:22 +01:00
Szymon
bdcf7cf5da Merge pull request #7 from szymon-jozef/rewrite/io
rewrite/io
2026-01-11 23:02:58 +01:00
858bd19f10 added io tests 2026-01-11 23:02:19 +01:00
eecadf4fab updated makefile, adding new io module 2026-01-11 22:54:16 +01:00
8556937f58 remove this annoying cache that i can't ignore for some reason 2026-01-11 22:52:31 +01:00
bc9228cdc7 change test to take into account new io system 2026-01-11 22:51:57 +01:00
74b46f4923 added cache to .gitignore 2026-01-11 22:34:47 +01:00
763761ea82 removed cache 2026-01-11 22:34:20 +01:00
1668cb8563 moved all the file operation logic into io module 2026-01-11 22:33:58 +01:00
5fa62d64ee added io functionality 2026-01-11 22:19:00 +01:00
b171f11982 create io 2026-01-11 21:59:31 +01:00
Szymon
d2efd637cd Merge pull request #6 from szymon-jozef/refactor/reorganize
refactor/reorganize
2026-01-11 21:39:17 +01:00
90adcd88e7 renamed tests_files to test_files
fixed test paths
2026-01-11 21:38:47 +01:00
ddc590571a update gitignore again 2026-01-11 21:35:14 +01:00
35cb6e3d82 renamed tests/ to tests_files/ 2026-01-11 21:32:02 +01:00
a2d76d4c4f fixed include paths 2026-01-11 21:31:41 +01:00
ef8fbfbb53 added clangd thing 2026-01-11 21:30:57 +01:00
e9b88530c2 update gitignore 2026-01-11 21:03:18 +01:00
322df82288 create build dir 2026-01-11 21:01:48 +01:00
fe02a16416 create modules 2026-01-11 21:00:13 +01:00
Szymon
dd2c6ca810 Merge pull request #5 from szymon-jozef/feature/html-substitution
feature/html substitution
2026-01-11 20:51:45 +01:00
2c8b9f2cfc rename replace to replace_once 2026-01-11 20:50:49 +01:00
cf3b502509 added formatter inject_html test 2026-01-08 12:32:04 +01:00
1754c9fbab added formatter to makefile 2026-01-08 11:38:07 +01:00
763a941042 created formatter 2026-01-08 11:36:05 +01:00
Szymon
a59773ef3e Merge pull request #4 from szymon-jozef/html-style/upgrade
Templates update
2026-01-08 11:20:27 +01:00
210f3ec04b updated templates 2026-01-08 11:18:45 +01:00
Szymon
ca7e6c8be6 Update README.md
Added status badge
2026-01-07 12:50:59 +01:00
94fbc3f30d removed test of removed function 2026-01-07 12:49:00 +01:00
55af7474d0 removed unneeded include 2026-01-07 12:47:33 +01:00
931a1908c1 Merge branch 'master' of github.com:szymon-jozef/static-site-generator 2026-01-07 12:44:13 +01:00
0bae066c1f removed convert_string_to_set() function declaration 2026-01-07 12:44:06 +01:00
Szymon
a486a04c2f Update c-cpp.yml
I hate ubuntu!!!!
2026-01-07 12:39:35 +01:00
Szymon
944a07ae32 Update c-cpp.yml 2026-01-07 12:35:19 +01:00
Szymon
cc344019dd Update c-cpp.yml 2026-01-07 12:22:34 +01:00
Szymon
173d1954b3 Create c-cpp.yml 2026-01-07 12:20:02 +01:00
Szymon
a1c8c67021 Merge pull request #3 from szymon-jozef/refactor/yaml-to-toml
Change metadata format from yaml to toml
2026-01-07 12:13:38 +01:00
3339e6d6a2 removed changing into set function, since no sets are used anymore 2026-01-07 12:10:55 +01:00
df9874b3e2 readme update 2026-01-07 12:06:24 +01:00
0e0bc02441 fixed date error 2026-01-07 12:00:05 +01:00
671bfc0a35 changed yaml to toml 2026-01-07 11:52:17 +01:00
f3a13fbfd6 readme update 2026-01-06 18:19:11 +01:00
Szymon
3ad3f873eb Merge pull request #2 from szymon-jozef/refactor/tests
Moved tests into it's own directory for better readability"
2026-01-06 18:15:59 +01:00
8679a627de move tests.cpp file to tests/ dir 2026-01-06 18:14:56 +01:00
38226a350f updated makefile for new tests 2026-01-06 18:13:22 +01:00
5fa92851eb updated gitignore 2026-01-06 18:12:41 +01:00
cf272d9156 update examples dir 2026-01-06 18:11:34 +01:00
546979c413 update readme 2026-01-06 18:10:54 +01:00
ba708e3f32 moved tests to it's own directory 2026-01-06 18:09:20 +01:00
Szymon
ad01b5e7ac Merge pull request #1 from szymon-jozef/parsing-markdown
Added parsing markdown functionality
2026-01-06 17:51:32 +01:00
81cc7979b7 added tests for get_html() function 2026-01-06 17:46:31 +01:00
6d4cb18809 added get_html() function 2026-01-06 17:46:10 +01:00
26 changed files with 546 additions and 179 deletions

28
.github/workflows/c-cpp.yml vendored Normal file
View File

@@ -0,0 +1,28 @@
name: C/C++ CI
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./src
steps:
- uses: actions/checkout@v4
- name: install dependencies
run: |
sudo apt update
sudo apt install -y g++ catch2 libmd4c-dev libmd4c-html0-dev libtomlplusplus-dev
- name: make
run: make
- name: make test
run: make test
- name: make clean
run: make clean

4
.gitignore vendored
View File

@@ -1,3 +1,7 @@
out/
posts/
src/build/
/src/blog
/src/tests
/src/.cache/
*.o

View File

@@ -1,3 +1,5 @@
[![C/C++ CI](https://github.com/szymon-jozef/static-site-generator/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/szymon-jozef/static-site-generator/actions/workflows/c-cpp.yml)
# IMPORTANT
This is still work in progress. It doesn't work yet.
I pushed it onto github just to have easy access from other computers.
@@ -8,10 +10,11 @@ As this is still a WIP, here is a short list of what works and what still needs
- [x] config parsing
- [x] metadata parsing
- [x] basic tests
- [ ] parsing markdown
- [x] parsing markdown
- [ ] generating html documents
- [ ] some nicer css styles
- [ ] more sensible templates
- [x] change yaml style metadata to toml
# What is this?
This is a small blog app. Basically a static site generator.
@@ -32,10 +35,11 @@ Every blog post needs to be in a different file in `/posts` directory.
An example from `/examples/post.md`
```md
---
author: "joseph"
date: "2026-01-01"
title: "Example of a blog post"
tags: ["blog", "test", "lalilulelo"]
[metadata]
author = "joseph"
date = 2026-01-01
title = "Example of a blog post"
tags = ["blog", "test", "lalilulelo"]
---
# This is an example post
@@ -80,7 +84,7 @@ From there you can rsync/push it to your server or do whatever you want to do wi
# How does the directory structure work?
Also pretty simple. You have:
- `/config` for config files. Initially empty so you need to copy it from example dir or make one yourself.
- `/examples` example of configuration files. Tests are dependent on it so don't edit the content of these files! You can copy it to `/config` and edit it there.
- `/examples` example of configuration files. You can copy it to `/config` and edit it there.
- `/out` for processed site.
- `/posts` here you write your blog posts. Every file should be in markdown format, metadata included!.
- `/src` for cpp files. Here's where you run the binary to translate your markdown posts into html.
@@ -115,20 +119,22 @@ signal = "signal url"
# Metadata explanation
Metadata is everything at the beginning of the markdown file between `---`.
It's written in a format similar to yaml.
It's also written in toml. Why toml? I already use it's parser so it's easy to implement.
Besides it's readable and easy to write.
## Fields
- author the author of particular blog entry | string
- date date of writing an entry | string YYYY-MM-DD
- date date of writing an entry | date
- title title of blog entry | string
- tags tags for blog entry. | json like list
- tags tags for blog entry. | array
## Example
```
author: "joseph"
date: "2026-01-01"
title: "Example of a blog post"
tags: ["blog", "test", "lalilulelo"]
[metadata]
author = "joseph"
date = 2026-01-01
title = "Example of a blog post"
tags = ["blog", "test", "lalilulelo"]
```
# Why did you do this?

74
src/compile_commands.json Normal file
View File

@@ -0,0 +1,74 @@
[
{
"file": "main.cpp",
"arguments": [
"g++",
"-std=c++17",
"-Wall",
"-Wextra",
"-O2",
"-I",
".",
"-c",
"main.cpp",
"-o",
"build/main.o"
],
"directory": "/home/szymon/Dokumenty/kodowanie/blog/src",
"output": "build/main.o"
},
{
"file": "config/config.cpp",
"arguments": [
"g++",
"-std=c++17",
"-Wall",
"-Wextra",
"-O2",
"-I",
".",
"-c",
"config/config.cpp",
"-o",
"build/config/config.o"
],
"directory": "/home/szymon/Dokumenty/kodowanie/blog/src",
"output": "build/config/config.o"
},
{
"file": "format/format.cpp",
"arguments": [
"g++",
"-std=c++17",
"-Wall",
"-Wextra",
"-O2",
"-I",
".",
"-c",
"format/format.cpp",
"-o",
"build/format/format.o"
],
"directory": "/home/szymon/Dokumenty/kodowanie/blog/src",
"output": "build/format/format.o"
},
{
"file": "markdown/markdown.cpp",
"arguments": [
"g++",
"-std=c++17",
"-Wall",
"-Wextra",
"-O2",
"-I",
".",
"-c",
"markdown/markdown.cpp",
"-o",
"build/markdown/markdown.o"
],
"directory": "/home/szymon/Dokumenty/kodowanie/blog/src",
"output": "build/markdown/markdown.o"
}
]

View File

@@ -5,10 +5,10 @@
#include <toml++/impl/parser.hpp>
#include <toml++/toml.hpp>
Config get_config(const std::string &CONFIG_FILE) {
Config get_config(const std::string &CONFIGURATION) {
toml::table toml_table;
try {
toml_table = toml::parse_file(CONFIG_FILE);
toml_table = toml::parse(CONFIGURATION);
} catch (const toml::parse_error &err) {
std::cerr << "Parsing failed:" << std::endl << err << std::endl;
}

View File

@@ -17,4 +17,7 @@ struct Config {
General general;
};
Config get_config(const std::string &CONFIG_FILE);
// @brief Load config into `Config` struct
// @param CONFIGURATION content of `.toml` file containing configuration
// @return Config with parsed configuration
Config get_config(const std::string &CONFIGURATION);

11
src/format/format.cpp Normal file
View File

@@ -0,0 +1,11 @@
#include "markdown/markdown.hpp"
#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)
return false;
str.replace(start_pos, from.length(), to);
return true;
}

7
src/format/format.hpp Normal file
View File

@@ -0,0 +1,7 @@
#include <string>
// Change template from file into html
// Return html in string
std::string inject_html_into_template(const std::string &FILE_NAME,
const std::string &TEMPLATE,
const std::string &HTML);

36
src/io/io.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include "io/io.hpp"
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string>
std::string read_file(const std::string &FILE_PATH) {
std::ifstream file(FILE_PATH);
if (!file) {
throw std::runtime_error("Error while opening file at " + FILE_PATH);
}
std::stringstream buffer;
buffer << file.rdbuf();
if (file.bad()) {
throw std::runtime_error("Error while reading file at " + FILE_PATH);
}
return buffer.str();
}
void write_file(const std::string &FILE_PATH, const std::string &CONTENT) {
std::ofstream out(FILE_PATH);
if (!out) {
throw std::runtime_error("Error while opening file for writing at " +
FILE_PATH);
}
out << CONTENT;
if (!out) {
throw std::runtime_error("Error while writing to the " + FILE_PATH);
}
out.close();
}

13
src/io/io.hpp Normal file
View File

@@ -0,0 +1,13 @@
#include <string>
// @brief Read file from given path
// @param FILE PATH Path to the file
// @return File content
// @throws std::runtime_error if any errors
std::string read_file(const std::string &FILE_PATH);
/// @brief Writes the given content into a file.
/// @param FILE_PATH Path to the file to write.
/// @param CONTENT Content to write into the file.
/// @throws std::runtime_error if the file cannot be opened or written.
void write_file(const std::string &FILE_PATH, const std::string &CONTENT);

View File

@@ -1,4 +1,4 @@
#include "config.hpp"
#include "config/config.hpp"
#include <iostream>
#include <string>

View File

@@ -1,33 +1,53 @@
CXX = g++
CXXFLAGS = -std=c++17 -Wall
TEST_LIBS = -lmd4c-html -lCatch2Main -lCatch2
CXXFLAGS = -std=c++17 -Wall -Wextra -O2 -I .
LIBS = -lmd4c-html
TEST_LIBS = -lmd4c-html -lCatch2Main -lCatch2
all: blog
BUILD_DIR = build
blog: main.o config.o utils.o
$(CXX) $(CXXFLAGS) main.o config.o -o blog $(LIBS)
SOURCES = main.cpp \
config/config.cpp \
format/format.cpp \
markdown/markdown.cpp \
io/io.cpp
main.o: main.cpp config.hpp
$(CXX) $(CXXFLAGS) -c main.cpp
OBJECTS = $(SOURCES:%.cpp=$(BUILD_DIR)/%.o)
config.o: config.cpp config.hpp
$(CXX) $(CXXFLAGS) -c config.cpp
TEST_SOURCES = tests.cpp \
config/config.cpp \
format/format.cpp \
markdown/markdown.cpp \
io/io.cpp
utils.o: utils.cpp utils.hpp
$(CXX) $(CXXFLAGS) -c utils.cpp
TEST_OBJECTS = $(TEST_SOURCES:%.cpp=$(BUILD_DIR)/%.o)
markdown.o: markdown.cpp markdown.hpp
$(CXX) $(CXXFLAGS) -c markdown.cpp
TARGET = blog
TEST_TARGET = tests
tests: tests.o config.o utils.o markdown.o
g++ -std=c++17 -Wall tests.o config.o utils.o markdown.o -o tests $(TEST_LIBS)
.PHONY: all clean test directories
tests.o: tests.cpp config.hpp
$(CXX) $(CXXFLAGS) -c tests.cpp
all: directories $(TARGET)
check: tests
./tests
directories:
@mkdir -p $(BUILD_DIR)/config
@mkdir -p $(BUILD_DIR)/format
@mkdir -p $(BUILD_DIR)/markdown
$(TARGET): $(OBJECTS)
$(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS)
$(TEST_TARGET): $(TEST_OBJECTS)
$(CXX) $(CXXFLAGS) -o $@ $^ $(TEST_LIBS)
$(BUILD_DIR)/%.o: %.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -c $< -o $@
test: directories $(TEST_TARGET)
./$(TEST_TARGET)
clean:
rm -f *.o blog tests
rm -rf $(BUILD_DIR)
rm -f $(TARGET) $(TEST_TARGET)
rebuild: clean all

View File

@@ -1,43 +0,0 @@
#include "markdown.hpp"
#include "utils.hpp"
#include <fstream>
#include <iostream>
#include <map>
#include <md4c-html.h>
#include <optional>
std::optional<Metadata> get_metadata(const std::string &FILE_PATH) {
std::fstream file(FILE_PATH);
std::string buffer;
if (!getline(file, buffer) || buffer != "---") {
std::cerr << "File " << FILE_PATH << "does not contain metadata."
<< std::endl;
return std::nullopt;
}
std::map<std::string, std::string> metadata_map;
while (getline(file, buffer)) {
if (buffer == "---") {
break;
}
size_t colon_pos = buffer.find(":");
if (colon_pos == std::string::npos) {
std::cerr << "The line: \n"
<< buffer << " does not contain a colon" << std::endl;
}
std::string key = buffer.substr(0, colon_pos);
trim(key);
std::string value = buffer.substr(colon_pos + 1);
trim(value);
metadata_map[key] = value;
}
return std::optional(Metadata{metadata_map["author"], metadata_map["date"],
metadata_map["title"],
convert_string_to_set(metadata_map["tags"])});
}

94
src/markdown/markdown.cpp Normal file
View File

@@ -0,0 +1,94 @@
#include "markdown.hpp"
#include <ctime>
#include <iostream>
#include <map>
#include <md4c-html.h>
#include <optional>
#include <sstream>
#include <toml++/toml.hpp>
std::optional<Metadata> get_metadata(const std::string &FILE_CONTENTS) {
std::stringstream file_contents(FILE_CONTENTS);
std::stringstream metadata_s;
std::string buffer;
/* if there's no ---, then no metadata in file there is */
if (!getline(file_contents, buffer) || buffer != "---") {
std::clog << "File does not contain any metadata. Is this intentional?"
<< std::endl;
return std::nullopt;
}
std::map<std::string, std::string> metadata_map;
while (getline(file_contents, buffer)) {
if (buffer == "---") {
break;
}
metadata_s << buffer << '\n';
}
toml::table toml_table = toml::parse(metadata_s);
std::vector<std::string> tags;
const auto &toml_tags = toml_table["metadata"]["tags"];
// translate toml into normal vector of strings
if (toml_tags.is_array()) {
for (auto iter = toml_tags.as_array()->begin();
iter < toml_tags.as_array()->end(); iter++) {
tags.emplace_back(static_cast<std::string>(iter->as_string()->get()));
}
}
// time
auto toml_time = toml_table["metadata"]["date"];
struct tm t = {};
time_t timestamp = time(NULL);
if (toml_time.is_date()) {
toml::date d = toml_time.as_date()->get();
t.tm_year = d.year - 1900;
t.tm_mon = d.month - 1;
t.tm_mday = d.day;
t.tm_isdst = 0;
timestamp = mktime(&t);
}
Metadata metadata = {toml_table["metadata"]["author"].value_or(""), timestamp,
toml_table["metadata"]["title"].value_or(""), tags};
return std::optional(metadata);
}
static void html_callback(const MD_CHAR *data, MD_SIZE size, void *userdata) {
std::string *out = static_cast<std::string *>(userdata);
out->append(data, size);
}
std::string get_html(const std::string &FILE_CONTENTS) {
std::stringstream file_contents(FILE_CONTENTS);
std::string buffer, file_string, out;
getline(file_contents, buffer);
bool is_metadata = buffer == "---";
/* skip metadata */
if (is_metadata) {
while (getline(file_contents, buffer)) {
if (buffer == "---") {
break;
}
}
} else {
file_string += buffer + '\n';
}
while (getline(file_contents, buffer)) {
file_string += buffer + '\n';
}
md_html(file_string.c_str(), file_string.size(), html_callback, &out, 0, 0);
return out;
}

View File

@@ -1,12 +1,14 @@
#include <ctime>
#include <optional>
#include <set>
#include <string>
#include <toml++/toml.hpp>
#include <vector>
struct Metadata {
std::string author;
std::string date;
time_t date;
std::string title;
std::set<std::string> tags;
std::vector<std::string> tags;
};
// Reads the metadata of the markdown file

View File

@@ -0,0 +1,8 @@
[general]
lang = "en"
title = "Blog"
[contact]
author = "author"
email = "email@example.com"
signal = "signal url"

View File

@@ -0,0 +1,11 @@
<!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>
{{replace_me}}
</body>
</html>

BIN
src/test_files/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 290 KiB

42
src/test_files/post.md Normal file
View File

@@ -0,0 +1,42 @@
---
[metadata]
author = "joseph"
date = 2026-01-01
title = "Example of a blog post"
tags = ["blog", "test", "lalilulelo"]
---
# This is an example post
In this example we will do a couple of cool markdown things.
## This is a h2 header
*This text is written in italic!*
### Look at this!
**This text is BOLD!**
## Hello world!
```cpp
#include <iostream>
int main() {
std::cout << "Hello world!" << std::endl;
}
```
This is super cool!
## Time to link some random website!
This [should be clickable](https://example.com)
## Image test
![this should show an image](./image.png)
## This is an ordered list
1. one
2. six
3. seven
## This is an unordered one
- six
- nine
- six
- nine

View File

@@ -1,15 +1,95 @@
#include "config.hpp"
#include "markdown.hpp"
#include "utils.hpp"
#include "config/config.hpp"
#include "format/format.hpp"
#include "io/io.hpp"
#include "markdown/markdown.hpp"
#include <catch2/catch_all.hpp>
#include <catch2/catch_test_macros.hpp>
#include <ctime>
#include <optional>
#include <string>
TEST_CASE("Checking config file read", "[config]") {
const std::string FILE_NAME = "../examples/config.toml";
namespace TestFiles {
constexpr const char *config_toml = R"([general]
lang = "en"
title = "Blog"
Config config = get_config(FILE_NAME);
[contact]
author = "author"
email = "email@example.com"
signal = "signal url"
)";
constexpr const char *formatter_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>
{{replace_me}}
</body>
</html>)";
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.)";
constexpr const char *post_md = R"(---
[metadata]
author = "joseph"
date = 2026-01-01
title = "Example of a blog post"
tags = ["blog", "test", "lalilulelo"]
---
# This is an example post
In this example we will do a couple of cool markdown things.
## This is a h2 header
*This text is written in italic!*
### Look at this!
**This text is BOLD!**
## Hello world!
```cpp
#include <iostream>
int main() {
std::cout << "Hello world!" << std::endl;
}
```
This is super cool!
## Time to link some random website!
This [should be clickable](https://example.com)
## Image test
![this should show an image](./image.png)
## This is an ordered list
1. one
2. six
3. seven
## This is an unordered one
- six
- nine
- six
- nine)";
} // namespace TestFiles
// IO tests
TEST_CASE("Test reading a file successful") {
std::string result = read_file("./test_files/config.toml");
REQUIRE(result == TestFiles::config_toml);
}
TEST_CASE("Test reading a file that doesn't exist") {
REQUIRE_THROWS(read_file("this/path/doesnt/exist"));
}
TEST_CASE("Checking config file read", "[config]") {
Config config = get_config(TestFiles::config_toml);
REQUIRE(config.contact.author == "author");
REQUIRE(config.contact.email == "email@example.com");
@@ -18,32 +98,65 @@ TEST_CASE("Checking config file read", "[config]") {
REQUIRE(config.general.title == "Blog");
}
TEST_CASE("convert_string_to_set parses inline YAML string list") {
const std::string input = "[\"haha\", \"dupa\", \"xdd\"]";
const std::set<std::string> output = {"haha", "dupa", "xdd"};
REQUIRE(convert_string_to_set(input) == output);
}
TEST_CASE("get_metadata function data correctness test") {
const std::string FILE_NAME = "../examples/post.md";
Metadata result = get_metadata(FILE_NAME).value();
Metadata result = get_metadata(TestFiles::post_md).value();
std::string expected_author = "joseph";
std::string expected_date = "2026-01-01";
struct tm datetime = {0};
datetime.tm_year = 2026 - 1900;
datetime.tm_mon = 1 - 1;
datetime.tm_mday = 1;
datetime.tm_isdst = 0;
time_t expected_date = mktime(&datetime);
std::string expected_title = "Example of a blog post";
std::set<std::string> expected_tags = {"blog", "test", "lalilulelo"};
std::vector<std::string> expected_tags = {"blog", "test", "lalilulelo"};
REQUIRE(result.author == expected_author);
REQUIRE(result.date == expected_date);
REQUIRE(result.title == expected_title);
for (const auto &tag : expected_tags) {
REQUIRE(result.tags.count(tag));
}
REQUIRE(result.tags == expected_tags);
}
TEST_CASE("get_metadata function test with a file that doesn't have metadata") {
const std::string FILE_NAME = "../examples/no_metadata_post.md";
std::optional<Metadata> result = get_metadata(FILE_NAME);
std::optional<Metadata> result = get_metadata(TestFiles::no_metadata_post_md);
REQUIRE(result == std::nullopt);
}
TEST_CASE("get_html function parsing test") {
const std::string EXPECTED_OUTPUT =
R"HTML(<h1>This is an example post</h1>
<p>In this example we will do a couple of cool markdown things.</p>
<h2>This is a h2 header</h2>
<p><em>This text is written in italic!</em></p>
<h3>Look at this!</h3>
<p><strong>This text is BOLD!</strong></p>
<h2>Hello world!</h2>
<pre><code class="language-cpp">#include &lt;iostream&gt;
int main() {
std::cout &lt;&lt; &quot;Hello world!&quot; &lt;&lt; std::endl;
}
</code></pre>
<p>This is super cool!</p>
<h2>Time to link some random website!</h2>
<p>This <a href="https://example.com">should be clickable</a></p>
<h2>Image test</h2>
<p><img src="./image.png" alt="this should show an image"></p>
<h2>This is an ordered list</h2>
<ol>
<li>one</li>
<li>six</li>
<li>seven</li>
</ol>
<h2>This is an unordered one</h2>
<ul>
<li>six</li>
<li>nine</li>
<li>six</li>
<li>nine</li>
</ul>
)HTML";
REQUIRE(get_html(TestFiles::post_md) == EXPECTED_OUTPUT);
}

View File

@@ -1,39 +0,0 @@
#include <set>
#include <sstream>
#include <string>
void trim(std::string &str) {
str.erase(str.find_last_not_of(' ') + 1);
str.erase(0, str.find_first_not_of(' '));
str.erase(str.find_last_not_of('"') + 1);
str.erase(0, str.find_first_not_of('"'));
}
std::set<std::string> convert_string_to_set(std::string input) {
std::set<std::string> result;
std::string buffer;
size_t left, right;
left = input.find_first_of('[');
right = input.find_last_of(']');
if (left == std::string::npos || right == std::string::npos ||
right <= left) {
return result;
}
input = input.substr(left + 1, right - left - 1);
std::stringstream ss(input);
while (getline(ss, buffer, ',')) {
buffer.erase(0, buffer.find_first_not_of(" \t"));
buffer.erase(buffer.find_last_not_of(" \t") + 1);
if (buffer.size() >= 2 && buffer.front() == '"' && buffer.back() == '"') {
buffer = buffer.substr(1, buffer.size() - 2);
result.insert(buffer);
}
}
return result;
}

View File

@@ -1,21 +0,0 @@
#include <set>
#include <string>
// trim string in-place, removing all whitespaces and surrounding '"' symbols
void trim(std::string &str);
// Convert a string containing an inline YAML/JSON-style list of strings
// into a std::set<std::string>.
//
// Supported format (single line):
// ["haha", "dupa", "xdd"]
//
// Limitations:
// - no nested structures
// - no escaping inside strings
// - elements must be quoted
//
// Example:
// Input: ["haha", "dupa", "xdd"]
// Output: {"dupa", "haha", "xdd"} // sorted, unique
std::set<std::string> convert_string_to_set(std::string input);

6
templates/head.html Normal file
View File

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

3
templates/header.html Normal file
View File

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

View File

@@ -1,23 +1,12 @@
<!DOCTYPE html>
<html lang="{{lang}}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{title}}</title>
<link rel="stylesheet" href="/style/style.css">
</head>
{{head}}
<body>
<header>
<h1>{{title}}</h1>
</header>
<nav id="nav">
</nav>
{{header}}
<main>
<ol id="posts">
{{posts}}
</ol>
</main>
</body>