Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 Fix mustache implementation after specification update. #35

Merged
merged 2 commits into from
Oct 30, 2020
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Crow is a C++ microframework for web. (inspired by Python Flask)

[![Travis Build](https://travis-ci.org/mrozigor/crow.svg?branch=master)](https://travis-ci.org/mrozigor/crow)
[![Build Status](https://travis-ci.com/mrozigor/crow.svg?branch=master)](https://travis-ci.com/mrozigor/crow)
[![Coverage Status](https://coveralls.io/repos/github/mrozigor/crow/badge.svg?branch=master)](https://coveralls.io/github/mrozigor/crow?branch=master)
[![Gitter](https://badges.gitter.im/crowfork/community.svg)](https://gitter.im/crowfork/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

Expand Down
37 changes: 33 additions & 4 deletions include/crow/mustache.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ namespace crow
{
return body_.substr(action.start, action.end - action.start);
}
auto find_context(const std::string& name, const std::vector<context*>& stack)->std::pair<bool, context&>
auto find_context(const std::string& name, const std::vector<context*>& stack, bool shouldUseOnlyFirstStackValue = false)->std::pair<bool, context&>
{
if (name == ".")
{
return {true, *stack.back()};
}
static json::wvalue empty_str;
empty_str = "";

int dotPosition = name.find(".");
if (dotPosition == (int)name.npos)
{
Expand Down Expand Up @@ -110,6 +113,9 @@ namespace crow
}
else
{
if (shouldUseOnlyFirstStackValue) {
return {false, empty_str};
}
found = false;
break;
}
Expand All @@ -120,8 +126,6 @@ namespace crow

}

static json::wvalue empty_str;
empty_str = "";
return {false, empty_str};
}

Expand All @@ -143,6 +147,27 @@ namespace crow
}
}

bool isTagInsideObjectBlock(const int& current, const std::vector<context*>& stack)
{
int openedBlock = 0;
int totalBlocksBefore = 0;
for (int i = current; i > 0; --i) {
++totalBlocksBefore;
auto& action = actions_[i - 1];

if (action.t == ActionType::OpenBlock) {
if (openedBlock == 0 && (*stack.rbegin())->t() == json::type::Object) {
return true;
}
--openedBlock;
} else if (action.t == ActionType::CloseBlock) {
++openedBlock;
}
}

return false;
}

void render_internal(int actionBegin, int actionEnd, std::vector<context*>& stack, std::string& out, int indent)
{
int current = actionBegin;
Expand Down Expand Up @@ -171,7 +196,11 @@ namespace crow
case ActionType::UnescapeTag:
case ActionType::Tag:
{
auto optional_ctx = find_context(tag_name(action), stack);
bool shouldUseOnlyFirstStackValue = false;
if (isTagInsideObjectBlock(current, stack)) {
shouldUseOnlyFirstStackValue = true;
}
auto optional_ctx = find_context(tag_name(action), stack, shouldUseOnlyFirstStackValue);
auto& ctx = optional_ctx.second;
switch(ctx.t())
{
Expand Down
3 changes: 0 additions & 3 deletions tests/template/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
for test in testdoc["tests"]:
if "lambda" in test["data"]:
continue
if "Dotted Names - Context Precedence" in test["name"]:
# Temporarily disabled - fix
continue

open('data', 'w').write(json.dumps(test["data"]))
open('template', 'w').write(test["template"])
Expand Down