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

Add support for raw/verbatim blocks in Jinja/Twig #1003

Merged
merged 4 commits into from
Jun 10, 2019
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
14 changes: 14 additions & 0 deletions lib/rouge/lexers/jinja.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def self.word_operators
end

state :statement do
rule /(raw|verbatim)(\s+)(\%\})/ do
groups Keyword, Text, Comment::Preproc
goto :raw
end

rule /(\w+\.?)/ do |m|
if self.class.keywords.include?(m[0])
groups Keyword
Expand All @@ -133,6 +138,15 @@ def self.word_operators

rule /\%\}/, Comment::Preproc, :pop!
end

state :raw do
rule %r{(\{\%)(\s+)(endverbatim|endraw)(\s+)(\%\})} do
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
groups Comment::Preproc, Text, Keyword, Text, Comment::Preproc
pop!
end

rule /(.+?)/m, Text
end
end
end
end
8 changes: 8 additions & 0 deletions spec/visual/samples/jinja
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ Hello {{ user.name|capitalize }} !
{# A comment on multiple lines
to hide a piece of code or whatever #}

{% raw %}
{% for item in seq %}
<li>{{ item }}</li>
{% endfor %}
{% endraw %}

{% raw %} One line {{ raw }} block {% endraw %}

{% include 'footer.html' %}
8 changes: 8 additions & 0 deletions spec/visual/samples/twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@
{% flush %}
{% endif %}

{% verbatim %}
{% for item in seq %}
<li>{{ item }}</li>
{% endfor %}
{% endverbatim %}

{% verbatim %} One line {{ verbatim }} block {% endverbatim %}

{% include 'footer.html' %}