aboutsummaryrefslogtreecommitdiff
path: root/lib/mstch/src/visitor/render_node.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/visitor/render_node.hpp
parent00afb767ae37488d99f78363d031b898b1932354 (diff)
add mstch
Diffstat (limited to 'lib/mstch/src/visitor/render_node.hpp')
-rw-r--r--lib/mstch/src/visitor/render_node.hpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/mstch/src/visitor/render_node.hpp b/lib/mstch/src/visitor/render_node.hpp
new file mode 100644
index 0000000..633dd4d
--- /dev/null
+++ b/lib/mstch/src/visitor/render_node.hpp
@@ -0,0 +1,56 @@
+#pragma once
+
+#include <sstream>
+#include <boost/variant/static_visitor.hpp>
+
+#include "render_context.hpp"
+#include "mstch/mstch.hpp"
+#include "utils.hpp"
+
+namespace mstch {
+
+class render_node: public boost::static_visitor<std::string> {
+ public:
+ enum class flag { none, escape_html };
+ render_node(render_context& ctx, flag p_flag = flag::none):
+ m_ctx(ctx), m_flag(p_flag)
+ {
+ }
+
+ template<class T>
+ std::string operator()(const T&) const {
+ return "";
+ }
+
+ std::string operator()(const int& value) const {
+ return std::to_string(value);
+ }
+
+ std::string operator()(const double& value) const {
+ std::stringstream ss;
+ ss << value;
+ return ss.str();
+ }
+
+ std::string operator()(const bool& value) const {
+ return value ? "true" : "false";
+ }
+
+ std::string operator()(const lambda& value) const {
+ template_type interpreted{value([this](const mstch::node& n) {
+ return visit(render_node(m_ctx), n);
+ })};
+ auto rendered = render_context::push(m_ctx).render(interpreted);
+ return (m_flag == flag::escape_html) ? html_escape(rendered) : rendered;
+ }
+
+ std::string operator()(const std::string& value) const {
+ return (m_flag == flag::escape_html) ? html_escape(value) : value;
+ }
+
+ private:
+ render_context& m_ctx;
+ flag m_flag;
+};
+
+}