coderay 1.0.9 → 1.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +2 -0
  3. data/bin/coderay +4 -4
  4. data/lib/coderay.rb +2 -3
  5. data/lib/coderay/encoders/debug.rb +5 -17
  6. data/lib/coderay/encoders/debug_lint.rb +62 -0
  7. data/lib/coderay/encoders/html.rb +84 -84
  8. data/lib/coderay/encoders/html/css.rb +7 -7
  9. data/lib/coderay/encoders/html/numbering.rb +24 -19
  10. data/lib/coderay/encoders/html/output.rb +1 -1
  11. data/lib/coderay/encoders/lint.rb +57 -0
  12. data/lib/coderay/encoders/statistic.rb +0 -1
  13. data/lib/coderay/encoders/terminal.rb +121 -105
  14. data/lib/coderay/helpers/file_type.rb +54 -47
  15. data/lib/coderay/helpers/plugin.rb +4 -13
  16. data/lib/coderay/scanner.rb +58 -26
  17. data/lib/coderay/scanners/c.rb +1 -1
  18. data/lib/coderay/scanners/cpp.rb +1 -1
  19. data/lib/coderay/scanners/css.rb +22 -25
  20. data/lib/coderay/scanners/diff.rb +53 -31
  21. data/lib/coderay/scanners/groovy.rb +17 -4
  22. data/lib/coderay/scanners/html.rb +38 -16
  23. data/lib/coderay/scanners/java.rb +1 -1
  24. data/lib/coderay/scanners/java_script.rb +30 -6
  25. data/lib/coderay/scanners/json.rb +15 -12
  26. data/lib/coderay/scanners/lua.rb +280 -0
  27. data/lib/coderay/scanners/php.rb +22 -4
  28. data/lib/coderay/scanners/python.rb +3 -3
  29. data/lib/coderay/scanners/raydebug.rb +8 -8
  30. data/lib/coderay/scanners/ruby.rb +2 -2
  31. data/lib/coderay/scanners/sass.rb +232 -0
  32. data/lib/coderay/scanners/sql.rb +7 -4
  33. data/lib/coderay/scanners/taskpaper.rb +36 -0
  34. data/lib/coderay/scanners/yaml.rb +2 -2
  35. data/lib/coderay/styles/alpha.rb +31 -21
  36. data/lib/coderay/token_kinds.rb +68 -71
  37. data/lib/coderay/tokens.rb +23 -77
  38. data/lib/coderay/version.rb +1 -1
  39. data/test/functional/examples.rb +3 -3
  40. data/test/functional/for_redcloth.rb +4 -10
  41. metadata +13 -14
  42. data/lib/coderay/helpers/gzip.rb +0 -41
@@ -47,7 +47,7 @@ module Scanners
47
47
  when !check(/(?:"[^"]*")(?=: |:$)/) && match = scan(/"/)
48
48
  encoder.begin_group :string
49
49
  encoder.text_token match, :delimiter
50
- encoder.text_token match, :content if match = scan(/ [^"\\]* (?: \\. [^"\\]* )* /mx)
50
+ encoder.text_token match, :content if (match = scan(/ [^"\\]* (?: \\. [^"\\]* )* /mx)) && !match.empty?
51
51
  encoder.text_token match, :delimiter if match = scan(/"/)
52
52
  encoder.end_group :string
53
53
  next
@@ -84,7 +84,7 @@ module Scanners
84
84
  when match = scan(/(?:"[^"\n]*"|'[^'\n]*')(?= *:(?: |$))/)
85
85
  encoder.begin_group :key
86
86
  encoder.text_token match[0,1], :delimiter
87
- encoder.text_token match[1..-2], :content
87
+ encoder.text_token match[1..-2], :content if match.size > 2
88
88
  encoder.text_token match[-1,1], :delimiter
89
89
  encoder.end_group :key
90
90
  key_indent = column(pos - match.size) - 1
@@ -3,14 +3,14 @@ module Styles
3
3
 
4
4
  # A colorful theme using CSS 3 colors (with alpha channel).
5
5
  class Alpha < Style
6
-
6
+
7
7
  register_for :alpha
8
-
8
+
9
9
  code_background = 'hsl(0,0%,95%)'
10
10
  numbers_background = 'hsl(180,65%,90%)'
11
11
  border_color = 'silver'
12
12
  normal_color = 'black'
13
-
13
+
14
14
  CSS_MAIN_STYLES = <<-MAIN # :nodoc:
15
15
  .CodeRay {
16
16
  background-color: #{code_background};
@@ -39,6 +39,9 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
39
39
  color: gray !important;
40
40
  text-decoration: none !important;
41
41
  }
42
+ .CodeRay .line-numbers pre {
43
+ word-break: normal;
44
+ }
42
45
  .CodeRay .line-numbers a:target { color: blue !important; }
43
46
  .CodeRay .line-numbers .highlighted { color: red !important; }
44
47
  .CodeRay .line-numbers .highlighted a { color: red !important; }
@@ -53,45 +56,52 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
53
56
  .annotation { color:#007 }
54
57
  .attribute-name { color:#b48 }
55
58
  .attribute-value { color:#700 }
56
- .binary { color:#509 }
59
+ .binary { color:#549 }
60
+ .binary .char { color:#325 }
61
+ .binary .delimiter { color:#325 }
62
+ .char { color:#D20 }
57
63
  .char .content { color:#D20 }
58
64
  .char .delimiter { color:#710 }
59
- .char { color:#D20 }
60
65
  .class { color:#B06; font-weight:bold }
61
66
  .class-variable { color:#369 }
62
67
  .color { color:#0A0 }
63
68
  .comment { color:#777 }
64
69
  .comment .char { color:#444 }
65
70
  .comment .delimiter { color:#444 }
66
- .complex { color:#A08 }
67
71
  .constant { color:#036; font-weight:bold }
68
72
  .decorator { color:#B0B }
69
73
  .definition { color:#099; font-weight:bold }
70
74
  .delimiter { color:black }
71
75
  .directive { color:#088; font-weight:bold }
72
- .doc { color:#970 }
73
- .doc-string { color:#D42; font-weight:bold }
76
+ .docstring { color:#D42; }
74
77
  .doctype { color:#34b }
78
+ .done { text-decoration: line-through; color: gray }
75
79
  .entity { color:#800; font-weight:bold }
76
80
  .error { color:#F00; background-color:#FAA }
77
81
  .escape { color:#666 }
78
82
  .exception { color:#C00; font-weight:bold }
79
83
  .float { color:#60E }
80
84
  .function { color:#06B; font-weight:bold }
85
+ .function .delimiter { color:#024; font-weight:bold }
81
86
  .global-variable { color:#d70 }
82
87
  .hex { color:#02b }
83
- .imaginary { color:#f00 }
88
+ .id { color:#33D; font-weight:bold }
84
89
  .include { color:#B44; font-weight:bold }
85
90
  .inline { background-color: hsla(0,0%,0%,0.07); color: black }
86
91
  .inline-delimiter { font-weight: bold; color: #666 }
87
92
  .instance-variable { color:#33B }
88
93
  .integer { color:#00D }
94
+ .imaginary { color:#f00 }
95
+ .important { color:#D00 }
96
+ .key { color: #606 }
89
97
  .key .char { color: #60f }
90
98
  .key .delimiter { color: #404 }
91
- .key { color: #606 }
92
99
  .keyword { color:#080; font-weight:bold }
93
100
  .label { color:#970; font-weight:bold }
94
- .local-variable { color:#963 }
101
+ .local-variable { color:#950 }
102
+ .map .content { color:#808 }
103
+ .map .delimiter { color:#40A}
104
+ .map { background-color:hsla(200,100%,50%,0.06); }
95
105
  .namespace { color:#707; font-weight:bold }
96
106
  .octal { color:#40E }
97
107
  .operator { }
@@ -100,30 +110,30 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
100
110
  .predefined-type { color:#0a5; font-weight:bold }
101
111
  .preprocessor { color:#579 }
102
112
  .pseudo-class { color:#00C; font-weight:bold }
113
+ .regexp { background-color:hsla(300,100%,50%,0.06); }
103
114
  .regexp .content { color:#808 }
104
115
  .regexp .delimiter { color:#404 }
105
116
  .regexp .modifier { color:#C2C }
106
- .regexp { background-color:hsla(300,100%,50%,0.06); }
107
117
  .reserved { color:#080; font-weight:bold }
118
+ .shell { background-color:hsla(120,100%,50%,0.06); }
108
119
  .shell .content { color:#2B2 }
109
120
  .shell .delimiter { color:#161 }
110
- .shell { background-color:hsla(120,100%,50%,0.06); }
121
+ .string { background-color:hsla(0,100%,50%,0.05); }
111
122
  .string .char { color: #b0b }
112
123
  .string .content { color: #D20 }
113
124
  .string .delimiter { color: #710 }
114
125
  .string .modifier { color: #E40 }
115
- .string { background-color:hsla(0,100%,50%,0.05); }
126
+ .symbol { color:#A60 }
116
127
  .symbol .content { color:#A60 }
117
128
  .symbol .delimiter { color:#630 }
118
- .symbol { color:#A60 }
119
- .tag { color:#070 }
129
+ .tag { color:#070; font-weight:bold }
120
130
  .type { color:#339; font-weight:bold }
121
- .value { color: #088; }
122
- .variable { color:#037 }
131
+ .value { color: #088 }
132
+ .variable { color:#037 }
123
133
 
124
134
  .insert { background: hsla(120,100%,50%,0.12) }
125
135
  .delete { background: hsla(0,100%,50%,0.12) }
126
- .change { color: #bbf; background: #007; }
136
+ .change { color: #bbf; background: #007 }
127
137
  .head { color: #f8f; background: #505 }
128
138
  .head .filename { color: white; }
129
139
 
@@ -135,8 +145,8 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
135
145
  .change .change { color: #88f }
136
146
  .head .head { color: #f4f }
137
147
  TOKENS
138
-
148
+
139
149
  end
140
-
150
+
141
151
  end
142
152
  end
@@ -10,81 +10,78 @@ module CodeRay
10
10
  TokenKinds.compare_by_identity if TokenKinds.respond_to? :compare_by_identity
11
11
 
12
12
  TokenKinds.update( # :nodoc:
13
- :annotation => 'annotation',
14
- :attribute_name => 'attribute-name',
15
- :attribute_value => 'attribute-value',
16
- :binary => 'bin',
17
- :char => 'char',
18
- :class => 'class',
19
- :class_variable => 'class-variable',
20
- :color => 'color',
21
- :comment => 'comment',
22
- :complex => 'complex',
23
- :constant => 'constant',
24
- :content => 'content',
25
- :debug => 'debug',
26
- :decorator => 'decorator',
27
- :definition => 'definition',
28
- :delimiter => 'delimiter',
29
- :directive => 'directive',
30
- :doc => 'doc',
31
- :doctype => 'doctype',
32
- :doc_string => 'doc-string',
33
- :entity => 'entity',
34
- :error => 'error',
35
- :escape => 'escape',
36
- :exception => 'exception',
37
- :filename => 'filename',
38
- :float => 'float',
39
- :function => 'function',
40
- :global_variable => 'global-variable',
41
- :hex => 'hex',
42
- :imaginary => 'imaginary',
43
- :important => 'important',
44
- :include => 'include',
45
- :inline => 'inline',
46
- :inline_delimiter => 'inline-delimiter',
47
- :instance_variable => 'instance-variable',
48
- :integer => 'integer',
49
- :key => 'key',
50
- :keyword => 'keyword',
51
- :label => 'label',
52
- :local_variable => 'local-variable',
53
- :modifier => 'modifier',
54
- :namespace => 'namespace',
55
- :octal => 'octal',
56
- :predefined => 'predefined',
57
- :predefined_constant => 'predefined-constant',
58
- :predefined_type => 'predefined-type',
59
- :preprocessor => 'preprocessor',
60
- :pseudo_class => 'pseudo-class',
61
- :regexp => 'regexp',
62
- :reserved => 'reserved',
63
- :shell => 'shell',
64
- :string => 'string',
65
- :symbol => 'symbol',
66
- :tag => 'tag',
67
- :type => 'type',
68
- :value => 'value',
69
- :variable => 'variable',
13
+ :debug => 'debug', # highlight for debugging (white on blue background)
70
14
 
71
- :change => 'change',
72
- :delete => 'delete',
73
- :head => 'head',
74
- :insert => 'insert',
15
+ :annotation => 'annotation', # Groovy, Java
16
+ :attribute_name => 'attribute-name', # HTML, CSS
17
+ :attribute_value => 'attribute-value', # HTML
18
+ :binary => 'binary', # Python, Ruby
19
+ :char => 'char', # most scanners, also inside of strings
20
+ :class => 'class', # lots of scanners, for different purposes also in CSS
21
+ :class_variable => 'class-variable', # Ruby, YAML
22
+ :color => 'color', # CSS
23
+ :comment => 'comment', # most scanners
24
+ :constant => 'constant', # PHP, Ruby
25
+ :content => 'content', # inside of strings, most scanners
26
+ :decorator => 'decorator', # Python
27
+ :definition => 'definition', # CSS
28
+ :delimiter => 'delimiter', # inside strings, comments and other types
29
+ :directive => 'directive', # lots of scanners
30
+ :doctype => 'doctype', # Goorvy, HTML, Ruby, YAML
31
+ :docstring => 'docstring', # Python
32
+ :done => 'done', # Taskpaper
33
+ :entity => 'entity', # HTML
34
+ :error => 'error', # invalid token, most scanners
35
+ :escape => 'escape', # Ruby (string inline variables like #$foo, #@bar)
36
+ :exception => 'exception', # Java, PHP, Python
37
+ :filename => 'filename', # Diff
38
+ :float => 'float', # most scanners
39
+ :function => 'function', # CSS, JavaScript, PHP
40
+ :global_variable => 'global-variable', # Ruby, YAML
41
+ :hex => 'hex', # hexadecimal number; lots of scanners
42
+ :id => 'id', # CSS
43
+ :imaginary => 'imaginary', # Python
44
+ :important => 'important', # CSS, Taskpaper
45
+ :include => 'include', # C, Groovy, Java, Python, Sass
46
+ :inline => 'inline', # nested code, eg. inline string evaluation; lots of scanners
47
+ :inline_delimiter => 'inline-delimiter', # used instead of :inline > :delimiter FIXME: Why use inline_delimiter?
48
+ :instance_variable => 'instance-variable', # Ruby
49
+ :integer => 'integer', # most scanners
50
+ :key => 'key', # lots of scanners, used together with :value
51
+ :keyword => 'keyword', # reserved word that's actually implemented; most scanners
52
+ :label => 'label', # C, PHP
53
+ :local_variable => 'local-variable', # local and magic variables; some scanners
54
+ :map => 'map', # Lua tables
55
+ :modifier => 'modifier', # used inside on strings; lots of scanners
56
+ :namespace => 'namespace', # Clojure, Java, Taskpaper
57
+ :octal => 'octal', # lots of scanners
58
+ :predefined => 'predefined', # predefined function: lots of scanners
59
+ :predefined_constant => 'predefined-constant',# lots of scanners
60
+ :predefined_type => 'predefined-type', # C, Java, PHP
61
+ :preprocessor => 'preprocessor', # C, Delphi, HTML
62
+ :pseudo_class => 'pseudo-class', # CSS
63
+ :regexp => 'regexp', # Groovy, JavaScript, Ruby
64
+ :reserved => 'reserved', # most scanners
65
+ :shell => 'shell', # Ruby
66
+ :string => 'string', # most scanners
67
+ :symbol => 'symbol', # Clojure, Ruby, YAML
68
+ :tag => 'tag', # CSS, HTML
69
+ :type => 'type', # CSS, Java, SQL, YAML
70
+ :value => 'value', # used together with :key; CSS, JSON, YAML
71
+ :variable => 'variable', # Sass, SQL, YAML
75
72
 
76
- :eyecatcher => 'eyecatcher',
73
+ :change => 'change', # Diff
74
+ :delete => 'delete', # Diff
75
+ :head => 'head', # Diff, YAML
76
+ :insert => 'insert', # Diff
77
+ :eyecatcher => 'eyecatcher', # Diff
77
78
 
78
- :ident => false,
79
- :operator => false,
79
+ :ident => false, # almost all scanners
80
+ :operator => false, # almost all scanners
80
81
 
81
- :space => false,
82
- :plain => false
82
+ :space => false, # almost all scanners
83
+ :plain => false # almost all scanners
83
84
  )
84
85
 
85
- TokenKinds[:method] = TokenKinds[:function]
86
- TokenKinds[:escape] = TokenKinds[:delimiter]
87
- TokenKinds[:docstring] = TokenKinds[:comment]
88
-
89
- TokenKinds.freeze
86
+ TokenKinds[:method] = TokenKinds[:function]
90
87
  end
@@ -1,55 +1,43 @@
1
1
  module CodeRay
2
2
 
3
- # GZip library for writing and reading token dumps.
4
- autoload :GZip, coderay_path('helpers', 'gzip')
5
-
6
- # = Tokens TODO: Rewrite!
7
- #
8
- # The Tokens class represents a list of tokens returnd from
9
- # a Scanner.
3
+ # The Tokens class represents a list of tokens returned from
4
+ # a Scanner. It's actually just an Array with a few helper methods.
10
5
  #
11
- # A token is not a special object, just a two-element Array
12
- # consisting of
6
+ # A token itself is not a special object, just two elements in an Array:
13
7
  # * the _token_ _text_ (the original source of the token in a String) or
14
8
  # a _token_ _action_ (begin_group, end_group, begin_line, end_line)
15
9
  # * the _token_ _kind_ (a Symbol representing the type of the token)
16
10
  #
17
- # A token looks like this:
11
+ # It looks like this:
18
12
  #
19
- # ['# It looks like this', :comment]
20
- # ['3.1415926', :float]
21
- # ['$^', :error]
13
+ # ..., '# It looks like this', :comment, ...
14
+ # ..., '3.1415926', :float, ...
15
+ # ..., '$^', :error, ...
22
16
  #
23
17
  # Some scanners also yield sub-tokens, represented by special
24
- # token actions, namely begin_group and end_group.
18
+ # token actions, for example :begin_group and :end_group.
25
19
  #
26
20
  # The Ruby scanner, for example, splits "a string" into:
27
21
  #
28
22
  # [
29
- # [:begin_group, :string],
30
- # ['"', :delimiter],
31
- # ['a string', :content],
32
- # ['"', :delimiter],
33
- # [:end_group, :string]
23
+ # :begin_group, :string,
24
+ # '"', :delimiter,
25
+ # 'a string', :content,
26
+ # '"', :delimiter,
27
+ # :end_group, :string
34
28
  # ]
35
29
  #
36
- # Tokens is the interface between Scanners and Encoders:
37
- # The input is split and saved into a Tokens object. The Encoder
38
- # then builds the output from this object.
39
- #
40
- # Thus, the syntax below becomes clear:
30
+ # Tokens can be used to save the output of a Scanners in a simple
31
+ # Ruby object that can be send to an Encoder later:
41
32
  #
42
- # CodeRay.scan('price = 2.59', :ruby).html
43
- # # the Tokens object is here -------^
44
- #
45
- # See how small it is? ;)
33
+ # tokens = CodeRay.scan('price = 2.59', :ruby).tokens
34
+ # tokens.encode(:html)
35
+ # tokens.html
36
+ # CodeRay.encoder(:html).encode_tokens(tokens)
46
37
  #
47
38
  # Tokens gives you the power to handle pre-scanned code very easily:
48
- # You can convert it to a webpage, a YAML file, or dump it into a gzip'ed string
49
- # that you put in your DB.
50
- #
51
- # It also allows you to generate tokens directly (without using a scanner),
52
- # to load them from a file, and still use any Encoder that CodeRay provides.
39
+ # You can serialize it to a JSON string and store it in a database, pass it
40
+ # around to encode it more than once, send it to other algorithms...
53
41
  class Tokens < Array
54
42
 
55
43
  # The Scanner instance that created the tokens.
@@ -58,8 +46,7 @@ module CodeRay
58
46
  # Encode the tokens using encoder.
59
47
  #
60
48
  # encoder can be
61
- # * a symbol like :html oder :statistic
62
- # * an Encoder class
49
+ # * a plugin name like :html oder 'statistic'
63
50
  # * an Encoder object
64
51
  #
65
52
  # options are passed to the encoder.
@@ -93,6 +80,7 @@ module CodeRay
93
80
  # This method is used by @Scanner#tokenize@ when called with an Array
94
81
  # of source strings. The Diff encoder uses it for inline highlighting.
95
82
  def split_into_parts *sizes
83
+ return Array.new(sizes.size) { Tokens.new } if size == 2 && first == ''
96
84
  parts = []
97
85
  opened = []
98
86
  content = nil
@@ -156,53 +144,11 @@ module CodeRay
156
144
  parts
157
145
  end
158
146
 
159
- # Dumps the object into a String that can be saved
160
- # in files or databases.
161
- #
162
- # The dump is created with Marshal.dump;
163
- # In addition, it is gzipped using GZip.gzip.
164
- #
165
- # The returned String object includes Undumping
166
- # so it has an #undump method. See Tokens.load.
167
- #
168
- # You can configure the level of compression,
169
- # but the default value 7 should be what you want
170
- # in most cases as it is a good compromise between
171
- # speed and compression rate.
172
- #
173
- # See GZip module.
174
- def dump gzip_level = 7
175
- dump = Marshal.dump self
176
- dump = GZip.gzip dump, gzip_level
177
- dump.extend Undumping
178
- end
179
-
180
147
  # Return the actual number of tokens.
181
148
  def count
182
149
  size / 2
183
150
  end
184
151
 
185
- # Include this module to give an object an #undump
186
- # method.
187
- #
188
- # The string returned by Tokens.dump includes Undumping.
189
- module Undumping
190
- # Calls Tokens.load with itself.
191
- def undump
192
- Tokens.load self
193
- end
194
- end
195
-
196
- # Undump the object using Marshal.load, then
197
- # unzip it using GZip.gunzip.
198
- #
199
- # The result is commonly a Tokens object, but
200
- # this is not guaranteed.
201
- def Tokens.load dump
202
- dump = GZip.gunzip dump
203
- @dump = Marshal.load dump
204
- end
205
-
206
152
  alias text_token push
207
153
  def begin_group kind; push :begin_group, kind end
208
154
  def end_group kind; push :end_group, kind end