diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2021-10-23 01:44:37 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2021-10-23 01:44:37 +0200 |
commit | ca7ca2218e07a24075cdc9d48e967cfdc2a3543b (patch) | |
tree | 69d0c5d6c0d064770a04b07faf005bc4fbbe7757 /src/content/markdown | |
parent | 09f45879c2b2e63689265924cb700dee5f02f653 (diff) |
Improved Blog/Markdown URI base path handling.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/content/markdown')
-rw-r--r-- | src/content/markdown/Markdown.cpp | 56 | ||||
-rw-r--r-- | src/content/markdown/Markdown.hpp | 1 |
2 files changed, 44 insertions, 13 deletions
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); |