coderay 0.5.0.121 → 0.7.1.147

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/FOLDERS +53 -0
  2. data/README +21 -13
  3. data/bin/coderay +79 -0
  4. data/demo/demo_cache.rb +12 -0
  5. data/demo/demo_html_list.rb +12 -0
  6. data/lib/coderay.rb +11 -2
  7. data/lib/coderay/duo.rb +29 -0
  8. data/lib/coderay/encoder.rb +4 -4
  9. data/lib/coderay/encoders/_map.rb +6 -6
  10. data/lib/coderay/encoders/count.rb +3 -3
  11. data/lib/coderay/encoders/debug.rb +38 -30
  12. data/lib/coderay/encoders/div.rb +4 -2
  13. data/lib/coderay/encoders/html.rb +9 -19
  14. data/lib/coderay/encoders/html/classes.rb +5 -2
  15. data/lib/coderay/encoders/html/css.rb +5 -6
  16. data/lib/coderay/encoders/html/numerization.rb +28 -18
  17. data/lib/coderay/encoders/html/output.rb +4 -4
  18. data/lib/coderay/encoders/null.rb +16 -16
  19. data/lib/coderay/encoders/page.rb +21 -0
  20. data/lib/coderay/encoders/span.rb +6 -3
  21. data/lib/coderay/encoders/statistic.rb +4 -2
  22. data/lib/coderay/encoders/tokens.rb +35 -35
  23. data/lib/coderay/encoders/xml.rb +53 -52
  24. data/lib/coderay/encoders/yaml.rb +13 -10
  25. data/lib/coderay/helpers/filetype.rb +41 -5
  26. data/lib/coderay/helpers/gzip_simple.rb +1 -1
  27. data/lib/coderay/helpers/plugin.rb +33 -17
  28. data/lib/coderay/scanner.rb +60 -19
  29. data/lib/coderay/scanners/_map.rb +12 -8
  30. data/lib/coderay/scanners/c.rb +16 -8
  31. data/lib/coderay/scanners/delphi.rb +9 -3
  32. data/lib/coderay/scanners/html.rb +167 -0
  33. data/lib/coderay/scanners/nitro_html.rb +125 -0
  34. data/lib/coderay/scanners/plaintext.rb +4 -2
  35. data/lib/coderay/scanners/rhtml.rb +65 -0
  36. data/lib/coderay/scanners/ruby.rb +51 -39
  37. data/lib/coderay/scanners/ruby/patterns.rb +12 -9
  38. data/lib/coderay/scanners/xml.rb +18 -0
  39. data/lib/coderay/scanners/yaml.rb +85 -0
  40. data/lib/coderay/styles/_map.rb +7 -0
  41. data/lib/coderay/styles/cycnus.rb +105 -99
  42. data/lib/coderay/styles/murphy.rb +18 -18
  43. metadata +19 -6
@@ -36,7 +36,7 @@ module Encoders
36
36
  #
37
37
  # You can also use Encoders::Div and Encoders::Span.
38
38
  #
39
- # Default: :page
39
+ # Default: nil
40
40
  #
41
41
  # === :line_numbers
42
42
  # Include line numbers in :table, :inline, :list or nil (no line numbers)
@@ -73,7 +73,7 @@ module Encoders
73
73
 
74
74
  :style => :cycnus,
75
75
 
76
- :wrap => :page,
76
+ :wrap => nil,
77
77
 
78
78
  :line_numbers => nil,
79
79
  :line_number_start => 1,
@@ -86,11 +86,6 @@ module Encoders
86
86
 
87
87
  attr_reader :css
88
88
 
89
- def initialize(*)
90
- super
91
- @last_options = nil
92
- end
93
-
94
89
  protected
95
90
 
96
91
  HTML_ESCAPE = { #:nodoc:
@@ -139,8 +134,6 @@ module Encoders
139
134
 
140
135
  def setup options
141
136
  super
142
- return if options == @last_options
143
- @last_options = options
144
137
 
145
138
  @HTML_ESCAPE = HTML_ESCAPE.dup
146
139
  @HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
@@ -205,26 +198,23 @@ module Encoders
205
198
  def finish options
206
199
  not_needed = @opened.shift
207
200
  @out << '</span>' * @opened.size
201
+ warn '%d tokens still open' % @opened.size unless @opened.empty?
208
202
 
209
203
  @out.extend Output
210
204
  @out.css = @css
211
- @out.numerize! options[:line_numbers], options # if options[:line_numbers]
212
- @out.wrap! options[:wrap] # if options[:wrap]
213
-
214
- #require 'pp'
215
- #pp @css_style, @css_style.size
205
+ @out.numerize! options[:line_numbers], options
206
+ @out.wrap! options[:wrap]
216
207
 
217
208
  super
218
209
  end
219
210
 
220
211
  def token text, type
221
- if text.is_a? String
212
+ if text.is_a? ::String
222
213
  if text =~ /#{HTML_ESCAPE_PATTERN}/o
223
214
  text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
224
215
  end
225
216
  @opened[0] = type
226
- style = @css_style[@opened]
227
- if style
217
+ if style = @css_style[@opened]
228
218
  @out << style << text << '</span>'
229
219
  else
230
220
  @out << text
@@ -233,11 +223,11 @@ module Encoders
233
223
  case text
234
224
  when :open
235
225
  @opened[0] = type
236
- @out << @css_style[@opened]
226
+ @out << (@css_style[@opened] || '<span>')
237
227
  @opened << type
238
228
  when :close
239
229
  unless @opened.empty?
240
- raise 'Not Token to be closed.' unless @opened.size > 1
230
+ raise 'Malformed token stream: Trying to close a token that was never opened.' unless @opened.size > 1
241
231
  @out << '</span>'
242
232
  @opened.pop
243
233
  end
@@ -1,4 +1,5 @@
1
- module CodeRay module Encoders
1
+ module CodeRay
2
+ module Encoders
2
3
 
3
4
  class HTML
4
5
 
@@ -20,6 +21,7 @@ module CodeRay module Encoders
20
21
  :directive => 'di',
21
22
  :doc => 'do',
22
23
  :doc_string => 'ds',
24
+ :entity => 'en',
23
25
  :error => 'er',
24
26
  :escape => 'e',
25
27
  :exception => 'ex',
@@ -67,4 +69,5 @@ module CodeRay module Encoders
67
69
 
68
70
  end
69
71
 
70
- end end
72
+ end
73
+ end
@@ -1,14 +1,12 @@
1
- module CodeRay module Encoders
1
+ module CodeRay
2
+ module Encoders
2
3
 
3
4
  class HTML
4
5
  class CSS
5
6
 
6
- DEFAULT_STYLESHEET_ID = :cycnus
7
-
8
7
  attr :stylesheet
9
8
 
10
- def CSS.load_stylesheet style = :default
11
- style = DEFAULT_STYLESHEET_ID if style == :default
9
+ def CSS.load_stylesheet style = nil
12
10
  CodeRay::Styles[style]
13
11
  end
14
12
 
@@ -58,7 +56,8 @@ module CodeRay module Encoders
58
56
  end
59
57
  end
60
58
 
61
- end end
59
+ end
60
+ end
62
61
 
63
62
  if $0 == __FILE__
64
63
  require 'pp'
@@ -1,5 +1,5 @@
1
1
  module CodeRay
2
- module Encoders
2
+ module Encoders
3
3
 
4
4
  class HTML
5
5
 
@@ -9,13 +9,13 @@ module CodeRay
9
9
  clone.numerize!(*args)
10
10
  end
11
11
 
12
- NUMERIZABLE_WRAPPINGS = {
13
- :table => [:div, :page],
12
+ =begin NUMERIZABLE_WRAPPINGS = {
13
+ :table => [:div, :page, nil],
14
14
  :inline => :all,
15
- :list => [:div, :page]
15
+ :list => [:div, :page, nil]
16
16
  }
17
17
  NUMERIZABLE_WRAPPINGS.default = :all
18
-
18
+ =end
19
19
  def numerize! mode = :table, options = {}
20
20
  return self unless mode
21
21
 
@@ -26,16 +26,17 @@ module CodeRay
26
26
  raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
27
27
  end
28
28
 
29
- allowed_wrappings = NUMERIZABLE_WRAPPINGS[mode]
30
- unless allowed_wrappings == :all or allowed_wrappings.include? options[:wrap]
31
- raise ArgumentError, "Can't numerize, :wrap must be in %p, but is %p" % [NUMERIZABLE_WRAPPINGS, options[:wrap]]
32
- end
29
+ #allowed_wrappings = NUMERIZABLE_WRAPPINGS[mode]
30
+ #unless allowed_wrappings == :all or allowed_wrappings.include? options[:wrap]
31
+ # raise ArgumentError, "Can't numerize, :wrap must be in %p, but is %p" % [NUMERIZABLE_WRAPPINGS, options[:wrap]]
32
+ #end
33
33
 
34
34
  bold_every = options[:bold_every]
35
35
  bolding =
36
- if bold_every == :no_bolding or bold_every == 0
36
+ if bold_every == false
37
37
  proc { |line| line.to_s }
38
38
  elsif bold_every.is_a? Integer
39
+ raise ArgumentError, ":bolding can't be 0." if bold_every == 0
39
40
  proc do |line|
40
41
  if line % bold_every == 0
41
42
  "<strong>#{line}</strong>" # every bold_every-th number in bold
@@ -44,22 +45,20 @@ module CodeRay
44
45
  end
45
46
  end
46
47
  else
47
- raise ArgumentError, "Invalid value %p for :bolding; :no_bolding or Integer expected." % bolding
48
+ raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
48
49
  end
49
50
 
50
- line_count = count("\n")
51
- line_count += 1 if self[-1] != ?\n
52
-
53
51
  case mode
54
52
  when :inline
55
53
  max_width = (start + line_count).to_s.size
56
54
  line = start
57
55
  gsub!(/^/) do
58
56
  line_number = bolding.call line
57
+ indent = ' ' * (max_width - line.to_s.size)
58
+ res = "<span class=\"no\">#{indent}#{line_number}</span> "
59
59
  line += 1
60
- "<span class=\"no\">#{ line_number.rjust(max_width) }</span> "
60
+ res
61
61
  end
62
- #wrap! :div
63
62
 
64
63
  when :table
65
64
  # This is really ugly.
@@ -97,13 +96,24 @@ module CodeRay
97
96
  @wrapped_in = :div
98
97
 
99
98
  else
100
- raise ArgumentError, "Unknown value %p for mode: expected one of %p" %
101
- [mode, NUMERIZABLE_WRAPPINGS.keys - [:all]]
99
+ raise ArgumentError, 'Unknown value %p for mode: expected one of %p' %
100
+ [mode, [:table, :list, :inline]]
102
101
  end
103
102
 
104
103
  self
105
104
  end
106
105
 
106
+ def line_count
107
+ line_count = count("\n")
108
+ position_of_last_newline = rindex(?\n)
109
+ if position_of_last_newline
110
+ after_last_newline = self[position_of_last_newline + 1 .. -1]
111
+ ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/]
112
+ line_count += 1 if not ends_with_newline
113
+ end
114
+ line_count
115
+ end
116
+
107
117
  end
108
118
 
109
119
  end
@@ -1,5 +1,5 @@
1
1
  module CodeRay
2
- module Encoders
2
+ module Encoders
3
3
 
4
4
  class HTML
5
5
 
@@ -158,10 +158,10 @@ module CodeRay
158
158
  DIV
159
159
 
160
160
  TABLE = <<-`TABLE`
161
- <table class="CodeRay"> <tr>
161
+ <table class="CodeRay"><tr>
162
162
  <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><%LINE_NUMBERS%></pre></td>
163
163
  <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><%CONTENT%></pre></td>
164
- </tr> </table>
164
+ </tr></table>
165
165
  TABLE
166
166
  # title="double click to expand"
167
167
 
@@ -174,7 +174,7 @@ module CodeRay
174
174
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
175
175
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="de">
176
176
  <head>
177
- <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
177
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
178
178
  <title>CodeRay HTML Encoder Example</title>
179
179
  <style type="text/css">
180
180
  <%CSS%>
@@ -1,26 +1,26 @@
1
1
  module CodeRay
2
- module Encoders
2
+ module Encoders
3
3
 
4
- # = Null Encoder
5
- #
6
- # Does nothing and returns an empty string.
7
- class Null < Encoder
4
+ # = Null Encoder
5
+ #
6
+ # Does nothing and returns an empty string.
7
+ class Null < Encoder
8
8
 
9
- include Streamable
10
- register_for :null
9
+ include Streamable
10
+ register_for :null
11
11
 
12
- # Defined for faster processing
13
- def to_proc
14
- proc {}
15
- end
16
-
17
- protected
12
+ # Defined for faster processing
13
+ def to_proc
14
+ proc {}
15
+ end
18
16
 
19
- def token(*)
20
- # do nothing
21
- end
17
+ protected
22
18
 
19
+ def token(*)
20
+ # do nothing
23
21
  end
24
22
 
25
23
  end
24
+
25
+ end
26
26
  end
@@ -0,0 +1,21 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ load :html
5
+
6
+ class Page < HTML
7
+
8
+ FILE_EXTENSION = 'html'
9
+
10
+ register_for :page
11
+
12
+ DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge({
13
+ :css => :class,
14
+ :wrap => :page,
15
+ :line_numbers => :table
16
+ })
17
+
18
+ end
19
+
20
+ end
21
+ end
@@ -1,5 +1,6 @@
1
- module CodeRay module Encoders
2
-
1
+ module CodeRay
2
+ module Encoders
3
+
3
4
  load :html
4
5
 
5
6
  class Span < HTML
@@ -12,6 +13,8 @@ module CodeRay module Encoders
12
13
  :css => :style,
13
14
  :wrap => :span,
14
15
  })
16
+
15
17
  end
16
18
 
17
- end end
19
+ end
20
+ end
@@ -1,4 +1,5 @@
1
- module CodeRay module Encoders
1
+ module CodeRay
2
+ module Encoders
2
3
 
3
4
  # Makes a statistic for the given tokens.
4
5
  class Statistic < Encoder
@@ -76,4 +77,5 @@ Token Types (%d):
76
77
 
77
78
  end
78
79
 
79
- end end
80
+ end
81
+ end
@@ -1,44 +1,44 @@
1
1
  module CodeRay
2
- module Encoders
2
+ module Encoders
3
3
 
4
- # The Tokens encoder converts the tokens to a simple
5
- # readable format. It doesn't use colors and is mainly
6
- # intended for console output.
7
- #
8
- # The tokens are converted with Tokens.write_token.
9
- #
10
- # The format is:
11
- #
12
- # <token-kind> \t <escaped token-text> \n
13
- #
14
- # Example:
15
- #
16
- # require 'coderay'
17
- # puts CodeRay.scan("puts 3 + 4", :ruby).tokens
18
- #
19
- # prints:
20
- #
21
- # ident puts
22
- # space
23
- # integer 3
24
- # space
25
- # operator +
26
- # space
27
- # integer 4
28
- #
29
- class Tokens < Encoder
4
+ # The Tokens encoder converts the tokens to a simple
5
+ # readable format. It doesn't use colors and is mainly
6
+ # intended for console output.
7
+ #
8
+ # The tokens are converted with Tokens.write_token.
9
+ #
10
+ # The format is:
11
+ #
12
+ # <token-kind> \t <escaped token-text> \n
13
+ #
14
+ # Example:
15
+ #
16
+ # require 'coderay'
17
+ # puts CodeRay.scan("puts 3 + 4", :ruby).tokens
18
+ #
19
+ # prints:
20
+ #
21
+ # ident puts
22
+ # space
23
+ # integer 3
24
+ # space
25
+ # operator +
26
+ # space
27
+ # integer 4
28
+ #
29
+ class Tokens < Encoder
30
30
 
31
- include Streamable
32
- register_for :tokens
31
+ include Streamable
32
+ register_for :tokens
33
33
 
34
- FILE_EXTENSION = 'tok'
35
-
36
- protected
37
- def token *args
38
- @out << CodeRay::Tokens.write_token(*args)
39
- end
34
+ FILE_EXTENSION = 'tok'
40
35
 
36
+ protected
37
+ def token *args
38
+ @out << CodeRay::Tokens.write_token(*args)
41
39
  end
42
40
 
43
41
  end
42
+
43
+ end
44
44
  end
@@ -1,70 +1,71 @@
1
1
  module CodeRay
2
- module Encoders
2
+ module Encoders
3
3
 
4
- # = Debug Encoder
5
- class XML < Encoder
4
+ # = XML Encoder
5
+ #
6
+ # Uses REXML. Very slow.
7
+ class XML < Encoder
6
8
 
7
- include Streamable
8
- register_for :xml
9
+ include Streamable
10
+ register_for :xml
9
11
 
10
- FILE_EXTENSION = 'xml'
12
+ FILE_EXTENSION = 'xml'
11
13
 
12
- require 'rexml/document'
14
+ require 'rexml/document'
13
15
 
14
- DEFAULT_OPTIONS = {
15
- :tab_width => 8,
16
- :pretty => -1,
17
- :transitive => false,
18
- }
16
+ DEFAULT_OPTIONS = {
17
+ :tab_width => 8,
18
+ :pretty => -1,
19
+ :transitive => false,
20
+ }
19
21
 
20
- protected
21
-
22
- def setup options
23
- @out = ''
24
- @doc = REXML::Document.new
25
- @doc << REXML::XMLDecl.new
26
- @tab_width = options[:tab_width]
27
- @root = @node = @doc.add_element('coderay-tokens')
28
- end
29
-
30
- def finish options
31
- @doc.write @out, options[:pretty], options[:transitive], true
32
- @out
22
+ protected
23
+
24
+ def setup options
25
+ @out = ''
26
+ @doc = REXML::Document.new
27
+ @doc << REXML::XMLDecl.new
28
+ @tab_width = options[:tab_width]
29
+ @root = @node = @doc.add_element('coderay-tokens')
30
+ end
31
+
32
+ def finish options
33
+ @doc.write @out, options[:pretty], options[:transitive], true
34
+ @out
35
+ end
36
+
37
+ def text_token text, kind
38
+ if kind == :space
39
+ token = @node
40
+ else
41
+ token = @node.add_element kind.to_s
33
42
  end
34
-
35
- def text_token text, kind
36
- if kind == :space
37
- token = @node
43
+ text.scan(/(\x20+)|(\t+)|(\n)|[^\x20\t\n]+/) do |space, tab, nl|
44
+ case
45
+ when space
46
+ token << REXML::Text.new(space, true)
47
+ when tab
48
+ token << REXML::Text.new(tab, true)
49
+ when nl
50
+ token << REXML::Text.new(nl, true)
38
51
  else
39
- token = @node.add_element kind.to_s
40
- end
41
- text.scan(/(\x20+)|(\t+)|(\n)|[^\x20\t\n]+/) do |space, tab, nl|
42
- case
43
- when space
44
- token << REXML::Text.new(space, true)
45
- when tab
46
- token << REXML::Text.new(tab, true)
47
- when nl
48
- token << REXML::Text.new(nl, true)
49
- else
50
- token << REXML::Text.new($&)
51
- end
52
+ token << REXML::Text.new($&)
52
53
  end
53
54
  end
55
+ end
54
56
 
55
- def open_token kind
56
- @node = @node.add_element kind.to_s
57
- end
57
+ def open_token kind
58
+ @node = @node.add_element kind.to_s
59
+ end
58
60
 
59
- def close_token kind
60
- if @node == @root
61
- raise 'no token to close!'
62
- end
63
- @node = @node.parent
61
+ def close_token kind
62
+ if @node == @root
63
+ raise 'no token to close!'
64
64
  end
65
-
65
+ @node = @node.parent
66
66
  end
67
67
 
68
68
  end
69
+
70
+ end
69
71
  end
70
-