Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/nntpchan-daemon/libmustache/visitor/render_node.hpp

53 lines
1.3 KiB
C++
Raw Normal View History

2017-10-11 18:48:27 +05:00
#pragma once
#include <boost/variant/static_visitor.hpp>
2017-10-17 19:29:56 +05:00
#include <sstream>
2017-10-11 18:48:27 +05:00
#include "../render_context.hpp"
#include "../utils.hpp"
2017-10-17 19:29:56 +05:00
#include "mstch/mstch.hpp"
2017-10-11 18:48:27 +05:00
2017-10-17 19:29:56 +05:00
namespace mstch
{
2017-10-11 18:48:27 +05:00
2017-10-17 19:29:56 +05:00
class render_node : public boost::static_visitor<std::string>
{
public:
enum class flag
2017-10-11 18:48:27 +05:00
{
2017-10-17 19:29:56 +05:00
none,
escape_html
};
render_node(render_context &ctx, flag p_flag = flag::none) : m_ctx(ctx), m_flag(p_flag) {}
2017-10-11 18:48:27 +05:00
2017-10-17 19:29:56 +05:00
template <class T> std::string operator()(const T &) const { return ""; }
2017-10-11 18:48:27 +05:00
2017-10-17 19:29:56 +05:00
std::string operator()(const int &value) const { return std::to_string(value); }
2017-10-11 18:48:27 +05:00
2017-10-17 19:29:56 +05:00
std::string operator()(const double &value) const
{
2017-10-11 18:48:27 +05:00
std::stringstream ss;
ss << value;
return ss.str();
}
2017-10-17 19:29:56 +05:00
std::string operator()(const bool &value) const { return value ? "true" : "false"; }
2017-10-11 18:48:27 +05:00
2017-10-17 19:29:56 +05:00
std::string operator()(const lambda &value) const
{
template_type interpreted{value([this](const mstch::node &n) { return visit(render_node(m_ctx), n); })};
2017-10-11 18:48:27 +05:00
auto rendered = render_context::push(m_ctx).render(interpreted);
return (m_flag == flag::escape_html) ? html_escape(rendered) : rendered;
}
2017-10-17 19:29:56 +05:00
std::string operator()(const std::string &value) const
{
2017-10-11 18:48:27 +05:00
return (m_flag == flag::escape_html) ? html_escape(value) : value;
}
2017-10-17 19:29:56 +05:00
private:
render_context &m_ctx;
2017-10-11 18:48:27 +05:00
flag m_flag;
};
}