mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 12:10:42 +02:00
Compare commits
27 Commits
html-style
...
a59773ef3e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a59773ef3e | ||
| 210f3ec04b | |||
|
|
ca7e6c8be6 | ||
| 94fbc3f30d | |||
| 55af7474d0 | |||
| 931a1908c1 | |||
| 0bae066c1f | |||
|
|
a486a04c2f | ||
|
|
944a07ae32 | ||
|
|
cc344019dd | ||
|
|
173d1954b3 | ||
|
|
a1c8c67021 | ||
| 3339e6d6a2 | |||
| df9874b3e2 | |||
| 0e0bc02441 | |||
| 671bfc0a35 | |||
| f3a13fbfd6 | |||
|
|
3ad3f873eb | ||
| 8679a627de | |||
| 38226a350f | |||
| 5fa92851eb | |||
| cf272d9156 | |||
| 546979c413 | |||
| ba708e3f32 | |||
|
|
ad01b5e7ac | ||
| 81cc7979b7 | |||
| 6d4cb18809 |
23
.github/workflows/c-cpp.yml
vendored
Normal file
23
.github/workflows/c-cpp.yml
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
name: C/C++ CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "master" ]
|
||||
pull_request:
|
||||
branches: [ "master" ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
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 -C src
|
||||
- name: make check
|
||||
run: make check -C src
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
out/
|
||||
posts/
|
||||
src/tests/tests
|
||||
*.o
|
||||
|
||||
32
README.md
32
README.md
@@ -1,3 +1,5 @@
|
||||
[](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?
|
||||
|
||||
13
src/makefile
13
src/makefile
@@ -2,6 +2,7 @@ CXX = g++
|
||||
CXXFLAGS = -std=c++17 -Wall
|
||||
TEST_LIBS = -lmd4c-html -lCatch2Main -lCatch2
|
||||
LIBS = -lmd4c-html
|
||||
TEST_DIR = tests
|
||||
|
||||
all: blog
|
||||
|
||||
@@ -20,14 +21,14 @@ utils.o: utils.cpp utils.hpp
|
||||
markdown.o: markdown.cpp markdown.hpp
|
||||
$(CXX) $(CXXFLAGS) -c markdown.cpp
|
||||
|
||||
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)
|
||||
tests.o: $(TEST_DIR)/tests.cpp config.hpp
|
||||
$(CXX) $(CXXFLAGS) -c $(TEST_DIR)/tests.cpp
|
||||
|
||||
tests.o: tests.cpp config.hpp
|
||||
$(CXX) $(CXXFLAGS) -c tests.cpp
|
||||
tests: tests.o config.o utils.o markdown.o
|
||||
g++ -std=c++17 -Wall tests.o config.o utils.o markdown.o -o $(TEST_DIR)/tests $(TEST_LIBS)
|
||||
|
||||
check: tests
|
||||
./tests
|
||||
$(TEST_DIR)/tests
|
||||
|
||||
clean:
|
||||
rm -f *.o blog tests
|
||||
rm -f *.o $(TEST_DIR)/*.o blog $(TEST_DIR)/tests
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
#include "markdown.hpp"
|
||||
#include "utils.hpp"
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#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_PATH) {
|
||||
std::fstream file(FILE_PATH);
|
||||
std::stringstream metadata_s;
|
||||
std::string buffer;
|
||||
|
||||
/* if there's no ---, then no metadata in file there is */
|
||||
if (!getline(file, buffer) || buffer != "---") {
|
||||
std::cerr << "File " << FILE_PATH << "does not contain metadata."
|
||||
std::cerr << "File " << FILE_PATH << " does not contain metadata. "
|
||||
<< std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
@@ -22,22 +27,70 @@ std::optional<Metadata> get_metadata(const std::string &FILE_PATH) {
|
||||
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;
|
||||
metadata_s << buffer << '\n';
|
||||
}
|
||||
|
||||
return std::optional(Metadata{metadata_map["author"], metadata_map["date"],
|
||||
metadata_map["title"],
|
||||
convert_string_to_set(metadata_map["tags"])});
|
||||
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_PATH) {
|
||||
std::fstream file(FILE_PATH);
|
||||
std::string buffer, file_string, out;
|
||||
getline(file, buffer);
|
||||
bool is_metadata = buffer == "---";
|
||||
|
||||
/* skip metadata */
|
||||
if (is_metadata) {
|
||||
while (getline(file, buffer)) {
|
||||
if (buffer == "---") {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
file_string += buffer + '\n';
|
||||
}
|
||||
|
||||
while (getline(file, buffer)) {
|
||||
file_string += buffer + '\n';
|
||||
}
|
||||
|
||||
md_html(file_string.c_str(), file_string.size(), html_callback, &out, 0, 0);
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
#include "config.hpp"
|
||||
#include "markdown.hpp"
|
||||
#include "utils.hpp"
|
||||
#include <catch2/catch_all.hpp>
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("Checking config file read", "[config]") {
|
||||
const std::string FILE_NAME = "../examples/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("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();
|
||||
|
||||
std::string expected_author = "joseph";
|
||||
std::string expected_date = "2026-01-01";
|
||||
std::string expected_title = "Example of a blog post";
|
||||
std::set<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));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
REQUIRE(result == std::nullopt);
|
||||
}
|
||||
8
src/tests/config.toml
Normal file
8
src/tests/config.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[general]
|
||||
lang = "en"
|
||||
title = "Blog"
|
||||
|
||||
[contact]
|
||||
author = "author"
|
||||
email = "email@example.com"
|
||||
signal = "signal url"
|
||||
BIN
src/tests/image.png
Normal file
BIN
src/tests/image.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 290 KiB |
42
src/tests/post.md
Normal file
42
src/tests/post.md
Normal 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 is an ordered list
|
||||
1. one
|
||||
2. six
|
||||
3. seven
|
||||
|
||||
## This is an unordered one
|
||||
- six
|
||||
- nine
|
||||
- six
|
||||
- nine
|
||||
85
src/tests/tests.cpp
Normal file
85
src/tests/tests.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#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);
|
||||
}
|
||||
@@ -8,32 +8,3 @@ void trim(std::string &str) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,21 +1,4 @@
|
||||
#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
6
templates/head.html
Normal 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
3
templates/header.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<header>
|
||||
<h1>{{title}}</h1>
|
||||
</header>
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user