simplify code
This commit is contained in:
parent
c6325f5581
commit
4f35bcae25
12 changed files with 35 additions and 65 deletions
|
@ -84,7 +84,7 @@ class lambda {
|
||||||
};
|
};
|
||||||
|
|
||||||
using node = boost::make_recursive_variant<
|
using node = boost::make_recursive_variant<
|
||||||
boost::blank, std::string, int, bool, lambda,
|
std::nullptr_t, std::string, int, bool, lambda,
|
||||||
std::shared_ptr<internal::object_t<boost::recursive_variant_>>,
|
std::shared_ptr<internal::object_t<boost::recursive_variant_>>,
|
||||||
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;
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
#include "mstch/mstch.hpp"
|
#include "mstch/mstch.hpp"
|
||||||
#include "render_context.hpp"
|
#include "render_context.hpp"
|
||||||
#include "utils.hpp"
|
|
||||||
|
|
||||||
using namespace mstch;
|
using namespace mstch;
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
#include "render_context.hpp"
|
#include "render_context.hpp"
|
||||||
#include "utils.hpp"
|
|
||||||
#include "state/outside_section.hpp"
|
#include "state/outside_section.hpp"
|
||||||
#include "visitor/get_token.hpp"
|
#include "visitor/get_token.hpp"
|
||||||
#include "visitor/has_token.hpp"
|
|
||||||
|
|
||||||
using namespace mstch;
|
using namespace mstch;
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#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;
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,7 @@ namespace mstch {
|
||||||
|
|
||||||
class in_section: public render_state {
|
class in_section: public render_state {
|
||||||
public:
|
public:
|
||||||
enum class type {
|
enum class type { inverted, normal };
|
||||||
inverted, normal
|
|
||||||
};
|
|
||||||
in_section(type type, const std::string §ion_name);
|
in_section(type type, const std::string §ion_name);
|
||||||
std::string render(render_context &context, const token &token) override;
|
std::string render(render_context &context, const token &token) override;
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
#include "visitor/render_node.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;
|
||||||
|
|
||||||
|
|
|
@ -19,21 +19,20 @@ void template_type::process_text(citer begin, citer end) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void template_type::tokenize(const std::string& tmplt) {
|
void template_type::tokenize(const std::string& tmplt) {
|
||||||
std::string open{"{{"}, close{"}}"};
|
std::string o{"{{"}, c{"}}"};
|
||||||
citer beg = tmplt.begin();
|
citer beg = tmplt.begin();
|
||||||
for (unsigned long pos = 0; pos < tmplt.size();) {
|
for (unsigned long pos = 0; pos < tmplt.size();) {
|
||||||
auto to = tmplt.find(open, pos);
|
auto to = tmplt.find(o, pos);
|
||||||
auto tc = tmplt.find(close, (to == std::string::npos)?to:(to + 1));
|
auto tc = tmplt.find(c, (to == std::string::npos)?to:(to + 1));
|
||||||
if (tc != std::string::npos && to != std::string::npos) {
|
if (tc != std::string::npos && to != std::string::npos) {
|
||||||
if (*(beg + to + open.size()) == '{' && *(beg + tc + close.size()) == '}')
|
if (*(beg + to + o.size()) == '{' && *(beg + tc + c.size()) == '}')
|
||||||
++tc;
|
++tc;
|
||||||
process_text(beg + pos, beg + to);
|
process_text(beg + pos, beg + to);
|
||||||
pos = tc + close.size();
|
pos = tc + c.size();
|
||||||
tokens.push_back({{beg + to, beg + tc + close.size()},
|
tokens.push_back({{beg + to, beg + tc + c.size()}, o.size(), c.size()});
|
||||||
open.size(), close.size()});
|
if (*(beg + to + o.size()) == '=' && *(beg + tc - 1) == '=') {
|
||||||
if (*(beg + to + open.size()) == '=' && *(beg + tc - 1) == '=') {
|
o = {beg + to + o.size() + 1, beg + tmplt.find(' ', to)};
|
||||||
open = {beg + to + open.size() + 1, beg + tmplt.find(' ', to)};
|
c = {beg + tmplt.find(' ', to) + 1, beg + tc - 1};
|
||||||
close = {beg + tmplt.find(' ', to) + 1, beg + tc - 1};
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
process_text(beg + pos, tmplt.end());
|
process_text(beg + pos, tmplt.end());
|
||||||
|
@ -43,7 +42,7 @@ void template_type::tokenize(const std::string& tmplt) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void template_type::strip_whitespace() {
|
void template_type::strip_whitespace() {
|
||||||
auto line_begin = tokens.begin();
|
auto lbegin = tokens.begin();
|
||||||
bool has_tag = false, non_space = false;
|
bool has_tag = false, non_space = false;
|
||||||
for (auto it = tokens.begin(); it != tokens.end(); ++it) {
|
for (auto it = tokens.begin(); it != tokens.end(); ++it) {
|
||||||
auto type = (*it).token_type();
|
auto type = (*it).token_type();
|
||||||
|
@ -53,17 +52,11 @@ void template_type::strip_whitespace() {
|
||||||
else if (!(*it).ws_only())
|
else if (!(*it).ws_only())
|
||||||
non_space = true;
|
non_space = true;
|
||||||
if ((*it).eol()) {
|
if ((*it).eol()) {
|
||||||
if (has_tag && !non_space) {
|
if (has_tag && !non_space)
|
||||||
auto line_it = line_begin;
|
for (auto c = lbegin; it != c-1; c = (*c).ws_only()?tokens.erase(c):++c)
|
||||||
for (; !(*line_it).eol(); ++line_it)
|
it = (*c).eol()?c-1:it;
|
||||||
if ((*line_it).ws_only())
|
|
||||||
line_it = tokens.erase(line_it);
|
|
||||||
if ((*line_it).ws_only())
|
|
||||||
line_it = tokens.erase(line_it);
|
|
||||||
it = line_it - 1;
|
|
||||||
}
|
|
||||||
non_space = has_tag = false;
|
non_space = has_tag = false;
|
||||||
line_begin = it + 1;
|
lbegin = it + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <boost/variant/static_visitor.hpp>
|
#include <boost/variant/static_visitor.hpp>
|
||||||
#include <boost/blank.hpp>
|
|
||||||
|
|
||||||
#include "mstch/mstch.hpp"
|
#include "mstch/mstch.hpp"
|
||||||
#include "has_token.hpp"
|
#include "has_token.hpp"
|
||||||
|
@ -26,12 +25,12 @@ class get_token: public boost::static_visitor<const mstch::node&> {
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline const mstch::node& get_token::operator()<map>(const map& map) const {
|
inline const mstch::node& get_token::operator()(const map& map) const {
|
||||||
return map.at(token);
|
return map.at(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline const mstch::node& get_token::operator()<std::shared_ptr<object>>(
|
inline const mstch::node& get_token::operator()(
|
||||||
const std::shared_ptr<object>& object) const
|
const std::shared_ptr<object>& object) const
|
||||||
{
|
{
|
||||||
return object->at(token);
|
return object->at(token);
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <boost/variant/static_visitor.hpp>
|
#include <boost/variant/static_visitor.hpp>
|
||||||
#include <boost/blank.hpp>
|
|
||||||
|
|
||||||
#include "mstch/mstch.hpp"
|
#include "mstch/mstch.hpp"
|
||||||
|
|
||||||
|
@ -22,14 +21,12 @@ class has_token: public boost::static_visitor<bool> {
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool has_token::operator()<map>(const map& map) const {
|
inline bool has_token::operator()(const map& map) const {
|
||||||
return map.count(token) == 1;
|
return map.count(token) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool has_token::operator()<std::shared_ptr<object>>(
|
inline bool has_token::operator()(const std::shared_ptr<object>& object) const {
|
||||||
const std::shared_ptr<object>& object) const
|
|
||||||
{
|
|
||||||
return object->has(token);
|
return object->has(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <boost/variant/static_visitor.hpp>
|
#include <boost/variant/static_visitor.hpp>
|
||||||
#include <boost/blank.hpp>
|
|
||||||
|
|
||||||
#include "mstch/mstch.hpp"
|
#include "mstch/mstch.hpp"
|
||||||
|
|
||||||
|
@ -9,38 +8,34 @@ namespace mstch {
|
||||||
|
|
||||||
class is_node_empty: public boost::static_visitor<bool> {
|
class is_node_empty: public boost::static_visitor<bool> {
|
||||||
public:
|
public:
|
||||||
template<class T> inline
|
template<class T>
|
||||||
bool operator()(const T& t) const {
|
inline bool operator()(const T& t) const {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool is_node_empty::operator()<boost::blank>(
|
inline bool is_node_empty::operator()(const std::nullptr_t& nul) const {
|
||||||
const boost::blank& blank) const
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool is_node_empty::operator()<int>(const int& value) const {
|
inline bool is_node_empty::operator()(const int& value) const {
|
||||||
return value == 0;
|
return value == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool is_node_empty::operator()<bool>(const bool& value) const {
|
inline bool is_node_empty::operator()(const bool& value) const {
|
||||||
return !value;
|
return !value;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool is_node_empty::operator()<std::string>(
|
inline bool is_node_empty::operator()(const std::string& value) const {
|
||||||
const std::string& value) const
|
|
||||||
{
|
|
||||||
return value == "";
|
return value == "";
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool is_node_empty::operator()<array>(const array& array) const {
|
inline bool is_node_empty::operator()(const array& array) const {
|
||||||
return array.size() == 0;
|
return array.size() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <boost/variant/static_visitor.hpp>
|
#include <boost/variant/static_visitor.hpp>
|
||||||
#include <boost/blank.hpp>
|
|
||||||
|
|
||||||
#include "mstch/mstch.hpp"
|
#include "mstch/mstch.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
@ -10,10 +9,7 @@ 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 {
|
enum class flag { none, escape_html };
|
||||||
none, escape_html
|
|
||||||
};
|
|
||||||
|
|
||||||
render_node(flag p_flag = flag::none): m_flag(p_flag) {
|
render_node(flag p_flag = flag::none): m_flag(p_flag) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,24 +23,22 @@ class render_node: public boost::static_visitor<std::string> {
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline std::string render_node::operator()<int>(const int& value) const {
|
inline std::string render_node::operator()(const int& value) const {
|
||||||
return std::to_string(value);
|
return std::to_string(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline std::string render_node::operator()<bool>(const bool& value) const {
|
inline std::string render_node::operator()(const bool& value) const {
|
||||||
return value?"true":"false";
|
return value?"true":"false";
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline std::string render_node::operator()<lambda>(const lambda& value) const {
|
inline std::string render_node::operator()(const lambda& value) const {
|
||||||
return (m_flag == flag::escape_html)?html_escape(value()):value();
|
return (m_flag == flag::escape_html)?html_escape(value()):value();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline std::string render_node::operator()<std::string>(
|
inline std::string render_node::operator()(const std::string& value) const {
|
||||||
const std::string& value) const
|
|
||||||
{
|
|
||||||
return (m_flag == flag::escape_html)?html_escape(value):value;
|
return (m_flag == flag::escape_html)?html_escape(value):value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <boost/variant/static_visitor.hpp>
|
#include <boost/variant/static_visitor.hpp>
|
||||||
#include <boost/blank.hpp>
|
|
||||||
|
|
||||||
#include "render_context.hpp"
|
#include "render_context.hpp"
|
||||||
#include "mstch/mstch.hpp"
|
#include "mstch/mstch.hpp"
|
||||||
|
@ -32,7 +31,7 @@ class render_section: public boost::static_visitor<std::string> {
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline std::string render_section::operator()<lambda>(const lambda& fun) const {
|
inline std::string render_section::operator()(const lambda& fun) const {
|
||||||
std::string section_str;
|
std::string section_str;
|
||||||
for(auto& token: section)
|
for(auto& token: section)
|
||||||
section_str += token.raw();
|
section_str += token.raw();
|
||||||
|
@ -42,10 +41,10 @@ inline std::string render_section::operator()<lambda>(const lambda& fun) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline std::string render_section::operator()<array>(const array& array) const {
|
inline std::string render_section::operator()(const array& array) const {
|
||||||
std::string out;
|
std::string out;
|
||||||
if(m_flag == flag::keep_array)
|
if(m_flag == flag::keep_array)
|
||||||
out += render_context::push(ctx, array).render(section);
|
return render_context::push(ctx, array).render(section);
|
||||||
else
|
else
|
||||||
for (auto& item: array)
|
for (auto& item: array)
|
||||||
out += visit(render_section(ctx, section, flag::keep_array), item);
|
out += visit(render_section(ctx, section, flag::keep_array), item);
|
||||||
|
|
Loading…
Reference in a new issue