mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 12:10:42 +02:00
Compare commits
1 Commits
master
...
html-style
| Author | SHA1 | Date | |
|---|---|---|---|
| c23c377ea9 |
28
.github/workflows/c-cpp.yml
vendored
28
.github/workflows/c-cpp.yml
vendored
@@ -1,28 +0,0 @@
|
|||||||
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
4
.gitignore
vendored
@@ -1,7 +1,3 @@
|
|||||||
out/
|
out/
|
||||||
posts/
|
posts/
|
||||||
src/build/
|
|
||||||
/src/blog
|
|
||||||
/src/tests
|
|
||||||
/src/.cache/
|
|
||||||
*.o
|
*.o
|
||||||
|
|||||||
36
README.md
36
README.md
@@ -1,5 +1,3 @@
|
|||||||
[](https://github.com/szymon-jozef/static-site-generator/actions/workflows/c-cpp.yml)
|
|
||||||
|
|
||||||
# IMPORTANT
|
# IMPORTANT
|
||||||
This is still work in progress. It doesn't work yet.
|
This is still work in progress. It doesn't work yet.
|
||||||
I pushed it onto github just to have easy access from other computers.
|
I pushed it onto github just to have easy access from other computers.
|
||||||
@@ -10,13 +8,10 @@ As this is still a WIP, here is a short list of what works and what still needs
|
|||||||
- [x] config parsing
|
- [x] config parsing
|
||||||
- [x] metadata parsing
|
- [x] metadata parsing
|
||||||
- [x] basic tests
|
- [x] basic tests
|
||||||
- [x] parsing markdown
|
- [ ] parsing markdown
|
||||||
- [x] generating html documents
|
- [ ] generating html documents
|
||||||
- [ ] some nicer css styles
|
- [ ] some nicer css styles
|
||||||
- [ ] more sensible templates
|
- [ ] more sensible templates
|
||||||
- [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.
|
||||||
@@ -37,11 +32,10 @@ Every blog post needs to be in a different file in `/posts` directory.
|
|||||||
An example from `/examples/post.md`
|
An example from `/examples/post.md`
|
||||||
```md
|
```md
|
||||||
---
|
---
|
||||||
[metadata]
|
author: "joseph"
|
||||||
author = "joseph"
|
date: "2026-01-01"
|
||||||
date = 2026-01-01
|
title: "Example of a blog post"
|
||||||
title = "Example of a blog post"
|
tags: ["blog", "test", "lalilulelo"]
|
||||||
tags = ["blog", "test", "lalilulelo"]
|
|
||||||
---
|
---
|
||||||
# This is an example post
|
# This is an example post
|
||||||
|
|
||||||
@@ -86,7 +80,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?
|
# How does the directory structure work?
|
||||||
Also pretty simple. You have:
|
Also pretty simple. You have:
|
||||||
- `/config` for config files. Initially empty so you need to copy it from example dir or make one yourself.
|
- `/config` for config files. Initially empty so you need to copy it from example dir or make one yourself.
|
||||||
- `/examples` example of configuration files. You can copy it to `/config` and edit it there.
|
- `/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.
|
||||||
- `/out` for processed site.
|
- `/out` for processed site.
|
||||||
- `/posts` here you write your blog posts. Every file should be in markdown format, metadata included!.
|
- `/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.
|
- `/src` for cpp files. Here's where you run the binary to translate your markdown posts into html.
|
||||||
@@ -121,22 +115,20 @@ signal = "signal url"
|
|||||||
|
|
||||||
# Metadata explanation
|
# Metadata explanation
|
||||||
Metadata is everything at the beginning of the markdown file between `---`.
|
Metadata is everything at the beginning of the markdown file between `---`.
|
||||||
It's also written in toml. Why toml? I already use it's parser so it's easy to implement.
|
It's written in a format similar to yaml.
|
||||||
Besides it's readable and easy to write.
|
|
||||||
|
|
||||||
## Fields
|
## Fields
|
||||||
- author – the author of particular blog entry | string
|
- author – the author of particular blog entry | string
|
||||||
- date – date of writing an entry | date
|
- date – date of writing an entry | string YYYY-MM-DD
|
||||||
- title – title of blog entry | string
|
- title – title of blog entry | string
|
||||||
- tags – tags for blog entry. | array
|
- tags – tags for blog entry. | json like list
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
```
|
```
|
||||||
[metadata]
|
author: "joseph"
|
||||||
author = "joseph"
|
date: "2026-01-01"
|
||||||
date = 2026-01-01
|
title: "Example of a blog post"
|
||||||
title = "Example of a blog post"
|
tags: ["blog", "test", "lalilulelo"]
|
||||||
tags = ["blog", "test", "lalilulelo"]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# Why did you do this?
|
# Why did you do this?
|
||||||
|
|||||||
27
flake.lock
generated
27
flake.lock
generated
@@ -1,27 +0,0 @@
|
|||||||
{
|
|
||||||
"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
26
flake.nix
@@ -1,26 +0,0 @@
|
|||||||
{
|
|
||||||
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
|
|
||||||
];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
#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);
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"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 &CONFIGURATION) {
|
Config get_config(const std::string &CONFIG_FILE) {
|
||||||
toml::table toml_table;
|
toml::table toml_table;
|
||||||
try {
|
try {
|
||||||
toml_table = toml::parse(CONFIGURATION);
|
toml_table = toml::parse_file(CONFIG_FILE);
|
||||||
} 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,7 +17,4 @@ struct Config {
|
|||||||
General general;
|
General general;
|
||||||
};
|
};
|
||||||
|
|
||||||
// @brief Load config into `Config` struct
|
Config get_config(const std::string &CONFIG_FILE);
|
||||||
// @param CONFIGURATION content of `.toml` file containing configuration
|
|
||||||
// @return Config with parsed configuration
|
|
||||||
Config get_config(const std::string &CONFIGURATION);
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
#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);
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
#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();
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
#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/config.hpp"
|
#include "config.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|||||||
64
src/makefile
64
src/makefile
@@ -1,57 +1,33 @@
|
|||||||
CXX = g++
|
CXX = g++
|
||||||
CXXFLAGS = -std=c++17 -Wall -Wextra -O2 -I .
|
CXXFLAGS = -std=c++17 -Wall
|
||||||
LIBS = -lmd4c-html
|
|
||||||
TEST_LIBS = -lmd4c-html -lCatch2Main -lCatch2
|
TEST_LIBS = -lmd4c-html -lCatch2Main -lCatch2
|
||||||
|
LIBS = -lmd4c-html
|
||||||
|
|
||||||
BUILD_DIR = build
|
all: blog
|
||||||
|
|
||||||
SOURCES = main.cpp \
|
blog: main.o config.o utils.o
|
||||||
config/config.cpp \
|
$(CXX) $(CXXFLAGS) main.o config.o -o blog $(LIBS)
|
||||||
format/format.cpp \
|
|
||||||
markdown/markdown.cpp \
|
|
||||||
io/io.cpp \
|
|
||||||
builder/builder.cpp
|
|
||||||
|
|
||||||
OBJECTS = $(SOURCES:%.cpp=$(BUILD_DIR)/%.o)
|
main.o: main.cpp config.hpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c main.cpp
|
||||||
|
|
||||||
TEST_SOURCES = tests.cpp \
|
config.o: config.cpp config.hpp
|
||||||
config/config.cpp \
|
$(CXX) $(CXXFLAGS) -c config.cpp
|
||||||
format/format.cpp \
|
|
||||||
markdown/markdown.cpp \
|
|
||||||
io/io.cpp \
|
|
||||||
builder/builder.cpp
|
|
||||||
|
|
||||||
TEST_OBJECTS = $(TEST_SOURCES:%.cpp=$(BUILD_DIR)/%.o)
|
utils.o: utils.cpp utils.hpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c utils.cpp
|
||||||
|
|
||||||
TARGET = blog
|
markdown.o: markdown.cpp markdown.hpp
|
||||||
TEST_TARGET = tests
|
$(CXX) $(CXXFLAGS) -c markdown.cpp
|
||||||
|
|
||||||
.PHONY: all clean test directories
|
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)
|
||||||
|
|
||||||
all: directories $(TARGET)
|
tests.o: tests.cpp config.hpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c tests.cpp
|
||||||
|
|
||||||
directories:
|
check: tests
|
||||||
@mkdir -p $(BUILD_DIR)/config
|
./tests
|
||||||
@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 -rf $(BUILD_DIR)
|
rm -f *.o blog tests
|
||||||
rm -f $(TARGET) $(TEST_TARGET)
|
|
||||||
|
|
||||||
rebuild: clean all
|
|
||||||
|
|||||||
43
src/markdown.cpp
Normal file
43
src/markdown.cpp
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#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"])});
|
||||||
|
}
|
||||||
@@ -1,15 +1,12 @@
|
|||||||
#pragma once
|
|
||||||
#include <ctime>
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <toml++/toml.hpp>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
struct Metadata {
|
struct Metadata {
|
||||||
std::string author;
|
std::string author;
|
||||||
time_t date;
|
std::string date;
|
||||||
std::string title;
|
std::string title;
|
||||||
std::vector<std::string> tags;
|
std::set<std::string> tags;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Reads the metadata of the markdown file
|
// Reads the metadata of the markdown file
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
[general]
|
|
||||||
lang = "en"
|
|
||||||
title = "Blog"
|
|
||||||
|
|
||||||
[contact]
|
|
||||||
author = "author"
|
|
||||||
email = "email@example.com"
|
|
||||||
signal = "signal url"
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
<!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>
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 290 KiB |
@@ -1,42 +0,0 @@
|
|||||||
---
|
|
||||||
[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
|
|
||||||
334
src/tests.cpp
334
src/tests.cpp
@@ -1,179 +1,15 @@
|
|||||||
#include "builder/builder.hpp"
|
#include "config.hpp"
|
||||||
#include "config/config.hpp"
|
#include "markdown.hpp"
|
||||||
#include "format/format.hpp"
|
#include "utils.hpp"
|
||||||
#include "io/io.hpp"
|
|
||||||
#include "markdown/markdown.hpp"
|
|
||||||
#include <catch2/catch_all.hpp>
|
#include <catch2/catch_all.hpp>
|
||||||
#include <catch2/catch_test_macros.hpp>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
#include <ctime>
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#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]") {
|
TEST_CASE("Checking config file read", "[config]") {
|
||||||
Config config = get_config(TestFiles::config_toml);
|
const std::string FILE_NAME = "../examples/config.toml";
|
||||||
|
|
||||||
|
Config config = get_config(FILE_NAME);
|
||||||
|
|
||||||
REQUIRE(config.contact.author == "author");
|
REQUIRE(config.contact.author == "author");
|
||||||
REQUIRE(config.contact.email == "email@example.com");
|
REQUIRE(config.contact.email == "email@example.com");
|
||||||
@@ -182,160 +18,32 @@ TEST_CASE("Checking config file read", "[config]") {
|
|||||||
REQUIRE(config.general.title == "Blog");
|
REQUIRE(config.general.title == "Blog");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Markdown tests
|
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") {
|
TEST_CASE("get_metadata function data correctness test") {
|
||||||
Metadata result = get_metadata(TestFiles::post_md).value();
|
const std::string FILE_NAME = "../examples/post.md";
|
||||||
|
Metadata result = get_metadata(FILE_NAME).value();
|
||||||
|
|
||||||
std::string expected_author = "joseph";
|
std::string expected_author = "joseph";
|
||||||
struct tm datetime = {};
|
std::string expected_date = "2026-01-01";
|
||||||
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::string expected_title = "Example of a blog post";
|
||||||
std::vector<std::string> expected_tags = {"blog", "test", "lalilulelo"};
|
std::set<std::string> expected_tags = {"blog", "test", "lalilulelo"};
|
||||||
|
|
||||||
REQUIRE(result.author == expected_author);
|
REQUIRE(result.author == expected_author);
|
||||||
REQUIRE(result.date == expected_date);
|
REQUIRE(result.date == expected_date);
|
||||||
REQUIRE(result.title == expected_title);
|
REQUIRE(result.title == expected_title);
|
||||||
REQUIRE(result.tags == expected_tags);
|
|
||||||
|
for (const auto &tag : expected_tags) {
|
||||||
|
REQUIRE(result.tags.count(tag));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("get_metadata function test with a file that doesn't have metadata") {
|
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);
|
const std::string FILE_NAME = "../examples/no_metadata_post.md";
|
||||||
|
std::optional<Metadata> result = get_metadata(FILE_NAME);
|
||||||
REQUIRE(result == std::nullopt);
|
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);
|
|
||||||
}
|
|
||||||
|
|||||||
39
src/utils.cpp
Normal file
39
src/utils.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
21
src/utils.hpp
Normal file
21
src/utils.hpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#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);
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
<!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>Blog owned by: {{config.author}}</p>
|
<p>Strona napisana przez: {{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 +0,0 @@
|
|||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>{{metadata.title}}</title>
|
|
||||||
<link rel="stylesheet" href="/style/style.css">
|
|
||||||
</head>
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
<header>
|
|
||||||
<h1>{{metadata.title}}</h1>
|
|
||||||
</header>
|
|
||||||
@@ -1,18 +1,24 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="{{config.lang}}">
|
<html lang="{{lang}}">
|
||||||
<head>
|
<head>
|
||||||
{{page.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>
|
<body>
|
||||||
<header>
|
<header>
|
||||||
{{page.header}}
|
<h1>{{title}}</h1>
|
||||||
</header>
|
</header>
|
||||||
<main>
|
|
||||||
{{page.main}}
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<footer>
|
<nav id="nav">
|
||||||
{{page.footer}}
|
|
||||||
</footer>
|
</nav>
|
||||||
|
|
||||||
|
<main>
|
||||||
|
<ol id="posts">
|
||||||
|
{{posts}}
|
||||||
|
</ol>
|
||||||
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user