| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/variant/static_visitor.hpp>
 | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04:00
										 |  |  | #include <sstream>
 | 
					
						
							| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "../render_context.hpp"
 | 
					
						
							|  |  |  | #include "../utils.hpp"
 | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04:00
										 |  |  | #include "mstch/mstch.hpp"
 | 
					
						
							| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04:00
										 |  |  | namespace mstch | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04:00
										 |  |  | class render_node : public boost::static_visitor<std::string> | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |   enum class flag | 
					
						
							| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04:00
										 |  |  |     none, | 
					
						
							|  |  |  |     escape_html | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   render_node(render_context &ctx, flag p_flag = flag::none) : m_ctx(ctx), m_flag(p_flag) {} | 
					
						
							| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04:00
										 |  |  |   template <class T> std::string operator()(const T &) const { return ""; } | 
					
						
							| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04:00
										 |  |  |   std::string operator()(const int &value) const { return std::to_string(value); } | 
					
						
							| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04:00
										 |  |  |   std::string operator()(const double &value) const | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  |     std::stringstream ss; | 
					
						
							|  |  |  |     ss << value; | 
					
						
							|  |  |  |     return ss.str(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04:00
										 |  |  |   std::string operator()(const bool &value) const { return value ? "true" : "false"; } | 
					
						
							| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04: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 09:48:27 -04:00
										 |  |  |     auto rendered = render_context::push(m_ctx).render(interpreted); | 
					
						
							|  |  |  |     return (m_flag == flag::escape_html) ? html_escape(rendered) : rendered; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04:00
										 |  |  |   std::string operator()(const std::string &value) const | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  |     return (m_flag == flag::escape_html) ? html_escape(value) : value; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 10:29:56 -04:00
										 |  |  | private: | 
					
						
							|  |  |  |   render_context &m_ctx; | 
					
						
							| 
									
										
										
										
											2017-10-11 09:48:27 -04:00
										 |  |  |   flag m_flag; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | } |