Added basic support for front matter
This commit is contained in:
parent
95d8d76fa7
commit
ef828e5f2b
36
main.cpp
36
main.cpp
@ -13,24 +13,43 @@ char* read(const char* name) {
|
||||
file_stream.seekg(0, file_stream.end);
|
||||
int length = file_stream.tellg();
|
||||
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.close();
|
||||
return data;
|
||||
}
|
||||
|
||||
string32 readfile(const char* name) {
|
||||
char* data = read(name);
|
||||
string32 readfile(std::string name) {
|
||||
char* data = read(name.c_str());
|
||||
string32 file = string32 (data);
|
||||
free (data);
|
||||
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() {
|
||||
std::string path = "source";
|
||||
|
||||
string32 layout = readfile("source/_layout.html");
|
||||
fs::remove_all("output");
|
||||
|
||||
for (const auto & file : fs::recursive_directory_iterator(path)) {
|
||||
@ -40,10 +59,13 @@ int main() {
|
||||
std::string ext = file.path().extension().string();
|
||||
fs::create_directories("output" + file.path().parent_path().string().substr(6));
|
||||
std::ofstream outfile("output" + file.path().string().substr(6));
|
||||
if (ext == ".html")
|
||||
outfile << layout.replace("{{ content }}", readfile(file.path().c_str()));
|
||||
else
|
||||
if (ext == ".html") {
|
||||
frontMatter fm = frontMatter(readfile(file.path().c_str()));
|
||||
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.close();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user