Skip to content

Commit

Permalink
Fix Twig/Jinja: incorrect recognition of some special tokens like key…
Browse files Browse the repository at this point in the history
…words (#1949)

`Twig` and `Jinja` lexers share some variables as class variables.
Both classes define `keywords` method like this:

```
class Jinja
  def self.keywords
    @@Keywords ||= ...
  end
end

class Twig < Jinja
  def self.keywords
    @@Keywords ||= ...
  end
end
```

If `Twig.keywords` is called before `Jinja.keywords`, `@@keywords` is
set to Twig's and both classes return the same value. If not,
`@@keywords` is initialized to Jinja's.
  • Loading branch information
nsfisis authored May 4, 2023
1 parent aa1a124 commit 96b11b5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
20 changes: 10 additions & 10 deletions lib/rouge/lexers/jinja.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ class Jinja < TemplateLexer
'text/html+django', 'text/html+jinja'

def self.keywords
@@keywords ||= %w(as context do else extends from ignore missing
import include reversed recursive scoped
autoescape endautoescape block endblock call endcall
filter endfilter for endfor if endif macro endmacro
set endset trans endtrans with endwith without)
@keywords ||= %w(as context do else extends from ignore missing
import include reversed recursive scoped
autoescape endautoescape block endblock call endcall
filter endfilter for endfor if endif macro endmacro
set endset trans endtrans with endwith without)
end

def self.tests
@@tests ||= %w(callable defined divisibleby equalto escaped even iterable
lower mapping none number odd sameas sequence string
undefined upper)
@tests ||= %w(callable defined divisibleby equalto escaped even iterable
lower mapping none number odd sameas sequence string
undefined upper)
end

def self.pseudo_keywords
@@pseudo_keywords ||= %w(true false none True False None)
@pseudo_keywords ||= %w(true false none True False None)
end

def self.word_operators
@@word_operators ||= %w(is in and or not)
@word_operators ||= %w(is in and or not)
end

state :root do
Expand Down
18 changes: 9 additions & 9 deletions lib/rouge/lexers/twig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ class Twig < Jinja
mimetypes 'application/x-twig', 'text/html+twig'

def self.keywords
@@keywords ||= %w(as do extends flush from import include use else starts
ends with without autoescape endautoescape block
endblock embed endembed filter endfilter for endfor
if endif macro endmacro sandbox endsandbox set endset
spaceless endspaceless)
@keywords ||= %w(as do extends flush from import include use else starts
ends with without autoescape endautoescape block
endblock embed endembed filter endfilter for endfor
if endif macro endmacro sandbox endsandbox set endset
spaceless endspaceless)
end

def self.tests
@@tests ||= %w(constant defined divisibleby empty even iterable null odd
sameas)
@tests ||= %w(constant defined divisibleby empty even iterable null odd
sameas)
end

def self.pseudo_keywords
@@pseudo_keywords ||= %w(true false none)
@pseudo_keywords ||= %w(true false none)
end

def self.word_operators
@@word_operators ||= %w(b-and b-or b-xor is in and or not)
@word_operators ||= %w(b-and b-or b-xor is in and or not)
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/lexers/twig_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@
assert_guess mimetype: 'text/html+twig'
end
end

describe 'regression: PR #1949' do
it 'has different keyword set than its superclass' do
refute_equal Rouge::Lexers::Twig.keywords, Rouge::Lexers::Jinja.keywords
end

it 'has different operator set than its superclass' do
refute_equal Rouge::Lexers::Twig.word_operators, Rouge::Lexers::Jinja.word_operators
end
end
end

0 comments on commit 96b11b5

Please sign in to comment.