To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / vendor / gems / coderay-0.9.7 / lib / coderay / scanners / nitro_xhtml.rb @ 442:753f1380d6bc

History | View | Annotate | Download (2.79 KB)

1
module CodeRay
2
module Scanners
3

    
4
  load :html
5
  load :ruby
6

    
7
  # Nitro XHTML Scanner
8
  class NitroXHTML < Scanner
9

    
10
    include Streamable
11
    register_for :nitro_xhtml
12
    file_extension :xhtml
13
    title 'Nitro XHTML'
14

    
15
    KINDS_NOT_LOC = HTML::KINDS_NOT_LOC
16
    
17
    NITRO_RUBY_BLOCK = /
18
      <\?r
19
      (?>
20
        [^\?]*
21
        (?> \?(?!>) [^\?]* )*
22
      )
23
      (?: \?> )?
24
    |
25
      <ruby>
26
      (?>
27
        [^<]*
28
        (?> <(?!\/ruby>) [^<]* )*
29
      )
30
      (?: <\/ruby> )?
31
    |
32
      <%
33
      (?>
34
        [^%]*
35
        (?> %(?!>) [^%]* )*
36
      )
37
      (?: %> )?
38
    /mx
39

    
40
    NITRO_VALUE_BLOCK = /
41
      \#
42
      (?:
43
        \{
44
        [^{}]*
45
        (?>
46
          \{ [^}]* \}
47
          (?> [^{}]* )
48
        )*
49
        \}?
50
      | \| [^|]* \|?
51
      | \( [^)]* \)?
52
      | \[ [^\]]* \]?
53
      | \\ [^\\]* \\?
54
      )
55
    /x
56

    
57
    NITRO_ENTITY = /
58
      % (?: \#\d+ | \w+ ) ;
59
    /
60

    
61
    START_OF_RUBY = /
62
      (?=[<\#%])
63
      < (?: \?r | % | ruby> )
64
    | \# [{(|]
65
    | % (?: \#\d+ | \w+ ) ;
66
    /x
67

    
68
    CLOSING_PAREN = Hash.new do |h, p|
69
      h[p] = p
70
    end.update( {
71
      '(' => ')',
72
      '[' => ']',
73
      '{' => '}',
74
    } )
75

    
76
  private
77

    
78
    def setup
79
      @ruby_scanner = CodeRay.scanner :ruby, :tokens => @tokens, :keep_tokens => true
80
      @html_scanner = CodeRay.scanner :html, :tokens => @tokens, :keep_tokens => true, :keep_state => true
81
    end
82

    
83
    def reset_instance
84
      super
85
      @html_scanner.reset
86
    end
87

    
88
    def scan_tokens tokens, options
89

    
90
      until eos?
91

    
92
        if (match = scan_until(/(?=#{START_OF_RUBY})/o) || scan_until(/\z/)) and not match.empty?
93
          @html_scanner.tokenize match
94

    
95
        elsif match = scan(/#{NITRO_VALUE_BLOCK}/o)
96
          start_tag = match[0,2]
97
          delimiter = CLOSING_PAREN[start_tag[1,1]]
98
          end_tag = match[-1,1] == delimiter ? delimiter : ''
99
          tokens << [:open, :inline]
100
          tokens << [start_tag, :inline_delimiter]
101
          code = match[start_tag.size .. -1 - end_tag.size]
102
          @ruby_scanner.tokenize code
103
          tokens << [end_tag, :inline_delimiter] unless end_tag.empty?
104
          tokens << [:close, :inline]
105

    
106
        elsif match = scan(/#{NITRO_RUBY_BLOCK}/o)
107
          start_tag = '<?r'
108
          end_tag = match[-2,2] == '?>' ? '?>' : ''
109
          tokens << [:open, :inline]
110
          tokens << [start_tag, :inline_delimiter]
111
          code = match[start_tag.size .. -(end_tag.size)-1]
112
          @ruby_scanner.tokenize code
113
          tokens << [end_tag, :inline_delimiter] unless end_tag.empty?
114
          tokens << [:close, :inline]
115

    
116
        elsif entity = scan(/#{NITRO_ENTITY}/o)
117
          tokens << [entity, :entity]
118
        
119
        elsif scan(/%/)
120
          tokens << [matched, :error]
121

    
122
        else
123
          raise_inspect 'else-case reached!', tokens
124
          
125
        end
126

    
127
      end
128

    
129
      tokens
130

    
131
    end
132

    
133
  end
134

    
135
end
136
end