aboutsummaryrefslogtreecommitdiff
path: root/lib/mstch/src/render_context.hpp
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-01-16 15:43:04 +0800
committerMistivia <i@mistivia.com>2025-01-16 15:53:58 +0800
commit8689a7c78c50676ea739f52fbcee9f091709f5c0 (patch)
treebbb72c68e5e1753f751133941a402caa43e29a16 /lib/mstch/src/render_context.hpp
parent00afb767ae37488d99f78363d031b898b1932354 (diff)
add mstch
Diffstat (limited to 'lib/mstch/src/render_context.hpp')
-rw-r--r--lib/mstch/src/render_context.hpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/mstch/src/render_context.hpp b/lib/mstch/src/render_context.hpp
new file mode 100644
index 0000000..b97d41b
--- /dev/null
+++ b/lib/mstch/src/render_context.hpp
@@ -0,0 +1,51 @@
+#pragma once
+
+#include <deque>
+#include <list>
+#include <sstream>
+#include <string>
+#include <stack>
+
+#include "mstch/mstch.hpp"
+#include "state/render_state.hpp"
+#include "template_type.hpp"
+
+namespace mstch {
+
+class render_context {
+ public:
+ class push {
+ public:
+ push(render_context& context, const mstch::node& node = {});
+ ~push();
+ std::string render(const template_type& templt);
+ private:
+ render_context& m_context;
+ };
+
+ render_context(
+ const mstch::node& node,
+ const std::map<std::string, template_type>& partials);
+ const mstch::node& get_node(const std::string& token);
+ std::string render(
+ const template_type& templt, const std::string& prefix = "");
+ std::string render_partial(
+ const std::string& partial_name, const std::string& prefix);
+ template<class T, class... Args>
+ void set_state(Args&& ... args) {
+ m_state.top() = std::unique_ptr<render_state>(
+ new T(std::forward<Args>(args)...));
+ }
+
+ private:
+ static const mstch::node null_node;
+ const mstch::node& find_node(
+ const std::string& token,
+ std::list<node const*> current_nodes);
+ std::map<std::string, template_type> m_partials;
+ std::deque<mstch::node> m_nodes;
+ std::list<const mstch::node*> m_node_ptrs;
+ std::stack<std::unique_ptr<render_state>> m_state;
+};
+
+}