finalises first pass of work on BlogPageBuilder.
Project can now build the webpages required! additionally adds some example blog posts from my old website
This commit is contained in:
parent
d7a080b47a
commit
d0298d9493
133 changed files with 2355 additions and 3808 deletions
|
@ -1,46 +1,79 @@
|
|||
#include "BlogPageBuilder.h"
|
||||
|
||||
|
||||
BlogPageBuilder::BlogPageBuilder(std::string templateFileUrl, std::string articleIdentifier)
|
||||
BlogPageBuilder::BlogPageBuilder(std::string templateFileUrl, std::string inFolder,
|
||||
std::string outFolder, std::string articleIdentifier)
|
||||
{
|
||||
pageTemplate = readFile(templateFileUrl);
|
||||
articleIdentifier = articleIdentifier;
|
||||
this->articleIdentifier = articleIdentifier;
|
||||
navIdentifier = "<nav>";
|
||||
|
||||
parser = new BlogParser();
|
||||
|
||||
collectPages();
|
||||
navSection = generateNavSection();
|
||||
}
|
||||
|
||||
std::string BlogPageBuilder::createPage(std::string text)
|
||||
BlogPageBuilder::~BlogPageBuilder()
|
||||
{
|
||||
std::stringstream buffer;
|
||||
|
||||
int articleIdentifierLocation = calculateArticleIdentifierLocation();
|
||||
buffer << pageTemplate.substr(0, articleIdentifierLocation) << "\n\n";
|
||||
|
||||
BlogParser* parser = new BlogParser();
|
||||
std::string mdFileContents = readFile(mdFileUrl);
|
||||
buffer << parser->parse(mdFileContents);
|
||||
|
||||
buffer << pageTemplate.substr(articleIdentifierLocation, pageTemplate.length());
|
||||
|
||||
return buffer.str();
|
||||
for(Page* page : pages)
|
||||
delete page;
|
||||
}
|
||||
|
||||
int BlogPageBuilder::calculateArticleIdentifierLocation(std::string text)
|
||||
void BlogPageBuilder::createPage(Page* page)
|
||||
{
|
||||
int location = pageTemplate.find(articleIdentifier) + articleIdentifier.length();
|
||||
if (location == -1)
|
||||
std::string buffer = pageTemplate;
|
||||
|
||||
int navLocation = calculateIdentifierLocation(navIdentifier, buffer);
|
||||
buffer.insert(navLocation, navSection);
|
||||
|
||||
int articleLocation = calculateIdentifierLocation(articleIdentifier, buffer);
|
||||
std::string articleText = parser->parse(page->getSourceFileContents());
|
||||
buffer.insert(articleLocation, articleText);
|
||||
|
||||
page->setOutFileContents(buffer);
|
||||
}
|
||||
|
||||
void BlogPageBuilder::buildAllPages()
|
||||
{
|
||||
for(Page* page : pages)
|
||||
{
|
||||
createPage(page);
|
||||
page->writePageToFile();
|
||||
}
|
||||
}
|
||||
|
||||
int BlogPageBuilder::calculateIdentifierLocation(std::string id, std::string text)
|
||||
{
|
||||
int location = text.find(id) + id.length();
|
||||
if (location == std::string::npos)
|
||||
printf("Warning: no article found in given template file");
|
||||
return location;
|
||||
}
|
||||
|
||||
void BlogPageBuilder::collectPages()
|
||||
{
|
||||
auto directoryIterator = std::filesystem::directory_iterator(SOURCE_FILE_FOLDER);
|
||||
for(auto file : directoryIterator)
|
||||
{
|
||||
pages.push_back(new Page(file.path(), OUTPUT_FILE_FOLDER));
|
||||
}
|
||||
}
|
||||
|
||||
std::string BlogPageBuilder::generateNavSection()
|
||||
{
|
||||
std::stringstream buffer;
|
||||
for (Page page : pages)
|
||||
buffer << "\n<h1>Navigation</h1>\n\n";
|
||||
for (Page* page : pages)
|
||||
{
|
||||
if
|
||||
if (page->getPageFlags().hidden)
|
||||
continue;
|
||||
buffer
|
||||
<< "<li><a href=" << page->getOutFileUrl() << ">"
|
||||
<< page->getPageTitle()
|
||||
<< "</a></li>\n";
|
||||
}
|
||||
return "";
|
||||
return buffer.str();
|
||||
}
|
||||
|
||||
std::string BlogPageBuilder::readFile(std::string fileUrl)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue