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

View file

@ -11,28 +11,23 @@ state::in_section::in_section(type type, const std::string& section_name):
} }
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>(); ctx.set_state<outside_section>();
return out; return out;
} else { } else
skipped_openings--; skipped_openings--;
} else if(token.token_type() == token::type::inverted_section_open ||
} else if(token.token_type() == token::type::inverted_section_open ||
token.token_type() == token::type::section_open) token.token_type() == token::type::section_open)
{
skipped_openings++; skipped_openings++;
}
section << token; section << token;
return ""; 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&> {
class get_token: public boost::static_visitor<const mstch::node&> {
public: public:
get_token(const std::string& token, const mstch::node& node): get_token(const std::string& token, const mstch::node& node):
token(token), node(node) token(token), node(node)
{ {
} }
template<class T> inline template<class T>
const mstch::node& operator()(const T& t) const { inline const mstch::node& operator()(const T& t) const {
return node; return node;
} }
private: private:
const std::string& token; const std::string& token;
const mstch::node& node; const mstch::node& node;
}; };
template<> inline template<>
const mstch::node& get_token::operator()<map>(const map& m) const { inline const mstch::node& get_token::operator()<map>(const map& map) const {
return m.at(token); return map.at(token);
} }
template<> inline template<>
const mstch::node& get_token::operator()<std::shared_ptr<object>>( inline const mstch::node& get_token::operator()<std::shared_ptr<object>>(
const std::shared_ptr<object>& obj) const const std::shared_ptr<object>& object) const
{ {
return obj->at(token); 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> {
class has_token: public boost::static_visitor<bool> {
public: public:
has_token(const std::string& token): token(token) { has_token(const std::string& token): token(token) {
} }
template<class T> inline template<class T>
bool operator()(const T& t) const { inline bool operator()(const T& t) const {
return token == "."; return token == ".";
} }
private: private:
const std::string& token; const std::string& token;
}; };
template<> inline template<>
bool has_token::operator()<map>(const map& m) const { inline bool has_token::operator()<map>(const map& map) const {
return m.count(token) == 1; return map.count(token) == 1;
} }
template<> inline template<>
bool has_token::operator()<std::shared_ptr<object>>( inline bool has_token::operator()<std::shared_ptr<object>>(
const std::shared_ptr<object>& obj) const const std::shared_ptr<object>& object) const
{ {
return obj->has(token); 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> {
class is_node_empty: public boost::static_visitor<bool> {
public: public:
template<class T> inline template<class T> inline
bool operator()(const T& t) const { bool operator()(const T& t) const {
return false; return false;
} }
}; };
template<> inline template<>
bool is_node_empty::operator()<boost::blank>( inline bool is_node_empty::operator()<boost::blank>(
const boost::blank& blank) const const boost::blank& blank) const
{ {
return true; return true;
} }
template<> inline template<>
bool is_node_empty::operator()<int>(const int& i) const { inline bool is_node_empty::operator()<int>(const int& val) const {
return i == 0; return val == 0;
} }
template<> inline template<>
bool is_node_empty::operator()<bool>(const bool& b) const { inline bool is_node_empty::operator()<bool>(const bool& val) const {
return !b; return !val;
} }
template<> inline template<>
bool is_node_empty::operator()<std::string>( inline bool is_node_empty::operator()<std::string>(
const std::string& str) const const std::string& 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()<array>(const array& array) const {
return arr.size() == 0; return array.size() == 0;
} }
}
}
} }

View file

@ -2,12 +2,14 @@
#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> {
class render_node: public boost::static_visitor<std::string> {
public: public:
enum class flag { enum class flag {
none, escape_html none, escape_html
@ -16,29 +18,31 @@ namespace mstch {
render_node(flag p_flag = flag::none): m_flag(p_flag) { render_node(flag p_flag = flag::none): m_flag(p_flag) {
} }
template<class T> inline template<class T>
std::string operator()(const T& t) const { inline std::string operator()(const T& t) const {
return ""; return "";
} }
private: private:
flag m_flag; flag m_flag;
}; };
template<> inline template<>
std::string render_node::operator()<int>(const int& i) const { inline std::string render_node::operator()<int>(const int& val) const {
return std::to_string(i); return std::to_string(val);
} }
template<> inline template<> inline
std::string render_node::operator()<bool>(const bool& b) const { std::string render_node::operator()<bool>(const bool& val) const {
return b?"true":"false"; return val?"true":"false";
} }
template<> inline template<>
std::string render_node::operator()<std::string>( inline std::string render_node::operator()<std::string>(
const std::string& str) const const std::string& val) const
{ {
return (m_flag == flag::escape_html)?html_escape(str):str; return (m_flag == flag::escape_html)?html_escape(val):val;
} }
}
}
} }

View file

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