optimize
This commit is contained in:
parent
244477f0a0
commit
8d1336d7df
9 changed files with 47 additions and 55 deletions
|
@ -13,7 +13,7 @@ namespace mstch {
|
|||
// to a bool.
|
||||
class boolean {
|
||||
private:
|
||||
bool state;
|
||||
const bool state;
|
||||
public:
|
||||
boolean(bool b): state(b) {}
|
||||
operator bool() const { return state; }
|
||||
|
@ -21,9 +21,11 @@ namespace mstch {
|
|||
|
||||
using renderer = std::function<std::string(const std::string&)>;
|
||||
using string_lambda = std::function<std::string()>;
|
||||
using renderer_lambda = std::function<std::function<std::string(const std::string&,renderer)>()>;
|
||||
using renderer_lambda = std::function<
|
||||
std::function<std::string(const std::string&,renderer)>()>;
|
||||
using node = boost::make_recursive_variant<
|
||||
boost::blank, std::string, int, boolean, string_lambda, renderer_lambda,
|
||||
boost::blank, std::string, int, boolean,
|
||||
string_lambda, renderer_lambda,
|
||||
std::map<const std::string,boost::recursive_variant_>,
|
||||
std::vector<boost::recursive_variant_>>::type;
|
||||
using object = std::map<const std::string,node>;
|
||||
|
@ -31,7 +33,7 @@ namespace mstch {
|
|||
|
||||
std::string render(
|
||||
const std::string& tmplt,
|
||||
const object& context,
|
||||
const object& root,
|
||||
const std::map<std::string,std::string>& partials =
|
||||
std::map<std::string,std::string>());
|
||||
}
|
||||
|
|
|
@ -8,9 +8,8 @@ using namespace mstch;
|
|||
|
||||
std::string mstch::render(
|
||||
const std::string& tmplt,
|
||||
const object& root_object,
|
||||
const object& root,
|
||||
const std::map<std::string,std::string>& partials)
|
||||
{
|
||||
return render_context(root_object, partials)
|
||||
.render(strip_whitespace(tmplt));
|
||||
return render_context(root, partials).render(strip_whitespace(tmplt));
|
||||
}
|
||||
|
|
|
@ -11,6 +11,26 @@
|
|||
|
||||
namespace mstch {
|
||||
class render_context {
|
||||
public:
|
||||
class push {
|
||||
public:
|
||||
push(render_context& context, const mstch::object& obj = {});
|
||||
~push();
|
||||
std::string render(const std::string& tmplt);
|
||||
private:
|
||||
render_context& context;
|
||||
};
|
||||
render_context(
|
||||
const mstch::object& object,
|
||||
const std::map<std::string,std::string>& partials);
|
||||
const mstch::node& get_node(const std::string& token);
|
||||
std::string render(const std::string& tmplt);
|
||||
std::string render_partial(const std::string& partial_name);
|
||||
template<class T, class... Args>
|
||||
void set_state(Args&&... args) {
|
||||
state.top() = std::unique_ptr<state::render_state>(
|
||||
new T(std::forward<Args>(args)...));
|
||||
}
|
||||
private:
|
||||
static const mstch::node null_node;
|
||||
const mstch::node& find_node(
|
||||
|
@ -24,29 +44,7 @@ namespace mstch {
|
|||
state.push(std::unique_ptr<state::render_state>(
|
||||
new T(std::forward<Args>(args)...)));
|
||||
}
|
||||
public:
|
||||
class push {
|
||||
private:
|
||||
render_context& context;
|
||||
public:
|
||||
push(render_context& context, const mstch::object& obj = {});
|
||||
~push();
|
||||
std::string render(const std::string& tmplt);
|
||||
};
|
||||
render_context(
|
||||
const mstch::object& object,
|
||||
const std::map<std::string,std::string>& partials);
|
||||
const mstch::node& get_node(const std::string& token);
|
||||
std::string render(const std::string& tmplt);
|
||||
std::string render_partial(const std::string& partial_name);
|
||||
template<class T, class... Args>
|
||||
void set_state(Args&&... args) {
|
||||
state.top() = std::unique_ptr<state::render_state>(
|
||||
new T(std::forward<Args>(args)...));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif //_MSTCH_RENDER_CONTEXT_H_
|
||||
|
|
|
@ -7,14 +7,14 @@
|
|||
namespace mstch {
|
||||
namespace state {
|
||||
class in_inverted_section: public render_state {
|
||||
private:
|
||||
const std::string section_name;
|
||||
std::ostringstream section_text;
|
||||
int skipped_openings;
|
||||
public:
|
||||
in_inverted_section(const std::string §ion_name);
|
||||
std::string render(
|
||||
render_context &context, const token &token) override;
|
||||
private:
|
||||
const std::string section_name;
|
||||
std::ostringstream section_text;
|
||||
int skipped_openings;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,14 +7,14 @@
|
|||
namespace mstch {
|
||||
namespace state {
|
||||
class in_section: public render_state {
|
||||
private:
|
||||
const std::string section_name;
|
||||
std::ostringstream section_text;
|
||||
int skipped_openings;
|
||||
public:
|
||||
in_section(const std::string& section_name);
|
||||
std::string render(
|
||||
render_context& context, const token& token) override;
|
||||
private:
|
||||
const std::string section_name;
|
||||
std::ostringstream section_text;
|
||||
int skipped_openings;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
#include "token.hpp"
|
||||
|
||||
#include "utils.hpp"
|
||||
#include <boost/algorithm/string/trim.hpp>
|
||||
#include <regex>
|
||||
|
||||
|
@ -13,12 +11,10 @@ std::tuple<int,int,token::type> token::token_info(const std::string& inside) {
|
|||
case '/': return std::make_tuple(1, 0, type::section_close);
|
||||
case '&': return std::make_tuple(1, 0, type::unescaped_variable);
|
||||
case '#': return std::make_tuple(1, 0, type::section_open);
|
||||
case '!': return std::make_tuple(1, 0, type::comment);
|
||||
case '{':
|
||||
if (inside.at(inside.size() - 1) == '}')
|
||||
return std::make_tuple(1, 1, type::unescaped_variable);
|
||||
else
|
||||
return std::make_tuple(0, 0, type::variable);
|
||||
case '!': return std::make_tuple(1, 0, type::comment);
|
||||
default: return std::make_tuple(0, 0, type::variable);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,16 +10,15 @@ namespace mstch {
|
|||
text, variable, section_open, section_close, inverted_section_open,
|
||||
unescaped_variable, comment, partial
|
||||
};
|
||||
token(const std::string& raw_token);
|
||||
type token_type() const;
|
||||
std::string content() const;
|
||||
std::string raw() const;
|
||||
private:
|
||||
type type_val;
|
||||
std::string content_val;
|
||||
std::string raw_val;
|
||||
std::tuple<int,int,type> token_info(const std::string& inside);
|
||||
public:
|
||||
token(const std::string& raw_token);
|
||||
type token_type() const;
|
||||
std::string content() const;
|
||||
std::string raw() const;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -12,9 +12,6 @@ namespace mstch {
|
|||
class render_node: public boost::static_visitor<std::string> {
|
||||
public:
|
||||
enum class flag { escape_html };
|
||||
private:
|
||||
std::set<flag> flags;
|
||||
public:
|
||||
render_node(std::set<flag> flags = {});
|
||||
std::string operator()(const boost::blank& blank) const;
|
||||
std::string operator()(const int& i) const;
|
||||
|
@ -24,6 +21,8 @@ namespace mstch {
|
|||
std::string operator()(const object& obj) const;
|
||||
std::string operator()(const string_lambda& lambda) const;
|
||||
std::string operator()(const renderer_lambda& lambda) const;
|
||||
private:
|
||||
std::set<flag> flags;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,11 +13,6 @@ namespace mstch {
|
|||
class render_section: public boost::static_visitor<std::string> {
|
||||
public:
|
||||
enum class flag { keep_array };
|
||||
private:
|
||||
render_context& ctx;
|
||||
std::string section;
|
||||
std::set<flag> flags;
|
||||
public:
|
||||
render_section(
|
||||
render_context& ctx,
|
||||
const std::string& section,
|
||||
|
@ -30,6 +25,10 @@ namespace mstch {
|
|||
std::string operator()(const object& obj) const;
|
||||
std::string operator()(const string_lambda& lambda) const;
|
||||
std::string operator()(const renderer_lambda& lambda) const;
|
||||
private:
|
||||
render_context& ctx;
|
||||
std::string section;
|
||||
std::set<flag> flags;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue