reformat
This commit is contained in:
parent
29980e299c
commit
f4dd438fcc
@ -11,8 +11,8 @@ std::string mstch::render(
|
|||||||
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,template_type> partial_templts;
|
std::map<std::string, template_type> partial_templts;
|
||||||
for(auto& partial: partials)
|
for (auto& partial: partials)
|
||||||
partial_templts.insert({partial.first, {partial.second}});
|
partial_templts.insert({partial.first, {partial.second}});
|
||||||
return render_context(root, partial_templts).render(tmplt);
|
return render_context(root, partial_templts).render(tmplt);
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include "visitor/has_token.hpp"
|
#include "visitor/has_token.hpp"
|
||||||
|
|
||||||
using namespace mstch;
|
using namespace mstch;
|
||||||
|
using namespace mstch::visitor;
|
||||||
|
|
||||||
const mstch::node render_context::null_node;
|
const mstch::node render_context::null_node;
|
||||||
|
|
||||||
@ -27,7 +28,7 @@ std::string render_context::push::render(const template_type& templt) {
|
|||||||
|
|
||||||
render_context::render_context(
|
render_context::render_context(
|
||||||
const mstch::node& node,
|
const mstch::node& node,
|
||||||
const std::map<std::string,template_type>& partials):
|
const std::map<std::string, template_type>& partials):
|
||||||
partials{partials},
|
partials{partials},
|
||||||
nodes{node}
|
nodes{node}
|
||||||
{
|
{
|
||||||
@ -44,9 +45,9 @@ const mstch::node& render_context::find_node(
|
|||||||
token.substr(token.rfind('.') + 1),
|
token.substr(token.rfind('.') + 1),
|
||||||
{find_node(token.substr(0, token.rfind('.')), current_nodes)});
|
{find_node(token.substr(0, token.rfind('.')), current_nodes)});
|
||||||
else
|
else
|
||||||
for (auto& i: current_nodes)
|
for (auto& node: current_nodes)
|
||||||
if (boost::apply_visitor(visitor::has_token(token), i))
|
if (visit(has_token(token), node))
|
||||||
return boost::apply_visitor(visitor::get_token(token, i), i);
|
return visit(get_token(token, node), node);
|
||||||
return null_node;
|
return null_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,8 @@
|
|||||||
#include "template_type.hpp"
|
#include "template_type.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
class render_context {
|
|
||||||
|
class render_context {
|
||||||
public:
|
public:
|
||||||
class push {
|
class push {
|
||||||
public:
|
public:
|
||||||
@ -20,24 +21,27 @@ namespace mstch {
|
|||||||
private:
|
private:
|
||||||
render_context& context;
|
render_context& context;
|
||||||
};
|
};
|
||||||
|
|
||||||
render_context(
|
render_context(
|
||||||
const mstch::node& node,
|
const mstch::node& node,
|
||||||
const std::map<std::string,template_type>& partials);
|
const std::map<std::string, template_type>& partials);
|
||||||
const mstch::node& get_node(const std::string& token);
|
const mstch::node& get_node(const std::string& token);
|
||||||
std::string render(const template_type& templt);
|
std::string render(const template_type& templt);
|
||||||
std::string render_partial(const std::string& partial_name);
|
std::string render_partial(const std::string& partial_name);
|
||||||
template<class T, class... Args>
|
template<class T, class... Args>
|
||||||
void set_state(Args&&... args) {
|
void set_state(Args&& ... args) {
|
||||||
state.top() = std::unique_ptr<state::render_state>(
|
state.top() = std::unique_ptr<state::render_state>(
|
||||||
new T(std::forward<Args>(args)...));
|
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(
|
||||||
const std::string& token,
|
const std::string& token,
|
||||||
const std::deque<node>& current_nodes);
|
const std::deque<node>& current_nodes);
|
||||||
const std::map<std::string,template_type>& partials;
|
const std::map<std::string, template_type>& partials;
|
||||||
std::deque<mstch::node> nodes;
|
std::deque<mstch::node> nodes;
|
||||||
std::stack<std::unique_ptr<state::render_state>> state;
|
std::stack<std::unique_ptr<state::render_state>> state;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
#include "outside_section.hpp"
|
#include "outside_section.hpp"
|
||||||
#include "visitor/is_node_empty.hpp"
|
#include "visitor/is_node_empty.hpp"
|
||||||
#include "visitor/render_section.hpp"
|
#include "visitor/render_section.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
using namespace mstch;
|
using namespace mstch;
|
||||||
|
using namespace mstch::visitor;
|
||||||
|
|
||||||
state::in_section::in_section(type type, const std::string& section_name):
|
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)
|
||||||
@ -11,21 +13,20 @@ 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& sn = ctx.get_node(section_name);
|
auto& node = ctx.get_node(section_name);
|
||||||
std::string out;
|
std::string out;
|
||||||
if(m_type == type::normal &&
|
if (m_type == type::normal && !visit(is_node_empty(), node))
|
||||||
!boost::apply_visitor(visitor::is_node_empty(), sn))
|
out = visit(render_section(ctx, section), node);
|
||||||
out = boost::apply_visitor(visitor::render_section(ctx, section), sn);
|
else if (m_type == type::inverted && visit(is_node_empty(), node))
|
||||||
else if(m_type == type::inverted &&
|
|
||||||
boost::apply_visitor(visitor::is_node_empty(), sn))
|
|
||||||
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;
|
||||||
|
@ -1,23 +1,28 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "render_state.hpp"
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "render_state.hpp"
|
||||||
#include "template_type.hpp"
|
#include "template_type.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
namespace state {
|
namespace state {
|
||||||
class in_section: public render_state {
|
|
||||||
|
class in_section: public render_state {
|
||||||
public:
|
public:
|
||||||
enum class type { inverted, normal };
|
enum class type {
|
||||||
in_section(type type, const std::string& section_name);
|
inverted, normal
|
||||||
std::string render(
|
};
|
||||||
render_context& context, const token& token) override;
|
in_section(type type, const std::string §ion_name);
|
||||||
|
std::string render(render_context &context, const token &token) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const type m_type;
|
const type m_type;
|
||||||
const std::string section_name;
|
const std::string section_name;
|
||||||
template_type section;
|
template_type section;
|
||||||
int skipped_openings;
|
int skipped_openings;
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
#include "visitor/render_node.hpp"
|
|
||||||
#include "outside_section.hpp"
|
#include "outside_section.hpp"
|
||||||
|
|
||||||
|
#include "visitor/render_node.hpp"
|
||||||
#include "in_section.hpp"
|
#include "in_section.hpp"
|
||||||
#include "render_context.hpp"
|
#include "render_context.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
using namespace mstch;
|
using namespace mstch;
|
||||||
|
using namespace mstch::visitor;
|
||||||
|
|
||||||
std::string state::outside_section::render(
|
std::string state::outside_section::render(
|
||||||
render_context& ctx, const token& token)
|
render_context& ctx, const token& token)
|
||||||
{
|
{
|
||||||
switch(token.token_type()) {
|
using flag = render_node::flag;
|
||||||
|
switch (token.token_type()) {
|
||||||
case token::type::section_open:
|
case token::type::section_open:
|
||||||
ctx.set_state<in_section>(in_section::type::normal, token.name());
|
ctx.set_state<in_section>(in_section::type::normal, token.name());
|
||||||
break;
|
break;
|
||||||
@ -16,18 +20,15 @@ std::string state::outside_section::render(
|
|||||||
ctx.set_state<in_section>(in_section::type::inverted, token.name());
|
ctx.set_state<in_section>(in_section::type::inverted, token.name());
|
||||||
break;
|
break;
|
||||||
case token::type::variable:
|
case token::type::variable:
|
||||||
return boost::apply_visitor(
|
return visit(render_node(flag::escape_html), ctx.get_node(token.name()));
|
||||||
visitor::render_node(visitor::render_node::flag::escape_html),
|
|
||||||
ctx.get_node(token.name()));
|
|
||||||
case token::type::unescaped_variable:
|
case token::type::unescaped_variable:
|
||||||
return boost::apply_visitor(
|
return visit(render_node(flag::none), ctx.get_node(token.name()));
|
||||||
visitor::render_node(visitor::render_node::flag::none),
|
|
||||||
ctx.get_node(token.name()));
|
|
||||||
case token::type::text:
|
case token::type::text:
|
||||||
return token.raw();
|
return token.raw();
|
||||||
case token::type::partial:
|
case token::type::partial:
|
||||||
return ctx.render_partial(token.name());
|
return ctx.render_partial(token.name());
|
||||||
default: break;
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,12 @@
|
|||||||
#include "render_state.hpp"
|
#include "render_state.hpp"
|
||||||
|
|
||||||
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(
|
std::string render(render_context& context, const token& token) override;
|
||||||
render_context& context, const token& token) override;
|
};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "token.hpp"
|
#include "token.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
class render_context;
|
|
||||||
namespace state {
|
class render_context;
|
||||||
class render_state {
|
|
||||||
|
namespace state {
|
||||||
|
|
||||||
|
class render_state {
|
||||||
public:
|
public:
|
||||||
virtual std::string render(
|
virtual std::string render(render_context& context, const token& token) = 0;
|
||||||
render_context& context, const token& token) = 0;
|
};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,28 +19,28 @@ void template_type::tokenize(const std::string& t) {
|
|||||||
pstate = parse_state::in_del_start;
|
pstate = parse_state::in_del_start;
|
||||||
tok_end = it;
|
tok_end = it;
|
||||||
del_pos = 1;
|
del_pos = 1;
|
||||||
} else if(*it == '\n') {
|
} else if (*it == '\n') {
|
||||||
tokens.push_back({{tok_start, it + 1}});
|
tokens.push_back({{tok_start, it + 1}});
|
||||||
tok_start = it + 1;
|
tok_start = it + 1;
|
||||||
}
|
}
|
||||||
} else if(pstate == parse_state::in_del_start) {
|
} else if (pstate == parse_state::in_del_start) {
|
||||||
if (*it == delim_start[del_pos] && ++del_pos == delim_start.size())
|
if (*it == delim_start[del_pos] && ++del_pos == delim_start.size())
|
||||||
pstate = parse_state::in_del;
|
pstate = parse_state::in_del;
|
||||||
else
|
else
|
||||||
pstate = parse_state::start;
|
pstate = parse_state::start;
|
||||||
} else if(pstate == parse_state::in_del) {
|
} else if (pstate == parse_state::in_del) {
|
||||||
if (*it == '{')
|
if (*it == '{')
|
||||||
pstate = parse_state::in_esccontent;
|
pstate = parse_state::in_esccontent;
|
||||||
else if (*it == delim_end[0] && (del_pos = 1))
|
else if (*it == delim_end[0] && (del_pos = 1))
|
||||||
pstate = parse_state::in_del_end;
|
pstate = parse_state::in_del_end;
|
||||||
else
|
else
|
||||||
pstate = parse_state::in_content;
|
pstate = parse_state::in_content;
|
||||||
} else if(pstate == parse_state::in_esccontent && *it == '}') {
|
} else if (pstate == parse_state::in_esccontent && *it == '}') {
|
||||||
pstate = parse_state::in_content;
|
pstate = parse_state::in_content;
|
||||||
} else if(pstate == parse_state::in_content && *it == delim_end[0]) {
|
} else if (pstate == parse_state::in_content && *it == delim_end[0]) {
|
||||||
pstate = parse_state::in_del_end;
|
pstate = parse_state::in_del_end;
|
||||||
del_pos = 1;
|
del_pos = 1;
|
||||||
} else if(pstate == parse_state::in_del_end) {
|
} else if (pstate == parse_state::in_del_end) {
|
||||||
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}});
|
||||||
|
@ -6,13 +6,15 @@
|
|||||||
#include "token.hpp"
|
#include "token.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
class template_type {
|
|
||||||
|
class template_type {
|
||||||
public:
|
public:
|
||||||
template_type() = default;
|
template_type() = default;
|
||||||
template_type(const std::string& str);
|
template_type(const std::string& str);
|
||||||
std::vector<token>::const_iterator begin() const{return tokens.begin();}
|
std::vector<token>::const_iterator begin() const { return tokens.begin(); }
|
||||||
std::vector<token>::const_iterator end() const{return tokens.end();}
|
std::vector<token>::const_iterator end() const { return tokens.end(); }
|
||||||
void operator<<(const token& token) { tokens.push_back(token); }
|
void operator<<(const token& token) { tokens.push_back(token); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum class parse_state {
|
enum class parse_state {
|
||||||
start, in_del_start, in_del, in_content, in_esccontent, in_del_end
|
start, in_del_start, in_del, in_content, in_esccontent, in_del_end
|
||||||
@ -20,5 +22,6 @@ namespace mstch {
|
|||||||
void tokenize(const std::string& str);
|
void tokenize(const std::string& str);
|
||||||
void strip_whitespace();
|
void strip_whitespace();
|
||||||
std::vector<token> tokens;
|
std::vector<token> tokens;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,17 +18,17 @@ token::type token::token_info(char c) {
|
|||||||
token::token(const std::string& str, std::size_t left, std::size_t 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(left != 0 && right != 0) {
|
if (left != 0 && right != 0) {
|
||||||
if(str[left] == '{' && str[str.size() - 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() + left + 1, str.end() - right),
|
m_name = {first_not_ws(str.begin() + left + 1, str.end() - right),
|
||||||
first_not_ws(str.rbegin() + 1 + right, str.rend() - left) + 1};
|
first_not_ws(str.rbegin() + 1 + right, str.rend() - left) + 1};
|
||||||
} else {
|
} else {
|
||||||
auto first = first_not_ws(str.begin() + left, str.end() - right);
|
auto c = first_not_ws(str.begin() + left, str.end() - right);
|
||||||
m_type = token_info(*first);
|
m_type = token_info(*c);
|
||||||
if(m_type != type::variable)
|
if (m_type != type::variable)
|
||||||
first = first_not_ws(first + 1, str.end() - right);
|
c = first_not_ws(c + 1, str.end() - right);
|
||||||
m_name = {first, first_not_ws(str.rbegin() + right, str.rend() - left) + 1};
|
m_name = {c, first_not_ws(str.rbegin() + right, str.rend() - left) + 1};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
m_type = type::text;
|
m_type = type::text;
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
class token {
|
|
||||||
|
class token {
|
||||||
public:
|
public:
|
||||||
enum class type {
|
enum class type {
|
||||||
text, variable, section_open, section_close, inverted_section_open,
|
text, variable, section_open, section_close, inverted_section_open,
|
||||||
@ -15,6 +16,7 @@ namespace mstch {
|
|||||||
const std::string& name() const { return m_name; };
|
const std::string& name() const { return m_name; };
|
||||||
bool eol() const { return m_eol; }
|
bool eol() const { return m_eol; }
|
||||||
bool ws_only() const { return m_ws_only; }
|
bool ws_only() const { return m_ws_only; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
type m_type;
|
type m_type;
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
@ -22,5 +24,6 @@ namespace mstch {
|
|||||||
bool m_eol;
|
bool m_eol;
|
||||||
bool m_ws_only;
|
bool m_ws_only;
|
||||||
type token_info(char c);
|
type token_info(char c);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,14 +3,14 @@
|
|||||||
#include <boost/algorithm/string/replace.hpp>
|
#include <boost/algorithm/string/replace.hpp>
|
||||||
|
|
||||||
mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end) {
|
mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end) {
|
||||||
for(auto it = begin; it != end; ++it)
|
for (auto it = begin; it != end; ++it)
|
||||||
if(*it != ' ') return it;
|
if (*it != ' ') return it;
|
||||||
return end;
|
return end;
|
||||||
}
|
}
|
||||||
|
|
||||||
mstch::citer mstch::first_not_ws(mstch::criter begin, mstch::criter end) {
|
mstch::citer mstch::first_not_ws(mstch::criter begin, mstch::criter end) {
|
||||||
for(auto rit = begin; rit != end; ++rit)
|
for (auto rit = begin; rit != end; ++rit)
|
||||||
if(*rit != ' ') return --(rit.base());
|
if (*rit != ' ') return --(rit.base());
|
||||||
return --(end.base());
|
return --(end.base());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,11 +1,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <boost/variant/apply_visitor.hpp>
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
using citer = std::string::const_iterator;
|
|
||||||
using criter = std::string::const_reverse_iterator;
|
using citer = std::string::const_iterator;
|
||||||
citer first_not_ws(citer begin, citer end);
|
using criter = std::string::const_reverse_iterator;
|
||||||
citer first_not_ws(criter begin, criter end);
|
|
||||||
std::string html_escape(std::string str);
|
citer first_not_ws(citer begin, citer end);
|
||||||
|
citer first_not_ws(criter begin, criter end);
|
||||||
|
std::string html_escape(std::string str);
|
||||||
|
|
||||||
|
template<class... Args>
|
||||||
|
auto visit(Args&& ... args) -> decltype(boost::apply_visitor(
|
||||||
|
std::forward<Args>(args)...))
|
||||||
|
{
|
||||||
|
return boost::apply_visitor(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#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 {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#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 {
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include "render_context.hpp"
|
#include "render_context.hpp"
|
||||||
#include "mstch/mstch.hpp"
|
#include "mstch/mstch.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
namespace mstch {
|
namespace mstch {
|
||||||
namespace visitor {
|
namespace visitor {
|
||||||
@ -49,8 +50,7 @@ inline std::string render_section::operator()<array>(const array& array) const {
|
|||||||
out += render_context::push(ctx, array).render(section);
|
out += render_context::push(ctx, array).render(section);
|
||||||
else
|
else
|
||||||
for (auto& item: array)
|
for (auto& item: array)
|
||||||
out += boost::apply_visitor(
|
out += visit(render_section(ctx, section, flag::keep_array), item);
|
||||||
render_section(ctx, section, flag::keep_array), item);
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,9 +9,9 @@ include_directories(
|
|||||||
add_executable(benchmark benchmark_main.cpp)
|
add_executable(benchmark benchmark_main.cpp)
|
||||||
target_link_libraries(benchmark mstch)
|
target_link_libraries(benchmark mstch)
|
||||||
|
|
||||||
add_executable(filetoheader filetoheader.cpp)
|
add_executable(headerize headerize.cpp)
|
||||||
target_link_libraries(filetoheader ${Boost_PROGRAM_OPTIONS_LIBRARY})
|
target_link_libraries(headerize ${Boost_PROGRAM_OPTIONS_LIBRARY})
|
||||||
set(filetoheader_exe ${CMAKE_CURRENT_BINARY_DIR}/filetoheader${CMAKE_EXECUTABLE_SUFFIX})
|
set(headerize_exe ${CMAKE_CURRENT_BINARY_DIR}/headerize${CMAKE_EXECUTABLE_SUFFIX})
|
||||||
|
|
||||||
file(GLOB data_files RELATIVE
|
file(GLOB data_files RELATIVE
|
||||||
"${CMAKE_SOURCE_DIR}/test/data"
|
"${CMAKE_SOURCE_DIR}/test/data"
|
||||||
@ -27,8 +27,7 @@ file(GLOB string_files RELATIVE
|
|||||||
"${CMAKE_SOURCE_DIR}/test/data"
|
"${CMAKE_SOURCE_DIR}/test/data"
|
||||||
"${CMAKE_SOURCE_DIR}/test/data/*.mustache"
|
"${CMAKE_SOURCE_DIR}/test/data/*.mustache"
|
||||||
"${CMAKE_SOURCE_DIR}/test/data/*.txt"
|
"${CMAKE_SOURCE_DIR}/test/data/*.txt"
|
||||||
"${CMAKE_SOURCE_DIR}/test/data/*.partial"
|
"${CMAKE_SOURCE_DIR}/test/data/*.partial")
|
||||||
)
|
|
||||||
|
|
||||||
foreach(string_file ${string_files})
|
foreach(string_file ${string_files})
|
||||||
list(APPEND genargs "-S${string_file}")
|
list(APPEND genargs "-S${string_file}")
|
||||||
@ -36,8 +35,8 @@ endforeach(string_file)
|
|||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp
|
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp
|
||||||
COMMAND ${filetoheader_exe} --output ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp --namespace mstchtest ${genargs}
|
COMMAND ${headerize_exe} --output ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp --namespace mstchtest ${genargs}
|
||||||
DEPENDS ${filetoheader_exe}
|
DEPENDS ${headerize_exe}
|
||||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test/data/)
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test/data/)
|
||||||
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp PROPERTIES GENERATED TRUE)
|
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp PROPERTIES GENERATED TRUE)
|
||||||
add_custom_target(test_data_hpp DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp)
|
add_custom_target(test_data_hpp DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/test_data.hpp)
|
||||||
|
@ -3,6 +3,11 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
unsigned long current_msec() {
|
||||||
|
return std::chrono::system_clock::now().time_since_epoch() /
|
||||||
|
std::chrono::milliseconds(1);
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::string comment_tmp{
|
std::string comment_tmp{
|
||||||
"<div class=\"comments\"><h3>{{header}}</h3><ul>"
|
"<div class=\"comments\"><h3>{{header}}</h3><ul>"
|
||||||
@ -20,20 +25,17 @@ int main() {
|
|||||||
}}
|
}}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<int> times;
|
std::vector<unsigned long> times;
|
||||||
for(int j = 0; j < 10; j++) {
|
for (int j = 0; j < 10; j++) {
|
||||||
unsigned long start =
|
unsigned long start = current_msec();
|
||||||
std::chrono::system_clock::now().time_since_epoch() /
|
for (int i = 0; i < 5000; i++)
|
||||||
std::chrono::milliseconds(1);
|
|
||||||
for(int i = 0; i < 5000; i++) {
|
|
||||||
mstch::render(comment_tmp, comment_view);
|
mstch::render(comment_tmp, comment_view);
|
||||||
}
|
times.push_back(current_msec() - start);
|
||||||
times.push_back((std::chrono::system_clock::now().time_since_epoch() /
|
|
||||||
std::chrono::milliseconds(1)) - start);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float avg = 0;
|
float avg = 0;
|
||||||
for(int i: times) avg += i;
|
for (auto i: times)
|
||||||
|
avg += i;
|
||||||
avg /= times.size();
|
avg /= times.size();
|
||||||
|
|
||||||
std::cout << avg << std::endl;
|
std::cout << avg << std::endl;
|
||||||
|
@ -1,82 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
#include <boost/algorithm/string/replace.hpp>
|
|
||||||
#include <boost/program_options/options_description.hpp>
|
|
||||||
#include <boost/program_options/variables_map.hpp>
|
|
||||||
#include <boost/program_options/parsers.hpp>
|
|
||||||
|
|
||||||
void wrap_code(std::istream& input, std::ostream& output) {
|
|
||||||
std::string line;
|
|
||||||
while (std::getline(input, line)) {
|
|
||||||
output << line;
|
|
||||||
if(!input.eof()) output << std::endl;
|
|
||||||
}
|
|
||||||
output << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wrap_string(std::istream& input, std::ostream& output, const std::string& variable_name) {
|
|
||||||
output << "const std::string " << variable_name << "{" << std::endl;;
|
|
||||||
std::string line;
|
|
||||||
while (std::getline(input, line)) {
|
|
||||||
boost::replace_all(line, "\\", "\\\\");
|
|
||||||
boost::replace_all(line, "\"", "\\\"");
|
|
||||||
output << " \"" << line;
|
|
||||||
if(!input.eof()) output << "\\n";
|
|
||||||
output << "\"" << std::endl;
|
|
||||||
}
|
|
||||||
output << "};" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
|
||||||
namespace po = boost::program_options;
|
|
||||||
|
|
||||||
po::options_description desc("Allowed options");
|
|
||||||
desc.add_options()
|
|
||||||
("help", "show help")
|
|
||||||
("output", po::value<std::string>(), "output file")
|
|
||||||
("namespace", po::value<std::string>(), "namespace to use")
|
|
||||||
("input-string,S", po::value<std::vector<std::string>>(), "files to parse as strings")
|
|
||||||
("input-code,C", po::value<std::vector<std::string>>(), "files to parse as code");
|
|
||||||
po::variables_map vm;
|
|
||||||
po::store(po::parse_command_line(argc, argv, desc), vm);
|
|
||||||
po::notify(vm);
|
|
||||||
|
|
||||||
if (vm.count("help")) {
|
|
||||||
std::cout << desc << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!vm.count("output")) {
|
|
||||||
std::cout << "Output file not set" << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ofstream output(vm["output"].as<std::string>(), std::ios::out);
|
|
||||||
|
|
||||||
if(vm.count("namespace"))
|
|
||||||
output << "namespace " << vm["namespace"].as<std::string>() << " {" << std::endl;
|
|
||||||
|
|
||||||
if(vm.count("input-string")) {
|
|
||||||
for (auto &string_filename: vm["input-string"].as<std::vector<std::string>>()) {
|
|
||||||
std::ifstream input(string_filename, std::ios::in);
|
|
||||||
wrap_string(input, output, boost::replace_all_copy(string_filename, ".", "_"));
|
|
||||||
input.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(vm.count("input-code")) {
|
|
||||||
for(auto& data_filename: vm["input-code"].as<std::vector<std::string>>()) {
|
|
||||||
std::ifstream input(data_filename, std::ios::in);
|
|
||||||
wrap_code(input, output);
|
|
||||||
input.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(vm.count("namespace"))
|
|
||||||
output << "}" << std::endl;
|
|
||||||
|
|
||||||
output.close();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
89
test/headerize.cpp
Normal file
89
test/headerize.cpp
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include <boost/algorithm/string/replace.hpp>
|
||||||
|
#include <boost/program_options/options_description.hpp>
|
||||||
|
#include <boost/program_options/variables_map.hpp>
|
||||||
|
#include <boost/program_options/parsers.hpp>
|
||||||
|
|
||||||
|
void wrap_code(std::istream& input, std::ostream& output) {
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(input, line)) {
|
||||||
|
output << line;
|
||||||
|
if (!input.eof())
|
||||||
|
output << std::endl;
|
||||||
|
}
|
||||||
|
output << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wrap_string(std::istream& input, std::ostream& output,
|
||||||
|
const std::string& variable_name)
|
||||||
|
{
|
||||||
|
output << "const std::string " << variable_name << "{" << std::endl;;
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(input, line)) {
|
||||||
|
boost::replace_all(line, "\\", "\\\\");
|
||||||
|
boost::replace_all(line, "\"", "\\\"");
|
||||||
|
output << " \"" << line;
|
||||||
|
if (!input.eof())
|
||||||
|
output << "\\n";
|
||||||
|
output << "\"" << std::endl;
|
||||||
|
}
|
||||||
|
output << "};" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
namespace po = boost::program_options;
|
||||||
|
|
||||||
|
po::options_description desc("Allowed options");
|
||||||
|
desc.add_options()
|
||||||
|
("help", "show help")
|
||||||
|
("output", po::value<std::string>(), "output file")
|
||||||
|
("namespace", po::value<std::string>(), "namespace to use")
|
||||||
|
("input-string,S", po::value<std::vector<std::string>>(),
|
||||||
|
"files to parse as strings")
|
||||||
|
("input-code,C", po::value<std::vector<std::string>>(),
|
||||||
|
"files to parse as code");
|
||||||
|
po::variables_map vm;
|
||||||
|
po::store(po::parse_command_line(argc, argv, desc), vm);
|
||||||
|
po::notify(vm);
|
||||||
|
|
||||||
|
if (vm.count("help")) {
|
||||||
|
std::cout << desc << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!vm.count("output")) {
|
||||||
|
std::cout << "Output file not set" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ofstream output(vm["output"].as<std::string>(), std::ios::out);
|
||||||
|
|
||||||
|
if (vm.count("namespace"))
|
||||||
|
output << "namespace " << vm["namespace"].as<std::string>() << " {" << std::endl;
|
||||||
|
|
||||||
|
if (vm.count("input-string")) {
|
||||||
|
for (auto& string_filename: vm["input-string"].as<std::vector<std::string>>()) {
|
||||||
|
std::ifstream input(string_filename, std::ios::in);
|
||||||
|
wrap_string(input, output,
|
||||||
|
boost::replace_all_copy(string_filename, ".", "_"));
|
||||||
|
input.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vm.count("input-code")) {
|
||||||
|
for (auto& data_filename: vm["input-code"].as<std::vector<std::string>>()) {
|
||||||
|
std::ifstream input(data_filename, std::ios::in);
|
||||||
|
wrap_code(input, output);
|
||||||
|
input.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vm.count("namespace"))
|
||||||
|
output << "}" << std::endl;
|
||||||
|
|
||||||
|
output.close();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user