From 30e30c8a7ba98d3508fa713188800a1a72794b4c Mon Sep 17 00:00:00 2001 From: jmsgrogan Date: Fri, 21 Oct 2022 08:28:32 +0100 Subject: [PATCH] Engine getting complicated - need some JSON for troubleshooting. --- src/compiler/TemplateNodes.h | 59 ++++++++++++++++++++++++++++++------ src/core/StringUtils.cpp | 19 ++++++++++++ src/core/StringUtils.h | 1 + 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/src/compiler/TemplateNodes.h b/src/compiler/TemplateNodes.h index 2c9991f..e75a901 100644 --- a/src/compiler/TemplateNodes.h +++ b/src/compiler/TemplateNodes.h @@ -25,6 +25,11 @@ public: return mChildren.size(); } + virtual std::string getIdentifier() const + { + return {}; + } + TemplateNode* getChild(std::size_t index) const { return mChildren[index].get(); @@ -41,13 +46,23 @@ public: } template - T* getFirstChildShallow() const + T* getFirstChildShallow(const std::string& identifier = {}) const { for (const auto& child : mChildren) { if (auto ret = dynamic_cast(child.get())) { - return ret; + if (!identifier.empty()) + { + if (child->getIdentifier() == identifier) + { + return ret; + } + } + else + { + return ret; + } } } return nullptr; @@ -58,12 +73,17 @@ public: mExtensionParent = parent; } - virtual std::string render() + virtual std::string render(TemplateNode* parentContext = nullptr) { + if (!parentContext && mExtensionParent) + { + parentContext = mExtensionParent; + } + std::string content; for (size_t idx = 0; idx < mChildren.size(); idx++) { - content += mChildren[idx]->render(); + content += mChildren[idx]->render(parentContext); } return content; } @@ -80,10 +100,9 @@ class TemplateExtends : public TemplateNode { public: TemplateExtends(TemplateNode* parent, const std::string& path) - : TemplateNode(parent), - mPath(path) + : TemplateNode(parent) { - + mPath = StringUtils::stripQuotes(path); }; std::string getRawContent() const override @@ -122,6 +141,28 @@ public: return content; } + std::string renderAsParent(TemplateNode* base) + { + return {}; + } + + std::string render(TemplateNode* parentContext) override + { + std::string content; + if (parentContext) + { + if (auto parent_node = parentContext->getFirstChildShallow(getIdentifier())) + { + content = dynamic_cast(parent_node)->renderAsParent(this); + } + } + else + { + + } + return content; + } + private: std::string mName; std::vector mBody; @@ -165,12 +206,12 @@ public: return !mContent.empty(); } - std::string render() override + std::string render(TemplateNode* parentContext) override { std::string content; for (const auto& line : mContent) { - content += line; + content += line + '\n'; } return content; } diff --git a/src/core/StringUtils.cpp b/src/core/StringUtils.cpp index e2727a1..a0faf38 100644 --- a/src/core/StringUtils.cpp +++ b/src/core/StringUtils.cpp @@ -121,3 +121,22 @@ std::string StringUtils::ToPaddedString(unsigned numBytes, unsigned entry) sstr << std::setfill('0') << std::setw(numBytes) << entry; return sstr.str(); } + +std::string StringUtils::stripQuotes(const std::string& input) +{ + if (input.size() < 3) + { + return input; + } + std::size_t start_index = 0; + std::size_t end_index = input.size()-1; + if (input[start_index] == '"') + { + start_index = 1; + } + if (input[end_index] == '"') + { + end_index = end_index - 1; + } + return input.substr(start_index, end_index - start_index + 1); +} diff --git a/src/core/StringUtils.h b/src/core/StringUtils.h index 3b85a50..14f50c1 100644 --- a/src/core/StringUtils.h +++ b/src/core/StringUtils.h @@ -23,4 +23,5 @@ public: static std::string ToPaddedString(unsigned numBytes, unsigned entry); static std::vector split(const std::string& input); static std::string strip(const std::string& input); + static std::string stripQuotes(const std::string& input); };