mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 12:10:42 +02:00
Merge branch 'master' of github.com:szymon-jozef/static-site-generator
This commit is contained in:
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
|
||||||
27
README.md
27
README.md
@@ -12,7 +12,7 @@ As this is still a WIP, here is a short list of what works and what still needs
|
|||||||
- [ ] generating html documents
|
- [ ] generating html documents
|
||||||
- [ ] some nicer css styles
|
- [ ] some nicer css styles
|
||||||
- [ ] more sensible templates
|
- [ ] more sensible templates
|
||||||
- [ ] change yaml style metadata to toml?
|
- [x] change yaml style metadata to toml
|
||||||
|
|
||||||
# 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.
|
||||||
@@ -33,10 +33,11 @@ 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
|
||||||
---
|
---
|
||||||
author: "joseph"
|
[metadata]
|
||||||
date: "2026-01-01"
|
author = "joseph"
|
||||||
title: "Example of a blog post"
|
date = 2026-01-01
|
||||||
tags: ["blog", "test", "lalilulelo"]
|
title = "Example of a blog post"
|
||||||
|
tags = ["blog", "test", "lalilulelo"]
|
||||||
---
|
---
|
||||||
# This is an example post
|
# This is an example post
|
||||||
|
|
||||||
@@ -116,20 +117,22 @@ 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 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
|
## Fields
|
||||||
- author – the author of particular blog entry | string
|
- 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
|
- title – title of blog entry | string
|
||||||
- tags – tags for blog entry. | json like list
|
- tags – tags for blog entry. | array
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
```
|
```
|
||||||
author: "joseph"
|
[metadata]
|
||||||
date: "2026-01-01"
|
author = "joseph"
|
||||||
title: "Example of a blog post"
|
date = 2026-01-01
|
||||||
tags: ["blog", "test", "lalilulelo"]
|
title = "Example of a blog post"
|
||||||
|
tags = ["blog", "test", "lalilulelo"]
|
||||||
```
|
```
|
||||||
|
|
||||||
# Why did you do this?
|
# Why did you do this?
|
||||||
|
|||||||
@@ -1,18 +1,22 @@
|
|||||||
#include "markdown.hpp"
|
#include "markdown.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
#include <cmath>
|
#include <ctime>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <md4c-html.h>
|
#include <md4c-html.h>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <sstream>
|
||||||
|
#include <toml++/toml.hpp>
|
||||||
|
|
||||||
std::optional<Metadata> get_metadata(const std::string &FILE_PATH) {
|
std::optional<Metadata> get_metadata(const std::string &FILE_PATH) {
|
||||||
std::fstream file(FILE_PATH);
|
std::fstream file(FILE_PATH);
|
||||||
|
std::stringstream metadata_s;
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
|
|
||||||
|
/* if there's no ---, then no metadata in file there is */
|
||||||
if (!getline(file, buffer) || buffer != "---") {
|
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;
|
<< std::endl;
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
@@ -23,24 +27,42 @@ std::optional<Metadata> get_metadata(const std::string &FILE_PATH) {
|
|||||||
if (buffer == "---") {
|
if (buffer == "---") {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
size_t colon_pos = buffer.find(":");
|
metadata_s << buffer << '\n';
|
||||||
|
|
||||||
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"],
|
toml::table toml_table = toml::parse(metadata_s);
|
||||||
metadata_map["title"],
|
std::vector<std::string> tags;
|
||||||
convert_string_to_set(metadata_map["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) {
|
static void html_callback(const MD_CHAR *data, MD_SIZE size, void *userdata) {
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
|
#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;
|
||||||
std::string date;
|
time_t date;
|
||||||
std::string title;
|
std::string title;
|
||||||
std::set<std::string> tags;
|
std::vector<std::string> tags;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Reads the metadata of the markdown file
|
// Reads the metadata of the markdown file
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
---
|
---
|
||||||
author: "joseph"
|
[metadata]
|
||||||
date: "2026-01-01"
|
author = "joseph"
|
||||||
title: "Example of a blog post"
|
date = 2026-01-01
|
||||||
tags: ["blog", "test", "lalilulelo"]
|
title = "Example of a blog post"
|
||||||
|
tags = ["blog", "test", "lalilulelo"]
|
||||||
---
|
---
|
||||||
# This is an example post
|
# This is an example post
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "../utils.hpp"
|
#include "../utils.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>
|
||||||
|
|
||||||
@@ -25,21 +26,24 @@ TEST_CASE("convert_string_to_set parses inline YAML string list") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("get_metadata function data correctness test") {
|
TEST_CASE("get_metadata function data correctness test") {
|
||||||
const std::string FILE_NAME = "./tests//post.md";
|
const std::string FILE_NAME = "./tests/post.md";
|
||||||
Metadata result = get_metadata(FILE_NAME).value();
|
Metadata result = get_metadata(FILE_NAME).value();
|
||||||
|
|
||||||
std::string expected_author = "joseph";
|
std::string expected_author = "joseph";
|
||||||
std::string expected_date = "2026-01-01";
|
struct tm datetime = {0};
|
||||||
|
datetime.tm_year = 2026 - 1900;
|
||||||
|
datetime.tm_mon = 1 - 1;
|
||||||
|
datetime.tm_mday = 1;
|
||||||
|
datetime.tm_isdst = 0;
|
||||||
|
|
||||||
|
time_t expected_date = mktime(&datetime);
|
||||||
std::string expected_title = "Example of a blog post";
|
std::string expected_title = "Example of a blog post";
|
||||||
std::set<std::string> expected_tags = {"blog", "test", "lalilulelo"};
|
std::vector<std::string> expected_tags = {"blog", "test", "lalilulelo"};
|
||||||
|
|
||||||
REQUIRE(result.author == expected_author);
|
REQUIRE(result.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") {
|
||||||
|
|||||||
@@ -8,32 +8,3 @@ void trim(std::string &str) {
|
|||||||
str.erase(str.find_last_not_of('"') + 1);
|
str.erase(str.find_last_not_of('"') + 1);
|
||||||
str.erase(0, str.find_first_not_of('"'));
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user