Updated Templ. Added many new features. See updated README.md for a few

This commit is contained in:
Xnoe 2020-07-13 22:02:58 +01:00 committed by Elsie
parent ef828e5f2b
commit 9191c45c5a

View File

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <filesystem> #include <filesystem>
#include <unordered_map>
#include "utf8.h" #include "utf8.h"
@ -27,42 +28,112 @@ string32 readfile(std::string name) {
return file; return file;
} }
struct frontMatter { class frontMatter {
void set(string32 key, string32 val) {
values[std::string(key.asChar())] = val;
}
public:
char* layout; char* layout;
bool hasLayout = false;
string32 parsed; string32 parsed;
std::unordered_map<std::string, string32> values;
frontMatter(string32 unparsedPage) { frontMatter(string32 unparsedPage) {
std::vector<string32> lines = unparsedPage.split("\n"); std::vector<string32> lines = unparsedPage.split("\n");
if (lines.front() == "---") { if (lines.front() == "---") {
int line = 1; int line = 1;
for (;lines[line]!="---";line++) { for (;lines[line]!="---";line++) {
std::vector dataToParse = lines[line].split(": "); std::vector dataToParse = lines[line].split(": ");
if (dataToParse[0] == "layout") if (dataToParse[0] == "layout") {
layout = dataToParse[1].asChar(); layout = dataToParse[1].asChar();
hasLayout = true;
} else
set("page." + dataToParse[0], dataToParse[1]);
} }
line++; line++;
for (;line<lines.size();line++) { for (;line<lines.size();line++) {
parsed += lines[line] + "\n"; parsed += lines[line] + "\n";
} }
} else {
parsed = unparsedPage;
} }
std::vector<string32> config = readfile("source/_config.yml").split("\n");
for (string32 line : config) {
std::vector<string32> split = line.split(": ");
set("site." + split[0], split[1]);
}
}
string32 operator[](string32 s) {
return values[std::string(s.asChar())];
} }
}; };
string32 parsePageHTML(string32 pageContents) {
frontMatter fm = frontMatter(pageContents);
string32 page;
if (fm.hasLayout)
page = readfile("source/_layouts/" + std::string(fm.layout) + ".html").replace("{{ content }}", fm.parsed);
else
page = fm.parsed;
for (auto pair : fm.values)
page.replaceSelfAll("{{ " + pair.first + " }}", pair.second);
int i (0);
string32 searchString = "{% ";
string32 endString = " %}";
while (i < page.cs.size()) {
int havematched (0);
for (;i<page.cs.size();i++) {
if (page.cs[i] == searchString[havematched])
havematched++;
else
havematched = 0;
if (havematched == searchString.cs.size())
break;
}
if (!havematched)
break;
int si (i-3);
int ei (0);
for (;i<page.cs.size();i++) {
if (page.cs[i] == endString[havematched]) {
havematched++;
ei = i;
} else
havematched = 0;
if (havematched == endString.cs.size()) {
ei -= endString.cs.size();
break;
}
}
string32 testString;
for (int j(si+4);j<=ei;j++)
testString += page[j];
page.cs.erase(page.cs.begin()+si, page.cs.begin()+ei+4);
std::vector<string32> split = testString.split(" ");
if (split[0] == "include") {
string32 toInclude = readfile("source/_includes/" + std::string(split[1].asChar()));
page.cs.insert(page.cs.begin()+si, toInclude.cs.begin(), toInclude.cs.end());
}
i = 0;
si = 0;
ei = 0;
}
return page;
}
int main() { int main() {
std::string path = "source"; std::string path = "source";
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)) {
if (file.is_regular_file()) { if (file.path().stem().string()[0] == '_' || file.path().string()[7] == '_')
if (file.path().stem().string().at(0) == '_')
continue; continue;
if (file.is_regular_file()) {
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") {
frontMatter fm = frontMatter(readfile(file.path().c_str())); outfile << parsePageHTML(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 { } else {
outfile << read(file.path().c_str()); outfile << read(file.path().c_str());
} }