aboutsummaryrefslogtreecommitdiff
path: root/src/content
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2021-10-23 01:44:37 +0200
committerToni Uhlig <matzeton@googlemail.com>2021-10-23 01:44:37 +0200
commitca7ca2218e07a24075cdc9d48e967cfdc2a3543b (patch)
tree69d0c5d6c0d064770a04b07faf005bc4fbbe7757 /src/content
parent09f45879c2b2e63689265924cb700dee5f02f653 (diff)
Improved Blog/Markdown URI base path handling.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/content')
-rw-r--r--src/content/blog/Blog.cpp63
-rw-r--r--src/content/blog/Blog.hpp6
-rw-r--r--src/content/markdown/Markdown.cpp56
-rw-r--r--src/content/markdown/Markdown.hpp1
4 files changed, 78 insertions, 48 deletions
diff --git a/src/content/blog/Blog.cpp b/src/content/blog/Blog.cpp
index a472b97..5eb126f 100644
--- a/src/content/blog/Blog.cpp
+++ b/src/content/blog/Blog.cpp
@@ -1,5 +1,7 @@
#include "Blog.hpp"
+#include "../../ContentManager.hpp"
+
#include <filesystem>
Blog::Blog(std::string uriBasePath, std::string blogPath, std::string mainTemplatePath)
@@ -29,10 +31,11 @@ bool Blog::Init()
for (auto const & jfile : fs.GetFiles())
{
+ std::string full_uri_path = m_UriBasePath + "/" + std::string(std::filesystem::path(jfile.first).stem());
+ ContentManager::RemoveGarbageSlashes(full_uri_path);
+
auto const json_metadata = inja::json::parse(jfile.second.data);
- BlogEntry be =
- std::make_shared<struct blog_entry>(jfile.first,
- std::string(std::filesystem::path(jfile.first).stem()) + ".md");
+ BlogEntry be = std::make_shared<struct blog_entry>(full_uri_path);
if (Blog::ValidateAndSetMetdadata(json_metadata, be) == false)
{
std::cerr << "Blog Metadata validation failed." << std::endl;
@@ -40,13 +43,13 @@ bool Blog::Init()
}
m_BlogEntriesSortedByDate.push_back(be);
- m_Redirections.push_back(m_UriBasePath + "/" + std::string(std::filesystem::path(jfile.first).stem()));
+ m_Redirections.push_back(full_uri_path);
}
- std::sort(m_BlogEntriesSortedByDate.begin(),
- m_BlogEntriesSortedByDate.end(),
- [](auto const & a, auto const & b) { return a->publishDate > b->publishDate; });
+ std::sort(m_BlogEntriesSortedByDate.begin(), m_BlogEntriesSortedByDate.end(), [](auto const & a, auto const & b) {
+ return a->publishDate > b->publishDate;
+ });
- m_BlogContents.Init();
+ m_BlogContents.Init(m_UriBasePath);
if (retval == false)
{
@@ -76,7 +79,7 @@ bool Blog::Render(RequestResponse & rr, RenderData & rd, std::string & out)
}
else
{
- rd["blog_content"] = "bla";
+ return false;
}
return true;
@@ -101,19 +104,17 @@ bool Blog::ValidateAndSetMetdadata(BlogMetadata const & blogMetadata, BlogEntry
{
bool retval = true;
std::function<bool(BlogMetadata const &, std::string const)> validateMetadata =
- [blogEntry](BlogMetadata const & bm, std::string const tname)
- {
- if (bm.find(tname) == bm.cend())
- {
- std::cerr << "Metadata validation: JSON key '" << tname << "' missing in " << blogEntry->metadata_filename
- << std::endl;
- return false;
- }
- return true;
- };
- std::function<bool(std::string const &, std::time_t &)> parseDateTime =
- [](std::string const & timeStr, std::time_t & time)
- {
+ [blogEntry](BlogMetadata const & bm, std::string const tname) {
+ if (bm.find(tname) == bm.cend())
+ {
+ std::cerr << "Metadata validation: JSON key '" << tname << "' missing in "
+ << blogEntry->filename + ".json" << std::endl;
+ return false;
+ }
+ return true;
+ };
+ std::function<bool(std::string const &, std::time_t &)> parseDateTime = [](std::string const & timeStr,
+ std::time_t & time) {
std::tm tm = {};
std::stringstream ss(timeStr);
ss >> std::get_time(&tm, "%d.%m.%y %H:%M");
@@ -175,10 +176,10 @@ bool Blog::ValidateEntries() const
for (auto const & e : m_BlogEntriesSortedByDate)
{
- if (m_BlogContents.HasMarkdownFile(e->content_filename) == false)
+ if (m_BlogContents.HasMarkdownFile(e->filename) == false)
{
- std::cerr << "Blog entry metadata " << e->metadata_filename << " exists, but markdown file "
- << e->content_filename << " not." << std::endl;
+ std::cerr << "Blog entry metadata " << e->filename << ".json exists, but markdown file " << e->filename
+ << ".md not." << std::endl;
retval = false;
}
}
@@ -186,9 +187,10 @@ bool Blog::ValidateEntries() const
{
if (std::any_of(m_BlogEntriesSortedByDate.cbegin(),
m_BlogEntriesSortedByDate.cend(),
- [m](BlogEntry const & be) { return m.first == be->content_filename; }) == false)
+ [m](BlogEntry const & be) { return m.first == be->filename; }) == false)
{
- std::cerr << "Blog entry markdown " << m.first << " exists, but metadata not." << std::endl;
+ std::cerr << "Blog entry markdown " << m.first << ".md exists, but metadata " << m.first << ".json not."
+ << std::endl;
retval = false;
}
}
@@ -206,17 +208,16 @@ void Blog::GenerateBlogListing(RenderData & rd)
}
RenderData re;
- re["metadata_filename"] = e->metadata_filename;
- re["content_filename"] = e->content_filename;
+ re["filename"] = e->filename;
re["title"] = e->title;
re["tags"] = e->tags;
re["author"] = e->author;
re["createDate"] = e->createDate;
re["publishDate"] = e->publishDate;
re["published"] = e->published;
- if (m_BlogContents.HasMarkdownFile(e->content_filename) == true)
+ if (m_BlogContents.HasMarkdownFile(e->filename) == true)
{
- re["content"] = m_BlogContents.GetMarkdownHTML(e->content_filename)->c_str();
+ re["content"] = m_BlogContents.GetMarkdownHTML(e->filename)->c_str();
}
else
{
diff --git a/src/content/blog/Blog.hpp b/src/content/blog/Blog.hpp
index 06f279c..79c06a2 100644
--- a/src/content/blog/Blog.hpp
+++ b/src/content/blog/Blog.hpp
@@ -9,13 +9,11 @@
struct blog_entry
{
- explicit blog_entry(std::string const & metadata_filename, std::string const & content_filename)
- : metadata_filename(metadata_filename), content_filename(content_filename)
+ explicit blog_entry(std::string const & filename) : filename(filename)
{
}
- std::string const metadata_filename;
- std::string const content_filename;
+ std::string const filename;
std::string title;
std::vector<std::string> tags;
diff --git a/src/content/markdown/Markdown.cpp b/src/content/markdown/Markdown.cpp
index 2b0bcc6..de05080 100644
--- a/src/content/markdown/Markdown.cpp
+++ b/src/content/markdown/Markdown.cpp
@@ -1,7 +1,11 @@
#include "Markdown.hpp"
+#include "../../ContentManager.hpp"
+
#include <md4c-html.h>
+#include <filesystem>
+
Markdown::Markdown(std::string uriBasePath, std::string markdownFilesPath, std::string mainTemplatePath)
: Content(),
m_UriBasePath(uriBasePath),
@@ -22,6 +26,12 @@ extern "C" void markdown_to_html_conversion(const MD_CHAR * const text, MD_SIZE
html->append(text, size);
}
+bool Markdown::Init(std::string const & uriBasePath)
+{
+ m_UriBasePath = uriBasePath;
+ return Init();
+}
+
bool Markdown::Init()
{
std::cout << "Markdown files path: " << m_MarkdownFilesPath << std::endl;
@@ -60,7 +70,10 @@ bool Markdown::Init()
return false;
}
- m_Markdowns[mfile.first] = std::make_shared<std::string>(html);
+ std::string full_uri_path = m_UriBasePath + "/" + std::string(std::filesystem::path(mfile.first).stem());
+ ContentManager::RemoveGarbageSlashes(full_uri_path);
+ m_Markdowns[full_uri_path] = std::make_shared<std::string>(html);
+ m_Redirections.push_back(full_uri_path);
}
return true;
@@ -84,35 +97,52 @@ bool Markdown::Render(RequestResponse & rr, RenderData & rd, std::string & out)
return false;
}
+ if (rr.UseUri() == false)
+ {
+ return false;
+ }
+
+ rd["uri"] = rr.GetUriPath();
+
if (rr.GetUriPath() == m_UriBasePath || rr.GetUriPath() == m_UriBasePath + "/" ||
rr.GetUriPath() == m_UriBasePath + "/index.html")
{
- rr.UseUri();
std::string requested_markdown;
- rd["uri"] = rr.GetUriPath();
if (rr.GetQueryValue("get", requested_markdown) == true)
{
- requested_markdown += ".md";
- if (HasMarkdownFile(requested_markdown) == true)
+ if (HasMarkdownFile(m_UriBasePath + "/" + requested_markdown) == true)
+ {
+ rd["content"] = *GetMarkdownHTML(m_UriBasePath + "/" + requested_markdown);
+ }
+ else if (HasMarkdownFile(m_UriBasePath + "/index.md") == true)
+ {
+ rd["content"] = *GetMarkdownHTML(m_UriBasePath + "/index.md");
+ }
+ else
{
- rd["content"] = *GetMarkdownHTML(requested_markdown);
- } else if (HasMarkdownFile("index.md") == true) {
- rd["content"] = *GetMarkdownHTML("index.md");
- } else {
return false;
}
}
- else if (HasMarkdownFile("index.md") == true)
+ else if (HasMarkdownFile(m_UriBasePath + "/index") == true)
+ {
+ rd["content"] = *GetMarkdownHTML(m_UriBasePath + "/index");
+ }
+ else
{
- rd["content"] = *GetMarkdownHTML("index.md");
- } else {
return false;
}
}
else
{
- return false;
+ if (HasMarkdownFile(rr.GetUriPath()) == true)
+ {
+ rd["content"] = *GetMarkdownHTML(rr.GetUriPath());
+ }
+ else
+ {
+ return false;
+ }
}
return true;
diff --git a/src/content/markdown/Markdown.hpp b/src/content/markdown/Markdown.hpp
index fa14b3a..fe0d968 100644
--- a/src/content/markdown/Markdown.hpp
+++ b/src/content/markdown/Markdown.hpp
@@ -11,6 +11,7 @@ class Markdown : public Content
public:
explicit Markdown(std::string uriBasePath, std::string markdownFilesPath, std::string mainTemplatePath = "");
+ bool Init(std::string const & uriBasePath);
bool Init();
void Shutdown();
bool Render(RequestResponse & rr, RenderData & rd, std::string & out);