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>
namespace mstch {
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];
}
namespace internal {
bool has(const std::string name) const {
return methods.count(name);
}
protected:
template<class S>
void register_methods(S* s, std::map<std::string,N(S::*)()> methods) {
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;
};
}
template<class N>
class object_t {
public:
const N& at(const std::string& name) const {
cache[name] = (methods.at(name))();
return cache[name];
}
using renderer = std::function<std::string(const std::string&)>;
class lambda {
public:
lambda(std::function<std::string(const std::string&,renderer)> fun): fun(fun) {
}
bool has(const std::string name) const {
return methods.count(name);
}
std::string operator()(const std::string& text, renderer renderer) const {
return fun(text, renderer);
}
private:
std::function<std::string(const std::string&,renderer)> fun;
};
protected:
template<class S>
void register_methods(S* s, std::map<std::string,N(S::*)()> methods) {
for(auto& item: methods)
this->methods.insert({item.first, std::bind(item.second, s)});
}
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>;
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&)>;
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;
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) {
if(token.token_type() == token::type::section_close) {
if(token.name() == section_name && skipped_openings == 0) {
auto& node = ctx.get_node(section_name);
std::string out;
if(m_type == type::normal) {
if (!boost::apply_visitor(visitor::is_node_empty(), node))
out = boost::apply_visitor(
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--;
}
} else if(token.token_type() == token::type::inverted_section_open ||
token.token_type() == token::type::section_open)
{
skipped_openings++;
}
section << token;
return "";
if(token.token_type() == token::type::section_close)
if(token.name() == section_name && skipped_openings == 0) {
auto& sn = ctx.get_node(section_name);
std::string out;
if(m_type == type::normal &&
!boost::apply_visitor(visitor::is_node_empty(), sn))
out = boost::apply_visitor(visitor::render_section(ctx, section), sn);
else if(m_type == type::inverted &&
boost::apply_visitor(visitor::is_node_empty(), sn))
out = render_context::push(ctx).render(section);
ctx.set_state<outside_section>();
return out;
} else
skipped_openings--;
else if(token.token_type() == token::type::inverted_section_open ||
token.token_type() == token::type::section_open)
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()) {
pstate = parse_state::start;
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;
} else {
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)
{
if(skip_left != 0 && skip_right != 0) {
if(str[skip_left] == '{' && str[str.size() - skip_right - 1] == '}') {
if(left != 0 && right != 0) {
if(str[left] == '{' && str[str.size() - right - 1] == '}') {
m_type = type::unescaped_variable;
m_name = {first_not_ws(str.begin() + skip_left + 1, str.end() - skip_right),
first_not_ws(str.rbegin() + 1 + skip_right, str.rend() - skip_left) + 1};
m_name = {first_not_ws(str.begin() + left + 1, str.end() - right),
first_not_ws(str.rbegin() + 1 + right, str.rend() - left) + 1};
} 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);
if(m_type != type::variable)
first = first_not_ws(first + 1, str.end() - skip_right);
m_name = {first, first_not_ws(str.rbegin() + skip_right, str.rend() - skip_left) + 1};
first = first_not_ws(first + 1, str.end() - right);
m_name = {first, first_not_ws(str.rbegin() + right, str.rend() - left) + 1};
}
} else {
m_type = type::text;

View File

@ -9,7 +9,7 @@ namespace mstch {
text, variable, section_open, section_close, inverted_section_open,
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; };
const std::string& raw() const { return m_raw; };
const std::string& name() const { return m_name; };

View File

@ -5,33 +5,36 @@
#include "mstch/mstch.hpp"
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):
token(token), node(node)
{
}
namespace visitor {
template<class T> inline
const mstch::node& operator()(const T& t) const {
return node;
}
private:
const std::string& token;
const mstch::node& node;
};
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<> inline
const mstch::node& get_token::operator()<map>(const map& m) const {
return m.at(token);
}
template<class T>
inline const mstch::node& operator()(const T& t) const {
return node;
}
template<> inline
const mstch::node& get_token::operator()<std::shared_ptr<object>>(
const std::shared_ptr<object>& obj) const
{
return obj->at(token);
}
}
private:
const std::string& token;
const mstch::node& node;
};
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"
namespace mstch {
namespace visitor {
class has_token: public boost::static_visitor<bool> {
public:
has_token(const std::string& token): token(token) {
}
namespace visitor {
template<class T> inline
bool operator()(const T& t) const {
return token == ".";
}
private:
const std::string& token;
};
class has_token: public boost::static_visitor<bool> {
public:
has_token(const std::string& token): token(token) {
}
template<> inline
bool has_token::operator()<map>(const map& m) const {
return m.count(token) == 1;
}
template<class T>
inline bool operator()(const T& t) const {
return token == ".";
}
private:
const std::string& token;
};
template<> inline
bool has_token::operator()<std::shared_ptr<object>>(
const std::shared_ptr<object>& obj) const
{
return obj->has(token);
}
}
template<>
inline bool has_token::operator()<map>(const map& map) const {
return map.count(token) == 1;
}
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/blank.hpp>
#include "mstch/mstch.hpp"
namespace mstch {
namespace visitor {
class is_node_empty: public boost::static_visitor<bool> {
public:
template<class T> inline
bool operator()(const T& t) const {
return false;
}
};
namespace visitor {
template<> inline
bool is_node_empty::operator()<boost::blank>(
const boost::blank& blank) const
{
return true;
}
class is_node_empty: public boost::static_visitor<bool> {
public:
template<class T> inline
bool operator()(const T& t) const {
return false;
}
};
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;
}
}
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& val) const {
return val == 0;
}
template<>
inline bool is_node_empty::operator()<bool>(const bool& val) const {
return !val;
}
template<>
inline bool is_node_empty::operator()<std::string>(
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/blank.hpp>
#include "mstch/mstch.hpp"
#include "utils.hpp"
namespace mstch {
namespace visitor {
class render_node: public boost::static_visitor<std::string> {
public:
enum class flag {
none, escape_html
};
namespace visitor {
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
std::string operator()(const T& t) const {
return "";
}
private:
flag m_flag;
};
render_node(flag p_flag = flag::none): m_flag(p_flag) {
}
template<> inline
std::string render_node::operator()<int>(const int& i) const {
return std::to_string(i);
}
template<class T>
inline std::string operator()(const T& t) const {
return "";
}
template<> inline
std::string render_node::operator()<bool>(const bool& b) const {
return b?"true":"false";
}
private:
flag m_flag;
};
template<> inline
std::string render_node::operator()<std::string>(
const std::string& str) const
{
return (m_flag == flag::escape_html)?html_escape(str):str;
}
}
template<>
inline std::string render_node::operator()<int>(const int& val) const {
return std::to_string(val);
}
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/blank.hpp>
#include "render_context.hpp"
#include "mstch/mstch.hpp"
namespace mstch {
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)
{
}
namespace visitor {
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;
};
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<> inline
std::string render_section::operator()<lambda>(const lambda& lam) const {
return "";
/*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<class T>
inline std::string operator()(const T& t) const {
return render_context::push(ctx, t).render(section);
}
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;
}
}
private:
render_context& ctx;
const template_type& section;
flag m_flag;
};
template<>
inline std::string render_section::operator()<lambda>(const lambda& fun) const {
return "";
/*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;
}
}
}