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

Implement raw/endraw #148

Merged
merged 2 commits into from
Oct 5, 2019
Merged

Implement raw/endraw #148

merged 2 commits into from
Oct 5, 2019

Conversation

morenol
Copy link
Contributor

@morenol morenol commented Oct 3, 2019

Solves #146

"noname.j2tpl:1:3: error: Unexpected token: '<<End of block>>'\n{{}}\n--^-------"},
InputOutputPair{"{% raw %}{% raw %}{{ x }{% endraw %}{% endraw %}",
"noname.j2tpl:1:37: error: Unexpected raw block end\n{% raw %}{% raw %}{{ x }{% endraw %}{% endraw %}\n ---^-------"},
// FIXME: InputOutputPair{"{% raw %}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not sure how to handle this case?

How should I solve this? @flexferrum

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the first endraw should close the first raw block and the second endraw should be unexpected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is ok. I saw that in this page https://jbmoelker.github.io/jinja-compat-tests/tags/raw/

That case I think is working properly.

The case that I am not sure how to handle is this one:

{% raw %} I mean, the error when the regex_iterator arrives to the EOF and the RawBlock has not been closed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case it's necessary to return ExpectedRawEnd error but points to the end of the {% raw %} tag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I mean, I have to modify the test InputOutputPair values, but the main error here is that this error is not being thrown because I am not sure where I should return ExpectedRawEnd.

Maybe you could suggest me where I should look at.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I suppose, you can do it at this line:

FinishCurrentBlock(m_template->size());

At this point you can check that the current block is finished or it is raw text. Otherwise you can return an error depends on the unclosed block type.

@@ -56,7 +56,7 @@ struct ParserTraits<char> : public ParserTraitsBase<>
{
static std::regex GetRoughTokenizer()
{
return std::regex(R"((\{\{)|(\}\})|(\{%)|(%\})|(\{#)|(#\})|(\n))");
return std::regex(R"((\{\{)|(\}\})|(\{%[\+\-]?\s+raw\s+[\+\-]?%\})|(\{%[\+\-]?\s+endraw\s+[\+\-]?%\})|(\{%)|(%\})|(\{#)|(#\})|(\n))");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should do right trimming for raw and left trimming for endraw. Need to check original jinja implementation.
Also I think we can use UNIVERSAL_STR here and get rid of copy/paste of the regexp string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me now and I change that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just checked. We should do trimming. So the current implementation is correct.

break;
else if (m_currentBlockInfo.type != TextBlockType::RawText && m_currentBlockInfo.type != TextBlockType::Comment)
{
FinishCurrentLine(match.position() + 12);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No sure we can use here the length as a constant. raw/endraw (according to regex) can contain spaces between the keyword and %}


size_t StripBlockRight(TextBlockInfo& currentBlockInfo, size_t position)
{
bool doTrim = m_settings.trimBlocks && (m_currentBlockInfo.type == TextBlockType::Statement || m_currentBlockInfo.type == TextBlockType::RawBlock );
Copy link
Collaborator

@flexferrum flexferrum Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TextBlockType::RawBlock )

Please, use the clang-format with the provided format file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I use that format, the file has a lot of changes, is that ok?

There are changes in the include order, the position of the curly brackets, ++ var changes to ++var, and the indentation inside the switches.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I see. I'll do it by myself.

@codecov
Copy link

codecov bot commented Oct 3, 2019

Codecov Report

Merging #148 into master will increase coverage by 0.14%.
The diff coverage is 96.92%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #148      +/-   ##
==========================================
+ Coverage      88%   88.14%   +0.14%     
==========================================
  Files          65       65              
  Lines        8163     8269     +106     
==========================================
+ Hits         7184     7289     +105     
- Misses        979      980       +1
Impacted Files Coverage Δ
src/lexer.h 94.52% <ø> (ø) ⬆️
test/errors_test.cpp 99.21% <ø> (ø) ⬆️
include/jinja2cpp/error_info.h 96.29% <ø> (ø) ⬆️
test/statements_tets.cpp 100% <ø> (ø) ⬆️
src/error_info.cpp 58.33% <100%> (+1.98%) ⬆️
src/template_parser.h 91.72% <96.61%> (+0.55%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bcda100...85158f1. Read the comment docs.

Copy link
Collaborator

@flexferrum flexferrum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

@morenol
Copy link
Contributor Author

morenol commented Oct 4, 2019

The only pending thing is the UNIVERSAL_STR for the regex

@flexferrum
Copy link
Collaborator

The only pending thing is the UNIVERSAL_STR for the regex

Don't mind. :)

@morenol
Copy link
Contributor Author

morenol commented Oct 4, 2019

Ok, then I only will squash the three last commits

@flexferrum
Copy link
Collaborator

Ok, then I only will squash the three last commits

I would do it during the merge. :)

@flexferrum flexferrum merged commit a6d544c into jinja2cpp:master Oct 5, 2019
@morenol morenol deleted the lmm/raw branch October 5, 2019 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants