refactor
This commit is contained in:
parent
8f7210f95d
commit
21a27f4047
@ -6,14 +6,8 @@ include_directories(
|
||||
${Boost_INCLUDE_DIR})
|
||||
|
||||
set(SRC
|
||||
state/in_inverted_section.cpp
|
||||
state/in_section.cpp
|
||||
state/outside_section.cpp
|
||||
visitor/get_token.cpp
|
||||
visitor/has_token.cpp
|
||||
visitor/is_node_empty.cpp
|
||||
visitor/render_node.cpp
|
||||
visitor/render_section.cpp
|
||||
mstch.cpp
|
||||
render_context.cpp
|
||||
template_type.cpp
|
||||
|
@ -44,9 +44,9 @@ const mstch::node& render_context::find_node(
|
||||
token.substr(token.rfind('.') + 1),
|
||||
{find_node(token.substr(0, token.rfind('.')), current_nodes)});
|
||||
else
|
||||
for (auto& n: current_nodes)
|
||||
if (boost::apply_visitor(visitor::has_token(token), n))
|
||||
return boost::apply_visitor(visitor::get_token(token, n), n);
|
||||
for (auto& node: current_nodes)
|
||||
if (boost::apply_visitor(visitor::has_token(token), node))
|
||||
return boost::apply_visitor(visitor::get_token(token, node), node);
|
||||
return null_node;
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,6 @@ namespace mstch {
|
||||
private:
|
||||
render_context& context;
|
||||
};
|
||||
|
||||
render_context(
|
||||
const mstch::node& node,
|
||||
const std::map<std::string,template_type>& partials);
|
||||
|
@ -1,43 +0,0 @@
|
||||
#include "in_inverted_section.hpp"
|
||||
#include "outside_section.hpp"
|
||||
#include "visitor/render_section.hpp"
|
||||
#include "visitor/is_node_empty.hpp"
|
||||
|
||||
using namespace mstch;
|
||||
|
||||
state::in_inverted_section::in_inverted_section(
|
||||
const std::string& section_name):
|
||||
section_name(section_name), skipped_openings(0)
|
||||
{
|
||||
}
|
||||
|
||||
std::string state::in_inverted_section::render(
|
||||
render_context& ctx, const token& token)
|
||||
{
|
||||
switch(token.token_type()) {
|
||||
case token::type::section_close:
|
||||
if(token.content() == section_name && skipped_openings == 0) {
|
||||
std::string out;
|
||||
auto& section_node = ctx.get_node(section_name);
|
||||
if(boost::apply_visitor(visitor::is_node_empty(), section_node))
|
||||
out = render_context::push(ctx).render(section);
|
||||
ctx.set_state<outside_section>();
|
||||
return out;
|
||||
} else {
|
||||
skipped_openings--;
|
||||
section << token;
|
||||
}
|
||||
break;
|
||||
case token::type::inverted_section_open:
|
||||
case token::type::section_open:
|
||||
skipped_openings++;
|
||||
case token::type::text:
|
||||
case token::type::variable:
|
||||
case token::type::unescaped_variable:
|
||||
case token::type::comment:
|
||||
case token::type::partial:
|
||||
section << token;
|
||||
break;
|
||||
};
|
||||
return "";
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
#include "render_state.hpp"
|
||||
#include <vector>
|
||||
#include "template_type.hpp"
|
||||
|
||||
namespace mstch {
|
||||
namespace state {
|
||||
class in_inverted_section: public render_state {
|
||||
public:
|
||||
in_inverted_section(const std::string& section_name);
|
||||
std::string render(
|
||||
render_context& context, const token& token) override;
|
||||
private:
|
||||
const std::string section_name;
|
||||
template_type section;
|
||||
int skipped_openings;
|
||||
};
|
||||
}
|
||||
}
|
@ -5,37 +5,34 @@
|
||||
|
||||
using namespace mstch;
|
||||
|
||||
state::in_section::in_section(const std::string& section_name):
|
||||
section_name(section_name), skipped_openings(0)
|
||||
state::in_section::in_section(type type, const std::string& section_name):
|
||||
m_type(type), section_name(section_name), skipped_openings(0)
|
||||
{
|
||||
}
|
||||
|
||||
std::string state::in_section::render(render_context& ctx, const token& token) {
|
||||
switch(token.token_type()) {
|
||||
case token::type::section_close:
|
||||
if(token.token_type() == token::type::section_close) {
|
||||
if(token.content() == section_name && skipped_openings == 0) {
|
||||
auto& section_node = ctx.get_node(section_name);
|
||||
auto& node = ctx.get_node(section_name);
|
||||
std::string out;
|
||||
if (!boost::apply_visitor(visitor::is_node_empty(), section_node))
|
||||
if(m_type == type::normal) {
|
||||
if (!boost::apply_visitor(visitor::is_node_empty(), node))
|
||||
out = boost::apply_visitor(
|
||||
visitor::render_section(ctx, section), section_node);
|
||||
visitor::render_section(ctx, section), node);
|
||||
} else if(m_type == type::inverted) {
|
||||
if (boost::apply_visitor(visitor::is_node_empty(), node))
|
||||
out = render_context::push(ctx).render(section);
|
||||
}
|
||||
ctx.set_state<outside_section>();
|
||||
return out;
|
||||
} else {
|
||||
skipped_openings--;
|
||||
section << token;
|
||||
}
|
||||
break;
|
||||
case token::type::inverted_section_open:
|
||||
case token::type::section_open:
|
||||
} else if(token.token_type() == token::type::inverted_section_open ||
|
||||
token.token_type() == token::type::section_open)
|
||||
{
|
||||
skipped_openings++;
|
||||
case token::type::text:
|
||||
case token::type::variable:
|
||||
case token::type::unescaped_variable:
|
||||
case token::type::comment:
|
||||
case token::type::partial:
|
||||
section << token;
|
||||
break;
|
||||
}
|
||||
section << token;
|
||||
return "";
|
||||
}
|
||||
|
@ -9,10 +9,12 @@ namespace mstch {
|
||||
namespace state {
|
||||
class in_section: public render_state {
|
||||
public:
|
||||
in_section(const std::string& section_name);
|
||||
enum class type { inverted, normal };
|
||||
in_section(type type, const std::string& section_name);
|
||||
std::string render(
|
||||
render_context& context, const token& token) override;
|
||||
private:
|
||||
const type m_type;
|
||||
const std::string section_name;
|
||||
template_type section;
|
||||
int skipped_openings;
|
||||
|
@ -1,34 +1,33 @@
|
||||
#include "visitor/render_node.hpp"
|
||||
#include "outside_section.hpp"
|
||||
#include "in_section.hpp"
|
||||
#include "in_inverted_section.hpp"
|
||||
#include "render_context.hpp"
|
||||
|
||||
using namespace mstch;
|
||||
using flag = visitor::render_node::flag;
|
||||
|
||||
std::string state::outside_section::render(
|
||||
render_context& ctx, const token& token)
|
||||
{
|
||||
switch(token.token_type()) {
|
||||
case token::type::section_open:
|
||||
ctx.set_state<in_section>(token.content());
|
||||
ctx.set_state<in_section>(in_section::type::normal, token.content());
|
||||
break;
|
||||
case token::type::inverted_section_open:
|
||||
ctx.set_state<in_inverted_section>(token.content());
|
||||
ctx.set_state<in_section>(in_section::type::inverted, token.content());
|
||||
break;
|
||||
case token::type::variable:
|
||||
return boost::apply_visitor(visitor::render_node(flag::escape_html),
|
||||
return boost::apply_visitor(
|
||||
visitor::render_node(visitor::render_node::flag::escape_html),
|
||||
ctx.get_node(token.content()));
|
||||
case token::type::unescaped_variable:
|
||||
return boost::apply_visitor(visitor::render_node(flag::none),
|
||||
return boost::apply_visitor(
|
||||
visitor::render_node(visitor::render_node::flag::none),
|
||||
ctx.get_node(token.content()));
|
||||
case token::type::comment: break;
|
||||
case token::type::text:
|
||||
return token.content();
|
||||
case token::type::partial:
|
||||
return ctx.render_partial(token.content());
|
||||
case token::type::section_close: break;
|
||||
default: break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
@ -1,38 +0,0 @@
|
||||
#include "get_token.hpp"
|
||||
|
||||
using namespace mstch;
|
||||
using namespace mstch::visitor;
|
||||
|
||||
get_token::get_token(const std::string& token, const mstch::node& node):
|
||||
token(token), node(node)
|
||||
{
|
||||
}
|
||||
|
||||
const mstch::node& get_token::operator()(const boost::blank& blank) const {
|
||||
return node;
|
||||
}
|
||||
|
||||
const mstch::node& get_token::operator()(const int& i) const {
|
||||
return node;
|
||||
}
|
||||
|
||||
const mstch::node& get_token::operator()(const bool& b) const {
|
||||
return node;
|
||||
}
|
||||
|
||||
const mstch::node& get_token::operator()(const std::string& str) const {
|
||||
return node;
|
||||
}
|
||||
|
||||
const mstch::node& get_token::operator()(const array& arr) const {
|
||||
return node;
|
||||
}
|
||||
|
||||
const mstch::node& get_token::operator()(const map& map) const {
|
||||
return map.at(token);
|
||||
}
|
||||
|
||||
const mstch::node& get_token::operator()(const std::shared_ptr<object>& obj) const {
|
||||
return obj->at(token);
|
||||
}
|
||||
|
@ -8,17 +8,30 @@ namespace mstch {
|
||||
namespace visitor {
|
||||
class get_token: public boost::static_visitor<const mstch::node&> {
|
||||
public:
|
||||
get_token(const std::string& token, const mstch::node& node);
|
||||
const mstch::node& operator()(const boost::blank& blank) const;
|
||||
const mstch::node& operator()(const int& i) const;
|
||||
const mstch::node& operator()(const bool& b) const;
|
||||
const mstch::node& operator()(const std::string& str) const;
|
||||
const mstch::node& operator()(const array& arr) const;
|
||||
const mstch::node& operator()(const map& map) const;
|
||||
const mstch::node& operator()(const std::shared_ptr<object>& obj) const;
|
||||
get_token(const std::string& token, const mstch::node& node):
|
||||
token(token), node(node)
|
||||
{
|
||||
}
|
||||
|
||||
template<class T> inline
|
||||
const mstch::node& operator()(const T& t) const {
|
||||
return node;
|
||||
}
|
||||
private:
|
||||
const std::string& token;
|
||||
const mstch::node& node;
|
||||
};
|
||||
|
||||
template<> inline
|
||||
const mstch::node& get_token::operator()<map>(const map& m) const {
|
||||
return m.at(token);
|
||||
}
|
||||
|
||||
template<> inline
|
||||
const mstch::node& get_token::operator()<std::shared_ptr<object>>(
|
||||
const std::shared_ptr<object>& obj) const
|
||||
{
|
||||
return obj->at(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,35 +0,0 @@
|
||||
#include "has_token.hpp"
|
||||
|
||||
using namespace mstch;
|
||||
using namespace mstch::visitor;
|
||||
|
||||
has_token::has_token(const std::string& token): token(token) {
|
||||
}
|
||||
|
||||
bool has_token::operator()(const boost::blank& blank) const {
|
||||
return token == ".";
|
||||
}
|
||||
|
||||
bool has_token::operator()(const int& i) const {
|
||||
return token == ".";
|
||||
}
|
||||
|
||||
bool has_token::operator()(const bool& b) const {
|
||||
return token == ".";
|
||||
}
|
||||
|
||||
bool has_token::operator()(const std::string& str) const {
|
||||
return token == ".";
|
||||
}
|
||||
|
||||
bool has_token::operator()(const array& arr) const {
|
||||
return token == ".";
|
||||
}
|
||||
|
||||
bool has_token::operator()(const map& map) const {
|
||||
return map.count(token) == 1;
|
||||
}
|
||||
|
||||
bool has_token::operator()(const std::shared_ptr<object>& obj) const {
|
||||
return obj->has(token);
|
||||
}
|
@ -8,16 +8,27 @@ namespace mstch {
|
||||
namespace visitor {
|
||||
class has_token: public boost::static_visitor<bool> {
|
||||
public:
|
||||
has_token(const std::string& token);
|
||||
bool operator()(const boost::blank& blank) const;
|
||||
bool operator()(const int& i) const;
|
||||
bool operator()(const bool& b) const;
|
||||
bool operator()(const std::string& str) const;
|
||||
bool operator()(const array& arr) const;
|
||||
bool operator()(const map& map) const;
|
||||
bool operator()(const std::shared_ptr<object>& obj) const;
|
||||
has_token(const std::string& token): token(token) {
|
||||
}
|
||||
|
||||
template<class T> inline
|
||||
bool operator()(const T& t) const {
|
||||
return token == ".";
|
||||
}
|
||||
private:
|
||||
const std::string& token;
|
||||
};
|
||||
|
||||
template<> inline
|
||||
bool has_token::operator()<map>(const map& m) const {
|
||||
return m.count(token) == 1;
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool has_token::operator()<std::shared_ptr<object>>(
|
||||
const std::shared_ptr<object>& obj) const
|
||||
{
|
||||
return obj->has(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,32 +0,0 @@
|
||||
#include "is_node_empty.hpp"
|
||||
|
||||
using namespace mstch;
|
||||
using namespace mstch::visitor;
|
||||
|
||||
bool is_node_empty::operator()(const boost::blank& blank) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_node_empty::operator()(const int& i) const {
|
||||
return i == 0;
|
||||
}
|
||||
|
||||
bool is_node_empty::operator()(const bool& b) const {
|
||||
return !b;
|
||||
}
|
||||
|
||||
bool is_node_empty::operator()(const std::string& str) const {
|
||||
return str == "";
|
||||
}
|
||||
|
||||
bool is_node_empty::operator()(const array& arr) const {
|
||||
return arr.size() == 0;
|
||||
}
|
||||
|
||||
bool is_node_empty::operator()(const map& map) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_node_empty::operator()(const std::shared_ptr<object>& obj) const {
|
||||
return false;
|
||||
}
|
@ -8,13 +8,39 @@ namespace mstch {
|
||||
namespace visitor {
|
||||
class is_node_empty: public boost::static_visitor<bool> {
|
||||
public:
|
||||
bool operator()(const boost::blank& blank) const;
|
||||
bool operator()(const int& i) const;
|
||||
bool operator()(const bool& b) const;
|
||||
bool operator()(const std::string& str) const;
|
||||
bool operator()(const array& arr) const;
|
||||
bool operator()(const map& map) const;
|
||||
bool operator()(const std::shared_ptr<object>& obj) const;
|
||||
template<class T> inline
|
||||
bool operator()(const T& t) const {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
template<> inline
|
||||
bool is_node_empty::operator()<boost::blank>(
|
||||
const boost::blank& blank) const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool is_node_empty::operator()<int>(const int& i) const {
|
||||
return i == 0;
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool is_node_empty::operator()<bool>(const bool& b) const {
|
||||
return !b;
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool is_node_empty::operator()<std::string>(
|
||||
const std::string& str) const
|
||||
{
|
||||
return str == "";
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool is_node_empty::operator()<array>(const array& arr) const {
|
||||
return arr.size() == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,36 +0,0 @@
|
||||
#include "utils.hpp"
|
||||
#include "render_node.hpp"
|
||||
|
||||
using namespace mstch;
|
||||
using namespace mstch::visitor;
|
||||
|
||||
render_node::render_node(flag p_flag): m_flag(p_flag) {
|
||||
}
|
||||
|
||||
std::string render_node::operator()(const boost::blank& blank) const {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string render_node::operator()(const int& i) const {
|
||||
return std::to_string(i);
|
||||
}
|
||||
|
||||
std::string render_node::operator()(const bool& b) const {
|
||||
return b?"true":"false";
|
||||
}
|
||||
|
||||
std::string render_node::operator()(const std::string& str) const {
|
||||
return (m_flag == flag::escape_html)?html_escape(str):str;
|
||||
}
|
||||
|
||||
std::string render_node::operator()(const array& arr) const {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string render_node::operator()(const map& map) const {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string render_node::operator()(const std::shared_ptr<object>& obj) const {
|
||||
return "";
|
||||
}
|
@ -12,16 +12,33 @@ namespace mstch {
|
||||
enum class flag {
|
||||
none, escape_html
|
||||
};
|
||||
render_node(flag p_flag = flag::none);
|
||||
std::string operator()(const boost::blank& blank) const;
|
||||
std::string operator()(const int& i) const;
|
||||
std::string operator()(const bool& b) const;
|
||||
std::string operator()(const std::string& str) const;
|
||||
std::string operator()(const array& arr) const;
|
||||
std::string operator()(const map& map) const;
|
||||
std::string operator()(const std::shared_ptr<object>& obj) const;
|
||||
|
||||
render_node(flag p_flag = flag::none): m_flag(p_flag) {
|
||||
}
|
||||
|
||||
template<class T> inline
|
||||
std::string operator()(const T& t) const {
|
||||
return "";
|
||||
}
|
||||
private:
|
||||
flag m_flag;
|
||||
};
|
||||
|
||||
template<> inline
|
||||
std::string render_node::operator()<int>(const int& i) const {
|
||||
return std::to_string(i);
|
||||
}
|
||||
|
||||
template<> inline
|
||||
std::string render_node::operator()<bool>(const bool& b) const {
|
||||
return b?"true":"false";
|
||||
}
|
||||
|
||||
template<> inline
|
||||
std::string render_node::operator()<std::string>(
|
||||
const std::string& str) const
|
||||
{
|
||||
return (m_flag == flag::escape_html)?html_escape(str):str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,47 +0,0 @@
|
||||
#include "render_section.hpp"
|
||||
|
||||
using namespace mstch;
|
||||
using namespace mstch::visitor;
|
||||
|
||||
render_section::render_section(
|
||||
render_context& ctx, const template_type& section, flag p_flag):
|
||||
ctx(ctx), section(section), m_flag(p_flag)
|
||||
{
|
||||
}
|
||||
|
||||
std::string render_section::operator()(
|
||||
const boost::blank& blank) const
|
||||
{
|
||||
return render_context::push(ctx, mstch::node{}).render(section);
|
||||
}
|
||||
|
||||
std::string render_section::operator()(const int& i) const {
|
||||
return render_context::push(ctx, i).render(section);
|
||||
}
|
||||
|
||||
std::string render_section::operator()(const bool& b) const {
|
||||
return render_context::push(ctx, b).render(section);
|
||||
}
|
||||
|
||||
std::string render_section::operator()(const std::string& str) const {
|
||||
return render_context::push(ctx, str).render(section);
|
||||
}
|
||||
|
||||
std::string render_section::operator()(const map& map) const {
|
||||
return render_context::push(ctx, map).render(section);
|
||||
}
|
||||
|
||||
std::string render_section::operator()(const array& a) const {
|
||||
std::string out;
|
||||
if(m_flag == flag::keep_array)
|
||||
out += render_context::push(ctx, a).render(section);
|
||||
else
|
||||
for (auto& item: a)
|
||||
out += boost::apply_visitor(
|
||||
render_section(ctx, section, flag::keep_array), item);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string render_section::operator()(const std::shared_ptr<object>& obj) const {
|
||||
return render_context::push(ctx, obj).render(section);
|
||||
}
|
@ -13,18 +13,31 @@ namespace mstch {
|
||||
render_section(
|
||||
render_context& ctx,
|
||||
const template_type& section,
|
||||
flag p_flag = flag::none);
|
||||
std::string operator()(const boost::blank& blank) const;
|
||||
std::string operator()(const int& i) const;
|
||||
std::string operator()(const bool& b) const;
|
||||
std::string operator()(const std::string& str) const;
|
||||
std::string operator()(const array& arr) const;
|
||||
std::string operator()(const map& map) const;
|
||||
std::string operator()(const std::shared_ptr<object>& obj) const;
|
||||
flag p_flag = flag::none):
|
||||
ctx(ctx), section(section), m_flag(p_flag)
|
||||
{
|
||||
}
|
||||
|
||||
template<class T> inline
|
||||
std::string operator()(const T& t) const {
|
||||
return render_context::push(ctx, t).render(section);
|
||||
}
|
||||
private:
|
||||
render_context& ctx;
|
||||
const template_type& section;
|
||||
flag m_flag;
|
||||
};
|
||||
|
||||
template<> inline
|
||||
std::string render_section::operator()<array>(const array& arr) const {
|
||||
std::string out;
|
||||
if(m_flag == flag::keep_array)
|
||||
out += render_context::push(ctx, arr).render(section);
|
||||
else
|
||||
for (auto& i: arr)
|
||||
out += boost::apply_visitor(
|
||||
render_section(ctx, section, flag::keep_array), i);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user