rouge 1.5.1 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a2656f650042e999c8fd16e15a9d440ea6f2bd11
4
- data.tar.gz: dcaba0c54bd59cdcdd6d7cc97fcddc97d2ea9aca
3
+ metadata.gz: 5d3e680acd54b6dc00269cb0a68721463fa898e6
4
+ data.tar.gz: 704addb88b523c80da72a59db3999e01ac502282
5
5
  SHA512:
6
- metadata.gz: d415e5ccd9ebd832db005db83307a700820cb28dea57e17c69ae69a947e9075d5e68809c61f74f5c9b7ef1c1a6b168dc8d62ae2fb10dd1d34df42bddf219f069
7
- data.tar.gz: 9db1da0a9913088718d1c0d4759c7923fad6defa487980f713ce135a08fb2f6b2fd77147b900d1a52cee1419f1ebb34d750d57737e7aa0a1aa9e89d2ce882061
6
+ metadata.gz: d5ac2591c06af1cc03516c314af662998dce6de22cf97be2b021f72e9aaa897c3662e01085c97cb806dfcea3737c7ca7d899dc194969438250eb3af4daa44367
7
+ data.tar.gz: 031632f8628d96c61f4ad3ce771f32676a0021a00cfa35c7521d3849085669b43a7a1cf5174efcf7fd148310a0ba84f03e66fa14fcce558e9268d429f6043a2c
@@ -0,0 +1,17 @@
1
+ doctype html
2
+ html
3
+ body
4
+ h1 Markup examples
5
+ #content
6
+ p
7
+ | Slim can have #{ruby_code} interpolated!
8
+ /[if IE]
9
+ javascript:
10
+ alert('Slim supports embedded javascript!')
11
+
12
+ - unless items.empty?
13
+ table
14
+ - for item in items do
15
+ tr
16
+ td.name = item.name
17
+ td.price = item.price
@@ -274,7 +274,7 @@ module Rouge
274
274
  end
275
275
 
276
276
  # path
277
- rule /#p?"(\\.|[^"])*"/i
277
+ rule /#p?"(\\.|[^"])*"/i, Str::Symbol
278
278
 
279
279
  # reference
280
280
  rule /#\d+[=#]/, Operator
@@ -205,11 +205,18 @@ module Rouge
205
205
  end
206
206
 
207
207
  state :interpolation do
208
- rule /(#\{)(#{dot}*?)(\})/ do |m|
209
- token Str::Interpol, m[1]
210
- delegate ruby, m[2]
211
- token Str::Interpol, m[3]
212
- end
208
+ rule /#[{]/, Str::Interpol, :ruby
209
+ end
210
+
211
+ state :ruby do
212
+ rule /[}]/, Str::Interpol, :pop!
213
+ mixin :ruby_inner
214
+ end
215
+
216
+ state :ruby_inner do
217
+ rule(/[{]/) { delegate ruby; push :ruby_inner }
218
+ rule(/[}]/) { delegate ruby; pop! }
219
+ rule(/[^{}]+/) { delegate ruby }
213
220
  end
214
221
 
215
222
  state :indented_block do
@@ -20,30 +20,61 @@ module Rouge
20
20
  state :comments_and_whitespace do
21
21
  rule /\s+/, Text
22
22
  rule /<!--/, Comment # really...?
23
- rule %r(//.*?\n), Comment::Single
23
+ rule %r(//.*?$), Comment::Single
24
24
  rule %r(/\*.*?\*/)m, Comment::Multiline
25
25
  end
26
26
 
27
- state :slash_starts_regex do
27
+ state :expr_start do
28
28
  mixin :comments_and_whitespace
29
29
 
30
- rule %r(
31
- / # opening slash
32
- ( \\. # escape sequences
33
- | [^/\\\n] # regular characters
34
- | \[ (\\. | [^\]\\\n])* \] # character classes
35
- )+
36
- / # closing slash
37
- (?:[gim]+\b|\B) # flags
38
- )x, Str::Regex, :pop!
39
-
40
- # if it's not matched by the above r.e., it's not
41
- # a valid expression, so we use :bad_regex to eat until the
42
- # end of the line.
43
- rule %r(/), Str::Regex, :bad_regex
30
+ rule %r(/) do
31
+ token Str::Regex
32
+ goto :regex
33
+ end
34
+
35
+ rule /[{]/, Punctuation, :object
36
+
44
37
  rule //, Text, :pop!
45
38
  end
46
39
 
40
+ state :regex do
41
+ rule %r(/) do
42
+ token Str::Regex
43
+ goto :regex_end
44
+ end
45
+
46
+ rule %r([^/]\n), Error, :pop!
47
+
48
+ rule /\n/, Error, :pop!
49
+ rule /\[\^/, Str::Escape, :regex_group
50
+ rule /\[/, Str::Escape, :regex_group
51
+ rule /\\./, Str::Escape
52
+ rule %r{[(][?][:=<!]}, Str::Escape
53
+ rule /[{][\d,]+[}]/, Str::Escape
54
+ rule /[()?]/, Str::Escape
55
+ rule /./, Str::Regex
56
+ end
57
+
58
+ state :regex_end do
59
+ rule /[gim]+/, Str::Regex, :pop!
60
+ rule(//) { pop! }
61
+ end
62
+
63
+ state :regex_group do
64
+ # specially highlight / in a group to indicate that it doesn't
65
+ # close the regex
66
+ rule /\//, Str::Escape
67
+
68
+ rule %r([^/]\n) do
69
+ token Error
70
+ pop! 2
71
+ end
72
+
73
+ rule /\]/, Str::Escape, :pop!
74
+ rule /\\./, Str::Escape
75
+ rule /./, Str::Regex
76
+ end
77
+
47
78
  state :bad_regex do
48
79
  rule /[^\n]+/, Error, :pop!
49
80
  end
@@ -86,37 +117,33 @@ module Rouge
86
117
  id = /[$a-zA-Z_][a-zA-Z0-9_]*/
87
118
 
88
119
  state :root do
89
- rule /\A\s*#!.*?\n/m, Comment::Preproc
90
- rule %r((?<=\n)(?=\s|/|<!--)), Text, :slash_starts_regex
120
+ rule /\A\s*#!.*?\n/m, Comment::Preproc, :statement
121
+ rule /\n/, Text, :statement
122
+ rule %r((?<=\n)(?=\s|/|<!--)), Text, :expr_start
91
123
  mixin :comments_and_whitespace
92
124
  rule %r(\+\+ | -- | ~ | && | \|\| | \\(?=\n) | << | >>>? | ===
93
125
  | !== )x,
94
- Operator, :slash_starts_regex
95
- rule %r([-<>+*%&|\^/!=]=?), Operator, :slash_starts_regex
96
- rule /[(\[;,]/, Punctuation, :slash_starts_regex
126
+ Operator, :expr_start
127
+ rule %r([-<>+*%&|\^/!=]=?), Operator, :expr_start
128
+ rule /[(\[,]/, Punctuation, :expr_start
129
+ rule /;/, Punctuation, :statement
97
130
  rule /[)\].]/, Punctuation
98
131
 
99
132
  rule /[?]/ do
100
133
  token Punctuation
101
134
  push :ternary
102
- push :slash_starts_regex
135
+ push :expr_start
103
136
  end
104
137
 
105
- rule /[{](?=\s*(#{id}|"[^\n]*?")\s*:)/m, Punctuation, :object
106
-
107
- rule /[{]/ do
108
- token Punctuation
109
- push :block
110
- push :slash_starts_regex
111
- end
138
+ rule /[{}]/, Punctuation, :statement
112
139
 
113
140
  rule id do |m|
114
141
  if self.class.keywords.include? m[0]
115
142
  token Keyword
116
- push :slash_starts_regex
143
+ push :expr_start
117
144
  elsif self.class.declarations.include? m[0]
118
145
  token Keyword::Declaration
119
- push :slash_starts_regex
146
+ push :expr_start
120
147
  elsif self.class.reserved.include? m[0]
121
148
  token Keyword::Reserved
122
149
  elsif self.class.constants.include? m[0]
@@ -136,28 +163,38 @@ module Rouge
136
163
  end
137
164
 
138
165
  # braced parts that aren't object literals
139
- state :block do
166
+ state :statement do
140
167
  rule /(#{id})(\s*)(:)/ do
141
168
  groups Name::Label, Text, Punctuation
142
169
  end
143
170
 
144
- rule /[}]/, Punctuation, :pop!
145
- mixin :root
171
+ mixin :expr_start
146
172
  end
147
173
 
148
174
  # object literals
149
175
  state :object do
150
- rule /[}]/, Punctuation, :pop!
176
+ mixin :comments_and_whitespace
177
+ rule /[}]/ do
178
+ token Punctuation
179
+ goto :statement
180
+ end
181
+
151
182
  rule /(#{id})(\s*)(:)/ do
152
183
  groups Name::Attribute, Text, Punctuation
184
+ push :expr_start
153
185
  end
186
+
154
187
  rule /:/, Punctuation
155
188
  mixin :root
156
189
  end
157
190
 
158
191
  # ternary expressions, where <id>: is not a label!
159
192
  state :ternary do
160
- rule /:/, Punctuation, :pop!
193
+ rule /:/ do
194
+ token Punctuation
195
+ goto :expr_start
196
+ end
197
+
161
198
  mixin :root
162
199
  end
163
200
  end
@@ -46,7 +46,7 @@ module Rouge
46
46
 
47
47
  prepend :statements do
48
48
  rule /@"/, Str, :string
49
- rule /@'(\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|\\.|[^\\'\n]')/
49
+ rule /@'(\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|\\.|[^\\'\n]')/,
50
50
  Str::Char
51
51
  rule /@(\d+[.]\d*|[.]\d+|\d+)e[+-]?\d+l?/i,
52
52
  Num::Float
@@ -72,7 +72,7 @@ module Rouge
72
72
  rule /#.*?\n/, Comment::Single
73
73
  rule %r(//.*?\n), Comment::Single
74
74
  # empty comment, otherwise seen as the start of a docstring
75
- rule %r(/\*\*/)
75
+ rule %r(/\*\*/), Comment::Multiline
76
76
  rule %r(/\*\*.*?\*/)m, Str::Doc
77
77
  rule %r(/\*.*?\*/)m, Comment::Multiline
78
78
  rule /(->|::)(\s*)([a-zA-Z_][a-zA-Z0-9_]*)/ do
@@ -22,6 +22,8 @@ module Rouge
22
22
  groups Keyword::Type, Text, Keyword, Text, Name::Label, Text, Punctuation
23
23
  push :type_block
24
24
  end
25
+
26
+ rule /[{]/, Punctuation, :push
25
27
  end
26
28
 
27
29
  state :type_block do
@@ -31,7 +33,7 @@ module Rouge
31
33
 
32
34
  rule /(#{id_with_dots})(\s*)(:)/ do
33
35
  groups Name::Label, Text, Punctuation
34
- push :slash_starts_regex
36
+ push :expr_start
35
37
  end
36
38
 
37
39
  rule /(signal)(\s+)(#{id_with_dots})/ do
@@ -41,7 +43,7 @@ module Rouge
41
43
 
42
44
  rule /(property)(\s+)(#{id_with_dots})(\s+)(#{id_with_dots})(\s*)(:?)/ do
43
45
  groups Keyword::Declaration, Text, Keyword::Type, Text, Name::Label, Text, Punctuation
44
- push :slash_starts_regex
46
+ push :expr_start
45
47
  end
46
48
 
47
49
  rule /[}]/, Punctuation, :pop!
@@ -0,0 +1,224 @@
1
+ # -*- coding: utf-8 -*- #
2
+
3
+ module Rouge
4
+ module Lexers
5
+ # A lexer for the Slim tempalte language
6
+ # @see http://slim-lang.org
7
+ class Slim < RegexLexer
8
+ include Indentation
9
+
10
+ desc 'The Slim template language'
11
+
12
+ tag 'slim'
13
+
14
+ filenames '*.slim'
15
+
16
+ # Ruby identifier characters
17
+ ruby_chars = /[\w\!\?\@\$]/
18
+
19
+ # Since you are allowed to wrap lines with a backslash, include \\\n in characters
20
+ dot = /(\\\n|.)/
21
+
22
+ def self.analyze_text(text)
23
+ return 1 if text.start_with? 'doctype'
24
+ return 1 if text =~ /(\*)(\{.+?\})/ # Contans a hash splat
25
+ end
26
+
27
+ def ruby
28
+ @ruby ||= Ruby.new(options)
29
+ end
30
+
31
+ def html
32
+ @html ||= HTML.new(options)
33
+ end
34
+
35
+ def filters
36
+ @filters ||= {
37
+ 'ruby' => ruby,
38
+ 'erb' => ERB.new(options),
39
+ 'javascript' => Javascript.new(options),
40
+ 'css' => CSS.new(options),
41
+ 'coffee' => Coffeescript.new(options),
42
+ 'markdown' => Markdown.new(options),
43
+ 'scss' => Scss.new(options),
44
+ 'sass' => Sass.new(options)
45
+ }
46
+ end
47
+
48
+ state :root do
49
+ rule /\s*\n/, Text
50
+ rule(/\s*/) { |m| token Text; indentation(m[0]) }
51
+ end
52
+
53
+ state :content do
54
+ mixin :css
55
+
56
+ rule /\/#{dot}*/, Comment, :indented_block
57
+
58
+ rule /(doctype)(\s+)(.*)/ do
59
+ groups Name::Namespace, Text::Whitespace, Text
60
+ pop!
61
+ end
62
+
63
+ # filters, shamelessly ripped from HAML
64
+ rule /(\w*):\s*\n/ do |m|
65
+ token Name::Decorator
66
+ pop!
67
+ starts_block :filter_block
68
+
69
+ filter_name = m[1].strip
70
+
71
+ @filter_lexer = self.filters[filter_name]
72
+ @filter_lexer.reset! unless @filter_lexer.nil?
73
+
74
+ puts " slim: filter #{filter_name.inspect} #{@filter_lexer.inspect}" if @debug
75
+ end
76
+
77
+ # Text
78
+ rule %r([\|'](?=\s)) do
79
+ token Punctuation
80
+ pop!
81
+ starts_block :plain_block
82
+ goto :plain_block
83
+ end
84
+
85
+ rule /-|==|=/, Punctuation, :ruby_line
86
+
87
+ # Dynamic tags
88
+ rule /(\*)(#{ruby_chars}+\(.*?\))/ do |m|
89
+ token Punctuation, m[1]
90
+ delegate ruby, m[2]
91
+ push :tag
92
+ end
93
+
94
+ rule /(\*)(#{ruby_chars}+)/ do |m|
95
+ token Punctuation, m[1]
96
+ delegate ruby, m[2]
97
+ push :tag
98
+ end
99
+
100
+ #rule /<\w+(?=.*>)/, Keyword::Constant, :tag # Maybe do this, look ahead and stuff
101
+ rule %r((</?[\w\s\=\'\"]+?/?>)) do |m| # Dirty html
102
+ delegate html, m[1]
103
+ pop!
104
+ end
105
+
106
+ # Ordinary slim tags
107
+ rule /\w+/, Name::Tag, :tag
108
+
109
+ end
110
+
111
+ state :tag do
112
+ mixin :css
113
+ mixin :indented_block
114
+ mixin :interpolation
115
+
116
+ # Whitespace control
117
+ rule /[<>]/, Punctuation
118
+
119
+ # Trim whitespace
120
+ rule /\s+?/, Text::Whitespace
121
+
122
+ # Splats, these two might be mergable?
123
+ rule /(\*)(#{ruby_chars}+)/ do |m|
124
+ token Punctuation, m[1]
125
+ delegate ruby, m[2]
126
+ end
127
+
128
+ rule /(\*)(\{#{dot}+?\})/ do |m|
129
+ token Punctuation, m[1]
130
+ delegate ruby, m[2]
131
+ end
132
+
133
+ # Attributes
134
+ rule /([\w\-]+)(\s*)(\=)/ do |m|
135
+ token Name::Attribute, m[1]
136
+ token Text::Whitespace, m[2]
137
+ token Punctuation, m[3]
138
+ push :html_attr
139
+ end
140
+
141
+ # Ruby value
142
+ rule /(\=)(#{dot}+)/ do |m|
143
+ token Punctuation, m[1]
144
+ #token Keyword::Constant, m[2]
145
+ delegate ruby, m[2]
146
+ end
147
+
148
+ rule /#{dot}+?/, Text
149
+
150
+ rule /\s*\n/, Text::Whitespace, :pop!
151
+ end
152
+
153
+ state :css do
154
+ rule(/\.\w+/) { token Name::Class; goto :tag }
155
+ rule(/#\w+/) { token Name::Function; goto :tag }
156
+ end
157
+
158
+ state :html_attr do
159
+ # Strings, double/single quoted
160
+ rule %r(\s*(['"])#{dot}*\1), Literal::String, :pop!
161
+
162
+ # Ruby stuff
163
+ rule(/(#{ruby_chars}+\(.*?\))/) { |m| delegate ruby, m[1]; pop! }
164
+ rule(/(#{ruby_chars}+)/) { |m| delegate ruby, m[1]; pop! }
165
+
166
+ rule /\s+/, Text::Whitespace
167
+ end
168
+
169
+ state :ruby_line do
170
+ # Need at top
171
+ mixin :indented_block
172
+
173
+ rule(/,\s*\n/) { delegate ruby }
174
+ rule /[ ]\|[ \t]*\n/, Str::Escape
175
+ rule(/.*?(?=(,$| \|)?[ \t]*$)/) { delegate ruby }
176
+ end
177
+
178
+ state :filter_block do
179
+ rule /([^#\n]|#[^{\n]|(\\\\)*\\#\{)+/ do
180
+ if @filter_lexer
181
+ delegate @filter_lexer
182
+ else
183
+ token Name::Decorator
184
+ end
185
+ end
186
+
187
+ mixin :interpolation
188
+ mixin :indented_block
189
+ end
190
+
191
+ state :plain_block do
192
+ mixin :interpolation
193
+
194
+ rule %r((</?[\w\s\=\'\"]+?/?>)) do |m| # Dirty html
195
+ delegate html, m[1]
196
+ end
197
+
198
+ #rule /([^#\n]|#[^{\n]|(\\\\)*\\#\{)+/ do
199
+ rule /#{dot}+?/, Text
200
+
201
+ mixin :indented_block
202
+ end
203
+
204
+ state :interpolation do
205
+ rule /#[{]/, Str::Interpol, :ruby_interp
206
+ end
207
+
208
+ state :ruby_interp do
209
+ rule /[}]/, Str::Interpol, :pop!
210
+ mixin :ruby_interp_inner
211
+ end
212
+
213
+ state :ruby_interp_inner do
214
+ rule(/[{]/) { delegate ruby; push :ruby_interp_inner }
215
+ rule(/[}]/) { delegate ruby; pop! }
216
+ rule(/[^{}]+/) { delegate ruby }
217
+ end
218
+
219
+ state :indented_block do
220
+ rule(/(?<!\\)\n/) { token Text; reset_stack }
221
+ end
222
+ end
223
+ end
224
+ end
@@ -93,6 +93,10 @@ module Rouge
93
93
  # {RegexLexer#token}, and {RegexLexer#delegate}. The first
94
94
  # argument can be used to access the match groups.
95
95
  def rule(re, tok=nil, next_state=nil, &callback)
96
+ if tok.nil? && callback.nil?
97
+ raise "please pass `rule` a token to yield or a callback"
98
+ end
99
+
96
100
  callback ||= case next_state
97
101
  when :pop!
98
102
  proc do |stream|
@@ -103,6 +107,8 @@ module Rouge
103
107
  end
104
108
  when :push
105
109
  proc do |stream|
110
+ puts " yielding #{tok.qualname}, #{stream[0].inspect}" if @debug
111
+ @output_stream.call(tok, stream[0])
106
112
  puts " pushing #{@stack.last.name}" if @debug
107
113
  @stack.push(@stack.last)
108
114
  end
@@ -114,11 +120,13 @@ module Rouge
114
120
  puts " pushing #{state.name}" if @debug
115
121
  @stack.push(state)
116
122
  end
117
- else
123
+ when nil
118
124
  proc do |stream|
119
125
  puts " yielding #{tok.qualname}, #{stream[0].inspect}" if @debug
120
126
  @output_stream.call(tok, stream[0])
121
127
  end
128
+ else
129
+ raise "invalid next state: #{next_state.inspect}"
122
130
  end
123
131
 
124
132
  rules << Rule.new(re, callback)
data/lib/rouge/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rouge
4
4
  def self.version
5
- "1.5.1"
5
+ "1.6.1"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rouge
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jay Adkisson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-13 00:00:00.000000000 Z
11
+ date: 2014-07-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Rouge aims to a be a simple, easy-to-extend drop-in replacement for pygments.
14
14
  email:
@@ -79,6 +79,7 @@ files:
79
79
  - lib/rouge/demos/scss
80
80
  - lib/rouge/demos/sed
81
81
  - lib/rouge/demos/shell
82
+ - lib/rouge/demos/slim
82
83
  - lib/rouge/demos/smalltalk
83
84
  - lib/rouge/demos/sml
84
85
  - lib/rouge/demos/sql
@@ -155,6 +156,7 @@ files:
155
156
  - lib/rouge/lexers/scss.rb
156
157
  - lib/rouge/lexers/sed.rb
157
158
  - lib/rouge/lexers/shell.rb
159
+ - lib/rouge/lexers/slim.rb
158
160
  - lib/rouge/lexers/smalltalk.rb
159
161
  - lib/rouge/lexers/sml.rb
160
162
  - lib/rouge/lexers/sql.rb