Adds beginnings of BlogPageBuilder and associated classes

This commit is contained in:
Rosia E Evans 2023-09-20 09:09:51 +01:00
parent 65aefe46ce
commit 1506f661c9
114 changed files with 4740 additions and 518 deletions

Binary file not shown.

53
Src/BlogPageBuilder.cpp Normal file
View file

@ -0,0 +1,53 @@
#include "BlogPageBuilder.h"
BlogPageBuilder::BlogPageBuilder(std::string templateFileUrl, std::string articleIdentifier)
{
pageTemplate = readFile(templateFileUrl);
articleIdentifier = articleIdentifier;
}
std::string BlogPageBuilder::createPage(std::string text)
{
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();
}
int BlogPageBuilder::calculateArticleIdentifierLocation(std::string text)
{
int location = pageTemplate.find(articleIdentifier) + articleIdentifier.length();
if (location == -1)
printf("Warning: no article found in given template file");
return location;
}
std::string BlogPageBuilder::generateNavSection()
{
std::stringstream buffer;
for (Page page : pages)
{
if
}
return "";
}
std::string BlogPageBuilder::readFile(std::string fileUrl)
{
std::ifstream file(fileUrl);
std::stringstream fileCache;
fileCache << file.rdbuf();
return fileCache.str();
}

35
Src/BlogPageBuilder.h Normal file
View file

@ -0,0 +1,35 @@
#pragma once
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "BlogParser.h"
#include "Page.h"
/*
* Class that builds blog pages, takes a template page to plut the blog-post in
* along with an optional artifleIdentifier. The template is searched for this
* and the blog is placed after it.
*/
class BlogPageBuilder
{
private:
std::string pageTemplate;
std::string articleIdentifier;
std::vector<Page> pages;
std::string navSection;
std::string readFile(std::string fileUrl);
int calculateArticleIdentifierLocation(std::string text);
public:
std::string createPage(std::string mdFileUrl);
std::string generateNavSection();
BlogPageBuilder(std::string templateFileUrl, std::string articleIdentifier = "<article>");
};

View file

@ -3,34 +3,28 @@
void BlogParser::saveTextToFile(const MD_CHAR* text, MD_SIZE size, void* userDataVoid)
void BlogParser::writeTextToString(const MD_CHAR* text, MD_SIZE size, void* userDataVoid)
{
std::string fileUrl = ((UserData*)userDataVoid)->outputFileName;
std::string* output = ((UserData*)userDataVoid)->output;
std::ofstream file;
file.open(fileUrl, std::ios_base::app);
file.write(text, size);
file.close();
output->append(text, size);
text = 0;
#ifdef DEBUG
printf("Parsed code produced:\n")
printf("%.*s", size, text);
#endif
}
int BlogParser::parse(std::string inputFileName, std::string outputFileName)
std::string BlogParser::parse(std::string text)
{
std::ifstream file(inputFileName);
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
std::string text = buffer.str();
int length = text.length();
UserData* userData = new UserData();
userData->outputFileName = outputFileName;
userData->output = new std::string();
void* userDataVoid = userData;
return md_html(text.c_str(), length, saveTextToFile, userDataVoid, 0, 1);
md_html(text.c_str(), text.length(), writeTextToString, userDataVoid, 0, 1);
return *(userData->output);
}

View file

@ -14,11 +14,11 @@
class BlogParser
{
private:
static void saveTextToFile(const MD_CHAR* text, MD_SIZE size, void* unneeded);
static void writeTextToString(const MD_CHAR* text, MD_SIZE size, void* unneeded);
struct UserData{
std::string outputFileName;
std::string* output;
};
public:
int parse(std::string inputFileName, std::string outputFileName);
std::string parse(std::string text);
};

View file

@ -1,8 +1,18 @@
add_executable(blogParser blog.cpp BlogParser.h BlogParser.cpp)
target_include_directories(blogParser PRIVATE Src)
add_executable(blog blog.cpp)
target_include_directories(blog PRIVATE Src)
add_subdirectory(Libs)
target_link_libraries(blogParser PUBLIC md4c)
target_link_libraries(blogParser PUBLIC md4c-html)
add_library(BlogParser BlogParser.cpp BlogParser.h)
target_link_libraries(BlogParser PRIVATE md4c)
target_link_libraries(BlogParser PRIVATE md4c-html)
add_library(BlogPageBuilder BlogPageBuilder.cpp BlogPageBuilder.h)
target_link_libraries(BlogPageBuilder PRIVATE BlogParser)
target_link_libraries(blog PUBLIC md4c)
target_link_libraries(blog PUBLIC md4c-html)
target_link_libraries(blog PRIVATE BlogParser)
target_link_libraries(blog PRIVATE BlogPageBuilder)
# This probable needs a severe clean up

65
Src/Page.cpp Normal file
View file

@ -0,0 +1,65 @@
#include "Page.h"
// reads flags from fileContents, consumes and flag data found
void Page::calculatePageFlags()
{
int endOfFirstLine = fileContents.find_first_of("\n");
std::string tagLine = fileContents.substr(0, endOfFirstLine);
fileContents = fileContents.substr(endOfFirstLine, fileContents.length()-endOfFirstLine);
FlagReader* flagReader = new FlagReader();
flags = flagReader->readFlags(tagLine);
delete flagReader;
}
std::string readFile(std::string fileUrl)
{
std::ifstream file(fileUrl);
std::stringstream fileCache;
fileCache << file.rdbuf();
return fileCache.str();
}
Page::Page(std::string fileUrl)
{
this->fileUrl = fileUrl;
fileContents = readFile(this->fileUrl);
calculatePageFlags();
}
std::string Page::getPageContents()
{
return fileContents;
}
FileFlags Page::getPageFlags()
{
return flags;
}
std::string Page::getPageTitle()
{
return title;
}
FileFlags FlagReader::readFlags(std::string flagString)
{
FileFlags flags;
for (char c : flagString)
{
switch (c)
{
case 'p':
flags.hidden = true;
break;
}
}
return flags;
}

43
Src/Page.h Normal file
View file

@ -0,0 +1,43 @@
#pragma once
#include <fstream>
#include <sstream>
#include <string>
struct FileFlags
{
bool hidden;
};
class FlagReader
{
private:
public:
FileFlags readFlags(std::string flagString);
};
/*
* Class to represent the files of a blog page, this represents
* the original file and its new output file
*/
class Page
{
private:
std::string fileUrl;
std::string fileContents;
std::string title;
FileFlags flags;
// file date should be added here
void calculatePageFlags();
std::string readFile(std::string fileUrl);
public:
Page(std::string fileUrl);
std::string getPageContents();
std::string getPageTitle();
FileFlags getPageFlags();
};

View file

@ -1,10 +1,19 @@
#include "BlogParser.h"
#include "../MacroDefinitions.h"
#include <string>
#include <fstream>
#include "BlogPageBuilder.h"
int main()
{
BlogParser* parser = new BlogParser();
BlogPageBuilder* builder = new BlogPageBuilder(RESOURCE_FOLDER "/Templates/BlogPageTemplate.html");
std::string result = builder->createPage(TEST_RESOURCE_FOLDER "/TestFullArticle.md");
parser->parse("../../inputfile.md", "../../outputfile.html");
std::ofstream file(RESOURCE_FOLDER "/output.html");
file << result;
file.close();
}