coderay 0.9.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. data/{lib/README → README_INDEX.rdoc} +10 -21
  2. data/Rakefile +6 -6
  3. data/bin/coderay +193 -64
  4. data/lib/coderay.rb +61 -105
  5. data/lib/coderay/duo.rb +17 -21
  6. data/lib/coderay/encoder.rb +100 -112
  7. data/lib/coderay/encoders/_map.rb +12 -7
  8. data/lib/coderay/encoders/comment_filter.rb +12 -30
  9. data/lib/coderay/encoders/count.rb +29 -11
  10. data/lib/coderay/encoders/debug.rb +32 -20
  11. data/lib/coderay/encoders/div.rb +13 -9
  12. data/lib/coderay/encoders/filter.rb +34 -51
  13. data/lib/coderay/encoders/html.rb +155 -161
  14. data/lib/coderay/encoders/html/css.rb +4 -9
  15. data/lib/coderay/encoders/html/numbering.rb +115 -0
  16. data/lib/coderay/encoders/html/output.rb +22 -70
  17. data/lib/coderay/encoders/json.rb +59 -45
  18. data/lib/coderay/encoders/lines_of_code.rb +12 -57
  19. data/lib/coderay/encoders/null.rb +6 -14
  20. data/lib/coderay/encoders/page.rb +13 -9
  21. data/lib/coderay/encoders/span.rb +13 -9
  22. data/lib/coderay/encoders/statistic.rb +58 -39
  23. data/lib/coderay/encoders/terminal.rb +179 -0
  24. data/lib/coderay/encoders/text.rb +31 -17
  25. data/lib/coderay/encoders/token_kind_filter.rb +111 -0
  26. data/lib/coderay/encoders/xml.rb +19 -18
  27. data/lib/coderay/encoders/yaml.rb +37 -9
  28. data/lib/coderay/for_redcloth.rb +4 -4
  29. data/lib/coderay/helpers/file_type.rb +127 -246
  30. data/lib/coderay/helpers/gzip.rb +41 -0
  31. data/lib/coderay/helpers/plugin.rb +241 -306
  32. data/lib/coderay/helpers/word_list.rb +65 -126
  33. data/lib/coderay/scanner.rb +173 -156
  34. data/lib/coderay/scanners/_map.rb +18 -17
  35. data/lib/coderay/scanners/c.rb +63 -77
  36. data/lib/coderay/scanners/clojure.rb +217 -0
  37. data/lib/coderay/scanners/cpp.rb +71 -84
  38. data/lib/coderay/scanners/css.rb +103 -120
  39. data/lib/coderay/scanners/debug.rb +47 -44
  40. data/lib/coderay/scanners/delphi.rb +70 -76
  41. data/lib/coderay/scanners/diff.rb +141 -50
  42. data/lib/coderay/scanners/erb.rb +81 -0
  43. data/lib/coderay/scanners/groovy.rb +104 -113
  44. data/lib/coderay/scanners/haml.rb +168 -0
  45. data/lib/coderay/scanners/html.rb +181 -110
  46. data/lib/coderay/scanners/java.rb +73 -75
  47. data/lib/coderay/scanners/java/builtin_types.rb +2 -0
  48. data/lib/coderay/scanners/java_script.rb +90 -101
  49. data/lib/coderay/scanners/json.rb +40 -53
  50. data/lib/coderay/scanners/php.rb +123 -147
  51. data/lib/coderay/scanners/python.rb +93 -91
  52. data/lib/coderay/scanners/raydebug.rb +66 -0
  53. data/lib/coderay/scanners/ruby.rb +343 -326
  54. data/lib/coderay/scanners/ruby/patterns.rb +40 -106
  55. data/lib/coderay/scanners/ruby/string_state.rb +71 -0
  56. data/lib/coderay/scanners/sql.rb +80 -66
  57. data/lib/coderay/scanners/text.rb +26 -0
  58. data/lib/coderay/scanners/xml.rb +1 -1
  59. data/lib/coderay/scanners/yaml.rb +74 -73
  60. data/lib/coderay/style.rb +10 -7
  61. data/lib/coderay/styles/_map.rb +3 -3
  62. data/lib/coderay/styles/alpha.rb +143 -0
  63. data/lib/coderay/token_kinds.rb +90 -0
  64. data/lib/coderay/tokens.rb +102 -277
  65. data/lib/coderay/tokens_proxy.rb +55 -0
  66. data/lib/coderay/version.rb +3 -0
  67. data/test/functional/basic.rb +200 -18
  68. data/test/functional/examples.rb +130 -0
  69. data/test/functional/for_redcloth.rb +15 -8
  70. data/test/functional/suite.rb +9 -6
  71. metadata +103 -123
  72. data/FOLDERS +0 -53
  73. data/bin/coderay_stylesheet +0 -4
  74. data/lib/coderay/encoders/html/numerization.rb +0 -133
  75. data/lib/coderay/encoders/term.rb +0 -158
  76. data/lib/coderay/encoders/token_class_filter.rb +0 -84
  77. data/lib/coderay/helpers/gzip_simple.rb +0 -123
  78. data/lib/coderay/scanners/nitro_xhtml.rb +0 -136
  79. data/lib/coderay/scanners/plaintext.rb +0 -20
  80. data/lib/coderay/scanners/rhtml.rb +0 -78
  81. data/lib/coderay/scanners/scheme.rb +0 -145
  82. data/lib/coderay/styles/cycnus.rb +0 -152
  83. data/lib/coderay/styles/murphy.rb +0 -134
  84. data/lib/coderay/token_classes.rb +0 -86
  85. data/test/functional/load_plugin_scanner.rb +0 -11
  86. data/test/functional/vhdl.rb +0 -126
  87. data/test/functional/word_list.rb +0 -79
@@ -1,158 +0,0 @@
1
- # encoders/term.rb
2
- # By Rob Aldred (http://robaldred.co.uk)
3
- # Based on idea by Nathan Weizenbaum (http://nex-3.com)
4
- # MIT License (http://www.opensource.org/licenses/mit-license.php)
5
- #
6
- # A CodeRay encoder that outputs code highlighted for a color terminal.
7
- # Check out http://robaldred.co.uk
8
-
9
- module CodeRay
10
- module Encoders
11
- class Term < Encoder
12
- register_for :term
13
-
14
- TOKEN_COLORS = {
15
- :annotation => '35',
16
- :attribute_name => '33',
17
- :attribute_name_fat => '33',
18
- :attribute_value => '31',
19
- :attribute_value_fat => '31',
20
- :bin => '1;35',
21
- :char => {:self => '36', :delimiter => '34'},
22
- :class => '1;35',
23
- :class_variable => '36',
24
- :color => '32',
25
- :comment => '37',
26
- :complex => '34',
27
- :constant => ['34', '4'],
28
- :decoration => '35',
29
- :definition => '1;32',
30
- :directive => ['32', '4'],
31
- :doc => '46',
32
- :doctype => '1;30',
33
- :doc_string => ['31', '4'],
34
- :entity => '33',
35
- :error => ['1;33', '41'],
36
- :exception => '1;31',
37
- :float => '1;35',
38
- :function => '1;34',
39
- :global_variable => '42',
40
- :hex => '1;36',
41
- :important => '1;31',
42
- :include => '33',
43
- :integer => '1;34',
44
- :interpreted => '1;35',
45
- :key => '35',
46
- :label => '1;4',
47
- :local_variable => '33',
48
- :oct => '1;35',
49
- :operator_name => '1;29',
50
- :pre_constant => '1;36',
51
- :pre_type => '1;30',
52
- :predefined => ['4', '1;34'],
53
- :preprocessor => '36',
54
- :pseudo_class => '34',
55
- :regexp => {
56
- :content => '31',
57
- :delimiter => '1;29',
58
- :modifier => '35',
59
- :function => '1;29'
60
- },
61
- :reserved => '1;31',
62
- :shell => {
63
- :self => '42',
64
- :content => '1;29',
65
- :delimiter => '37',
66
- },
67
- :string => {
68
- :self => '32',
69
- :modifier => '1;32',
70
- :escape => '1;36',
71
- :delimiter => '1;32',
72
- },
73
- :symbol => '1;32',
74
- :tag => '34',
75
- :tag_fat => '1;34',
76
- :tag_special => ['34', '4'],
77
- :type => '1;34',
78
- :value => '36',
79
- :variable => '34',
80
- :insert => '42',
81
- :delete => '41',
82
- :change => '44',
83
- :head => '45',
84
- }
85
- TOKEN_COLORS[:keyword] = TOKEN_COLORS[:reserved]
86
- TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
87
- TOKEN_COLORS[:imaginary] = TOKEN_COLORS[:complex]
88
- TOKEN_COLORS[:open] = TOKEN_COLORS[:close] = TOKEN_COLORS[:nesting_delimiter] = TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
89
-
90
- protected
91
-
92
- def setup(options)
93
- @out = ''
94
- @opened = [nil]
95
- @subcolors = nil
96
- end
97
-
98
- def finish(options)
99
- super
100
- end
101
-
102
- def token text, type = :plain
103
- case text
104
-
105
- when nil
106
- # raise 'Token with nil as text was given: %p' % [[text, type]]
107
-
108
- when String
109
-
110
- if color = (@subcolors || TOKEN_COLORS)[type]
111
- color = color[:self] || return if Hash === color
112
-
113
- @out << col(color) + text.gsub("\n", col(0) + "\n" + col(color)) + col(0)
114
- @out << col(@subcolors[:self]) if @subcolors && @subcolors[:self]
115
- else
116
- @out << text
117
- end
118
-
119
- # token groups, eg. strings
120
- when :open
121
- @opened[0] = type
122
- if color = TOKEN_COLORS[type]
123
- if Hash === color
124
- @subcolors = color
125
- @out << col(color[:self]) if color[:self]
126
- else
127
- @subcolors = {}
128
- @out << col(color)
129
- end
130
- end
131
- @opened << type
132
- when :close
133
- if @opened.empty?
134
- # nothing to close
135
- else
136
- @out << col(0) if (@subcolors || {})[:self]
137
- @subcolors = nil
138
- @opened.pop
139
- end
140
-
141
- # whole lines to be highlighted, eg. a added/modified/deleted lines in a diff
142
- when :begin_line
143
-
144
- when :end_line
145
-
146
- else
147
- raise 'unknown token kind: %p' % [text]
148
- end
149
- end
150
-
151
- private
152
-
153
- def col(color)
154
- Array(color).map { |c| "\e[#{c}m" }.join
155
- end
156
- end
157
- end
158
- end
@@ -1,84 +0,0 @@
1
- ($:.unshift '../..'; require 'coderay') unless defined? CodeRay
2
- module CodeRay
3
- module Encoders
4
-
5
- load :filter
6
-
7
- class TokenClassFilter < Filter
8
-
9
- include Streamable
10
- register_for :token_class_filter
11
-
12
- DEFAULT_OPTIONS = {
13
- :exclude => [],
14
- :include => :all
15
- }
16
-
17
- protected
18
- def setup options
19
- super
20
- @exclude = options[:exclude]
21
- @exclude = Array(@exclude) unless @exclude == :all
22
- @include = options[:include]
23
- @include = Array(@include) unless @include == :all
24
- end
25
-
26
- def include_text_token? text, kind
27
- (@include == :all || @include.include?(kind)) &&
28
- !(@exclude == :all || @exclude.include?(kind))
29
- end
30
-
31
- end
32
-
33
- end
34
- end
35
-
36
- if $0 == __FILE__
37
- $VERBOSE = true
38
- $: << File.join(File.dirname(__FILE__), '..')
39
- eval DATA.read, nil, $0, __LINE__ + 4
40
- end
41
-
42
- __END__
43
- require 'test/unit'
44
-
45
- class TokenClassFilterTest < Test::Unit::TestCase
46
-
47
- def test_creation
48
- assert CodeRay::Encoders::TokenClassFilter < CodeRay::Encoders::Encoder
49
- assert CodeRay::Encoders::TokenClassFilter < CodeRay::Encoders::Filter
50
- filter = nil
51
- assert_nothing_raised do
52
- filter = CodeRay.encoder :token_class_filter
53
- end
54
- assert_instance_of CodeRay::Encoders::TokenClassFilter, filter
55
- end
56
-
57
- def test_filtering_text_tokens
58
- tokens = CodeRay::Tokens.new
59
- for i in 1..10
60
- tokens << [i.to_s, :index]
61
- tokens << [' ', :space] if i < 10
62
- end
63
- assert_equal 10, CodeRay::Encoders::TokenClassFilter.new.encode_tokens(tokens, :exclude => :space).size
64
- assert_equal 10, tokens.token_class_filter(:exclude => :space).size
65
- assert_equal 9, CodeRay::Encoders::TokenClassFilter.new.encode_tokens(tokens, :include => :space).size
66
- assert_equal 9, tokens.token_class_filter(:include => :space).size
67
- assert_equal 0, CodeRay::Encoders::TokenClassFilter.new.encode_tokens(tokens, :exclude => :all).size
68
- assert_equal 0, tokens.token_class_filter(:exclude => :all).size
69
- end
70
-
71
- def test_filtering_block_tokens
72
- tokens = CodeRay::Tokens.new
73
- 10.times do |i|
74
- tokens << [:open, :index]
75
- tokens << [i.to_s, :content]
76
- tokens << [:close, :index]
77
- end
78
- assert_equal 20, CodeRay::Encoders::TokenClassFilter.new.encode_tokens(tokens, :include => :blubb).size
79
- assert_equal 20, tokens.token_class_filter(:include => :blubb).size
80
- assert_equal 30, CodeRay::Encoders::TokenClassFilter.new.encode_tokens(tokens, :exclude => :index).size
81
- assert_equal 30, tokens.token_class_filter(:exclude => :index).size
82
- end
83
-
84
- end
@@ -1,123 +0,0 @@
1
- # =GZip Simple
2
- #
3
- # A simplified interface to the gzip library +zlib+ (from the Ruby Standard Library.)
4
- #
5
- # Author: murphy (mail to murphy rubychan de)
6
- #
7
- # Version: 0.2 (2005.may.28)
8
- #
9
- # ==Documentation
10
- #
11
- # See +GZip+ module and the +String+ extensions.
12
- #
13
- module GZip
14
-
15
- require 'zlib'
16
-
17
- # The default zipping level. 7 zips good and fast.
18
- DEFAULT_GZIP_LEVEL = 7
19
-
20
- # Unzips the given string +s+.
21
- #
22
- # Example:
23
- # require 'gzip_simple'
24
- # print GZip.gunzip(File.read('adresses.gz'))
25
- def GZip.gunzip s
26
- Zlib::Inflate.inflate s
27
- end
28
-
29
- # Zips the given string +s+.
30
- #
31
- # Example:
32
- # require 'gzip_simple'
33
- # File.open('adresses.gz', 'w') do |file
34
- # file.write GZip.gzip('Mum: 0123 456 789', 9)
35
- # end
36
- #
37
- # If you provide a +level+, you can control how strong
38
- # the string is compressed:
39
- # - 0: no compression, only convert to gzip format
40
- # - 1: compress fast
41
- # - 7: compress more, but still fast (default)
42
- # - 8: compress more, slower
43
- # - 9: compress best, very slow
44
- def GZip.gzip s, level = DEFAULT_GZIP_LEVEL
45
- Zlib::Deflate.new(level).deflate s, Zlib::FINISH
46
- end
47
- end
48
-
49
-
50
- # String extensions to use the GZip module.
51
- #
52
- # The methods gzip and gunzip provide an even more simple
53
- # interface to the ZLib:
54
- #
55
- # # create a big string
56
- # x = 'a' * 1000
57
- #
58
- # # zip it
59
- # x_gz = x.gzip
60
- #
61
- # # test the result
62
- # puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size]
63
- # #-> Zipped 1000 bytes to 19 bytes.
64
- #
65
- # # unzipping works
66
- # p x_gz.gunzip == x #-> true
67
- class String
68
- # Returns the string, unzipped.
69
- # See GZip.gunzip
70
- def gunzip
71
- GZip.gunzip self
72
- end
73
- # Replaces the string with its unzipped value.
74
- # See GZip.gunzip
75
- def gunzip!
76
- replace gunzip
77
- end
78
-
79
- # Returns the string, zipped.
80
- # +level+ is the gzip compression level, see GZip.gzip.
81
- def gzip level = GZip::DEFAULT_GZIP_LEVEL
82
- GZip.gzip self, level
83
- end
84
- # Replaces the string with its zipped value.
85
- # See GZip.gzip.
86
- def gzip!(*args)
87
- replace gzip(*args)
88
- end
89
- end
90
-
91
- if $0 == __FILE__
92
- eval DATA.read, nil, $0, __LINE__+4
93
- end
94
-
95
- __END__
96
- #CODE
97
-
98
- # Testing / Benchmark
99
- x = 'a' * 1000
100
- x_gz = x.gzip
101
- puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size] #-> Zipped 1000 bytes to 19 bytes.
102
- p x_gz.gunzip == x #-> true
103
-
104
- require 'benchmark'
105
-
106
- INFO = 'packed to %0.3f%%' # :nodoc:
107
-
108
- x = Array.new(100000) { rand(255).chr + 'aaaaaaaaa' + rand(255).chr }.join
109
- Benchmark.bm(10) do |bm|
110
- for level in 0..9
111
- bm.report "zip #{level}" do
112
- $x = x.gzip level
113
- end
114
- puts INFO % [100.0 * $x.size / x.size]
115
- end
116
- bm.report 'zip' do
117
- $x = x.gzip
118
- end
119
- puts INFO % [100.0 * $x.size / x.size]
120
- bm.report 'unzip' do
121
- $x.gunzip
122
- end
123
- end
@@ -1,136 +0,0 @@
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_rest) && !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