moved all the file operation logic into io module

This commit is contained in:
2026-01-11 22:33:58 +01:00
parent 5fa62d64ee
commit 1668cb8563
10 changed files with 16 additions and 33 deletions

Binary file not shown.

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 "";
}

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';
}