Skip to content

Commit

Permalink
Merge pull request #35 from mrozigor/mustache_fix
Browse files Browse the repository at this point in the history
#6 Fix mustache implementation after specification update.
  • Loading branch information
mrozigor authored Oct 30, 2020
2 parents e3cf473 + ad71f44 commit 689ba2d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
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

0 comments on commit 689ba2d

Please sign in to comment.