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.
|
// to a bool.
|
||||||
class boolean {
|
class boolean {
|
||||||
private:
|
private:
|
||||||
bool state;
|
const bool state;
|
||||||
public:
|
public:
|
||||||
boolean(bool b): state(b) {}
|
boolean(bool b): state(b) {}
|
||||||
operator bool() const { return state; }
|
operator bool() const { return state; }
|
||||||
|
@ -21,9 +21,11 @@ namespace mstch {
|
||||||
|
|
||||||
using renderer = std::function<std::string(const std::string&)>;
|
using renderer = std::function<std::string(const std::string&)>;
|
||||||
using string_lambda = std::function<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<
|
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::map<const std::string,boost::recursive_variant_>,
|
||||||
std::vector<boost::recursive_variant_>>::type;
|
std::vector<boost::recursive_variant_>>::type;
|
||||||
using object = std::map<const std::string,node>;
|
using object = std::map<const std::string,node>;
|
||||||
|
@ -31,7 +33,7 @@ namespace mstch {
|
||||||
|
|
||||||
std::string render(
|
std::string render(
|
||||||
const std::string& tmplt,
|
const std::string& tmplt,
|
||||||
const object& context,
|
const object& 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>());
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,8 @@ using namespace mstch;
|
||||||
|
|
||||||
std::string mstch::render(
|
std::string mstch::render(
|
||||||
const std::string& tmplt,
|
const std::string& tmplt,
|
||||||
const object& root_object,
|
const object& root,
|
||||||
const std::map<std::string,std::string>& partials)
|
const std::map<std::string,std::string>& partials)
|
||||||
{
|
{
|
||||||
return render_context(root_object, partials)
|
return render_context(root, partials).render(strip_whitespace(tmplt));
|
||||||
.render(strip_whitespace(tmplt));
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,26 @@
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
class render_context {
|
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:
|
private:
|
||||||
static const mstch::node null_node;
|
static const mstch::node null_node;
|
||||||
const mstch::node& find_node(
|
const mstch::node& find_node(
|
||||||
|
@ -24,29 +44,7 @@ namespace mstch {
|
||||||
state.push(std::unique_ptr<state::render_state>(
|
state.push(std::unique_ptr<state::render_state>(
|
||||||
new T(std::forward<Args>(args)...)));
|
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_
|
#endif //_MSTCH_RENDER_CONTEXT_H_
|
||||||
|
|
|
@ -7,14 +7,14 @@
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
namespace state {
|
namespace state {
|
||||||
class in_inverted_section: public render_state {
|
class in_inverted_section: public render_state {
|
||||||
private:
|
|
||||||
const std::string section_name;
|
|
||||||
std::ostringstream section_text;
|
|
||||||
int skipped_openings;
|
|
||||||
public:
|
public:
|
||||||
in_inverted_section(const std::string §ion_name);
|
in_inverted_section(const std::string §ion_name);
|
||||||
std::string render(
|
std::string render(
|
||||||
render_context &context, const token &token) override;
|
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 mstch {
|
||||||
namespace state {
|
namespace state {
|
||||||
class in_section: public render_state {
|
class in_section: public render_state {
|
||||||
private:
|
|
||||||
const std::string section_name;
|
|
||||||
std::ostringstream section_text;
|
|
||||||
int skipped_openings;
|
|
||||||
public:
|
public:
|
||||||
in_section(const std::string& section_name);
|
in_section(const std::string& section_name);
|
||||||
std::string render(
|
std::string render(
|
||||||
render_context& context, const token& token) override;
|
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 "token.hpp"
|
||||||
|
|
||||||
#include "utils.hpp"
|
|
||||||
#include <boost/algorithm/string/trim.hpp>
|
#include <boost/algorithm/string/trim.hpp>
|
||||||
#include <regex>
|
#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::section_close);
|
||||||
case '&': return std::make_tuple(1, 0, type::unescaped_variable);
|
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::section_open);
|
||||||
|
case '!': return std::make_tuple(1, 0, type::comment);
|
||||||
case '{':
|
case '{':
|
||||||
if (inside.at(inside.size() - 1) == '}')
|
if (inside.at(inside.size() - 1) == '}')
|
||||||
return std::make_tuple(1, 1, type::unescaped_variable);
|
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);
|
default: return std::make_tuple(0, 0, type::variable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,16 +10,15 @@ 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& raw_token);
|
||||||
|
type token_type() const;
|
||||||
|
std::string content() const;
|
||||||
|
std::string raw() const;
|
||||||
private:
|
private:
|
||||||
type type_val;
|
type type_val;
|
||||||
std::string content_val;
|
std::string content_val;
|
||||||
std::string raw_val;
|
std::string raw_val;
|
||||||
std::tuple<int,int,type> token_info(const std::string& inside);
|
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> {
|
class render_node: public boost::static_visitor<std::string> {
|
||||||
public:
|
public:
|
||||||
enum class flag { escape_html };
|
enum class flag { escape_html };
|
||||||
private:
|
|
||||||
std::set<flag> flags;
|
|
||||||
public:
|
|
||||||
render_node(std::set<flag> flags = {});
|
render_node(std::set<flag> flags = {});
|
||||||
std::string operator()(const boost::blank& blank) const;
|
std::string operator()(const boost::blank& blank) const;
|
||||||
std::string operator()(const int& i) 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 object& obj) const;
|
||||||
std::string operator()(const string_lambda& lambda) const;
|
std::string operator()(const string_lambda& lambda) const;
|
||||||
std::string operator()(const renderer_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> {
|
class render_section: public boost::static_visitor<std::string> {
|
||||||
public:
|
public:
|
||||||
enum class flag { keep_array };
|
enum class flag { keep_array };
|
||||||
private:
|
|
||||||
render_context& ctx;
|
|
||||||
std::string section;
|
|
||||||
std::set<flag> flags;
|
|
||||||
public:
|
|
||||||
render_section(
|
render_section(
|
||||||
render_context& ctx,
|
render_context& ctx,
|
||||||
const std::string& section,
|
const std::string& section,
|
||||||
|
@ -30,6 +25,10 @@ namespace mstch {
|
||||||
std::string operator()(const object& obj) const;
|
std::string operator()(const object& obj) const;
|
||||||
std::string operator()(const string_lambda& lambda) const;
|
std::string operator()(const string_lambda& lambda) const;
|
||||||
std::string operator()(const renderer_lambda& lambda) const;
|
std::string operator()(const renderer_lambda& lambda) const;
|
||||||
|
private:
|
||||||
|
render_context& ctx;
|
||||||
|
std::string section;
|
||||||
|
std::set<flag> flags;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue