Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions include/minja/minja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2601,8 +2601,8 @@ class Parser {

auto text = text_token->text;
if (post_space == SpaceHandling::Strip) {
static std::regex trailing_space_regex(R"(\s+$)");
text = std::regex_replace(text, trailing_space_regex, "");
auto pos = text.find_last_not_of(" \t\n\r\f\v");
text.resize(pos == std::string::npos ? 0 : pos + 1);
} else if (options.lstrip_blocks && it != end) {
auto i = text.size();
while (i > 0 && (text[i - 1] == ' ' || text[i - 1] == '\t')) i--;
Expand All @@ -2611,8 +2611,7 @@ class Parser {
}
}
if (pre_space == SpaceHandling::Strip) {
static std::regex leading_space_regex(R"(^\s+)");
text = std::regex_replace(text, leading_space_regex, "");
text.erase(0, text.find_first_not_of(" \t\n\r\f\v"));
} else if (options.trim_blocks && (it - 1) != begin && !dynamic_cast<ExpressionTemplateToken*>((*(it - 2)).get())) {
if (!text.empty() && text[0] == '\n') {
text.erase(0, 1);
Expand Down
23 changes: 2 additions & 21 deletions tests/test-supported-template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ static void assert_equals(const T &expected, const T &actual){
}
}

#ifdef _WIN32
// Workaround for https://github.com/ochafik/minja/issues/16
// On Windows, C++ minja outputs fewer newlines than Python Jinja2 for certain templates.
// This function collapses consecutive blank lines to normalize comparison.
static std::string collapse_blank_lines(const std::string &s) {
static const std::regex blank_lines_regex("\n\n+");
return std::regex_replace(s, blank_lines_regex, "\n");
}
#endif

static std::string read_file(const std::string &path) {
std::ifstream fs(path, std::ios_base::binary);
if (!fs.is_open()) {
Expand Down Expand Up @@ -162,21 +152,12 @@ int main(int argc, char *argv[]) {
return 1;
}

#ifdef _WIN32
// On Windows, collapse blank lines for comparison due to known whitespace handling issues
auto expected_cmp = collapse_blank_lines(expected);
auto actual_cmp = collapse_blank_lines(actual);
#else
auto expected_cmp = expected;
auto actual_cmp = actual;
#endif

if (expected_cmp != actual_cmp) {
if (expected != actual) {
if (getenv("WRITE_GOLDENS")) {
write_file(golden_file, actual);
std::cerr << "Updated golden file: " << golden_file << "\n";
} else {
assert_equals(expected_cmp, actual_cmp);
assert_equals(expected, actual);
}
}

Expand Down
Loading