This commit is contained in:
Daniel Sipka 2015-04-23 11:06:44 +02:00
parent dd33046a56
commit 29980e299c
10 changed files with 261 additions and 238 deletions

View file

@ -8,55 +8,64 @@
#include <boost/variant.hpp> #include <boost/variant.hpp>
namespace mstch { namespace mstch {
namespace internal { namespace internal {
template<class N>
class object_t {
public:
const N& at(const std::string& name) const {
cache[name] = (methods.at(name))();
return cache[name];
}
bool has(const std::string name) const { template<class N>
return methods.count(name); class object_t {
} public:
protected: const N& at(const std::string& name) const {
template<class S> cache[name] = (methods.at(name))();
void register_methods(S* s, std::map<std::string,N(S::*)()> methods) { return cache[name];
for(auto& i: methods) }
this->methods.insert({i.first, std::bind(i.second, s)});
}
private:
std::map<std::string, std::function<N()>> methods;
mutable std::map<std::string, N> cache;
};
}
using renderer = std::function<std::string(const std::string&)>; bool has(const std::string name) const {
class lambda { return methods.count(name);
public: }
lambda(std::function<std::string(const std::string&,renderer)> fun): fun(fun) {
}
std::string operator()(const std::string& text, renderer renderer) const { protected:
return fun(text, renderer); template<class S>
} void register_methods(S* s, std::map<std::string,N(S::*)()> methods) {
private: for(auto& item: methods)
std::function<std::string(const std::string&,renderer)> fun; this->methods.insert({item.first, std::bind(item.second, s)});
}; }
using node = boost::make_recursive_variant< private:
boost::blank, std::string, int, bool, lambda, std::map<std::string, std::function<N()>> methods;
std::shared_ptr<internal::object_t<boost::recursive_variant_>>, mutable std::map<std::string, N> cache;
std::map<const std::string,boost::recursive_variant_>, };
std::vector<boost::recursive_variant_>>::type;
using object = internal::object_t<node>; }
using map = std::map<const std::string,node>;
using array = std::vector<node>; using renderer = std::function<std::string(const std::string&)>;
class lambda {
public:
lambda(std::function<std::string(const std::string&,renderer)> fun):
fun(fun)
{
}
std::string operator()(const std::string& text, renderer renderer) const {
return fun(text, renderer);
}
private:
std::function<std::string(const std::string&,renderer)> fun;
};
using node = boost::make_recursive_variant<
boost::blank, std::string, int, bool, lambda,
std::shared_ptr<internal::object_t<boost::recursive_variant_>>,
std::map<const std::string,boost::recursive_variant_>,
std::vector<boost::recursive_variant_>>::type;
using object = internal::object_t<node>;
using map = std::map<const std::string,node>;
using array = std::vector<node>;
std::string render(
const std::string& tmplt,
const node& root,
const std::map<std::string,std::string>& partials =
std::map<std::string,std::string>());
std::string render(
const std::string& tmplt,
const node& root,
const std::map<std::string,std::string>& partials =
std::map<std::string,std::string>());
} }

View file

@ -6,33 +6,28 @@
using namespace mstch; using namespace mstch;
state::in_section::in_section(type type, const std::string& section_name): state::in_section::in_section(type type, const std::string& section_name):
m_type(type), section_name(section_name), skipped_openings(0) m_type(type), section_name(section_name), skipped_openings(0)
{ {
} }
std::string state::in_section::render(render_context& ctx, const token& token) { std::string state::in_section::render(render_context& ctx, const token& token) {
if(token.token_type() == token::type::section_close) { if(token.token_type() == token::type::section_close)
if(token.name() == section_name && skipped_openings == 0) { if(token.name() == section_name && skipped_openings == 0) {
auto& node = ctx.get_node(section_name); auto& sn = ctx.get_node(section_name);
std::string out; std::string out;
if(m_type == type::normal) { if(m_type == type::normal &&
if (!boost::apply_visitor(visitor::is_node_empty(), node)) !boost::apply_visitor(visitor::is_node_empty(), sn))
out = boost::apply_visitor( out = boost::apply_visitor(visitor::render_section(ctx, section), sn);
visitor::render_section(ctx, section), node); else if(m_type == type::inverted &&
} else if(m_type == type::inverted) { boost::apply_visitor(visitor::is_node_empty(), sn))
if (boost::apply_visitor(visitor::is_node_empty(), node)) out = render_context::push(ctx).render(section);
out = render_context::push(ctx).render(section); ctx.set_state<outside_section>();
} return out;
ctx.set_state<outside_section>(); } else
return out; skipped_openings--;
} else { else if(token.token_type() == token::type::inverted_section_open ||
skipped_openings--; token.token_type() == token::type::section_open)
} skipped_openings++;
} else if(token.token_type() == token::type::inverted_section_open || section << token;
token.token_type() == token::type::section_open) return "";
{
skipped_openings++;
}
section << token;
return "";
} }

View file

@ -44,7 +44,10 @@ void template_type::tokenize(const std::string& t) {
if (*it == delim_end[del_pos] && ++del_pos == delim_end.size()) { if (*it == delim_end[del_pos] && ++del_pos == delim_end.size()) {
pstate = parse_state::start; pstate = parse_state::start;
tokens.push_back({{tok_start, tok_end}}); tokens.push_back({{tok_start, tok_end}});
tokens.push_back({{tok_end, it + 1}, delim_start.size(), delim_end.size()}); tokens.push_back(
{{tok_end, it + 1},
delim_start.size(),
delim_end.size()});
tok_start = it + 1; tok_start = it + 1;
} else { } else {
pstate = parse_state::start; pstate = parse_state::start;

View file

@ -15,20 +15,20 @@ token::type token::token_info(char c) {
} }
} }
token::token(const std::string& str, std::size_t skip_left, std::size_t skip_right): token::token(const std::string& str, std::size_t left, std::size_t right):
m_raw(str), m_eol(false), m_ws_only(false) m_raw(str), m_eol(false), m_ws_only(false)
{ {
if(skip_left != 0 && skip_right != 0) { if(left != 0 && right != 0) {
if(str[skip_left] == '{' && str[str.size() - skip_right - 1] == '}') { if(str[left] == '{' && str[str.size() - right - 1] == '}') {
m_type = type::unescaped_variable; m_type = type::unescaped_variable;
m_name = {first_not_ws(str.begin() + skip_left + 1, str.end() - skip_right), m_name = {first_not_ws(str.begin() + left + 1, str.end() - right),
first_not_ws(str.rbegin() + 1 + skip_right, str.rend() - skip_left) + 1}; first_not_ws(str.rbegin() + 1 + right, str.rend() - left) + 1};
} else { } else {
auto first = first_not_ws(str.begin() + skip_left, str.end() - skip_right); auto first = first_not_ws(str.begin() + left, str.end() - right);
m_type = token_info(*first); m_type = token_info(*first);
if(m_type != type::variable) if(m_type != type::variable)
first = first_not_ws(first + 1, str.end() - skip_right); first = first_not_ws(first + 1, str.end() - right);
m_name = {first, first_not_ws(str.rbegin() + skip_right, str.rend() - skip_left) + 1}; m_name = {first, first_not_ws(str.rbegin() + right, str.rend() - left) + 1};
} }
} else { } else {
m_type = type::text; m_type = type::text;

View file

@ -9,7 +9,7 @@ namespace mstch {
text, variable, section_open, section_close, inverted_section_open, text, variable, section_open, section_close, inverted_section_open,
unescaped_variable, comment, partial unescaped_variable, comment, partial
}; };
token(const std::string& str, std::size_t skip_left = 0, std::size_t skip_right = 0); token(const std::string& str, std::size_t left = 0, std::size_t right = 0);
type token_type() const { return m_type; }; type token_type() const { return m_type; };
const std::string& raw() const { return m_raw; }; const std::string& raw() const { return m_raw; };
const std::string& name() const { return m_name; }; const std::string& name() const { return m_name; };

View file

@ -5,33 +5,36 @@
#include "mstch/mstch.hpp" #include "mstch/mstch.hpp"
namespace mstch { namespace mstch {
namespace visitor { namespace visitor {
class get_token: public boost::static_visitor<const mstch::node&> {
public:
get_token(const std::string& token, const mstch::node& node):
token(token), node(node)
{
}
template<class T> inline class get_token: public boost::static_visitor<const mstch::node&> {
const mstch::node& operator()(const T& t) const { public:
return node; get_token(const std::string& token, const mstch::node& node):
} token(token), node(node)
private: {
const std::string& token; }
const mstch::node& node;
};
template<> inline template<class T>
const mstch::node& get_token::operator()<map>(const map& m) const { inline const mstch::node& operator()(const T& t) const {
return m.at(token); return node;
} }
template<> inline private:
const mstch::node& get_token::operator()<std::shared_ptr<object>>( const std::string& token;
const std::shared_ptr<object>& obj) const const mstch::node& node;
{ };
return obj->at(token);
} template<>
} inline const mstch::node& get_token::operator()<map>(const map& map) const {
return map.at(token);
}
template<>
inline const mstch::node& get_token::operator()<std::shared_ptr<object>>(
const std::shared_ptr<object>& object) const
{
return object->at(token);
}
}
} }

View file

@ -5,30 +5,32 @@
#include "mstch/mstch.hpp" #include "mstch/mstch.hpp"
namespace mstch { namespace mstch {
namespace visitor { namespace visitor {
class has_token: public boost::static_visitor<bool> {
public:
has_token(const std::string& token): token(token) {
}
template<class T> inline class has_token: public boost::static_visitor<bool> {
bool operator()(const T& t) const { public:
return token == "."; has_token(const std::string& token): token(token) {
} }
private:
const std::string& token;
};
template<> inline template<class T>
bool has_token::operator()<map>(const map& m) const { inline bool operator()(const T& t) const {
return m.count(token) == 1; return token == ".";
} }
private:
const std::string& token;
};
template<> inline template<>
bool has_token::operator()<std::shared_ptr<object>>( inline bool has_token::operator()<map>(const map& map) const {
const std::shared_ptr<object>& obj) const return map.count(token) == 1;
{ }
return obj->has(token);
} template<>
} inline bool has_token::operator()<std::shared_ptr<object>>(
const std::shared_ptr<object>& object) const
{
return object->has(token);
}
}
} }

View file

@ -2,45 +2,48 @@
#include <boost/variant/static_visitor.hpp> #include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp> #include <boost/blank.hpp>
#include "mstch/mstch.hpp" #include "mstch/mstch.hpp"
namespace mstch { namespace mstch {
namespace visitor { namespace visitor {
class is_node_empty: public boost::static_visitor<bool> {
public:
template<class T> inline
bool operator()(const T& t) const {
return false;
}
};
template<> inline class is_node_empty: public boost::static_visitor<bool> {
bool is_node_empty::operator()<boost::blank>( public:
const boost::blank& blank) const template<class T> inline
{ bool operator()(const T& t) const {
return true; return false;
} }
};
template<> inline template<>
bool is_node_empty::operator()<int>(const int& i) const { inline bool is_node_empty::operator()<boost::blank>(
return i == 0; const boost::blank& blank) const
} {
return true;
template<> inline }
bool is_node_empty::operator()<bool>(const bool& b) const {
return !b; template<>
} inline bool is_node_empty::operator()<int>(const int& val) const {
return val == 0;
template<> inline }
bool is_node_empty::operator()<std::string>(
const std::string& str) const template<>
{ inline bool is_node_empty::operator()<bool>(const bool& val) const {
return str == ""; return !val;
} }
template<> inline template<>
bool is_node_empty::operator()<array>(const array& arr) const { inline bool is_node_empty::operator()<std::string>(
return arr.size() == 0; const std::string& val) const
} {
} return val == "";
}
template<>
inline bool is_node_empty::operator()<array>(const array& array) const {
return array.size() == 0;
}
}
} }

View file

@ -2,43 +2,47 @@
#include <boost/variant/static_visitor.hpp> #include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp> #include <boost/blank.hpp>
#include "mstch/mstch.hpp" #include "mstch/mstch.hpp"
#include "utils.hpp" #include "utils.hpp"
namespace mstch { namespace mstch {
namespace visitor { namespace visitor {
class render_node: public boost::static_visitor<std::string> {
public:
enum class flag {
none, escape_html
};
render_node(flag p_flag = flag::none): m_flag(p_flag) { class render_node: public boost::static_visitor<std::string> {
} public:
enum class flag {
none, escape_html
};
template<class T> inline render_node(flag p_flag = flag::none): m_flag(p_flag) {
std::string operator()(const T& t) const { }
return "";
}
private:
flag m_flag;
};
template<> inline template<class T>
std::string render_node::operator()<int>(const int& i) const { inline std::string operator()(const T& t) const {
return std::to_string(i); return "";
} }
template<> inline private:
std::string render_node::operator()<bool>(const bool& b) const { flag m_flag;
return b?"true":"false"; };
}
template<> inline template<>
std::string render_node::operator()<std::string>( inline std::string render_node::operator()<int>(const int& val) const {
const std::string& str) const return std::to_string(val);
{ }
return (m_flag == flag::escape_html)?html_escape(str):str;
} template<> inline
} std::string render_node::operator()<bool>(const bool& val) const {
return val?"true":"false";
}
template<>
inline std::string render_node::operator()<std::string>(
const std::string& val) const
{
return (m_flag == flag::escape_html)?html_escape(val):val;
}
}
} }

View file

@ -2,53 +2,57 @@
#include <boost/variant/static_visitor.hpp> #include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp> #include <boost/blank.hpp>
#include "render_context.hpp" #include "render_context.hpp"
#include "mstch/mstch.hpp" #include "mstch/mstch.hpp"
namespace mstch { namespace mstch {
namespace visitor { namespace visitor {
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,
flag p_flag = flag::none):
ctx(ctx), section(section), m_flag(p_flag)
{
}
template<class T> inline class render_section: public boost::static_visitor<std::string> {
std::string operator()(const T& t) const { public:
return render_context::push(ctx, t).render(section); enum class flag { none, keep_array };
} render_section(
private: render_context& ctx,
render_context& ctx; const template_type& section,
const template_type& section; flag p_flag = flag::none):
flag m_flag; ctx(ctx), section(section), m_flag(p_flag)
}; {
}
template<> inline template<class T>
std::string render_section::operator()<lambda>(const lambda& lam) const { inline std::string operator()(const T& t) const {
return ""; return render_context::push(ctx, t).render(section);
/*std::string section_str; }
for(auto& token: section)
section_str += token.raw();
return lam(section_str, [this](const std::string& str) {
return ctx.render(template_type{str});
});*/
}
template<> inline private:
std::string render_section::operator()<array>(const array& arr) const { render_context& ctx;
std::string out; const template_type& section;
if(m_flag == flag::keep_array) flag m_flag;
out += render_context::push(ctx, arr).render(section); };
else
for (auto& i: arr) template<>
out += boost::apply_visitor( inline std::string render_section::operator()<lambda>(const lambda& fun) const {
render_section(ctx, section, flag::keep_array), i); return "";
return out; /*std::string section_str;
} for(auto& token: section)
} section_str += token.raw();
return lam(section_str, [this](const std::string& str) {
return ctx.render(template_type{str});
});*/
}
template<>
inline std::string render_section::operator()<array>(const array& array) const {
std::string out;
if(m_flag == flag::keep_array)
out += render_context::push(ctx, array).render(section);
else
for (auto& item: array)
out += boost::apply_visitor(
render_section(ctx, section, flag::keep_array), item);
return out;
}
}
} }