standalone partial whitespace prefix
This commit is contained in:
parent
b88a7888fe
commit
0afaf7b8a1
@ -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) : "";
|
||||
}
|
||||
|
@ -26,8 +26,10 @@ class render_context {
|
||||
const mstch::node& node,
|
||||
const std::map<std::string, template_type>& 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<class T, class... Args>
|
||||
void set_state(Args&& ... args) {
|
||||
state.top() = std::unique_ptr<render_state>(
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user