X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2Fmstch%2Fvisitor%2Frender_section.hpp;fp=lib%2Fmstch%2Fvisitor%2Frender_section.hpp;h=f2d5259a4ec567d75d58d9d1d543a7f969c93611;hb=2e142df11d6f312a2a2b5097b8da73571ed523e8;hp=0000000000000000000000000000000000000000;hpb=61b3659afe961ed248f30e26f9ca8f28fcfafddc;p=cpPlugins.git diff --git a/lib/mstch/visitor/render_section.hpp b/lib/mstch/visitor/render_section.hpp new file mode 100644 index 0000000..f2d5259 --- /dev/null +++ b/lib/mstch/visitor/render_section.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include + +#include "render_context.hpp" +#include "mstch/mstch.hpp" +#include "utils.hpp" +#include "render_node.hpp" + +namespace mstch { + +class render_section: public boost::static_visitor { + public: + enum class flag { none, keep_array }; + render_section( + render_context& ctx, + const template_type& section, + const delim_type& delims, + flag p_flag = flag::none): + m_ctx(ctx), m_section(section), m_delims(delims), m_flag(p_flag) + { + } + + template + std::string operator()(const T& t) const { + return render_context::push(m_ctx, t).render(m_section); + } + + std::string operator()(const lambda& fun) const { + std::string section_str; + for (auto& token: m_section) + section_str += token.raw(); + template_type interpreted{fun([this](const mstch::node& n) { + return visit(render_node(m_ctx), n); + }, section_str), m_delims}; + return render_context::push(m_ctx).render(interpreted); + } + + std::string operator()(const array& array) const { + std::string out; + if (m_flag == flag::keep_array) + return render_context::push(m_ctx, array).render(m_section); + else + for (auto& item: array) + out += visit(render_section( + m_ctx, m_section, m_delims, flag::keep_array), item); + return out; + } + + private: + render_context& m_ctx; + const template_type& m_section; + const delim_type& m_delims; + flag m_flag; +}; + +}