mirror of
https://github.com/szymon-jozef/static-site-generator.git
synced 2026-09-07 12:10:42 +02:00
added get_html() function
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "markdown.hpp"
|
||||
#include "utils.hpp"
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
@@ -41,3 +42,33 @@ std::optional<Metadata> get_metadata(const std::string &FILE_PATH) {
|
||||
metadata_map["title"],
|
||||
convert_string_to_set(metadata_map["tags"])});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user