coderay 1.0.9 → 1.1.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -1,15 +1,15 @@
1
1
  module CodeRay
2
2
  module Encoders
3
-
3
+
4
4
  class HTML
5
-
5
+
6
6
  module Numbering # :nodoc:
7
-
7
+
8
8
  def self.number! output, mode = :table, options = {}
9
9
  return self unless mode
10
-
10
+
11
11
  options = DEFAULT_OPTIONS.merge options
12
-
12
+
13
13
  start = options[:line_number_start]
14
14
  unless start.is_a? Integer
15
15
  raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
@@ -26,7 +26,7 @@ module Encoders
26
26
  "<a href=\"##{anchor}\" name=\"#{anchor}\">#{line}</a>"
27
27
  end
28
28
  else
29
- proc { |line| line.to_s } # :to_s.to_proc in Ruby 1.8.7+
29
+ :to_s.to_proc
30
30
  end
31
31
 
32
32
  bold_every = options[:bold_every]
@@ -56,12 +56,17 @@ module Encoders
56
56
  raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
57
57
  end
58
58
 
59
- line_count = output.count("\n")
60
- position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n)
61
- if position_of_last_newline
59
+ if position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n)
62
60
  after_last_newline = output[position_of_last_newline + 1 .. -1]
63
61
  ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/]
64
- line_count += 1 if not ends_with_newline
62
+
63
+ if ends_with_newline
64
+ line_count = output.count("\n")
65
+ else
66
+ line_count = output.count("\n") + 1
67
+ end
68
+ else
69
+ line_count = 1
65
70
  end
66
71
 
67
72
  case mode
@@ -70,34 +75,34 @@ module Encoders
70
75
  line_number = start
71
76
  output.gsub!(/^.*$\n?/) do |line|
72
77
  line_number_text = bolding.call line_number
73
- indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x)
78
+ indent = ' ' * (max_width - line_number.to_s.size)
74
79
  line_number += 1
75
80
  "<span class=\"line-numbers\">#{indent}#{line_number_text}</span>#{line}"
76
81
  end
77
-
82
+
78
83
  when :table
79
84
  line_numbers = (start ... start + line_count).map(&bolding).join("\n")
80
85
  line_numbers << "\n"
81
86
  line_numbers_table_template = Output::TABLE.apply('LINE_NUMBERS', line_numbers)
82
-
87
+
83
88
  output.gsub!(/<\/div>\n/, '</div>')
84
89
  output.wrap_in! line_numbers_table_template
85
90
  output.wrapped_in = :div
86
-
91
+
87
92
  when :list
88
93
  raise NotImplementedError, 'The :list option is no longer available. Use :table.'
89
-
94
+
90
95
  else
91
96
  raise ArgumentError, 'Unknown value %p for mode: expected one of %p' %
92
97
  [mode, [:table, :inline]]
93
98
  end
94
-
99
+
95
100
  output
96
101
  end
97
-
102
+
98
103
  end
99
-
104
+
100
105
  end
101
-
106
+
102
107
  end
103
108
  end
@@ -124,7 +124,7 @@ module Encoders
124
124
 
125
125
  TABLE = Template.new <<-TABLE
126
126
  <table class="CodeRay"><tr>
127
- <td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><%LINE_NUMBERS%></pre></td>
127
+ <td class="line-numbers"><pre><%LINE_NUMBERS%></pre></td>
128
128
  <td class="code"><pre><%CONTENT%></pre></td>
129
129
  </tr></table>
130
130
  TABLE
@@ -0,0 +1,57 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ # = Lint Encoder
5
+ #
6
+ # Checks for:
7
+ #
8
+ # - empty tokens
9
+ # - incorrect nesting
10
+ #
11
+ # It will raise an InvalidTokenStream exception when any of the above occurs.
12
+ #
13
+ # See also: Encoders::DebugLint
14
+ class Lint < Debug
15
+
16
+ register_for :lint
17
+
18
+ InvalidTokenStream = Class.new StandardError
19
+ EmptyToken = Class.new InvalidTokenStream
20
+ IncorrectTokenGroupNesting = Class.new InvalidTokenStream
21
+
22
+ def text_token text, kind
23
+ raise EmptyToken, 'empty token' if text.empty?
24
+ end
25
+
26
+ def begin_group kind
27
+ @opened << kind
28
+ end
29
+
30
+ def end_group kind
31
+ raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_group)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
32
+ @opened.pop
33
+ end
34
+
35
+ def begin_line kind
36
+ @opened << kind
37
+ end
38
+
39
+ def end_line kind
40
+ raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_line)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
41
+ @opened.pop
42
+ end
43
+
44
+ protected
45
+
46
+ def setup options
47
+ @opened = []
48
+ end
49
+
50
+ def finish options
51
+ raise 'Some tokens still open at end of token stream: %p' % [@opened] unless @opened.empty?
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+ end
@@ -67,7 +67,6 @@ Token Types (%d):
67
67
  @type_stats['TOTAL'].count += 1
68
68
  end
69
69
 
70
- # TODO Hierarchy handling
71
70
  def begin_group kind
72
71
  block_token ':begin_group', kind
73
72
  end
@@ -19,105 +19,135 @@ module CodeRay
19
19
  register_for :terminal
20
20
 
21
21
  TOKEN_COLORS = {
22
- :annotation => '35',
23
- :attribute_name => '33',
24
- :attribute_value => '31',
25
- :binary => '1;35',
22
+ :debug => "\e[1;37;44m",
23
+
24
+ :annotation => "\e[34m",
25
+ :attribute_name => "\e[35m",
26
+ :attribute_value => "\e[31m",
27
+ :binary => {
28
+ :self => "\e[31m",
29
+ :char => "\e[1;31m",
30
+ :delimiter => "\e[1;31m",
31
+ },
26
32
  :char => {
27
- :self => '36', :delimiter => '1;34'
33
+ :self => "\e[35m",
34
+ :delimiter => "\e[1;35m"
35
+ },
36
+ :class => "\e[1;35;4m",
37
+ :class_variable => "\e[36m",
38
+ :color => "\e[32m",
39
+ :comment => {
40
+ :self => "\e[1;30m",
41
+ :char => "\e[37m",
42
+ :delimiter => "\e[37m",
28
43
  },
29
- :class => '1;35',
30
- :class_variable => '36',
31
- :color => '32',
32
- :comment => '37',
33
- :complex => '1;34',
34
- :constant => ['1;34', '4'],
35
- :decoration => '35',
36
- :definition => '1;32',
37
- :directive => ['32', '4'],
38
- :doc => '46',
39
- :doctype => '1;30',
40
- :doc_string => ['31', '4'],
41
- :entity => '33',
42
- :error => ['1;33', '41'],
43
- :exception => '1;31',
44
- :float => '1;35',
45
- :function => '1;34',
46
- :global_variable => '42',
47
- :hex => '1;36',
48
- :include => '33',
49
- :integer => '1;34',
50
- :key => '35',
51
- :label => '1;15',
52
- :local_variable => '33',
53
- :octal => '1;35',
54
- :operator_name => '1;29',
55
- :predefined_constant => '1;36',
56
- :predefined_type => '1;30',
57
- :predefined => ['4', '1;34'],
58
- :preprocessor => '36',
59
- :pseudo_class => '1;34',
44
+ :constant => "\e[1;34;4m",
45
+ :decorator => "\e[35m",
46
+ :definition => "\e[1;33m",
47
+ :directive => "\e[33m",
48
+ :docstring => "\e[31m",
49
+ :doctype => "\e[1;34m",
50
+ :done => "\e[1;30;2m",
51
+ :entity => "\e[31m",
52
+ :error => "\e[1;37;41m",
53
+ :exception => "\e[1;31m",
54
+ :float => "\e[1;35m",
55
+ :function => "\e[1;34m",
56
+ :global_variable => "\e[1;32m",
57
+ :hex => "\e[1;36m",
58
+ :id => "\e[1;34m",
59
+ :include => "\e[31m",
60
+ :integer => "\e[1;34m",
61
+ :imaginary => "\e[1;34m",
62
+ :important => "\e[1;31m",
63
+ :key => {
64
+ :self => "\e[35m",
65
+ :char => "\e[1;35m",
66
+ :delimiter => "\e[1;35m",
67
+ },
68
+ :keyword => "\e[32m",
69
+ :label => "\e[1;33m",
70
+ :local_variable => "\e[33m",
71
+ :namespace => "\e[1;35m",
72
+ :octal => "\e[1;34m",
73
+ :predefined => "\e[36m",
74
+ :predefined_constant => "\e[1;36m",
75
+ :predefined_type => "\e[1;32m",
76
+ :preprocessor => "\e[1;36m",
77
+ :pseudo_class => "\e[1;34m",
60
78
  :regexp => {
61
- :self => '31',
62
- :content => '31',
63
- :delimiter => '1;29',
64
- :modifier => '35',
79
+ :self => "\e[35m",
80
+ :delimiter => "\e[1;35m",
81
+ :modifier => "\e[35m",
82
+ :char => "\e[1;35m",
65
83
  },
66
- :reserved => '1;31',
84
+ :reserved => "\e[32m",
67
85
  :shell => {
68
- :self => '42',
69
- :content => '1;29',
70
- :delimiter => '37',
86
+ :self => "\e[33m",
87
+ :char => "\e[1;33m",
88
+ :delimiter => "\e[1;33m",
89
+ :escape => "\e[1;33m",
71
90
  },
72
91
  :string => {
73
- :self => '32',
74
- :modifier => '1;32',
75
- :escape => '1;36',
76
- :delimiter => '1;32',
77
- :char => '1;36',
92
+ :self => "\e[31m",
93
+ :modifier => "\e[1;31m",
94
+ :char => "\e[1;35m",
95
+ :delimiter => "\e[1;31m",
96
+ :escape => "\e[1;31m",
97
+ },
98
+ :symbol => {
99
+ :self => "\e[33m",
100
+ :delimiter => "\e[1;33m",
78
101
  },
79
- :symbol => '1;32',
80
- :tag => '1;34',
81
- :type => '1;34',
82
- :value => '36',
83
- :variable => '1;34',
102
+ :tag => "\e[32m",
103
+ :type => "\e[1;34m",
104
+ :value => "\e[36m",
105
+ :variable => "\e[34m",
84
106
 
85
- :insert => '42',
86
- :delete => '41',
87
- :change => '44',
88
- :head => '45'
107
+ :insert => {
108
+ :self => "\e[42m",
109
+ :insert => "\e[1;32;42m",
110
+ :eyecatcher => "\e[102m",
111
+ },
112
+ :delete => {
113
+ :self => "\e[41m",
114
+ :delete => "\e[1;31;41m",
115
+ :eyecatcher => "\e[101m",
116
+ },
117
+ :change => {
118
+ :self => "\e[44m",
119
+ :change => "\e[37;44m",
120
+ },
121
+ :head => {
122
+ :self => "\e[45m",
123
+ :filename => "\e[37;45m"
124
+ },
89
125
  }
126
+
90
127
  TOKEN_COLORS[:keyword] = TOKEN_COLORS[:reserved]
91
128
  TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
92
- TOKEN_COLORS[:imaginary] = TOKEN_COLORS[:complex]
93
- TOKEN_COLORS[:begin_group] = TOKEN_COLORS[:end_group] =
94
- TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
129
+ TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
95
130
 
96
131
  protected
97
132
 
98
133
  def setup(options)
99
134
  super
100
135
  @opened = []
101
- @subcolors = nil
136
+ @color_scopes = [TOKEN_COLORS]
102
137
  end
103
138
 
104
139
  public
105
140
 
106
141
  def text_token text, kind
107
- if color = (@subcolors || TOKEN_COLORS)[kind]
108
- if Hash === color
109
- if color[:self]
110
- color = color[:self]
111
- else
112
- @out << text
113
- return
114
- end
115
- end
142
+ if color = @color_scopes.last[kind]
143
+ color = color[:self] if color.is_a? Hash
116
144
 
117
- @out << ansi_colorize(color)
118
- @out << text.gsub("\n", ansi_clear + "\n" + ansi_colorize(color))
119
- @out << ansi_clear
120
- @out << ansi_colorize(@subcolors[:self]) if @subcolors && @subcolors[:self]
145
+ @out << color
146
+ @out << (text.index("\n") ? text.gsub("\n", "\e[0m\n" + color) : text)
147
+ @out << "\e[0m"
148
+ if outer_color = @color_scopes.last[:self]
149
+ @out << outer_color
150
+ end
121
151
  else
122
152
  @out << text
123
153
  end
@@ -130,50 +160,36 @@ module CodeRay
130
160
  alias begin_line begin_group
131
161
 
132
162
  def end_group kind
133
- if @opened.empty?
134
- # nothing to close
135
- else
136
- @opened.pop
137
- @out << ansi_clear
138
- @out << open_token(@opened.last)
163
+ if @opened.pop
164
+ @color_scopes.pop
165
+ @out << "\e[0m"
166
+ if outer_color = @color_scopes.last[:self]
167
+ @out << outer_color
168
+ end
139
169
  end
140
170
  end
141
171
 
142
172
  def end_line kind
143
- if @opened.empty?
144
- # nothing to close
145
- else
146
- @opened.pop
147
- # whole lines to be highlighted,
148
- # eg. added/modified/deleted lines in a diff
149
- @out << "\t" * 100 + ansi_clear
150
- @out << open_token(@opened.last)
151
- end
173
+ @out << (@line_filler ||= "\t" * 100)
174
+ end_group kind
152
175
  end
153
176
 
154
177
  private
155
178
 
156
179
  def open_token kind
157
- if color = TOKEN_COLORS[kind]
158
- if Hash === color
159
- @subcolors = color
160
- ansi_colorize(color[:self]) if color[:self]
180
+ if color = @color_scopes.last[kind]
181
+ if color.is_a? Hash
182
+ @color_scopes << color
183
+ color[:self]
161
184
  else
162
- @subcolors = {}
163
- ansi_colorize(color)
185
+ @color_scopes << @color_scopes.last
186
+ color
164
187
  end
165
188
  else
166
- @subcolors = nil
189
+ @color_scopes << @color_scopes.last
167
190
  ''
168
191
  end
169
192
  end
170
-
171
- def ansi_colorize(color)
172
- Array(color).map { |c| "\e[#{c}m" }.join
173
- end
174
- def ansi_clear
175
- ansi_colorize(0)
176
- end
177
193
  end
178
194
  end
179
- end
195
+ end
@@ -77,53 +77,57 @@ module CodeRay
77
77
  end
78
78
 
79
79
  TypeFromExt = {
80
- 'c' => :c,
81
- 'cfc' => :xml,
82
- 'cfm' => :xml,
83
- 'clj' => :clojure,
84
- 'css' => :css,
85
- 'diff' => :diff,
86
- 'dpr' => :delphi,
87
- 'erb' => :erb,
88
- 'gemspec' => :ruby,
89
- 'groovy' => :groovy,
90
- 'gvy' => :groovy,
91
- 'h' => :c,
92
- 'haml' => :haml,
93
- 'htm' => :html,
94
- 'html' => :html,
95
- 'html.erb' => :erb,
96
- 'java' => :java,
97
- 'js' => :java_script,
98
- 'json' => :json,
99
- 'mab' => :ruby,
100
- 'pas' => :delphi,
101
- 'patch' => :diff,
102
- 'php' => :php,
103
- 'php3' => :php,
104
- 'php4' => :php,
105
- 'php5' => :php,
106
- 'prawn' => :ruby,
107
- 'py' => :python,
108
- 'py3' => :python,
109
- 'pyw' => :python,
110
- 'rake' => :ruby,
111
- 'raydebug' => :raydebug,
112
- 'rb' => :ruby,
113
- 'rbw' => :ruby,
114
- 'rhtml' => :erb,
115
- 'rjs' => :ruby,
116
- 'rpdf' => :ruby,
117
- 'ru' => :ruby,
118
- 'rxml' => :ruby,
119
- # 'sch' => :scheme,
120
- 'sql' => :sql,
121
- # 'ss' => :scheme,
122
- 'tmproj' => :xml,
123
- 'xhtml' => :html,
124
- 'xml' => :xml,
125
- 'yaml' => :yaml,
126
- 'yml' => :yaml,
80
+ 'c' => :c,
81
+ 'cfc' => :xml,
82
+ 'cfm' => :xml,
83
+ 'clj' => :clojure,
84
+ 'css' => :css,
85
+ 'diff' => :diff,
86
+ 'dpr' => :delphi,
87
+ 'erb' => :erb,
88
+ 'gemspec' => :ruby,
89
+ 'groovy' => :groovy,
90
+ 'gvy' => :groovy,
91
+ 'h' => :c,
92
+ 'haml' => :haml,
93
+ 'htm' => :html,
94
+ 'html' => :html,
95
+ 'html.erb' => :erb,
96
+ 'java' => :java,
97
+ 'js' => :java_script,
98
+ 'json' => :json,
99
+ 'lua' => :lua,
100
+ 'mab' => :ruby,
101
+ 'pas' => :delphi,
102
+ 'patch' => :diff,
103
+ 'phtml' => :php,
104
+ 'php' => :php,
105
+ 'php3' => :php,
106
+ 'php4' => :php,
107
+ 'php5' => :php,
108
+ 'prawn' => :ruby,
109
+ 'py' => :python,
110
+ 'py3' => :python,
111
+ 'pyw' => :python,
112
+ 'rake' => :ruby,
113
+ 'raydebug' => :raydebug,
114
+ 'rb' => :ruby,
115
+ 'rbw' => :ruby,
116
+ 'rhtml' => :erb,
117
+ 'rjs' => :ruby,
118
+ 'rpdf' => :ruby,
119
+ 'ru' => :ruby, # config.ru
120
+ 'rxml' => :ruby,
121
+ 'sass' => :sass,
122
+ 'sql' => :sql,
123
+ 'taskpaper' => :taskpaper,
124
+ 'template' => :json, # AWS CloudFormation template
125
+ 'tmproj' => :xml,
126
+ 'xaml' => :xml,
127
+ 'xhtml' => :html,
128
+ 'xml' => :xml,
129
+ 'yaml' => :yaml,
130
+ 'yml' => :yaml,
127
131
  }
128
132
  for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu]
129
133
  TypeFromExt[cpp_alias] = :cpp
@@ -136,6 +140,9 @@ module CodeRay
136
140
  'Rakefile' => :ruby,
137
141
  'Rantfile' => :ruby,
138
142
  'Gemfile' => :ruby,
143
+ 'Guardfile' => :ruby,
144
+ 'Vagrantfile' => :ruby,
145
+ 'Appraisals' => :ruby
139
146
  }
140
147
 
141
148
  end