mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 20:20:41 +02:00
Compare commits
42 Commits
a59773ef3e
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| c4a916b0fb | |||
| 527b78d319 | |||
| e933b9d45b | |||
| f3776cac72 | |||
|
|
116ac006b7 | ||
| 241121b355 | |||
| 78c43c630a | |||
| 29c2406c04 | |||
|
|
4c904c0659 | ||
| a89e4bb60f | |||
| e542115cb8 | |||
| 3b676a8c49 | |||
|
|
0b41ff834d | ||
| 19d86e8dc7 | |||
| 1a58577b3e | |||
| 8796199757 | |||
|
|
84148f92df | ||
|
|
2013826acb | ||
|
|
bdcf7cf5da | ||
| 858bd19f10 | |||
| eecadf4fab | |||
| 8556937f58 | |||
| bc9228cdc7 | |||
| 74b46f4923 | |||
| 763761ea82 | |||
| 1668cb8563 | |||
| 5fa62d64ee | |||
| b171f11982 | |||
|
|
d2efd637cd | ||
| 90adcd88e7 | |||
| ddc590571a | |||
| 35cb6e3d82 | |||
| a2d76d4c4f | |||
| ef8fbfbb53 | |||
| e9b88530c2 | |||
| 322df82288 | |||
| fe02a16416 | |||
|
|
dd2c6ca810 | ||
| 2c8b9f2cfc | |||
| cf3b502509 | |||
| 1754c9fbab | |||
| 763a941042 |
13
.github/workflows/c-cpp.yml
vendored
13
.github/workflows/c-cpp.yml
vendored
@@ -10,7 +10,10 @@ jobs:
|
|||||||
build:
|
build:
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: ./src
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: install dependencies
|
- name: install dependencies
|
||||||
@@ -18,6 +21,8 @@ jobs:
|
|||||||
sudo apt update
|
sudo apt update
|
||||||
sudo apt install -y g++ catch2 libmd4c-dev libmd4c-html0-dev libtomlplusplus-dev
|
sudo apt install -y g++ catch2 libmd4c-dev libmd4c-html0-dev libtomlplusplus-dev
|
||||||
- name: make
|
- name: make
|
||||||
run: make -C src
|
run: make
|
||||||
- name: make check
|
- name: make test
|
||||||
run: make check -C src
|
run: make test
|
||||||
|
- name: make clean
|
||||||
|
run: make clean
|
||||||
|
|||||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,4 +1,7 @@
|
|||||||
out/
|
out/
|
||||||
posts/
|
posts/
|
||||||
src/tests/tests
|
src/build/
|
||||||
|
/src/blog
|
||||||
|
/src/tests
|
||||||
|
/src/.cache/
|
||||||
*.o
|
*.o
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|||||||
27
flake.lock
generated
Normal file
27
flake.lock
generated
Normal 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
26
flake.nix
Normal 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
58
src/builder/builder.cpp
Normal 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
31
src/builder/builder.hpp
Normal 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);
|
||||||
74
src/compile_commands.json
Normal file
74
src/compile_commands.json
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
@@ -5,10 +5,10 @@
|
|||||||
#include <toml++/impl/parser.hpp>
|
#include <toml++/impl/parser.hpp>
|
||||||
#include <toml++/toml.hpp>
|
#include <toml++/toml.hpp>
|
||||||
|
|
||||||
Config get_config(const std::string &CONFIG_FILE) {
|
Config get_config(const std::string &CONFIGURATION) {
|
||||||
toml::table toml_table;
|
toml::table toml_table;
|
||||||
try {
|
try {
|
||||||
toml_table = toml::parse_file(CONFIG_FILE);
|
toml_table = toml::parse(CONFIGURATION);
|
||||||
} catch (const toml::parse_error &err) {
|
} catch (const toml::parse_error &err) {
|
||||||
std::cerr << "Parsing failed:" << std::endl << err << std::endl;
|
std::cerr << "Parsing failed:" << std::endl << err << std::endl;
|
||||||
}
|
}
|
||||||
@@ -17,4 +17,7 @@ struct Config {
|
|||||||
General general;
|
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);
|
||||||
71
src/format/format.cpp
Normal file
71
src/format/format.cpp
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
#include "format/format.hpp"
|
||||||
|
#include "markdown/markdown.hpp"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string format_file(const std::string &TEMPLATE_FILE,
|
||||||
|
const std::string &TEMPLATE, const std::string &INPUT) {
|
||||||
|
std::string result = TEMPLATE_FILE;
|
||||||
|
size_t current_pos = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
size_t found_pos = result.find(TEMPLATE, current_pos);
|
||||||
|
|
||||||
|
if (found_pos == std::string::npos) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.replace(found_pos, TEMPLATE.length(), INPUT);
|
||||||
|
current_pos = found_pos + INPUT.length();
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
21
src/format/format.hpp
Normal file
21
src/format/format.hpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
// @brief Format file.
|
||||||
|
// @param TEMPLATE_FILE File that you want to format in string.
|
||||||
|
// @param TEMPLATE Template that you want to replace. For example `{{title}}`
|
||||||
|
// @param INPUT Text that you want to replace TEMPLATE with.
|
||||||
|
// @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_conditionally(const std::string &TEMPLATE_FILE,
|
||||||
|
const std::string &START_TAG,
|
||||||
|
const std::string &END_TAG,
|
||||||
|
const std::string &TEMPLATE,
|
||||||
|
const std::string &INPUT);
|
||||||
36
src/io/io.cpp
Normal file
36
src/io/io.cpp
Normal 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
13
src/io/io.hpp
Normal 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);
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "config.hpp"
|
#include "config/config.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|||||||
65
src/makefile
65
src/makefile
@@ -1,34 +1,57 @@
|
|||||||
CXX = g++
|
CXX = g++
|
||||||
CXXFLAGS = -std=c++17 -Wall
|
CXXFLAGS = -std=c++17 -Wall -Wextra -O2 -I .
|
||||||
TEST_LIBS = -lmd4c-html -lCatch2Main -lCatch2
|
|
||||||
LIBS = -lmd4c-html
|
LIBS = -lmd4c-html
|
||||||
TEST_DIR = tests
|
TEST_LIBS = -lmd4c-html -lCatch2Main -lCatch2
|
||||||
|
|
||||||
all: blog
|
BUILD_DIR = build
|
||||||
|
|
||||||
blog: main.o config.o utils.o
|
SOURCES = main.cpp \
|
||||||
$(CXX) $(CXXFLAGS) main.o config.o -o blog $(LIBS)
|
config/config.cpp \
|
||||||
|
format/format.cpp \
|
||||||
|
markdown/markdown.cpp \
|
||||||
|
io/io.cpp \
|
||||||
|
builder/builder.cpp
|
||||||
|
|
||||||
main.o: main.cpp config.hpp
|
OBJECTS = $(SOURCES:%.cpp=$(BUILD_DIR)/%.o)
|
||||||
$(CXX) $(CXXFLAGS) -c main.cpp
|
|
||||||
|
|
||||||
config.o: config.cpp config.hpp
|
TEST_SOURCES = tests.cpp \
|
||||||
$(CXX) $(CXXFLAGS) -c config.cpp
|
config/config.cpp \
|
||||||
|
format/format.cpp \
|
||||||
|
markdown/markdown.cpp \
|
||||||
|
io/io.cpp \
|
||||||
|
builder/builder.cpp
|
||||||
|
|
||||||
utils.o: utils.cpp utils.hpp
|
TEST_OBJECTS = $(TEST_SOURCES:%.cpp=$(BUILD_DIR)/%.o)
|
||||||
$(CXX) $(CXXFLAGS) -c utils.cpp
|
|
||||||
|
|
||||||
markdown.o: markdown.cpp markdown.hpp
|
TARGET = blog
|
||||||
$(CXX) $(CXXFLAGS) -c markdown.cpp
|
TEST_TARGET = tests
|
||||||
|
|
||||||
tests.o: $(TEST_DIR)/tests.cpp config.hpp
|
.PHONY: all clean test directories
|
||||||
$(CXX) $(CXXFLAGS) -c $(TEST_DIR)/tests.cpp
|
|
||||||
|
|
||||||
tests: tests.o config.o utils.o markdown.o
|
all: directories $(TARGET)
|
||||||
g++ -std=c++17 -Wall tests.o config.o utils.o markdown.o -o $(TEST_DIR)/tests $(TEST_LIBS)
|
|
||||||
|
|
||||||
check: tests
|
directories:
|
||||||
$(TEST_DIR)/tests
|
@mkdir -p $(BUILD_DIR)/config
|
||||||
|
@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)
|
||||||
|
|
||||||
|
$(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:
|
clean:
|
||||||
rm -f *.o $(TEST_DIR)/*.o blog $(TEST_DIR)/tests
|
rm -rf $(BUILD_DIR)
|
||||||
|
rm -f $(TARGET) $(TEST_TARGET)
|
||||||
|
|
||||||
|
rebuild: clean all
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
#include "markdown.hpp"
|
#include "markdown.hpp"
|
||||||
#include "utils.hpp"
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <md4c-html.h>
|
#include <md4c-html.h>
|
||||||
@@ -9,21 +7,21 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <toml++/toml.hpp>
|
#include <toml++/toml.hpp>
|
||||||
|
|
||||||
std::optional<Metadata> get_metadata(const std::string &FILE_PATH) {
|
std::optional<Metadata> get_metadata(const std::string &FILE_CONTENTS) {
|
||||||
std::fstream file(FILE_PATH);
|
std::stringstream file_contents(FILE_CONTENTS);
|
||||||
std::stringstream metadata_s;
|
std::stringstream metadata_s;
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
|
|
||||||
/* if there's no ---, then no metadata in file there is */
|
/* if there's no ---, then no metadata in file there is */
|
||||||
if (!getline(file, buffer) || buffer != "---") {
|
if (!getline(file_contents, buffer) || buffer != "---") {
|
||||||
std::cerr << "File " << FILE_PATH << " does not contain metadata. "
|
std::clog << "File does not contain any metadata. Is this intentional?"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, std::string> metadata_map;
|
std::map<std::string, std::string> metadata_map;
|
||||||
|
|
||||||
while (getline(file, buffer)) {
|
while (getline(file_contents, buffer)) {
|
||||||
if (buffer == "---") {
|
if (buffer == "---") {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -70,15 +68,15 @@ static void html_callback(const MD_CHAR *data, MD_SIZE size, void *userdata) {
|
|||||||
out->append(data, size);
|
out->append(data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string get_html(const std::string &FILE_PATH) {
|
std::string get_html(const std::string &FILE_CONTENTS) {
|
||||||
std::fstream file(FILE_PATH);
|
std::stringstream file_contents(FILE_CONTENTS);
|
||||||
std::string buffer, file_string, out;
|
std::string buffer, file_string, out;
|
||||||
getline(file, buffer);
|
getline(file_contents, buffer);
|
||||||
bool is_metadata = buffer == "---";
|
bool is_metadata = buffer == "---";
|
||||||
|
|
||||||
/* skip metadata */
|
/* skip metadata */
|
||||||
if (is_metadata) {
|
if (is_metadata) {
|
||||||
while (getline(file, buffer)) {
|
while (getline(file_contents, buffer)) {
|
||||||
if (buffer == "---") {
|
if (buffer == "---") {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -87,7 +85,7 @@ std::string get_html(const std::string &FILE_PATH) {
|
|||||||
file_string += buffer + '\n';
|
file_string += buffer + '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
while (getline(file, buffer)) {
|
while (getline(file_contents, buffer)) {
|
||||||
file_string += buffer + '\n';
|
file_string += buffer + '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
11
src/test_files/formatter.html
Normal file
11
src/test_files/formatter.html
Normal 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>
|
||||||
|
Before Width: | Height: | Size: 290 KiB After Width: | Height: | Size: 290 KiB |
341
src/tests.cpp
Normal file
341
src/tests.cpp
Normal file
@@ -0,0 +1,341 @@
|
|||||||
|
#include "builder/builder.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>
|
||||||
|
|
||||||
|
namespace TestFiles {
|
||||||
|
constexpr const char *config_toml = R"([general]
|
||||||
|
lang = "en"
|
||||||
|
title = "Blog"
|
||||||
|
|
||||||
|
[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 *formated_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>
|
||||||
|
I'm formatted!
|
||||||
|
</body>
|
||||||
|
</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.
|
||||||
|
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 is an ordered list
|
||||||
|
1. one
|
||||||
|
2. six
|
||||||
|
3. seven
|
||||||
|
|
||||||
|
## This is an unordered one
|
||||||
|
- six
|
||||||
|
- 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
|
||||||
|
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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Config tests
|
||||||
|
|
||||||
|
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");
|
||||||
|
REQUIRE(config.contact.signal == "signal url");
|
||||||
|
REQUIRE(config.general.lang == "en");
|
||||||
|
REQUIRE(config.general.title == "Blog");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Markdown tests
|
||||||
|
|
||||||
|
TEST_CASE("get_metadata function data correctness test") {
|
||||||
|
Metadata result = get_metadata(TestFiles::post_md).value();
|
||||||
|
|
||||||
|
std::string expected_author = "joseph";
|
||||||
|
struct tm datetime = {};
|
||||||
|
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::vector<std::string> expected_tags = {"blog", "test", "lalilulelo"};
|
||||||
|
|
||||||
|
REQUIRE(result.author == expected_author);
|
||||||
|
REQUIRE(result.date == expected_date);
|
||||||
|
REQUIRE(result.title == expected_title);
|
||||||
|
REQUIRE(result.tags == expected_tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("get_metadata function test with a file that doesn't have metadata") {
|
||||||
|
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 <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::cout << "Hello world!" << 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format tests
|
||||||
|
TEST_CASE("Test format_file function with correct data") {
|
||||||
|
REQUIRE(TestFiles::formated_html == format_file(TestFiles::formatter_html,
|
||||||
|
"{{replace_me}}",
|
||||||
|
"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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
#include "../config.hpp"
|
|
||||||
#include "../markdown.hpp"
|
|
||||||
#include "../utils.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 = "tests/config.toml";
|
|
||||||
|
|
||||||
Config config = get_config(FILE_NAME);
|
|
||||||
|
|
||||||
REQUIRE(config.contact.author == "author");
|
|
||||||
REQUIRE(config.contact.email == "email@example.com");
|
|
||||||
REQUIRE(config.contact.signal == "signal url");
|
|
||||||
REQUIRE(config.general.lang == "en");
|
|
||||||
REQUIRE(config.general.title == "Blog");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("get_metadata function data correctness test") {
|
|
||||||
const std::string FILE_NAME = "./tests/post.md";
|
|
||||||
Metadata result = get_metadata(FILE_NAME).value();
|
|
||||||
|
|
||||||
std::string expected_author = "joseph";
|
|
||||||
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::vector<std::string> expected_tags = {"blog", "test", "lalilulelo"};
|
|
||||||
|
|
||||||
REQUIRE(result.author == expected_author);
|
|
||||||
REQUIRE(result.date == expected_date);
|
|
||||||
REQUIRE(result.title == expected_title);
|
|
||||||
REQUIRE(result.tags == expected_tags);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("get_metadata function test with a file that doesn't have metadata") {
|
|
||||||
const std::string FILE_NAME = ".tests/no_metadata_post.md";
|
|
||||||
std::optional<Metadata> result = get_metadata(FILE_NAME);
|
|
||||||
REQUIRE(result == std::nullopt);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("get_html function parsing test") {
|
|
||||||
const std::string INPUT = "./tests/post.md";
|
|
||||||
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 <iostream>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
std::cout << "Hello world!" << 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(INPUT) == EXPECTED_OUTPUT);
|
|
||||||
}
|
|
||||||
@@ -1,10 +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('"'));
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
#include <string>
|
|
||||||
|
|
||||||
// trim string in-place, removing all whitespaces and surrounding '"' symbols
|
|
||||||
void trim(std::string &str);
|
|
||||||
21
templates/blog_entry.html
Normal file
21
templates/blog_entry.html
Normal 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>
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<footer>
|
<footer>
|
||||||
<p>Strona napisana przez: {{author}}</p>
|
<p>Blog owned by: {{config.author}}</p>
|
||||||
<ul>
|
<ul>
|
||||||
{{if email}}
|
{{if email}}
|
||||||
<li><a href="mailto:{{email}}">{{email}}</a></li>
|
<li><a href="mailto:{{email}}">{{email}}</a></li>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<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">
|
<link rel="stylesheet" href="/style/style.css">
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
<header>
|
<header>
|
||||||
<h1>{{title}}</h1>
|
<h1>{{metadata.title}}</h1>
|
||||||
</header>
|
</header>
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="{{lang}}">
|
<html lang="{{config.lang}}">
|
||||||
{{head}}
|
<head>
|
||||||
|
{{page.head}}
|
||||||
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{{header}}
|
<header>
|
||||||
|
{{page.header}}
|
||||||
|
</header>
|
||||||
<main>
|
<main>
|
||||||
<ol id="posts">
|
{{page.main}}
|
||||||
{{posts}}
|
|
||||||
</ol>
|
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<footer>
|
||||||
|
{{page.footer}}
|
||||||
|
</footer>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user