]> Creatis software - cpPlugins.git/blob - lib/mstch/visitor/render_node.hpp
Moved to version 1.0
[cpPlugins.git] / lib / mstch / visitor / render_node.hpp
1 #pragma once
2
3 #include <sstream>
4 #include <boost/variant/static_visitor.hpp>
5
6 #include "render_context.hpp"
7 #include "mstch/mstch.hpp"
8 #include "utils.hpp"
9
10 namespace mstch {
11
12 class render_node: public boost::static_visitor<std::string> {
13  public:
14   enum class flag { none, escape_html };
15   render_node(render_context& ctx, flag p_flag = flag::none):
16       m_ctx(ctx), m_flag(p_flag)
17   {
18   }
19
20   template<class T>
21   std::string operator()(const T&) const {
22     return "";
23   }
24
25   std::string operator()(const int& value) const {
26     return std::to_string(value);
27   }
28
29   std::string operator()(const double& value) const {
30     std::stringstream ss;
31     ss << value;
32     return ss.str();
33   }
34
35   std::string operator()(const bool& value) const {
36     return value ? "true" : "false";
37   }
38
39   std::string operator()(const lambda& value) const {
40     template_type interpreted{value([this](const mstch::node& n) {
41       return visit(render_node(m_ctx), n);
42     })};
43     auto rendered = render_context::push(m_ctx).render(interpreted);
44     return (m_flag == flag::escape_html) ? html_escape(rendered) : rendered;
45   }
46
47   std::string operator()(const std::string& value) const {
48     return (m_flag == flag::escape_html) ? html_escape(value) : value;
49   }
50
51  private:
52   render_context& m_ctx;
53   flag m_flag;
54 };
55
56 }