rouge 2.0.7 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +7 -9
  3. data/lib/rouge.rb +1 -0
  4. data/lib/rouge/cli.rb +20 -0
  5. data/lib/rouge/demos/awk +4 -0
  6. data/lib/rouge/demos/console +6 -0
  7. data/lib/rouge/demos/digdag +19 -0
  8. data/lib/rouge/demos/dot +5 -0
  9. data/lib/rouge/demos/graphql +17 -0
  10. data/lib/rouge/demos/hylang +10 -0
  11. data/lib/rouge/demos/igorpro +9 -0
  12. data/lib/rouge/demos/irb +4 -0
  13. data/lib/rouge/demos/irb_output +2 -0
  14. data/lib/rouge/demos/lasso +12 -0
  15. data/lib/rouge/demos/mosel +10 -0
  16. data/lib/rouge/demos/plist +142 -0
  17. data/lib/rouge/demos/pony +17 -0
  18. data/lib/rouge/demos/q +2 -0
  19. data/lib/rouge/demos/sieve +10 -0
  20. data/lib/rouge/demos/tsx +17 -0
  21. data/lib/rouge/demos/turtle +0 -0
  22. data/lib/rouge/demos/wollok +11 -0
  23. data/lib/rouge/formatters/html_inline.rb +9 -1
  24. data/lib/rouge/formatters/html_pygments.rb +2 -2
  25. data/lib/rouge/formatters/html_table.rb +1 -1
  26. data/lib/rouge/formatters/terminal256.rb +1 -1
  27. data/lib/rouge/lexer.rb +124 -37
  28. data/lib/rouge/lexers/abap.rb +2 -2
  29. data/lib/rouge/lexers/apache.rb +1 -1
  30. data/lib/rouge/lexers/awk.rb +161 -0
  31. data/lib/rouge/lexers/clojure.rb +2 -2
  32. data/lib/rouge/lexers/coffeescript.rb +1 -1
  33. data/lib/rouge/lexers/console.rb +136 -0
  34. data/lib/rouge/lexers/csharp.rb +26 -7
  35. data/lib/rouge/lexers/digdag.rb +72 -0
  36. data/lib/rouge/lexers/docker.rb +1 -1
  37. data/lib/rouge/lexers/dot.rb +68 -0
  38. data/lib/rouge/lexers/elixir.rb +52 -27
  39. data/lib/rouge/lexers/fortran.rb +56 -28
  40. data/lib/rouge/lexers/fsharp.rb +1 -1
  41. data/lib/rouge/lexers/gherkin/keywords.rb +4 -4
  42. data/lib/rouge/lexers/graphql.rb +243 -0
  43. data/lib/rouge/lexers/groovy.rb +5 -1
  44. data/lib/rouge/lexers/haml.rb +19 -24
  45. data/lib/rouge/lexers/html.rb +48 -4
  46. data/lib/rouge/lexers/hylang.rb +93 -0
  47. data/lib/rouge/lexers/igorpro.rb +407 -0
  48. data/lib/rouge/lexers/irb.rb +66 -0
  49. data/lib/rouge/lexers/javascript.rb +21 -10
  50. data/lib/rouge/lexers/json.rb +3 -2
  51. data/lib/rouge/lexers/json_doc.rb +6 -0
  52. data/lib/rouge/lexers/jsx.rb +2 -1
  53. data/lib/rouge/lexers/lasso.rb +217 -0
  54. data/lib/rouge/lexers/lasso/keywords.yml +446 -0
  55. data/lib/rouge/lexers/lua.rb +3 -0
  56. data/lib/rouge/lexers/lua/builtins.rb +1 -1
  57. data/lib/rouge/lexers/markdown.rb +2 -3
  58. data/lib/rouge/lexers/matlab/builtins.rb +1 -1
  59. data/lib/rouge/lexers/moonscript.rb +8 -4
  60. data/lib/rouge/lexers/mosel.rb +231 -0
  61. data/lib/rouge/lexers/ocaml.rb +9 -20
  62. data/lib/rouge/lexers/php.rb +40 -20
  63. data/lib/rouge/lexers/php/builtins.rb +27 -37
  64. data/lib/rouge/lexers/plain_text.rb +4 -3
  65. data/lib/rouge/lexers/plist.rb +49 -0
  66. data/lib/rouge/lexers/pony.rb +93 -0
  67. data/lib/rouge/lexers/powershell.rb +36 -0
  68. data/lib/rouge/lexers/properties.rb +2 -2
  69. data/lib/rouge/lexers/q.rb +124 -0
  70. data/lib/rouge/lexers/r.rb +2 -2
  71. data/lib/rouge/lexers/ruby.rb +26 -13
  72. data/lib/rouge/lexers/rust.rb +7 -5
  73. data/lib/rouge/lexers/sed.rb +4 -2
  74. data/lib/rouge/lexers/shell.rb +38 -16
  75. data/lib/rouge/lexers/sieve.rb +96 -0
  76. data/lib/rouge/lexers/sml.rb +3 -2
  77. data/lib/rouge/lexers/tsx.rb +19 -0
  78. data/lib/rouge/lexers/turtle.rb +0 -0
  79. data/lib/rouge/lexers/typescript.rb +3 -27
  80. data/lib/rouge/lexers/typescript/common.rb +33 -0
  81. data/lib/rouge/lexers/viml/keywords.rb +2 -2
  82. data/lib/rouge/lexers/wollok.rb +107 -0
  83. data/lib/rouge/lexers/xml.rb +1 -1
  84. data/lib/rouge/lexers/yaml.rb +4 -1
  85. data/lib/rouge/regex_lexer.rb +1 -0
  86. data/lib/rouge/template_lexer.rb +3 -5
  87. data/lib/rouge/theme.rb +14 -4
  88. data/lib/rouge/themes/igor_pro.rb +20 -0
  89. data/lib/rouge/themes/pastie.rb +69 -0
  90. data/lib/rouge/themes/thankful_eyes.rb +8 -5
  91. data/lib/rouge/version.rb +1 -1
  92. metadata +40 -6
  93. data/lib/rouge/demos/shell_session +0 -10
  94. data/lib/rouge/lexers/shell_session.rb +0 -29
@@ -7,9 +7,9 @@ module Rouge
7
7
  end
8
8
 
9
9
  def stream(tokens, &b)
10
- yield %(<pre class="#{@css_class}"><code>)
10
+ yield %(<div class="highlight"><pre class="#{@css_class}"><code>)
11
11
  @inner.stream(tokens, &b)
12
- yield "</code></pre>"
12
+ yield "</code></pre></div>"
13
13
  end
14
14
  end
15
15
  end
@@ -54,7 +54,7 @@ module Rouge
54
54
  yield formatted
55
55
  yield '</pre></td>'
56
56
 
57
- yield "</tr></tbody></table>\n"
57
+ yield "</tr></tbody></table>"
58
58
  end
59
59
  end
60
60
  end
@@ -17,7 +17,7 @@ module Rouge
17
17
  tokens.each do |tok, val|
18
18
  escape = escape_sequence(tok)
19
19
  yield escape.style_string
20
- yield val.gsub("\n", "\n#{escape.style_string}")
20
+ yield val.gsub("\n", "#{escape.reset_string}\n#{escape.style_string}")
21
21
  yield escape.reset_string
22
22
  end
23
23
  end
data/lib/rouge/lexer.rb CHANGED
@@ -11,6 +11,8 @@ module Rouge
11
11
  class Lexer
12
12
  include Token::Tokens
13
13
 
14
+ @option_docs = {}
15
+
14
16
  class << self
15
17
  # Lexes `stream` with the given options. The lex is delegated to a
16
18
  # new instance.
@@ -20,12 +22,6 @@ module Rouge
20
22
  new(opts).lex(stream, &b)
21
23
  end
22
24
 
23
- def default_options(o={})
24
- @default_options ||= {}
25
- @default_options.merge!(o)
26
- @default_options
27
- end
28
-
29
25
  # Given a string, return the correct lexer class.
30
26
  def find(name)
31
27
  registry[name.to_s]
@@ -45,19 +41,30 @@ module Rouge
45
41
  # This is used in the Redcarpet plugin as well as Rouge's own
46
42
  # markdown lexer for highlighting internal code blocks.
47
43
  #
48
- def find_fancy(str, code=nil)
44
+ def find_fancy(str, code=nil, additional_options={})
45
+ if str && !str.include?('?') && str != 'guess'
46
+ lexer_class = find(str)
47
+ return lexer_class && lexer_class.new(additional_options)
48
+ end
49
+
49
50
  name, opts = str ? str.split('?', 2) : [nil, '']
50
51
 
51
52
  # parse the options hash from a cgi-style string
52
53
  opts = CGI.parse(opts || '').map do |k, vals|
53
- [ k.to_sym, vals.empty? ? true : vals[0] ]
54
+ val = case vals.size
55
+ when 0 then true
56
+ when 1 then vals[0]
57
+ else vals
58
+ end
59
+
60
+ [ k.to_s, val ]
54
61
  end
55
62
 
56
- opts = Hash[opts]
63
+ opts = additional_options.merge(Hash[opts])
57
64
 
58
65
  lexer_class = case name
59
66
  when 'guess', nil
60
- self.guess(:source => code, :mimetype => opts[:mimetype])
67
+ self.guess(:source => code, :mimetype => opts['mimetype'])
61
68
  when String
62
69
  self.find(name)
63
70
  end
@@ -82,6 +89,14 @@ module Rouge
82
89
  end
83
90
  end
84
91
 
92
+ def option_docs
93
+ @option_docs ||= InheritableHash.new(superclass.option_docs)
94
+ end
95
+
96
+ def option(name, desc)
97
+ option_docs[name.to_s] = desc
98
+ end
99
+
85
100
  # Specify or get the path name containing a small demo for
86
101
  # this lexer (can be overriden by {demo}).
87
102
  def demo_file(arg=:absent)
@@ -156,7 +171,17 @@ module Rouge
156
171
  guess :source => source
157
172
  end
158
173
 
159
- private
174
+ def enable_debug!
175
+ @debug_enabled = true
176
+ end
177
+
178
+ def disable_debug!
179
+ @debug_enabled = false
180
+ end
181
+
182
+ def debug_enabled?
183
+ !!@debug_enabled
184
+ end
160
185
 
161
186
  protected
162
187
  # @private
@@ -234,6 +259,7 @@ module Rouge
234
259
 
235
260
  # -*- instance methods -*- #
236
261
 
262
+ attr_reader :options
237
263
  # Create a new lexer with the given options. Individual lexers may
238
264
  # specify extra options. The only current globally accepted option
239
265
  # is `:debug`.
@@ -244,42 +270,103 @@ module Rouge
244
270
  # state stack at the beginning of each step, along with each regex
245
271
  # tried and each stream consumed. Try it, it's pretty useful.
246
272
  def initialize(opts={})
247
- options(opts)
273
+ @options = {}
274
+ opts.each { |k, v| @options[k.to_s] = v }
275
+
276
+ @debug = Lexer.debug_enabled? && bool_option(:debug)
277
+ end
248
278
 
249
- @debug = option(:debug)
279
+ def as_bool(val)
280
+ case val
281
+ when nil, false, 0, '0', 'off'
282
+ false
283
+ when Array
284
+ val.empty? ? true : as_bool(val.last)
285
+ else
286
+ true
287
+ end
250
288
  end
251
289
 
252
- # get and/or specify the options for this lexer.
253
- def options(o={})
254
- (@options ||= {}).merge!(o)
290
+ def as_string(val)
291
+ return as_string(val.last) if val.is_a?(Array)
255
292
 
256
- self.class.default_options.merge(@options)
293
+ val ? val.to_s : nil
257
294
  end
258
295
 
259
- # get or specify one option for this lexer
260
- def option(k, v=:absent)
261
- if v == :absent
262
- options[k]
296
+ def as_list(val)
297
+ case val
298
+ when Array
299
+ val.flat_map { |v| as_list(v) }
300
+ when String
301
+ val.split(',')
263
302
  else
264
- options({ k => v })
303
+ []
265
304
  end
266
305
  end
267
306
 
268
- # @deprecated
269
- # Instead of `debug { "foo" }`, simply `puts "foo" if @debug`.
270
- #
271
- # Leave a debug message if the `:debug` option is set. The message
272
- # is given as a block because some debug messages contain calculated
273
- # information that is unnecessary for lexing in the real world.
274
- #
275
- # Calls to this method should be guarded with "if @debug" for best
276
- # performance when debugging is turned off.
277
- #
278
- # @example
279
- # debug { "hello, world!" } if @debug
280
- def debug
281
- warn "Lexer#debug is deprecated. Simply puts if @debug instead."
282
- puts yield if @debug
307
+ def as_lexer(val)
308
+ return as_lexer(val.last) if val.is_a?(Array)
309
+ return val.new(@options) if val.is_a?(Class) && val < Lexer
310
+
311
+ case val
312
+ when Lexer
313
+ val
314
+ when String
315
+ lexer_class = Lexer.find(val)
316
+ lexer_class && lexer_class.new(@options)
317
+ end
318
+ end
319
+
320
+ def as_token(val)
321
+ return as_token(val.last) if val.is_a?(Array)
322
+ case val
323
+ when Token
324
+ val
325
+ else
326
+ Token[val]
327
+ end
328
+ end
329
+
330
+ def bool_option(name, &default)
331
+ if @options.key?(name.to_s)
332
+ as_bool(@options[name.to_s])
333
+ else
334
+ default ? default.call : false
335
+ end
336
+ end
337
+
338
+ def string_option(name, &default)
339
+ as_string(@options.delete(name.to_s, &default))
340
+ end
341
+
342
+ def lexer_option(name, &default)
343
+ as_lexer(@options.delete(name.to_s, &default))
344
+ end
345
+
346
+ def list_option(name, &default)
347
+ as_list(@options.delete(name.to_s, &default))
348
+ end
349
+
350
+ def token_option(name, &default)
351
+ as_token(@options.delete(name.to_s, &default))
352
+ end
353
+
354
+ def hash_option(name, defaults, &val_cast)
355
+ name = name.to_s
356
+ out = defaults.dup
357
+
358
+ base = @options.delete(name.to_s)
359
+ base = {} unless base.is_a?(Hash)
360
+ base.each { |k, v| out[k.to_s] = val_cast ? val_cast.call(v) : v }
361
+
362
+ @options.keys.each do |key|
363
+ next unless key =~ /(\w+)\[(\w+)\]/ and $1 == name
364
+ value = @options.delete(key)
365
+
366
+ out[$2] = val_cast ? val_cast.call(value) : value
367
+ end
368
+
369
+ out
283
370
  end
284
371
 
285
372
  # @abstract
@@ -217,13 +217,13 @@ module Rouge
217
217
 
218
218
  # operators
219
219
  rule %r((->|->>|=>)), Operator
220
- rule %r([-\*\+%/~=&\?<>!#@\^]+), Operator
220
+ rule %r([-\*\+%/~=&\?<>!#\@\^]+), Operator
221
221
 
222
222
  end
223
223
 
224
224
  state :operators do
225
225
  rule %r((->|->>|=>)), Operator
226
- rule %r([-\*\+%/~=&\?<>!#@\^]+), Operator
226
+ rule %r([-\*\+%/~=&\?<>!#\@\^]+), Operator
227
227
  end
228
228
 
229
229
  state :single_string do
@@ -13,7 +13,7 @@ module Rouge
13
13
  attr_reader :keywords
14
14
  end
15
15
  # Load Apache keywords from separate YML file
16
- @keywords = ::YAML.load(File.open(Pathname.new(__FILE__).dirname.join('apache/keywords.yml'))).tap do |h|
16
+ @keywords = ::YAML.load_file(Pathname.new(__FILE__).dirname.join('apache/keywords.yml')).tap do |h|
17
17
  h.each do |k,v|
18
18
  h[k] = Set.new v
19
19
  end
@@ -0,0 +1,161 @@
1
+ # -*- coding: utf-8 -*- #
2
+
3
+ module Rouge
4
+ module Lexers
5
+ class Awk < RegexLexer
6
+ title "Awk"
7
+ desc "pattern-directed scanning and processing language"
8
+
9
+ tag 'awk'
10
+ filenames '*.awk'
11
+ mimetypes 'application/x-awk'
12
+
13
+ def self.analyze_text(text)
14
+ return 1 if text.shebang?('awk')
15
+ end
16
+
17
+ id = /[$a-zA-Z_][a-zA-Z0-9_]*/
18
+
19
+ def self.keywords
20
+ @keywords ||= Set.new %w(
21
+ if else while for do break continue return next nextfile delete
22
+ exit print printf getline
23
+ )
24
+ end
25
+
26
+ def self.declarations
27
+ @declarations ||= Set.new %w(function)
28
+ end
29
+
30
+ def self.reserved
31
+ @reserved ||= Set.new %w(BEGIN END)
32
+ end
33
+
34
+ def self.constants
35
+ @constants ||= Set.new %w(
36
+ CONVFMT FS NF NR FNR FILENAME RS OFS ORS OFMT SUBSEP ARGC ARGV
37
+ ENVIRON
38
+ )
39
+ end
40
+
41
+ def self.builtins
42
+ @builtins ||= %w(
43
+ exp log sqrt sin cos atan2 length rand srand int substr index match
44
+ split sub gsub sprintf system tolower toupper
45
+ )
46
+ end
47
+
48
+ state :comments_and_whitespace do
49
+ rule /\s+/, Text
50
+ rule %r(#.*?$), Comment::Single
51
+ end
52
+
53
+ state :expr_start do
54
+ mixin :comments_and_whitespace
55
+ rule %r(/) do
56
+ token Str::Regex
57
+ goto :regex
58
+ end
59
+ rule //, Text, :pop!
60
+ end
61
+
62
+ state :regex do
63
+ rule %r(/) do
64
+ token Str::Regex
65
+ goto :regex_end
66
+ end
67
+
68
+ rule %r([^/]\n), Error, :pop!
69
+
70
+ rule /\n/, Error, :pop!
71
+ rule /\[\^/, Str::Escape, :regex_group
72
+ rule /\[/, Str::Escape, :regex_group
73
+ rule /\\./, Str::Escape
74
+ rule %r{[(][?][:=<!]}, Str::Escape
75
+ rule /[{][\d,]+[}]/, Str::Escape
76
+ rule /[()?]/, Str::Escape
77
+ rule /./, Str::Regex
78
+ end
79
+
80
+ state :regex_end do
81
+ rule(//) { pop! }
82
+ end
83
+
84
+ state :regex_group do
85
+ # specially highlight / in a group to indicate that it doesn't
86
+ # close the regex
87
+ rule /\//, Str::Escape
88
+
89
+ rule %r([^/]\n) do
90
+ token Error
91
+ pop! 2
92
+ end
93
+
94
+ rule /\]/, Str::Escape, :pop!
95
+ rule /\\./, Str::Escape
96
+ rule /./, Str::Regex
97
+ end
98
+
99
+ state :bad_regex do
100
+ rule /[^\n]+/, Error, :pop!
101
+ end
102
+
103
+ state :root do
104
+ mixin :comments_and_whitespace
105
+ rule %r((?<=\n)(?=\s|/)), Text, :expr_start
106
+ rule %r([-<>+*/%\^!=]=?|in\b|\+\+|--|\|), Operator, :expr_start
107
+ rule %r(&&|\|\||~!?), Operator, :expr_start
108
+ rule /[(\[,]/, Punctuation, :expr_start
109
+ rule /;/, Punctuation, :statement
110
+ rule /[)\].]/, Punctuation
111
+
112
+ rule /[?]/ do
113
+ token Punctuation
114
+ push :ternary
115
+ push :expr_start
116
+ end
117
+
118
+ rule /[{}]/, Punctuation, :statement
119
+
120
+ rule id do |m|
121
+ if self.class.keywords.include? m[0]
122
+ token Keyword
123
+ push :expr_start
124
+ elsif self.class.declarations.include? m[0]
125
+ token Keyword::Declaration
126
+ push :expr_start
127
+ elsif self.class.reserved.include? m[0]
128
+ token Keyword::Reserved
129
+ elsif self.class.constants.include? m[0]
130
+ token Keyword::Constant
131
+ elsif self.class.builtins.include? m[0]
132
+ token Name::Builtin
133
+ elsif m[0] =~ /^\$/
134
+ token Name::Variable
135
+ else
136
+ token Name::Other
137
+ end
138
+ end
139
+
140
+ rule /[0-9]+\.[0-9]+/, Num::Float
141
+ rule /[0-9]+/, Num::Integer
142
+ rule /"(\\[\\"]|[^"])*"/, Str::Double
143
+ rule /:/, Punctuation
144
+ end
145
+
146
+ state :statement do
147
+ rule /[{}]/, Punctuation
148
+ mixin :expr_start
149
+ end
150
+
151
+ state :ternary do
152
+ rule /:/ do
153
+ token Punctuation
154
+ goto :expr_start
155
+ end
156
+
157
+ mixin :root
158
+ end
159
+ end
160
+ end
161
+ end
@@ -9,7 +9,7 @@ module Rouge
9
9
  tag 'clojure'
10
10
  aliases 'clj', 'cljs'
11
11
 
12
- filenames '*.clj', '*.cljs', '*.cljc', 'build.boot'
12
+ filenames '*.clj', '*.cljs', '*.cljc', 'build.boot', '*.edn'
13
13
 
14
14
  mimetypes 'text/x-clojure', 'application/x-clojure'
15
15
 
@@ -86,7 +86,7 @@ module Rouge
86
86
  rule /\\(.|[a-z]+)/i, Str::Char
87
87
 
88
88
 
89
- rule /~@|[`\'#^~&]/, Operator
89
+ rule /~@|[`\'#^~&@]/, Operator
90
90
 
91
91
  rule /(\()(\s*)(#{identifier})/m do |m|
92
92
  token Punctuation, m[1]