diff options
| author | Mistivia <i@mistivia.com> | 2025-01-16 15:43:04 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-01-16 15:53:58 +0800 |
| commit | 8689a7c78c50676ea739f52fbcee9f091709f5c0 (patch) | |
| tree | bbb72c68e5e1753f751133941a402caa43e29a16 /lib/mstch/src/visitor/render_section.hpp | |
| parent | 00afb767ae37488d99f78363d031b898b1932354 (diff) | |
add mstch
Diffstat (limited to 'lib/mstch/src/visitor/render_section.hpp')
| -rw-r--r-- | lib/mstch/src/visitor/render_section.hpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/mstch/src/visitor/render_section.hpp b/lib/mstch/src/visitor/render_section.hpp new file mode 100644 index 0000000..f2d5259 --- /dev/null +++ b/lib/mstch/src/visitor/render_section.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include <boost/variant/static_visitor.hpp> + +#include "render_context.hpp" +#include "mstch/mstch.hpp" +#include "utils.hpp" +#include "render_node.hpp" + +namespace mstch { + +class render_section: public boost::static_visitor<std::string> { + 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<class T> + 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; +}; + +} |
