2015-04-12 13:35:13 +00:00
|
|
|
#include "render_context.hpp"
|
2015-04-13 00:15:51 +00:00
|
|
|
#include "state/outside_section.hpp"
|
2015-04-16 19:05:59 +00:00
|
|
|
#include "visitor/get_token.hpp"
|
2015-04-09 18:41:27 +00:00
|
|
|
|
|
|
|
using namespace mstch;
|
|
|
|
|
|
|
|
const mstch::node render_context::null_node;
|
|
|
|
|
2015-04-16 19:05:59 +00:00
|
|
|
render_context::push::push(render_context& context, const mstch::node& node):
|
2015-04-23 10:54:08 +00:00
|
|
|
context(context)
|
2015-04-09 18:41:27 +00:00
|
|
|
{
|
2015-04-23 10:54:08 +00:00
|
|
|
context.nodes.emplace_front(node);
|
2015-04-23 13:55:18 +00:00
|
|
|
context.state.push(std::unique_ptr<render_state>(new outside_section));
|
2015-04-13 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render_context::push::~push() {
|
2015-04-23 10:54:08 +00:00
|
|
|
context.nodes.pop_front();
|
|
|
|
context.state.pop();
|
2015-04-13 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 22:49:45 +00:00
|
|
|
std::string render_context::push::render(const template_type& templt) {
|
2015-04-23 10:54:08 +00:00
|
|
|
return context.render(templt);
|
2015-04-09 18:41:27 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 10:56:08 +00:00
|
|
|
render_context::render_context(
|
2015-04-23 10:54:08 +00:00
|
|
|
const mstch::node& node,
|
|
|
|
const std::map<std::string, template_type>& partials):
|
2015-09-24 09:43:27 +00:00
|
|
|
partials(partials), nodes(1, node)
|
2015-04-09 18:41:27 +00:00
|
|
|
{
|
2015-04-23 13:55:18 +00:00
|
|
|
state.push(std::unique_ptr<render_state>(new outside_section));
|
2015-04-09 18:41:27 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 13:04:11 +00:00
|
|
|
const mstch::node& render_context::find_node(
|
2015-04-23 10:54:08 +00:00
|
|
|
const std::string& token,
|
|
|
|
const std::deque<node>& current_nodes)
|
2015-04-10 10:56:08 +00:00
|
|
|
{
|
2015-04-23 10:54:08 +00:00
|
|
|
if (token != "." && token.find('.') != std::string::npos)
|
2015-04-23 13:55:18 +00:00
|
|
|
return find_node(token.substr(token.rfind('.') + 1),
|
2015-04-23 10:54:08 +00:00
|
|
|
{find_node(token.substr(0, token.rfind('.')), current_nodes)});
|
|
|
|
else
|
|
|
|
for (auto& node: current_nodes)
|
|
|
|
if (visit(has_token(token), node))
|
|
|
|
return visit(get_token(token, node), node);
|
|
|
|
return null_node;
|
2015-04-09 18:41:27 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 13:04:11 +00:00
|
|
|
const mstch::node& render_context::get_node(const std::string& token) {
|
2015-04-23 10:54:08 +00:00
|
|
|
return find_node(token, nodes);
|
2015-04-09 18:41:27 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 12:12:51 +00:00
|
|
|
std::string render_context::render(
|
|
|
|
const template_type& templt, const std::string& prefix)
|
|
|
|
{
|
2015-04-23 10:54:08 +00:00
|
|
|
std::string output;
|
2015-05-04 12:12:51 +00:00
|
|
|
bool prev_eol = true;
|
|
|
|
for (auto& token: templt) {
|
|
|
|
if (prev_eol && prefix.length() != 0)
|
|
|
|
output += state.top()->render(*this, {prefix});
|
2015-04-23 10:54:08 +00:00
|
|
|
output += state.top()->render(*this, token);
|
2015-05-04 12:12:51 +00:00
|
|
|
prev_eol = token.eol();
|
|
|
|
}
|
2015-04-23 10:54:08 +00:00
|
|
|
return output;
|
2015-04-09 18:41:27 +00:00
|
|
|
}
|
2015-04-12 15:11:49 +00:00
|
|
|
|
2015-05-04 12:12:51 +00:00
|
|
|
std::string render_context::render_partial(
|
|
|
|
const std::string& partial_name, const std::string& prefix)
|
|
|
|
{
|
|
|
|
return partials.count(partial_name) ?
|
|
|
|
render(partials.at(partial_name), prefix) : "";
|
2015-04-12 15:11:49 +00:00
|
|
|
}
|