Sha256: ef359ce22169e785b270dc0ca63df1981949dbc63c87fa32960925eec3983fb2

Contents?: true

Size: 1.99 KB

Versions: 25

Compression:

Stored size: 1.99 KB

Contents

module Rouge
  module Lexers
    class Handlebars < TemplateLexer
      desc 'the Handlebars and Mustache templating languages'
      tag 'handlebars'
      aliases 'hbs', 'mustache'
      filenames '*.handlebars', '*.hbs', '*.mustache'
      mimetypes 'text/x-handlebars', 'text/x-mustache'

      id = %r([\w$-]+)

      state :root do
        # escaped slashes
        rule(/\\{+/) { delegate parent }

        # block comments
        rule /{{!--/, 'Comment', :comment
        rule /{{!.*?}}/, 'Comment'

        rule /{{{?/ do
          token 'Keyword'
          push :stache
          push :open_sym
        end

        rule(/(.+?)(?=\\|{{)/m) { delegate parent }

        # if we get here, there's no more mustache tags, so we eat
        # the rest of the doc
        rule(/.+/m) { delegate parent }
      end

      state :comment do
        rule(/{{/) { token 'Comment'; push }
        rule(/}}/) { token 'Comment'; pop! }
        rule(/[^{}]+/m) { token 'Comment' }
        rule(/[{}]/) { token 'Comment' }
      end

      state :stache do
        rule /}}}?/, 'Keyword', :pop!
        rule /\s+/m, 'Text'
        rule /[=]/, 'Operator'
        rule /[\[\]]/, 'Punctuation'
        rule /[.](?=[}\s])/, 'Name.Variable'
        rule /[.][.]/, 'Name.Variable'
        rule %r([/.]), 'Punctuation'
        rule /"(\\.|.)*?"/, 'Literal.String.Double'
        rule /'(\\.|.)*?'/, 'Literal.String.Single'
        rule /\d+(?=}\s)/, 'Literal.Number'
        rule /(true|false)(?=[}\s])/, 'Keyword.Constant'
        rule /else(?=[}\s])/, 'Keyword'
        rule /this(?=[}\s])/, 'Name.Builtin.Pseudo'
        rule /@#{id}/, 'Name.Attribute'
        rule id, 'Name.Variable'
      end

      state :open_sym do
        rule %r([#/]) do
          token 'Keyword'
          pop!; push :block_name
        end

        rule /[>^&]/, 'Keyword'

        rule(//) { pop! }
      end

      state :block_name do
        rule /if(?=[}\s])/, 'Keyword'
        rule id, 'Name.Namespace', :pop!
        rule(//) { pop! }
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
rouge-0.4.0 lib/rouge/lexers/handlebars.rb
rouge-0.3.10 lib/rouge/lexers/handlebars.rb
rouge-0.3.9 lib/rouge/lexers/handlebars.rb
rouge-0.3.8 lib/rouge/lexers/handlebars.rb
rouge-0.3.7 lib/rouge/lexers/handlebars.rb
rouge-0.3.6 lib/rouge/lexers/handlebars.rb
rouge-0.3.5 lib/rouge/lexers/handlebars.rb
rouge-0.3.4 lib/rouge/lexers/handlebars.rb
rouge-0.3.3 lib/rouge/lexers/handlebars.rb
rouge-0.3.2 lib/rouge/lexers/handlebars.rb
rouge-0.3.1 lib/rouge/lexers/handlebars.rb
rouge-0.3.0 lib/rouge/lexers/handlebars.rb
rouge-0.2.15 lib/rouge/lexers/handlebars.rb
rouge-0.2.14 lib/rouge/lexers/handlebars.rb
rouge-0.2.13 lib/rouge/lexers/handlebars.rb
rouge-0.2.12 lib/rouge/lexers/handlebars.rb
rouge-0.2.11 lib/rouge/lexers/handlebars.rb
rouge-0.2.10 lib/rouge/lexers/handlebars.rb
rouge-0.2.9 lib/rouge/lexers/handlebars.rb
rouge-0.2.8 lib/rouge/lexers/handlebars.rb