standalone partial whitespace prefix

This commit is contained in:
Daniel Sipka 2015-05-04 14:12:51 +02:00
parent b88a7888fe
commit 0afaf7b8a1
5 changed files with 34 additions and 10 deletions

View File

@ -48,13 +48,23 @@ const mstch::node& render_context::get_node(const std::string& token) {
return find_node(token, nodes); return find_node(token, nodes);
} }
std::string render_context::render(const template_type& templt) { std::string render_context::render(
const template_type& templt, const std::string& prefix)
{
std::string output; std::string output;
for (auto& token: templt) bool prev_eol = true;
for (auto& token: templt) {
if (prev_eol && prefix.length() != 0)
output += state.top()->render(*this, {prefix});
output += state.top()->render(*this, token); output += state.top()->render(*this, token);
prev_eol = token.eol();
}
return output; return output;
} }
std::string render_context::render_partial(const std::string& partial_name) { std::string render_context::render_partial(
return partials.count(partial_name) ? render(partials.at(partial_name)) : ""; const std::string& partial_name, const std::string& prefix)
{
return partials.count(partial_name) ?
render(partials.at(partial_name), prefix) : "";
} }

View File

@ -26,8 +26,10 @@ class 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(
std::string render_partial(const std::string& partial_name); const template_type& templt, const std::string& prefix = "");
std::string render_partial(
const std::string& partial_name, const std::string& prefix);
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<render_state>( state.top() = std::unique_ptr<render_state>(

View File

@ -24,7 +24,7 @@ std::string outside_section::render(
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(), token.partial_prefix());
default: default:
break; break;
} }

View File

@ -37,9 +37,10 @@ void template_type::tokenize(const std::string& tmp) {
cur_pos = close_pos + close.size(); cur_pos = close_pos + close.size();
tokens.push_back({{beg + open_pos, beg + close_pos + close.size()}, tokens.push_back({{beg + open_pos, beg + close_pos + close.size()},
open.size(), close.size()}); open.size(), close.size()});
if(cur_pos == tmp.size()) { if(cur_pos == tmp.size()) {
tokens.push_back({{""}}); tokens.push_back({{""}});
tokens.back().set_eol(true); tokens.back().eol(true);
} }
if (*(beg + open_pos + open.size()) == '=' && if (*(beg + open_pos + open.size()) == '=' &&
@ -72,10 +73,16 @@ void template_type::strip_whitespace() {
non_space = true; non_space = true;
if ((*it).eol()) { if ((*it).eol()) {
if (has_tag && !non_space) if (has_tag && !non_space) {
for (auto cur = line_begin; !(*cur).eol(); ++cur)
if ((*cur).token_type() == token::type::partial &&
cur != line_begin && (*(cur - 1)).ws_only())
(*cur).partial_prefix((*(cur - 1)).raw());
for (auto cur = line_begin; it != cur - 1; for (auto cur = line_begin; it != cur - 1;
cur = (*cur).ws_only() ? tokens.erase(cur) : cur + 1) cur = (*cur).ws_only() ? tokens.erase(cur) : cur + 1)
it = (*cur).eol() ? cur - 1 : it; it = (*cur).eol() ? cur - 1 : it;
}
non_space = has_tag = false; non_space = has_tag = false;
line_begin = it + 1; line_begin = it + 1;

View File

@ -14,14 +14,19 @@ class token {
type token_type() const { return m_type; }; type token_type() const { return m_type; };
const std::string& raw() const { return m_raw; }; const std::string& raw() const { return m_raw; };
const std::string& name() const { return m_name; }; const std::string& name() const { return m_name; };
const std::string& partial_prefix() const { return m_partial_prefix; };
void partial_prefix(const std::string& p_partial_prefix) {
m_partial_prefix = p_partial_prefix;
};
bool eol() const { return m_eol; } bool eol() const { return m_eol; }
void eol(bool eol) { m_eol = eol; }
bool ws_only() const { return m_ws_only; } bool ws_only() const { return m_ws_only; }
void set_eol(bool eol) { m_eol = eol; }
private: private:
type m_type; type m_type;
std::string m_name; std::string m_name;
std::string m_raw; std::string m_raw;
std::string m_partial_prefix;
bool m_eol; bool m_eol;
bool m_ws_only; bool m_ws_only;
type token_info(char c); type token_info(char c);