coderay 0.8.260 → 0.8.263

Sign up to get free protection for your applications and to get access to all the features.
@@ -133,7 +133,7 @@ module CodeRay
133
133
  # whether +text+ is a String.
134
134
  def token text, kind
135
135
  out =
136
- if text.is_a? ::String # Ruby 1.9: watch out, :open.is_a? String is true
136
+ if text.is_a? ::String
137
137
  text_token text, kind
138
138
  elsif text.is_a? ::Symbol
139
139
  block_token text, kind
@@ -171,9 +171,14 @@ module CodeRay
171
171
  #
172
172
  # The already created +tokens+ object must be used; it can be a
173
173
  # TokenStream or a Tokens object.
174
- def compile tokens, options
175
- tokens.each { |text, kind| token text, kind } # FIXME for Ruby 1.9?
176
- #tokens.each(&self)
174
+ if RUBY_VERSION >= '1.9'
175
+ def compile tokens, options
176
+ tokens.each { |text, kind| token text, kind } # FIXME for Ruby 1.9?
177
+ end
178
+ else
179
+ def compile tokens, options
180
+ tokens.each(&self)
181
+ end
177
182
  end
178
183
 
179
184
  end
@@ -220,8 +220,13 @@ module Encoders
220
220
  super
221
221
  end
222
222
 
223
- def token text, type
224
- if text.is_a? ::String
223
+ def token text, type = :plain
224
+ case text
225
+
226
+ when nil
227
+ # raise 'Token with nil as text was given: %p' % [[text, type]]
228
+
229
+ when String
225
230
  if text =~ /#{HTML_ESCAPE_PATTERN}/o
226
231
  text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
227
232
  end
@@ -231,53 +236,49 @@ module Encoders
231
236
  else
232
237
  @out << text
233
238
  end
234
- else
235
-
236
- case text
237
-
238
- # token groups, eg. strings
239
- when :open
240
- @opened[0] = type
241
- @out << (@css_style[@opened] || '<span>')
242
- @opened << type
243
- when :close
244
- if @opened.empty?
245
- # nothing to close
246
- else
247
- if $DEBUG and (@opened.size == 1 or @opened.last != type)
248
- raise 'Malformed token stream: Trying to close a token (%p) \
249
- that is not open. Open are: %p.' % [type, @opened[1..-1]]
250
- end
251
- @out << '</span>'
252
- @opened.pop
253
- end
254
239
 
255
- # whole lines to be highlighted, eg. a deleted line in a diff
256
- when :begin_line
257
- @opened[0] = type
258
- if style = @css_style[@opened]
259
- @out << style.sub('<span', '<div')
260
- else
261
- @out << '<div>'
262
- end
263
- @opened << type
264
- when :end_line
265
- if @opened.empty?
266
- # nothing to close
267
- else
268
- if $DEBUG and (@opened.size == 1 or @opened.last != type)
269
- raise 'Malformed token stream: Trying to close a line (%p) \
270
- that is not open. Open are: %p.' % [type, @opened[1..-1]]
271
- end
272
- @out << '</div>'
273
- @opened.pop
240
+
241
+ # token groups, eg. strings
242
+ when :open
243
+ @opened[0] = type
244
+ @out << (@css_style[@opened] || '<span>')
245
+ @opened << type
246
+ when :close
247
+ if @opened.empty?
248
+ # nothing to close
249
+ else
250
+ if $DEBUG and (@opened.size == 1 or @opened.last != type)
251
+ raise 'Malformed token stream: Trying to close a token (%p) \
252
+ that is not open. Open are: %p.' % [type, @opened[1..-1]]
274
253
  end
275
-
276
- when nil
277
- raise 'Token with nil as text was given: %p' % [[text, type]]
254
+ @out << '</span>'
255
+ @opened.pop
256
+ end
257
+
258
+ # whole lines to be highlighted, eg. a deleted line in a diff
259
+ when :begin_line
260
+ @opened[0] = type
261
+ if style = @css_style[@opened]
262
+ @out << style.sub('<span', '<div')
278
263
  else
279
- raise 'unknown token kind: %p' % text
264
+ @out << '<div>'
280
265
  end
266
+ @opened << type
267
+ when :end_line
268
+ if @opened.empty?
269
+ # nothing to close
270
+ else
271
+ if $DEBUG and (@opened.size == 1 or @opened.last != type)
272
+ raise 'Malformed token stream: Trying to close a line (%p) \
273
+ that is not open. Open are: %p.' % [type, @opened[1..-1]]
274
+ end
275
+ @out << '</div>'
276
+ @opened.pop
277
+ end
278
+
279
+ else
280
+ raise 'unknown token kind: %p' % [text]
281
+
281
282
  end
282
283
  end
283
284
 
@@ -177,7 +177,7 @@ module Encoders
177
177
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="de">
178
178
  <head>
179
179
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
180
- <title>CodeRay HTML Encoder Example</title>
180
+ <title>CodeRay Output</title>
181
181
  <style type="text/css">
182
182
  <%CSS%>
183
183
  </style>
@@ -88,7 +88,12 @@ module FileType
88
88
  'cpp' => :c,
89
89
  'c' => :c,
90
90
  'h' => :c,
91
+ 'java' => :java,
91
92
  'js' => :java_script,
93
+ 'json' => :json,
94
+ 'diff' => :diff,
95
+ 'patch' => :diff,
96
+ 'css' => :css,
92
97
  'xml' => :xml,
93
98
  'htm' => :html,
94
99
  'html' => :html,
@@ -4,7 +4,7 @@ module CodeRay
4
4
 
5
5
  # = Scanners
6
6
  #
7
- # $Id: scanner.rb 253 2008-09-16 22:07:44Z murphy $
7
+ # $Id: scanner.rb 270 2009-01-01 03:19:28Z murphy $
8
8
  #
9
9
  # This module holds the Scanner class and its subclasses.
10
10
  # For example, the Ruby scanner is named CodeRay::Scanners::Ruby
@@ -180,6 +180,11 @@ module CodeRay
180
180
  def line
181
181
  string[0..pos].count("\n") + 1
182
182
  end
183
+
184
+ def column pos = self.pos
185
+ return 0 if pos <= 0
186
+ pos - (string.rindex(?\n, pos) || 0)
187
+ end
183
188
 
184
189
  protected
185
190
 
@@ -216,7 +221,7 @@ module CodeRay
216
221
  tokens:
217
222
  %s
218
223
 
219
- current line: %d pos = %d
224
+ current line: %d column: %d pos: %d
220
225
  matched: %p state: %p
221
226
  bol? = %p, eos? = %p
222
227
 
@@ -231,10 +236,10 @@ surrounding code:
231
236
  msg,
232
237
  tokens.size,
233
238
  tokens.last(10).map { |t| t.inspect }.join("\n"),
234
- line, pos,
239
+ line, column, pos,
235
240
  matched, state, bol?, eos?,
236
- string[pos-ambit,ambit],
237
- string[pos,ambit],
241
+ string[pos - ambit, ambit],
242
+ string[pos, ambit],
238
243
  ]
239
244
  end
240
245
 
@@ -7,7 +7,8 @@ module Scanners
7
7
  :irb => :ruby,
8
8
  :xhtml => :nitro_xhtml,
9
9
  :javascript => :java_script,
10
- :nitro => :nitro_xhtml
10
+ :nitro => :nitro_xhtml,
11
+ :yml => :yaml
11
12
 
12
13
  default :plain
13
14
 
@@ -168,7 +168,6 @@ module Scanners
168
168
  raise_inspect 'Empty token', tokens unless match
169
169
 
170
170
  tokens << [match, kind]
171
- # tokens << [states.inspect, :error]
172
171
 
173
172
  end
174
173
 
@@ -2,8 +2,6 @@ module CodeRay
2
2
  module Scanners
3
3
 
4
4
  # HTML Scanner
5
- #
6
- # $Id$
7
5
  class HTML < Scanner
8
6
 
9
7
  include Streamable
@@ -65,7 +63,7 @@ module Scanners
65
63
  if scan(/<!--.*?-->/m)
66
64
  kind = :comment
67
65
  elsif scan(/<!DOCTYPE.*?>/m)
68
- kind = :preprocessor
66
+ kind = :doctype
69
67
  elsif scan(/<\?xml.*?\?>/m)
70
68
  kind = :preprocessor
71
69
  elsif scan(/<\?.*?\?>|<%.*?%>/m)
@@ -34,11 +34,11 @@ module Scanners
34
34
  elsif match = scan(/ [:,\[{\]}] /x)
35
35
  kind = :operator
36
36
  case match
37
- when '{': stack << :object; key_expected = true
38
- when '[': stack << :array
39
- when ':': key_expected = false
40
- when ',': key_expected = true if stack.last == :object
41
- when '}', ']': stack.pop # no error recovery, but works for valid JSON
37
+ when '{' then stack << :object; key_expected = true
38
+ when '[' then stack << :array
39
+ when ':' then key_expected = false
40
+ when ',' then key_expected = true if stack.last == :object
41
+ when '}', ']' then stack.pop # no error recovery, but works for valid JSON
42
42
  end
43
43
  elsif match = scan(/ true | false | null /x)
44
44
  kind = IDENT_KIND[match]
@@ -166,13 +166,14 @@ module Scanners
166
166
  { }
167
167
  ] ]
168
168
 
169
- CLOSING_PAREN.values.each { |o| o.freeze } # debug, if I try to change it with <<
169
+ CLOSING_PAREN.each { |k,v| k.freeze; v.freeze } # debug, if I try to change it with <<
170
170
  OPENING_PAREN = CLOSING_PAREN.invert
171
171
 
172
172
  STRING_PATTERN = Hash.new { |h, k|
173
173
  delim, interpreted = *k
174
- delim_pattern = Regexp.escape(delim.dup)
174
+ delim_pattern = Regexp.escape(delim)
175
175
  if closing_paren = CLOSING_PAREN[delim]
176
+ delim_pattern = delim_pattern[0..-1] if defined? JRUBY_VERSION # JRuby fix
176
177
  delim_pattern << Regexp.escape(closing_paren)
177
178
  end
178
179
 
@@ -0,0 +1,141 @@
1
+ module CodeRay
2
+ module Scanners
3
+
4
+ # YAML Scanner
5
+ #
6
+ # Based on the YAML scanner from Syntax by Jamis Buck.
7
+ class YAML < Scanner
8
+
9
+ register_for :yaml
10
+ file_extension 'yml'
11
+
12
+ def scan_tokens tokens, options
13
+
14
+ value_expected = nil
15
+ state = :initial
16
+ key_indent = indent = 0
17
+
18
+ until eos?
19
+
20
+ kind = nil
21
+ match = nil
22
+
23
+ if bol?
24
+ key_indent = nil
25
+ if $DEBUG
26
+ indent = check(/ +/) ? matched.size : 0
27
+ tokens << [indent.to_s, :debug]
28
+ end
29
+ end
30
+
31
+ if match = scan(/ +[\t ]*/)
32
+ kind = :space
33
+
34
+ elsif match = scan(/\n+/)
35
+ kind = :space
36
+ state = :initial if match.index(?\n)
37
+
38
+ elsif match = scan(/#.*/)
39
+ kind = :comment
40
+
41
+ elsif bol? and case
42
+ when match = scan(/---|\.\.\./)
43
+ tokens << [:open, :head]
44
+ tokens << [match, :head]
45
+ tokens << [:close, :head]
46
+ next
47
+ when match = scan(/%.*/)
48
+ tokens << [match, :doctype]
49
+ next
50
+ end
51
+
52
+ elsif state == :value and case
53
+ when !check(/(?:"[^"]*")(?=: |:$)/) && scan(/"/)
54
+ tokens << [:open, :string]
55
+ tokens << [matched, :delimiter]
56
+ tokens << [matched, :content] if scan(/ [^"\\]* (?: \\. [^"\\]* )* /mx)
57
+ tokens << [matched, :delimiter] if scan(/"/)
58
+ tokens << [:close, :string]
59
+ next
60
+ when match = scan(/[|>][-+]?/)
61
+ tokens << [:open, :string]
62
+ tokens << [match, :delimiter]
63
+ tokens << [matched, :content] if scan(/(?:\n+ {#{key_indent + 1}}.*)+/)
64
+ tokens << [:close, :string]
65
+ next
66
+ when match = scan(/(?![!"*&]).+?(?=$|\s+#)/)
67
+ tokens << [match, :string]
68
+ string_indent = key_indent || column(pos - match.size - 1)
69
+ tokens << [matched, :string] if scan(/(?:\n+ {#{string_indent + 1}}.*)+/)
70
+ next
71
+ end
72
+
73
+ elsif case
74
+ when match = scan(/[-:](?= |$)/)
75
+ state = :value if state == :colon && (match == ':' || match == '-')
76
+ state = :value if state == :initial && match == '-'
77
+ kind = :operator
78
+ when match = scan(/[,{}\[\]]/)
79
+ kind = :operator
80
+ when state == :initial && match = scan(/[\w.() ]*\S(?=: |:$)/)
81
+ kind = :key
82
+ key_indent = column(pos - match.size - 1)
83
+ # tokens << [key_indent.inspect, :debug]
84
+ state = :colon
85
+ when match = scan(/(?:"[^"\n]*"|'[^'\n]*')(?=: |:$)/)
86
+ tokens << [:open, :key]
87
+ tokens << [match[0,1], :delimiter]
88
+ tokens << [match[1..-2], :content]
89
+ tokens << [match[-1,1], :delimiter]
90
+ tokens << [:close, :key]
91
+ key_indent = column(pos - match.size - 1)
92
+ # tokens << [key_indent.inspect, :debug]
93
+ state = :colon
94
+ next
95
+ when scan(/(![\w\/]+)(:([\w:]+))?/)
96
+ tokens << [self[1], :type]
97
+ if self[2]
98
+ tokens << [':', :operator]
99
+ tokens << [self[3], :class]
100
+ end
101
+ next
102
+ when scan(/&\S+/)
103
+ kind = :variable
104
+ when scan(/\*\w+/)
105
+ kind = :global_variable
106
+ when scan(/<</)
107
+ kind = :class_variable
108
+ when scan(/\d\d:\d\d:\d\d/)
109
+ kind = :oct
110
+ when scan(/\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d(\.\d+)? [-+]\d\d:\d\d/)
111
+ kind = :oct
112
+ when scan(/:\w+/)
113
+ kind = :symbol
114
+ when scan(/[^:\s]+(:(?! |$)[^:\s]*)* .*/)
115
+ kind = :error
116
+ when scan(/[^:\s]+(:(?! |$)[^:\s]*)*/)
117
+ kind = :error
118
+ end
119
+
120
+ else
121
+ getch
122
+ kind = :error
123
+
124
+ end
125
+
126
+ match ||= matched
127
+
128
+ raise_inspect 'Error token %p in line %d' % [[match, kind], line], tokens if $DEBUG && !kind
129
+ raise_inspect 'Empty token', tokens unless match
130
+
131
+ tokens << [match, kind]
132
+
133
+ end
134
+
135
+ tokens
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+ end
@@ -64,6 +64,7 @@ ol.CodeRay li { white-space: pre }
64
64
  .di { color:#088; font-weight:bold }
65
65
  .dl { color:black }
66
66
  .do { color:#970 }
67
+ .dt { color:#34b }
67
68
  .ds { color:#D42; font-weight:bold }
68
69
  .e { color:#666; font-weight:bold }
69
70
  .en { color:#800; font-weight:bold }
@@ -107,15 +108,15 @@ ol.CodeRay li { white-space: pre }
107
108
  .rx .mod { color:#C2C }
108
109
  .rx .fu { color:#404; font-weight: bold }
109
110
 
110
- .s { background-color:#fff0f0 }
111
+ .s { background-color:#fff0f0; color: #D20; }
111
112
  .s .s { background-color:#ffe0e0 }
112
113
  .s .s .s { background-color:#ffd0d0 }
113
- .s .k { color: #D20; }
114
+ .s .k { }
114
115
  .s .ch { color: #b0b; }
115
116
  .s .dl { color: #710; }
116
117
 
117
- .sh { background-color:#f0fff0 }
118
- .sh .k { color:#2B2 }
118
+ .sh { background-color:#f0fff0; color:#2B2 }
119
+ .sh .k { }
119
120
  .sh .dl { color:#161 }
120
121
 
121
122
  .sy { color:#A60 }
@@ -21,6 +21,7 @@ module CodeRay
21
21
  :delimiter => 'dl',
22
22
  :directive => 'di',
23
23
  :doc => 'do',
24
+ :doctype => 'dt',
24
25
  :doc_string => 'ds',
25
26
  :entity => 'en',
26
27
  :error => 'er',
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coderay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.260
4
+ version: 0.8.263
5
5
  platform: ruby
6
6
  authors:
7
7
  - murphy
@@ -9,7 +9,7 @@ autorequire: coderay
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-08 00:00:00 +02:00
12
+ date: 2009-01-01 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -68,6 +68,7 @@ files:
68
68
  - ./lib/coderay/scanners/sql.Keith.rb
69
69
  - ./lib/coderay/scanners/sql.rb
70
70
  - ./lib/coderay/scanners/xml.rb
71
+ - ./lib/coderay/scanners/yaml.rb
71
72
  - ./lib/coderay/style.rb
72
73
  - ./lib/coderay/styles/_map.rb
73
74
  - ./lib/coderay/styles/cycnus.rb
@@ -104,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
105
  requirements:
105
106
  - strscan
106
107
  rubyforge_project: coderay
107
- rubygems_version: 1.3.0
108
+ rubygems_version: 1.3.1
108
109
  signing_key:
109
110
  specification_version: 2
110
111
  summary: CodeRay is a fast syntax highlighter engine for many languages.