File: handlebars.rb

package info (click to toggle)
ruby-rugments 1.0.0~beta8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 820 kB
  • sloc: ruby: 10,293; makefile: 2
file content (77 lines) | stat: -rw-r--r-- 1,988 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module Rugments
  module Lexers
    class Handlebars < TemplateLexer
      title 'Handlebars'
      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 /"(\\.|.)*?"/, Str::Double
        rule /'(\\.|.)*?'/, Str::Single
        rule /\d+(?=}\s)/, Num
        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
          goto :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