This commit is contained in:
Daniel Sipka 2015-04-10 12:56:08 +02:00
parent 3b4580186c
commit e31207b9cf
17 changed files with 160 additions and 93 deletions

View file

@ -7,7 +7,8 @@ namespace mstch {
std::string render( std::string render(
const std::string& tmplt, const std::string& tmplt,
const object& context, const object& context,
const std::map<std::string,std::string>& partials = std::map<std::string,std::string>()); const std::map<std::string,std::string>& partials =
std::map<std::string,std::string>());
} }
#endif // _MSTCH_H_ #endif // _MSTCH_H_

View file

@ -7,7 +7,8 @@
using namespace mstch; using namespace mstch;
std::string strip_whitespace(std::string tmplt) { std::string strip_whitespace(std::string tmplt) {
tmplt = std::regex_replace(tmplt, std::regex("\\{\\{![^\\}]*\\}\\}"), "{{!}}"); std::regex comment_match("\\{\\{![^\\}]*\\}\\}");
tmplt = std::regex_replace(tmplt, comment_match, "{{!}}");
std::ostringstream out; std::ostringstream out;
std::istringstream in(tmplt); std::istringstream in(tmplt);
std::string line; std::string line;
@ -29,5 +30,6 @@ std::string mstch::render(
const std::string& tmplt, const object& root_object, const std::string& tmplt, const object& root_object,
const std::map<std::string,std::string>& partials) const std::map<std::string,std::string>& partials)
{ {
return render_context(root_object, partials).render(strip_whitespace(tmplt)); return render_context(root_object, partials)
.render(strip_whitespace(tmplt));
} }

View file

@ -8,14 +8,18 @@ using namespace mstch;
const mstch::node render_context::null_node; const mstch::node render_context::null_node;
render_context::render_context(const mstch::object &object, const std::map<std::string,std::string>& partials): render_context::render_context(
const mstch::object &object,
const std::map<std::string,std::string>& partials):
partials(partials), partials(partials),
objects{object}, objects{object},
state(new state::outside_section) state(new state::outside_section)
{ {
} }
render_context::render_context(const mstch::object& object, const render_context& context): render_context::render_context(
const mstch::object& object,
const render_context& context):
partials(context.partials), partials(context.partials),
objects(context.objects), objects(context.objects),
state(new state::outside_section) state(new state::outside_section)
@ -23,10 +27,16 @@ render_context::render_context(const mstch::object& object, const render_context
objects.push_front(object); objects.push_front(object);
} }
const mstch::node& render_context::find_node(const std::string &token, const std::deque<object> &current_objects) { const mstch::node& render_context::find_node(
const std::string &token,
const std::deque<object> &current_objects)
{
if(token != "." && token.find('.') != std::string::npos) { if(token != "." && token.find('.') != std::string::npos) {
return find_node(token.substr(token.rfind('.') + 1), return find_node(
{boost::get<object>(find_node(token.substr(0, token.rfind('.')), current_objects))}); token.substr(token.rfind('.') + 1),
{boost::get<object>(find_node(
token.substr(0, token.rfind('.')),
current_objects))});
} else { } else {
for (auto& object: current_objects) for (auto& object: current_objects)
if (object.count(token) != 0) if (object.count(token) != 0)

View file

@ -12,18 +12,24 @@ namespace mstch {
class render_context { class render_context {
private: private:
static const mstch::node null_node; static const mstch::node null_node;
const mstch::node& find_node(const std::string& token, const std::deque<object>& current_objects); const mstch::node& find_node(
const std::string& token,
const std::deque<object>& current_objects);
const std::map<std::string,std::string>& partials; const std::map<std::string,std::string>& partials;
std::deque<mstch::object> objects; std::deque<mstch::object> objects;
std::unique_ptr<state::render_state> state; std::unique_ptr<state::render_state> state;
public: public:
render_context(const mstch::object& object, const std::map<std::string,std::string>& partials); render_context(
render_context(const mstch::object& object, const render_context& context); const mstch::object& object,
const std::map<std::string,std::string>& partials);
render_context(
const mstch::object& object, const render_context& context);
const mstch::node& get_node(const std::string& token); const mstch::node& get_node(const std::string& token);
std::string render(const std::string& tmplt); std::string render(const std::string& tmplt);
template<class T, class... Args> template<class T, class... Args>
void set_state(Args&&... args) { void set_state(Args&&... args) {
state = std::unique_ptr<state::render_state>(new T(std::forward<Args>(args)...)); state = std::unique_ptr<state::render_state>(
new T(std::forward<Args>(args)...));
} }
}; };
} }

View file

@ -7,36 +7,43 @@
using namespace mstch; using namespace mstch;
state::in_inverted_section::in_inverted_section(const std::string& section_name): state::in_inverted_section::in_inverted_section(
const std::string& section_name):
section_name(section_name), section_name(section_name),
skipped_openings(0) skipped_openings(0)
{ {
} }
std::string state::in_inverted_section::render(render_context& context, const token& token) { std::string state::in_inverted_section::render(
render_context& ctx,
const token& token)
{
switch(token.type()) { switch(token.type()) {
case token_type::section_close: case token_type::section_close:
if(token.content() == section_name && skipped_openings == 0) { if(token.content() == section_name && skipped_openings == 0) {
std::ostringstream out; std::ostringstream out;
if(boost::apply_visitor(visitor::is_node_empty(), context.get_node(section_name))) auto& section_node = ctx.get_node(section_name);
out << render_context(mstch::object{}, context).render(section_text.str()); if(boost::apply_visitor(visitor::is_node_empty(), section_node)) {
context.set_state<outside_section>(); auto empty = mstch::object{};
return out.str(); out << render_context(empty, ctx).render(section_text.str());
} else {
skipped_openings--;
section_text << token.raw();
} }
break; ctx.set_state<outside_section>();
case token_type::inverted_section_open: return out.str();
case token_type::section_open: } else {
skipped_openings++; skipped_openings--;
case token_type::text:
case token_type::variable:
case token_type::unescaped_variable:
case token_type::comment:
case token_type::partial:
section_text << token.raw(); section_text << token.raw();
break; }
break;
case token_type::inverted_section_open:
case token_type::section_open:
skipped_openings++;
case token_type::text:
case token_type::variable:
case token_type::unescaped_variable:
case token_type::comment:
case token_type::partial:
section_text << token.raw();
break;
} }
return ""; return "";
} }

View file

@ -6,14 +6,16 @@
namespace mstch { namespace mstch {
namespace state { namespace state {
class in_inverted_section : public render_state, public std::enable_shared_from_this<in_inverted_section> { class in_inverted_section: public render_state {
private: private:
const std::string section_name; const std::string section_name;
std::ostringstream section_text; std::ostringstream section_text;
int skipped_openings; int skipped_openings;
public: public:
in_inverted_section(const std::string &section_name); in_inverted_section(const std::string &section_name);
std::string render(render_context &context, const token &token) override; std::string render(
render_context &context,
const token &token) override;
}; };
} }
} }

View file

@ -5,34 +5,40 @@
using namespace mstch; using namespace mstch;
state::in_section::in_section(const std::string& section_name): section_name(section_name), skipped_openings(0) { state::in_section::in_section(const std::string& section_name):
section_name(section_name), skipped_openings(0)
{
} }
std::string state::in_section::render(render_context& context, const token& token) { std::string state::in_section::render(
render_context& ctx,
const token& token)
{
switch(token.type()) { switch(token.type()) {
case token_type::section_close: case token_type::section_close:
if(token.content() == section_name && skipped_openings == 0) { if(token.content() == section_name && skipped_openings == 0) {
auto section_node = context.get_node(section_name); auto section_node = ctx.get_node(section_name);
std::string out(""); std::string out("");
if (!boost::apply_visitor(visitor::is_node_empty(), section_node)) if (!boost::apply_visitor(visitor::is_node_empty(), section_node))
out = boost::apply_visitor(visitor::render_section(context, section_text.str()), section_node); out = boost::apply_visitor(visitor::render_section(
context.set_state<outside_section>(); ctx, section_text.str()), section_node);
return out; ctx.set_state<outside_section>();
} else { return out;
skipped_openings--; } else {
section_text << token.raw(); skipped_openings--;
}
break;
case token_type::inverted_section_open:
case token_type::section_open:
skipped_openings++;
case token_type::text:
case token_type::variable:
case token_type::unescaped_variable:
case token_type::comment:
case token_type::partial:
section_text << token.raw(); section_text << token.raw();
break; }
break;
case token_type::inverted_section_open:
case token_type::section_open:
skipped_openings++;
case token_type::text:
case token_type::variable:
case token_type::unescaped_variable:
case token_type::comment:
case token_type::partial:
section_text << token.raw();
break;
} }
return ""; return "";
} }

View file

@ -13,7 +13,9 @@ namespace mstch {
int skipped_openings; int skipped_openings;
public: public:
in_section(const std::string& section_name); in_section(const std::string& section_name);
std::string render(render_context& context, const token& token) override; std::string render(
render_context& context,
const token& token) override;
}; };
} }
} }

View file

@ -6,26 +6,30 @@
using namespace mstch; using namespace mstch;
std::string state::outside_section::render(render_context& context, const token& token) { std::string state::outside_section::render(
render_context& ctx,
const token& token)
{
switch(token.type()) { switch(token.type()) {
case token_type::section_open: case token_type::section_open:
context.set_state<in_section>(token.content()); ctx.set_state<in_section>(token.content());
break; break;
case token_type::inverted_section_open: case token_type::inverted_section_open:
context.set_state<in_inverted_section>(token.content()); ctx.set_state<in_inverted_section>(token.content());
break; break;
case token_type::variable: case token_type::variable:
case token_type::unescaped_variable: case token_type::unescaped_variable:
return boost::apply_visitor(visitor::render_node(token.type() == token_type::variable), return boost::apply_visitor(
context.get_node(token.content())); visitor::render_node(token.type() == token_type::variable),
case token_type::comment: break; ctx.get_node(token.content()));
case token_type::text: case token_type::comment: break;
return token.raw(); case token_type::text:
case token_type::partial: return token.raw();
break;//render_context(mstch::object{{".", i}}, context).render(section); case token_type::partial:
case token_type::section_close: break;//render_context(mstch::object{{".", i}}, ctx).render(section);
// TODO ERROR case token_type::section_close:
break; // TODO ERROR
break;
} }
return ""; return "";
} }

View file

@ -5,9 +5,11 @@
namespace mstch { namespace mstch {
namespace state { namespace state {
class outside_section : public render_state { class outside_section: public render_state {
public: public:
std::string render(render_context &context, const token &token) override; std::string render(
render_context &context,
const token &token) override;
}; };
} }
} }

View file

@ -10,7 +10,9 @@ namespace mstch {
class render_state { class render_state {
public: public:
virtual ~render_state() {} virtual ~render_state() {}
virtual std::string render(render_context& context, const token& token) = 0; virtual std::string render(
render_context& context,
const token& token) = 0;
}; };
} }
} }

View file

@ -6,7 +6,8 @@
using namespace mstch; using namespace mstch;
token::token(const std::string& raw_token): raw_val(raw_token) { token::token(const std::string& raw_token): raw_val(raw_token) {
if(std::regex_match(raw_token, std::regex("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}"))) { std::regex token_match("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}");
if(std::regex_match(raw_token, token_match)) {
std::string inside = raw_token.substr(2, raw_token.size() - 4); std::string inside = raw_token.substr(2, raw_token.size() - 4);
inside = trim(inside); inside = trim(inside);
if (inside.size() > 0 && inside.at(0) == '#') { if (inside.size() > 0 && inside.at(0) == '#') {
@ -24,7 +25,9 @@ token::token(const std::string& raw_token): raw_val(raw_token) {
} else if (inside.size() > 0 && inside.at(0) == '&') { } else if (inside.size() > 0 && inside.at(0) == '&') {
type_val = token_type::unescaped_variable; type_val = token_type::unescaped_variable;
content_val = inside.substr(1); content_val = inside.substr(1);
} else if (inside.size() > 0 && inside.at(0) == '{' && inside.at(inside.size() - 1) == '}') { } else if (inside.size() > 0 && inside.at(0) == '{' &&
inside.at(inside.size() - 1) == '}')
{
type_val = token_type::unescaped_variable; type_val = token_type::unescaped_variable;
content_val = inside.substr(1, inside.size() - 2); content_val = inside.substr(1, inside.size() - 2);
} else if (inside.size() > 0 && inside.at(0) == '!') { } else if (inside.size() > 0 && inside.at(0) == '!') {

View file

@ -5,7 +5,8 @@
namespace mstch { namespace mstch {
enum class token_type { enum class token_type {
text, variable, section_open, section_close, inverted_section_open, unescaped_variable, comment, partial text, variable, section_open, section_close, inverted_section_open,
unescaped_variable, comment, partial
}; };
class token { class token {

View file

@ -3,12 +3,18 @@
#include <algorithm> #include <algorithm>
std::string& mstch::ltrim(std::string& s) { std::string& mstch::ltrim(std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace)))); s.erase(s.begin(), std::find_if(
s.begin(),
s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return s; return s;
} }
std::string& mstch::rtrim(std::string& s) { std::string& mstch::rtrim(std::string& s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end()); s.erase(std::find_if(
s.rbegin(),
s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s; return s;
} }
@ -16,7 +22,11 @@ std::string& mstch::trim(std::string& s) {
return ltrim(rtrim(s)); return ltrim(rtrim(s));
} }
std::string mstch::replace_all(std::string str, const std::string& from, const std::string& to) { std::string mstch::replace_all(
std::string str,
const std::string& from,
const std::string& to)
{
size_t start_pos = 0; size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) { while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to); str.replace(start_pos, from.length(), to);

View file

@ -7,7 +7,10 @@ namespace mstch {
std::string& ltrim(std::string& s); std::string& ltrim(std::string& s);
std::string& rtrim(std::string& s); std::string& rtrim(std::string& s);
std::string& trim(std::string& s); std::string& trim(std::string& s);
std::string replace_all(std::string str, const std::string& from, const std::string& to); std::string replace_all(
std::string str,
const std::string& from,
const std::string& to);
std::string html_escape(std::string str); std::string html_escape(std::string str);
} }

View file

@ -3,7 +3,9 @@
using namespace mstch; using namespace mstch;
visitor::render_node::render_node(bool html_escaped): html_escaped(html_escaped) { visitor::render_node::render_node(bool html_escaped):
html_escaped(html_escaped)
{
} }
std::string visitor::render_node::operator()(const boost::blank& blank) const { std::string visitor::render_node::operator()(const boost::blank& blank) const {

View file

@ -3,13 +3,17 @@
using namespace mstch; using namespace mstch;
visitor::render_section::render_section(render_context& context, const std::string& section): visitor::render_section::render_section(
render_context& context,
const std::string& section):
context(context), context(context),
section(section) section(section)
{ {
} }
std::string visitor::render_section::operator()(const boost::blank& blank) const { std::string visitor::render_section::operator()(
const boost::blank& blank) const
{
return ""; return "";
} }