rouge-ruby / rouge

A pure Ruby code highlighter that is compatible with Pygments
https://rouge.jneen.net/
Other
3.3k stars 732 forks source link

Fix Twig/Jinja: incorrect recognition of some special tokens like keywords #1949

Closed nsfisis closed 1 year ago

nsfisis commented 1 year ago

Bug

Twig and Jinja may return incorrect token types for some special tokens like keywords. It depends on loading order.

cf. https://github.com/rouge-ruby/rouge/pull/1939#issuecomment-1499007812

Cause

Twig and Jinja lexers share 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 methods return the same value. If not, @@keywords is initialized to Jinja's.

Solution

I changed class variables to instance variables. Other lexer classes define their keywords as instance variables.

tancnle commented 1 year ago

Thanks, @nsfisis. The change looks great to me 🚀