Skip to content

Commit

Permalink
Add support for raw/verbatim blocks in Jinja/Twig
Browse files Browse the repository at this point in the history
Jinja provides a `raw` tag and Twig provides a `raw` tag in its
1.x version and a `verbatim` one in newer versions.

To make the code simple, since the Twig lexer extends from the Jinja
one, the latter holds the logic for both tags.
  • Loading branch information
robin850 committed Sep 23, 2018
1 parent c2daa8d commit 25febe9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/rouge/lexers/jinja.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def self.word_operators
end

state :root do
# Raw and verbatim blocks
open_raw_tags = %r{(\{\%)(\s+)(verbatim|raw)(\s+)(\%\})}
close_raw_tags = %r{(\{\%)(\s+)(endverbatim|endraw)(\s+)(\%\})}

rule /#{open_raw_tags}(.*?)#{close_raw_tags}/m do
groups Comment::Preproc, Text, Keyword, Text, Comment::Preproc,
Text,
Comment::Preproc, Text, Keyword, Text, Comment::Preproc
end

# Comments
rule /{#/, Comment, :comment

Expand Down
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' %}

0 comments on commit 25febe9

Please sign in to comment.