mstch/src/render_context.cpp

67 lines
1.9 KiB
C++
Raw Normal View History

2015-04-12 15:35:13 +02:00
#include "render_context.hpp"
2015-04-12 17:11:49 +02:00
#include "utils.hpp"
2015-04-13 02:15:51 +02:00
#include "state/outside_section.hpp"
2015-04-09 20:41:27 +02:00
using namespace mstch;
const mstch::node render_context::null_node;
2015-04-13 02:15:51 +02:00
render_context::push::push(render_context& context, const mstch::object& obj):
context(context)
2015-04-09 20:41:27 +02:00
{
2015-04-15 01:42:51 +02:00
context.objects.emplace_front(obj);
context.state.push(std::unique_ptr<state::render_state>(
new state::outside_section));
2015-04-13 02:15:51 +02:00
}
render_context::push::~push() {
context.objects.pop_front();
context.state.pop();
}
std::string render_context::push::render(const template_type& templt) {
return context.render(templt);
2015-04-09 20:41:27 +02:00
}
2015-04-10 12:56:08 +02:00
render_context::render_context(
const mstch::object& object,
const std::map<std::string,template_type>& partials):
2015-04-13 02:15:51 +02:00
partials{partials},
objects{object}
2015-04-09 20:41:27 +02:00
{
2015-04-15 22:32:27 +02:00
state.push(std::unique_ptr<state::render_state>(
new state::outside_section));
2015-04-09 20:41:27 +02:00
}
2015-04-10 12:56:08 +02:00
const mstch::node& render_context::find_node(
const std::string& token,
const std::deque<object>& current_objects)
2015-04-10 12:56:08 +02:00
{
2015-04-11 16:29:12 +02:00
if (token != "." && token.find('.') != std::string::npos)
2015-04-10 12:56:08 +02:00
return find_node(
token.substr(token.rfind('.') + 1),
{boost::get<object>(find_node(
token.substr(0, token.rfind('.')),
current_objects))});
2015-04-11 16:29:12 +02:00
else
2015-04-09 20:41:27 +02:00
for (auto& object: current_objects)
2015-04-11 16:29:12 +02:00
if (object.count(token))
2015-04-09 20:41:27 +02:00
return object.at(token);
2015-04-11 16:29:12 +02:00
return null_node;
2015-04-09 20:41:27 +02:00
}
const mstch::node& render_context::get_node(const std::string& token) {
return find_node(token, objects);
}
std::string render_context::render(const template_type& templt) {
2015-04-13 16:35:12 +02:00
std::string output;
for (auto& token: templt)
2015-04-13 16:35:12 +02:00
output += state.top()->render(*this, token);
return output;
2015-04-09 20:41:27 +02:00
}
2015-04-12 17:11:49 +02:00
std::string render_context::render_partial(const std::string& partial_name) {
return (partials.count(partial_name))?render(partials.at(partial_name)):"";
}