Merge pull request #7 from szymon-jozef/rewrite/io

rewrite/io
This commit is contained in:
Szymon
2026-01-11 23:02:58 +01:00
committed by GitHub
10 changed files with 163 additions and 68 deletions

View File

@@ -17,7 +17,9 @@ jobs:
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
- name: make all
run: make all
- name: make test
run: make test
- name: make clean
run: make clean

1
.gitignore vendored
View File

@@ -3,4 +3,5 @@ posts/
src/build/
/src/blog
/src/tests
/src/.cache/
*.o

View File

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

View File

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

View File

@@ -1,6 +1,4 @@
#include "markdown/markdown.hpp"
#include <fstream>
#include <sstream>
#include <string>
static bool replace_once(std::string &str, const std::string &from,
@@ -11,20 +9,3 @@ static bool replace_once(std::string &str, const std::string &from,
str.replace(start_pos, from.length(), to);
return true;
}
std::string inject_html_into_template(const std::string &FILE_NAME,
const std::string &TEMPLATE,
const std::string &HTML) {
std::string result;
std::ifstream file(FILE_NAME);
if (!file.is_open()) {
return "";
}
std::ostringstream ss;
ss << file.rdbuf();
result = ss.str();
if (replace_once(result, TEMPLATE, HTML)) {
return result;
}
return "";
}

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

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

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

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

View File

@@ -8,14 +8,16 @@ BUILD_DIR = build
SOURCES = main.cpp \
config/config.cpp \
format/format.cpp \
markdown/markdown.cpp
markdown/markdown.cpp \
io/io.cpp
OBJECTS = $(SOURCES:%.cpp=$(BUILD_DIR)/%.o)
TEST_SOURCES = tests.cpp \
config/config.cpp \
format/format.cpp \
markdown/markdown.cpp
markdown/markdown.cpp \
io/io.cpp
TEST_OBJECTS = $(TEST_SOURCES:%.cpp=$(BUILD_DIR)/%.o)

View File

@@ -1,6 +1,5 @@
#include "markdown.hpp"
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <md4c-html.h>
@@ -8,21 +7,21 @@
#include <sstream>
#include <toml++/toml.hpp>
std::optional<Metadata> get_metadata(const std::string &FILE_PATH) {
std::fstream file(FILE_PATH);
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, buffer) || buffer != "---") {
std::cerr << "File " << FILE_PATH << " does not contain metadata. "
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, buffer)) {
while (getline(file_contents, buffer)) {
if (buffer == "---") {
break;
}
@@ -69,15 +68,15 @@ static void html_callback(const MD_CHAR *data, MD_SIZE size, void *userdata) {
out->append(data, size);
}
std::string get_html(const std::string &FILE_PATH) {
std::fstream file(FILE_PATH);
std::string get_html(const std::string &FILE_CONTENTS) {
std::stringstream file_contents(FILE_CONTENTS);
std::string buffer, file_string, out;
getline(file, buffer);
getline(file_contents, buffer);
bool is_metadata = buffer == "---";
/* skip metadata */
if (is_metadata) {
while (getline(file, buffer)) {
while (getline(file_contents, buffer)) {
if (buffer == "---") {
break;
}
@@ -86,7 +85,7 @@ std::string get_html(const std::string &FILE_PATH) {
file_string += buffer + '\n';
}
while (getline(file, buffer)) {
while (getline(file_contents, buffer)) {
file_string += buffer + '\n';
}

View File

@@ -1,5 +1,6 @@
#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>
@@ -7,10 +8,88 @@
#include <optional>
#include <string>
TEST_CASE("Checking config file read", "[config]") {
const std::string FILE_NAME = "test_files/config.toml";
namespace TestFiles {
constexpr const char *config_toml = R"([general]
lang = "en"
title = "Blog"
Config config = get_config(FILE_NAME);
[contact]
author = "author"
email = "email@example.com"
signal = "signal url"
)";
constexpr const char *formatter_html = R"(<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{{replace_me}}
</body>
</html>)";
constexpr const char *no_metadata_post_md = R"(# This markdown has no metadata.
That's bad. Don't do it. It's for tests only.)";
constexpr const char *post_md = R"(---
[metadata]
author = "joseph"
date = 2026-01-01
title = "Example of a blog post"
tags = ["blog", "test", "lalilulelo"]
---
# This is an example post
In this example we will do a couple of cool markdown things.
## This is a h2 header
*This text is written in italic!*
### Look at this!
**This text is BOLD!**
## Hello world!
```cpp
#include <iostream>
int main() {
std::cout << "Hello world!" << std::endl;
}
```
This is super cool!
## Time to link some random website!
This [should be clickable](https://example.com)
## Image test
![this should show an image](./image.png)
## This is an ordered list
1. one
2. six
3. seven
## This is an unordered one
- six
- nine
- six
- nine)";
} // namespace TestFiles
// IO tests
TEST_CASE("Test reading a file successful") {
std::string result = read_file("./test_files/config.toml");
REQUIRE(result == TestFiles::config_toml);
}
TEST_CASE("Test reading a file that doesn't exist") {
REQUIRE_THROWS(read_file("this/path/doesnt/exist"));
}
TEST_CASE("Checking config file read", "[config]") {
Config config = get_config(TestFiles::config_toml);
REQUIRE(config.contact.author == "author");
REQUIRE(config.contact.email == "email@example.com");
@@ -20,8 +99,7 @@ TEST_CASE("Checking config file read", "[config]") {
}
TEST_CASE("get_metadata function data correctness test") {
const std::string FILE_NAME = "./test_files/post.md";
Metadata result = get_metadata(FILE_NAME).value();
Metadata result = get_metadata(TestFiles::post_md).value();
std::string expected_author = "joseph";
struct tm datetime = {0};
@@ -41,14 +119,13 @@ TEST_CASE("get_metadata function data correctness test") {
}
TEST_CASE("get_metadata function test with a file that doesn't have metadata") {
const std::string FILE_NAME = ".test_files/no_metadata_post.md";
std::optional<Metadata> result = get_metadata(FILE_NAME);
std::optional<Metadata> result = get_metadata(TestFiles::no_metadata_post_md);
REQUIRE(result == std::nullopt);
}
TEST_CASE("get_html function parsing test") {
const std::string INPUT = "./test_files/post.md";
const std::string EXPECTED_OUTPUT = R"HTML(<h1>This is an example post</h1>
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>
@@ -81,24 +158,5 @@ int main() {
</ul>
)HTML";
REQUIRE(get_html(INPUT) == EXPECTED_OUTPUT);
}
TEST_CASE("formatter injection test") {
const std::string FILE_NAME = "./test_files/formatter.html";
const std::string EXPECTED_OUTPUT = 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 replaced!
</body>
</html>
)";
const std::string output =
inject_html_into_template(FILE_NAME, "{{replace_me}}", "I'm replaced!");
REQUIRE(EXPECTED_OUTPUT == output);
REQUIRE(get_html(TestFiles::post_md) == EXPECTED_OUTPUT);
}