diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index a91f505..4737c01 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -1474,7 +1474,9 @@ struct LexerConfig { std::string expression_close {"}}"}; std::string expression_close_force_rstrip {"-}}"}; std::string comment_open {"{#"}; + std::string comment_open_force_lstrip {"{#-"}; std::string comment_close {"#}"}; + std::string comment_close_force_rstrip {"-#}"}; std::string open_chars {"#{"}; bool trim_blocks {false}; @@ -1503,6 +1505,9 @@ struct LexerConfig { if (open_chars.find(comment_open[0]) == std::string::npos) { open_chars += comment_open[0]; } + if (open_chars.find(comment_open_force_lstrip[0]) == std::string::npos) { + open_chars += comment_open_force_lstrip[0]; + } } }; @@ -1925,6 +1930,7 @@ class Lexer { StatementStartForceLstrip, StatementBody, CommentStart, + CommentStartForceLstrip, CommentBody, }; @@ -2140,7 +2146,7 @@ class Lexer { } if (pos < m_in.size()) { - char ch = m_in[pos]; + const char ch = m_in[pos]; if (ch == '\n') { pos += 1; } else if (ch == '\r') { @@ -2155,7 +2161,7 @@ class Lexer { static nonstd::string_view clear_final_line_if_whitespace(nonstd::string_view text) { nonstd::string_view result = text; while (!result.empty()) { - char ch = result.back(); + const char ch = result.back(); if (ch == ' ' || ch == '\t') { result.remove_suffix(1); } else if (ch == '\n' || ch == '\r') { @@ -2228,8 +2234,13 @@ public: must_lstrip = config.lstrip_blocks; } } else if (inja::string_view::starts_with(open_str, config.comment_open)) { - state = State::CommentStart; - must_lstrip = config.lstrip_blocks; + if (inja::string_view::starts_with(open_str, config.comment_open_force_lstrip)) { + state = State::CommentStartForceLstrip; + must_lstrip = true; + } else { + state = State::CommentStart; + must_lstrip = config.lstrip_blocks; + } } else if ((pos == 0 || m_in[pos - 1] == '\n') && inja::string_view::starts_with(open_str, config.line_statement)) { state = State::LineStart; } else { @@ -2282,6 +2293,11 @@ public: pos += config.comment_open.size(); return make_token(Token::Kind::CommentOpen); } + case State::CommentStartForceLstrip: { + state = State::CommentBody; + pos += config.comment_open_force_lstrip.size(); + return make_token(Token::Kind::CommentOpen); + } case State::ExpressionBody: return scan_body(config.expression_close, Token::Kind::ExpressionClose, config.expression_close_force_rstrip); case State::LineBody: @@ -2290,16 +2306,21 @@ public: return scan_body(config.statement_close, Token::Kind::StatementClose, config.statement_close_force_rstrip, config.trim_blocks); case State::CommentBody: { // fast-scan to comment close - size_t end = m_in.substr(pos).find(config.comment_close); + const size_t end = m_in.substr(pos).find(config.comment_close); if (end == nonstd::string_view::npos) { pos = m_in.size(); return make_token(Token::Kind::Eof); } + + // Check for trim pattern + const bool must_rstrip = inja::string_view::starts_with(m_in.substr(pos + end - 1), config.comment_close_force_rstrip); + // return the entire comment in the close token state = State::Text; pos += end + config.comment_close.size(); Token tok = make_token(Token::Kind::CommentClose); - if (config.trim_blocks) { + + if (must_rstrip || config.trim_blocks) { skip_whitespaces_and_first_newline(); } return tok; @@ -4208,7 +4229,9 @@ public: /// Sets the opener and closer for template comments void set_comment(const std::string &open, const std::string &close) { lexer_config.comment_open = open; + lexer_config.comment_open_force_lstrip = open + "-"; lexer_config.comment_close = close; + lexer_config.comment_close_force_rstrip = "-" + close; lexer_config.update_open_chars(); }