section array render bugfix

This commit is contained in:
Daniel Sipka 2015-04-11 13:55:59 +02:00
parent 4c43b33291
commit 95ec6728af
4 changed files with 31 additions and 18 deletions

View file

@ -23,10 +23,9 @@ std::string state::in_inverted_section::render(
if(token.content() == section_name && skipped_openings == 0) { if(token.content() == section_name && skipped_openings == 0) {
std::ostringstream out; std::ostringstream out;
auto& section_node = ctx.get_node(section_name); auto& section_node = ctx.get_node(section_name);
if(boost::apply_visitor(visitor::is_node_empty(), section_node)) { if(boost::apply_visitor(visitor::is_node_empty(), section_node))
auto empty = mstch::object{}; out << render_context(mstch::object{}, ctx)
out << render_context(empty, ctx).render(section_text.str()); .render(section_text.str());
}
ctx.set_state<outside_section>(); ctx.set_state<outside_section>();
return out.str(); return out.str();
} else { } else {

View file

@ -20,8 +20,9 @@ std::string state::in_section::render(
auto section_node = ctx.get_node(section_name); auto section_node = ctx.get_node(section_name);
std::string out(""); std::string out("");
if (!boost::apply_visitor(visitor::is_node_empty(), section_node)) if (!boost::apply_visitor(visitor::is_node_empty(), section_node))
out = boost::apply_visitor(visitor::render_section( out = boost::apply_visitor(
ctx, section_text.str()), section_node); visitor::render_section(ctx, section_text.str()),
section_node);
ctx.set_state<outside_section>(); ctx.set_state<outside_section>();
return out; return out;
} else { } else {

View file

@ -5,16 +5,19 @@ using namespace mstch;
visitor::render_section::render_section( visitor::render_section::render_section(
render_context& context, render_context& context,
const std::string& section): const std::string& section,
std::set<flag> flags):
context(context), context(context),
section(section) section(section),
flags(flags)
{ {
} }
std::string visitor::render_section::operator()( std::string visitor::render_section::operator()(
const boost::blank& blank) const const boost::blank& blank) const
{ {
return ""; return render_context(mstch::object{{".", mstch::node{}}}, context)
.render(section);
} }
std::string visitor::render_section::operator()(const int& i) const { std::string visitor::render_section::operator()(const int& i) const {
@ -29,14 +32,17 @@ std::string visitor::render_section::operator()(const std::string& str) const {
return render_context(mstch::object{{".", str}}, context).render(section); return render_context(mstch::object{{".", str}}, context).render(section);
} }
std::string visitor::render_section::operator()(const array& arr) const {
std::ostringstream out;
for (auto& item: arr)
out << render_context(mstch::object{{".", item}}, context)
.render(section);
return out.str();
}
std::string visitor::render_section::operator()(const object& obj) const { std::string visitor::render_section::operator()(const object& obj) const {
return render_context(obj, context).render(section); return render_context(obj, context).render(section);
} }
std::string visitor::render_section::operator()(const array& a) const {
std::ostringstream out;
if(flags.find(flag::keep_array) != flags.end())
out << render_context(mstch::object{{".", a}}, context).render(section);
else
for (auto& item: a)
out << boost::apply_visitor(
render_section(context, section, {flag::keep_array}), item);
return out.str();
}

View file

@ -4,17 +4,24 @@
#include <boost/variant/static_visitor.hpp> #include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp> #include <boost/blank.hpp>
#include <render_context.h> #include <render_context.h>
#include <set>
#include "types.h" #include "types.h"
namespace mstch { namespace mstch {
namespace visitor { namespace visitor {
class render_section: public boost::static_visitor<std::string> { class render_section: public boost::static_visitor<std::string> {
public:
enum class flag { keep_array };
private: private:
render_context& context; render_context& context;
std::string section; std::string section;
std::set<flag> flags;
public: public:
render_section(render_context& context, const std::string& section); render_section(
render_context& context,
const std::string& section,
std::set<flag> flags = {});
std::string operator()(const boost::blank& blank) const; std::string operator()(const boost::blank& blank) const;
std::string operator()(const int& i) const; std::string operator()(const int& i) const;
std::string operator()(const bool& b) const; std::string operator()(const bool& b) const;