Cleaned up code slightly. Fixed correctly copying over binary files. Started preparations for initial Markdown support

This commit is contained in:
Xnoe 2020-07-23 03:09:02 +01:00 committed by Elsie
parent 21520d7a9d
commit 849f3c6c62

View File

@ -10,12 +10,12 @@ namespace fs = std::filesystem;
char* read(const char* name) { char* read(const char* name) {
std::ifstream file_stream(name, std::ios_base::binary); std::ifstream file_stream(name, std::ios_base::binary);
int count = 0;
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, 1); char* data = (char*) calloc(length+5, 1);
file_stream.read(data, length); file_stream.read(data+4, length);
memcpy(data, &length, 4);
file_stream.close(); file_stream.close();
return data; return data;
@ -23,7 +23,7 @@ char* read(const char* name) {
string32 readfile(std::string name) { string32 readfile(std::string name) {
char* data = read(name.c_str()); char* data = read(name.c_str());
string32 file = string32 (data); string32 file = string32 (data+4);
free (data); free (data);
return file; return file;
} }
@ -66,9 +66,10 @@ public:
} }
}; };
string32 parsePageHTML(string32 pageContents) { string32 parsePage(string32 pageContents) {
frontMatter fm = frontMatter(pageContents); frontMatter fm = frontMatter(pageContents);
string32 page; string32 page;
if (fm.hasLayout) if (fm.hasLayout)
page = readfile("source/_layouts/" + std::string(fm.layout) + ".html").replace("{{ content }}", fm.parsed); page = readfile("source/_layouts/" + std::string(fm.layout) + ".html").replace("{{ content }}", fm.parsed);
else else
@ -83,18 +84,21 @@ string32 parsePageHTML(string32 pageContents) {
while (i < page.len()) { while (i < page.len()) {
int havematched = 0; int havematched = 0;
for (;i<page.len();i++) { for (;i<page.len();i++) {
if (havematched == tofind)
break;
if (page.cs[i] == searchString[havematched]) if (page.cs[i] == searchString[havematched])
havematched++; havematched++;
else else
havematched = 0; havematched = 0;
if (havematched == tofind)
break;
} }
if (!havematched) if (!havematched)
break; break;
havematched--; havematched--;
int si (i-3); int si (i-3);
int ei (0); int ei (0);
for (;i<page.len();i++) { for (;i<page.len();i++) {
if (page[i] == searchString[havematched]) if (page[i] == searchString[havematched])
ei = i, havematched--; ei = i, havematched--;
@ -105,8 +109,9 @@ string32 parsePageHTML(string32 pageContents) {
break; break;
} }
} }
std::vector<string32> split = string32(page, si+4, ei+1).split(" "); std::vector<string32> split = string32(page, si+3, ei+1).split(" ");
page.cs.erase(page.cs.begin()+si, page.cs.begin()+ei+4); page.cs.erase(page.cs.begin()+si, page.cs.begin()+ei+4);
if (split[0] == "include") { if (split[0] == "include") {
string32 toInclude = readfile("source/_includes/" + std::string(split[1].asChar())); string32 toInclude = readfile("source/_includes/" + std::string(split[1].asChar()));
page.cs.insert(page.cs.begin()+si, toInclude.cs.begin(), toInclude.cs.end()); page.cs.insert(page.cs.begin()+si, toInclude.cs.begin(), toInclude.cs.end());
@ -115,6 +120,10 @@ string32 parsePageHTML(string32 pageContents) {
return page; return page;
} }
string32 parsePageHTML(string32 htmlContents) {
return parsePage(htmlContents);
}
int main() { int main() {
std::string path = "source"; std::string path = "source";
@ -130,7 +139,10 @@ int main() {
if (ext == ".html") { if (ext == ".html") {
outfile << parsePageHTML(readfile(file.path().c_str())); outfile << parsePageHTML(readfile(file.path().c_str()));
} else { } else {
outfile << read(file.path().c_str()); char* data = read(file.path().c_str());
int length = *(int*)data;
outfile.write(data+4, length);
delete data;
} }
outfile.close(); outfile.close();
} }