Added basic support for front matter

This commit is contained in:
Xnoe 2020-07-12 21:35:51 +01:00 committed by Elsie
parent 95d8d76fa7
commit ef828e5f2b

View File

@ -13,24 +13,43 @@ char* read(const char* name) {
file_stream.seekg(0, file_stream.end); file_stream.seekg(0, file_stream.end);
int length = file_stream.tellg(); int length = file_stream.tellg();
file_stream.seekg(0, file_stream.beg); file_stream.seekg(0, file_stream.beg);
char* data = (char*) calloc(length, 1); char* data = (char*) calloc(length+1, 1);
file_stream.read(data, length); file_stream.read(data, length);
file_stream.close(); file_stream.close();
return data; return data;
} }
string32 readfile(const char* name) { string32 readfile(std::string name) {
char* data = read(name); char* data = read(name.c_str());
string32 file = string32 (data); string32 file = string32 (data);
free (data); free (data);
return file; return file;
} }
struct frontMatter {
char* layout;
string32 parsed;
frontMatter(string32 unparsedPage) {
std::vector<string32> lines = unparsedPage.split("\n");
if (lines.front() == "---") {
int line = 1;
for (;lines[line]!="---";line++) {
std::vector dataToParse = lines[line].split(": ");
if (dataToParse[0] == "layout")
layout = dataToParse[1].asChar();
}
line++;
for (;line<lines.size();line++) {
parsed += lines[line] + "\n";
}
}
}
};
int main() { int main() {
std::string path = "source"; std::string path = "source";
string32 layout = readfile("source/_layout.html");
fs::remove_all("output"); fs::remove_all("output");
for (const auto & file : fs::recursive_directory_iterator(path)) { for (const auto & file : fs::recursive_directory_iterator(path)) {
@ -40,10 +59,13 @@ int main() {
std::string ext = file.path().extension().string(); std::string ext = file.path().extension().string();
fs::create_directories("output" + file.path().parent_path().string().substr(6)); fs::create_directories("output" + file.path().parent_path().string().substr(6));
std::ofstream outfile("output" + file.path().string().substr(6)); std::ofstream outfile("output" + file.path().string().substr(6));
if (ext == ".html") if (ext == ".html") {
outfile << layout.replace("{{ content }}", readfile(file.path().c_str())); frontMatter fm = frontMatter(readfile(file.path().c_str()));
else std::cout << "Layout: `" << fm.layout << "`\nParsed: " << fm.parsed << "\n";
outfile << readfile("source/_layouts/" + std::string(fm.layout) + ".html").replace("{{ content }}", fm.parsed);
} else {
outfile << read(file.path().c_str()); outfile << read(file.path().c_str());
}
outfile.close(); outfile.close();
} }
} }