From 0afaf7b8a11cb8ed96cdbe1fc7072014e914ec9b Mon Sep 17 00:00:00 2001 From: Daniel Sipka Date: Mon, 4 May 2015 14:12:51 +0200 Subject: [PATCH] standalone partial whitespace prefix --- src/render_context.cpp | 18 ++++++++++++++---- src/render_context.hpp | 6 ++++-- src/state/outside_section.cpp | 2 +- src/template_type.cpp | 11 +++++++++-- src/token.hpp | 7 ++++++- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/render_context.cpp b/src/render_context.cpp index 9a0d4cd..18d7b99 100644 --- a/src/render_context.cpp +++ b/src/render_context.cpp @@ -48,13 +48,23 @@ const mstch::node& render_context::get_node(const std::string& token) { 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; - 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); + prev_eol = token.eol(); + } return output; } -std::string render_context::render_partial(const std::string& partial_name) { - return partials.count(partial_name) ? render(partials.at(partial_name)) : ""; +std::string render_context::render_partial( + const std::string& partial_name, const std::string& prefix) +{ + return partials.count(partial_name) ? + render(partials.at(partial_name), prefix) : ""; } diff --git a/src/render_context.hpp b/src/render_context.hpp index af2e9db..b8039c3 100644 --- a/src/render_context.hpp +++ b/src/render_context.hpp @@ -26,8 +26,10 @@ class render_context { const mstch::node& node, const std::map& partials); const mstch::node& get_node(const std::string& token); - std::string render(const template_type& templt); - std::string render_partial(const std::string& partial_name); + std::string render( + const template_type& templt, const std::string& prefix = ""); + std::string render_partial( + const std::string& partial_name, const std::string& prefix); template void set_state(Args&& ... args) { state.top() = std::unique_ptr( diff --git a/src/state/outside_section.cpp b/src/state/outside_section.cpp index 8c8ed93..c6080e1 100644 --- a/src/state/outside_section.cpp +++ b/src/state/outside_section.cpp @@ -24,7 +24,7 @@ std::string outside_section::render( case token::type::text: return token.raw(); case token::type::partial: - return ctx.render_partial(token.name()); + return ctx.render_partial(token.name(), token.partial_prefix()); default: break; } diff --git a/src/template_type.cpp b/src/template_type.cpp index 803a924..53f6e81 100644 --- a/src/template_type.cpp +++ b/src/template_type.cpp @@ -37,9 +37,10 @@ void template_type::tokenize(const std::string& tmp) { cur_pos = close_pos + close.size(); tokens.push_back({{beg + open_pos, beg + close_pos + close.size()}, open.size(), close.size()}); + if(cur_pos == tmp.size()) { tokens.push_back({{""}}); - tokens.back().set_eol(true); + tokens.back().eol(true); } if (*(beg + open_pos + open.size()) == '=' && @@ -72,10 +73,16 @@ void template_type::strip_whitespace() { non_space = true; 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; cur = (*cur).ws_only() ? tokens.erase(cur) : cur + 1) it = (*cur).eol() ? cur - 1 : it; + } non_space = has_tag = false; line_begin = it + 1; diff --git a/src/token.hpp b/src/token.hpp index ef88074..4ee4ddf 100644 --- a/src/token.hpp +++ b/src/token.hpp @@ -14,14 +14,19 @@ class token { type token_type() const { return m_type; }; const std::string& raw() const { return m_raw; }; 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; } + void eol(bool eol) { m_eol = eol; } bool ws_only() const { return m_ws_only; } - void set_eol(bool eol) { m_eol = eol; } private: type m_type; std::string m_name; std::string m_raw; + std::string m_partial_prefix; bool m_eol; bool m_ws_only; type token_info(char c);