raldred-coderay 0.9.0 → 0.9.339

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 (58) hide show
  1. data/lib/README +128 -0
  2. data/lib/coderay.rb +319 -0
  3. data/lib/coderay/duo.rb +85 -0
  4. data/lib/coderay/encoder.rb +187 -0
  5. data/lib/coderay/encoders/_map.rb +9 -0
  6. data/lib/coderay/encoders/count.rb +21 -0
  7. data/lib/coderay/encoders/debug.rb +49 -0
  8. data/lib/coderay/encoders/div.rb +20 -0
  9. data/lib/coderay/encoders/html.rb +306 -0
  10. data/lib/coderay/encoders/html/css.rb +70 -0
  11. data/lib/coderay/encoders/html/numerization.rb +133 -0
  12. data/lib/coderay/encoders/html/output.rb +206 -0
  13. data/lib/coderay/encoders/json.rb +19 -0
  14. data/lib/coderay/encoders/null.rb +26 -0
  15. data/lib/coderay/encoders/page.rb +21 -0
  16. data/lib/coderay/encoders/span.rb +20 -0
  17. data/lib/coderay/encoders/statistic.rb +77 -0
  18. data/lib/coderay/encoders/term.rb +114 -0
  19. data/lib/coderay/encoders/text.rb +32 -0
  20. data/lib/coderay/encoders/tokens.rb +44 -0
  21. data/lib/coderay/encoders/xml.rb +71 -0
  22. data/lib/coderay/encoders/yaml.rb +22 -0
  23. data/lib/coderay/for_redcloth.rb +73 -0
  24. data/lib/coderay/helpers/file_type.rb +226 -0
  25. data/lib/coderay/helpers/gzip_simple.rb +123 -0
  26. data/lib/coderay/helpers/plugin.rb +339 -0
  27. data/lib/coderay/helpers/word_list.rb +124 -0
  28. data/lib/coderay/scanner.rb +271 -0
  29. data/lib/coderay/scanners/_map.rb +21 -0
  30. data/lib/coderay/scanners/c.rb +166 -0
  31. data/lib/coderay/scanners/css.rb +202 -0
  32. data/lib/coderay/scanners/debug.rb +61 -0
  33. data/lib/coderay/scanners/delphi.rb +150 -0
  34. data/lib/coderay/scanners/diff.rb +104 -0
  35. data/lib/coderay/scanners/groovy.rb +271 -0
  36. data/lib/coderay/scanners/html.rb +175 -0
  37. data/lib/coderay/scanners/java.rb +173 -0
  38. data/lib/coderay/scanners/java/builtin_types.rb +419 -0
  39. data/lib/coderay/scanners/java_script.rb +195 -0
  40. data/lib/coderay/scanners/json.rb +107 -0
  41. data/lib/coderay/scanners/nitro_xhtml.rb +132 -0
  42. data/lib/coderay/scanners/php.rb +404 -0
  43. data/lib/coderay/scanners/plaintext.rb +18 -0
  44. data/lib/coderay/scanners/python.rb +232 -0
  45. data/lib/coderay/scanners/rhtml.rb +71 -0
  46. data/lib/coderay/scanners/ruby.rb +386 -0
  47. data/lib/coderay/scanners/ruby/patterns.rb +232 -0
  48. data/lib/coderay/scanners/scheme.rb +142 -0
  49. data/lib/coderay/scanners/sql.rb +162 -0
  50. data/lib/coderay/scanners/xml.rb +17 -0
  51. data/lib/coderay/scanners/yaml.rb +142 -0
  52. data/lib/coderay/style.rb +20 -0
  53. data/lib/coderay/styles/_map.rb +7 -0
  54. data/lib/coderay/styles/cycnus.rb +151 -0
  55. data/lib/coderay/styles/murphy.rb +132 -0
  56. data/lib/coderay/token_classes.rb +86 -0
  57. data/lib/coderay/tokens.rb +387 -0
  58. metadata +59 -1
@@ -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
@@ -0,0 +1,20 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ load :html
5
+
6
+ class Span < HTML
7
+
8
+ FILE_EXTENSION = 'span.html'
9
+
10
+ register_for :span
11
+
12
+ DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge({
13
+ :css => :style,
14
+ :wrap => :span,
15
+ })
16
+
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,77 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ # Makes a statistic for the given tokens.
5
+ class Statistic < Encoder
6
+
7
+ include Streamable
8
+ register_for :stats, :statistic
9
+
10
+ attr_reader :type_stats, :real_token_count
11
+
12
+ protected
13
+
14
+ TypeStats = Struct.new :count, :size
15
+
16
+ def setup options
17
+ @type_stats = Hash.new { |h, k| h[k] = TypeStats.new 0, 0 }
18
+ @real_token_count = 0
19
+ end
20
+
21
+ def generate tokens, options
22
+ @tokens = tokens
23
+ super
24
+ end
25
+
26
+ def text_token text, kind
27
+ @real_token_count += 1 unless kind == :space
28
+ @type_stats[kind].count += 1
29
+ @type_stats[kind].size += text.size
30
+ @type_stats['TOTAL'].size += text.size
31
+ @type_stats['TOTAL'].count += 1
32
+ end
33
+
34
+ # TODO Hierarchy handling
35
+ def block_token action, kind
36
+ @type_stats['TOTAL'].count += 1
37
+ @type_stats['open/close'].count += 1
38
+ end
39
+
40
+ STATS = <<-STATS
41
+
42
+ Code Statistics
43
+
44
+ Tokens %8d
45
+ Non-Whitespace %8d
46
+ Bytes Total %8d
47
+
48
+ Token Types (%d):
49
+ type count ratio size (average)
50
+ -------------------------------------------------------------
51
+ %s
52
+ STATS
53
+ # space 12007 33.81 % 1.7
54
+ TOKEN_TYPES_ROW = <<-TKR
55
+ %-20s %8d %6.2f %% %5.1f
56
+ TKR
57
+
58
+ def finish options
59
+ all = @type_stats['TOTAL']
60
+ all_count, all_size = all.count, all.size
61
+ @type_stats.each do |type, stat|
62
+ stat.size /= stat.count.to_f
63
+ end
64
+ types_stats = @type_stats.sort_by { |k, v| [-v.count, k.to_s] }.map do |k, v|
65
+ TOKEN_TYPES_ROW % [k, v.count, 100.0 * v.count / all_count, v.size]
66
+ end.join
67
+ STATS % [
68
+ all_count, @real_token_count, all_size,
69
+ @type_stats.delete_if { |k, v| k.is_a? String }.size,
70
+ types_stats
71
+ ]
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,114 @@
1
+ # encoders/term.rb
2
+ # By Nathan Weizenbaum (http://nex-3.com)
3
+ # MIT License (http://www.opensource.org/licenses/mit-license.php)
4
+ #
5
+ # A CodeRay highlighter that outputs code highlighted for a color terminal.
6
+ # Check out http://nex-3.com/posts/37-terminal-syntax-highlighting
7
+
8
+ module CodeRay
9
+ module Encoders
10
+ class Term < Encoder
11
+ register_for :term
12
+
13
+ TOKEN_COLORS = {
14
+ :attribute_name => '34',
15
+ :attribute_name_fat => '34',
16
+ :attribute_value => '31',
17
+ :attribute_value_fat => '31',
18
+ :bin => '1;35',
19
+ :char => {:self => '36', :delimiter => '34'},
20
+ :class => '1;35',
21
+ :class_variable => '36',
22
+ :color => '32',
23
+ :comment => '37',
24
+ :constant => ['34', '4'],
25
+ :definition => '1;32',
26
+ :directive => ['32', '4'],
27
+ :doc => '46',
28
+ :doc_string => ['31', '4'],
29
+ :entity => '33',
30
+ :error => ['1;33', '41'],
31
+ :exception => '1;31',
32
+ :float => '1;35',
33
+ :function => '1;34',
34
+ :global_variable => '42',
35
+ :hex => '1;36',
36
+ :include => '33',
37
+ :integer => '1;34',
38
+ :interpreted => '1;35',
39
+ :label => '1;4',
40
+ :local_variable => '33',
41
+ :oct => '1;35',
42
+ :operator_name => '1;29',
43
+ :pre_constant => '1;36',
44
+ :pre_type => '1;30',
45
+ :predefined => ['4', '1;34'],
46
+ :preprocessor => '36',
47
+ :regexp => {
48
+ :content => '31',
49
+ :delimiter => '1;29',
50
+ :modifier => '35',
51
+ :function => '1;29'
52
+ },
53
+ :reserved => '1;31',
54
+ :shell => {:self => '42', :content => '1;29'},
55
+ :string => '32',
56
+ :symbol => '1;32',
57
+ :tag => '34',
58
+ :tag_fat => '1;34',
59
+ :tag_special => ['34', '4'],
60
+ :type => '1;34',
61
+ :variable => '34'
62
+ }
63
+ TOKEN_COLORS[:procedure] = TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
64
+ TOKEN_COLORS[:open] = TOKEN_COLORS[:close] = TOKEN_COLORS[:nesting_delimiter] = TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
65
+
66
+ protected
67
+
68
+ def setup(options)
69
+ @out = ''
70
+ @subcolors = nil
71
+ end
72
+
73
+ def finish(options)
74
+ @out
75
+ end
76
+
77
+ def text_token(text, kind)
78
+ if color = (@subcolors || TOKEN_COLORS)[kind]
79
+ color = color[:self] || return if Hash === color
80
+
81
+ @out << col(color) + text.gsub("\n", col(0) + "\n" + col(color)) + col(0)
82
+ @out << col(@subcolors[:self]) if @subcolors && @subcolors[:self]
83
+ else
84
+ @out << text
85
+ end
86
+ end
87
+
88
+ def open_token(kind)
89
+ if color = TOKEN_COLORS[kind]
90
+ if Hash === color
91
+ @subcolors = color
92
+ @out << col(color[:self]) if color[:self]
93
+ else
94
+ @subcolors = {}
95
+ @out << col(color)
96
+ end
97
+ end
98
+ end
99
+
100
+ def close_token(kind)
101
+ if (@subcolors || {})[:self]
102
+ @out << col(0)
103
+ end
104
+ @subcolors = nil
105
+ end
106
+
107
+ private
108
+
109
+ def col(color)
110
+ Array(color).map { |c| "\e[#{c}m" }.join
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,32 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ class Text < Encoder
5
+
6
+ include Streamable
7
+ register_for :text
8
+
9
+ FILE_EXTENSION = 'txt'
10
+
11
+ DEFAULT_OPTIONS = {
12
+ :separator => ''
13
+ }
14
+
15
+ protected
16
+ def setup options
17
+ @out = ''
18
+ @sep = options[:separator]
19
+ end
20
+
21
+ def token text, kind
22
+ @out << text + @sep if text.is_a? ::String
23
+ end
24
+
25
+ def finish options
26
+ @out.chomp @sep
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,44 @@
1
+ module CodeRay
2
+ module Encoders
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
30
+
31
+ include Streamable
32
+ register_for :tokens
33
+
34
+ FILE_EXTENSION = 'tok'
35
+
36
+ protected
37
+ def token text, kind
38
+ @out << CodeRay::Tokens.write_token(text, kind)
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,71 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ # = XML Encoder
5
+ #
6
+ # Uses REXML. Very slow.
7
+ class XML < Encoder
8
+
9
+ include Streamable
10
+ register_for :xml
11
+
12
+ FILE_EXTENSION = 'xml'
13
+
14
+ require 'rexml/document'
15
+
16
+ DEFAULT_OPTIONS = {
17
+ :tab_width => 8,
18
+ :pretty => -1,
19
+ :transitive => false,
20
+ }
21
+
22
+ protected
23
+
24
+ def setup options
25
+ @doc = REXML::Document.new
26
+ @doc << REXML::XMLDecl.new
27
+ @tab_width = options[:tab_width]
28
+ @root = @node = @doc.add_element('coderay-tokens')
29
+ end
30
+
31
+ def finish options
32
+ # FIXME: broken!
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
42
+ end
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)
51
+ else
52
+ token << REXML::Text.new($&)
53
+ end
54
+ end
55
+ end
56
+
57
+ def open_token kind
58
+ @node = @node.add_element kind.to_s
59
+ end
60
+
61
+ def close_token kind
62
+ if @node == @root
63
+ raise 'no token to close!'
64
+ end
65
+ @node = @node.parent
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,22 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ # = YAML Encoder
5
+ #
6
+ # Slow.
7
+ class YAML < Encoder
8
+
9
+ register_for :yaml
10
+
11
+ FILE_EXTENSION = 'yaml'
12
+
13
+ protected
14
+ def compile tokens, options
15
+ require 'yaml'
16
+ @out = tokens.to_a.to_yaml
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,73 @@
1
+ module CodeRay
2
+
3
+ # A little hack to enable CodeRay highlighting in RedCloth.
4
+ #
5
+ # Usage:
6
+ # require 'coderay'
7
+ # require 'coderay/for_redcloth'
8
+ # RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
9
+ #
10
+ # Make sure you have RedCloth 4.0.3 activated, for example by calling
11
+ # require 'rubygems'
12
+ # before RedCloth is loaded and before calling CodeRay.for_redcloth.
13
+ module ForRedCloth
14
+
15
+ def self.install
16
+ gem 'RedCloth', '>= 4.0.3' rescue nil
17
+ require 'redcloth'
18
+ raise 'CodeRay.for_redcloth needs RedCloth 4.0.3 or later.' unless RedCloth::VERSION.to_s >= '4.0.3'
19
+ RedCloth::TextileDoc.send :include, ForRedCloth::TextileDoc
20
+ RedCloth::Formatters::HTML.module_eval do
21
+ def unescape(html)
22
+ replacements = {
23
+ '&amp;' => '&',
24
+ '&quot;' => '"',
25
+ '&gt;' => '>',
26
+ '&lt;' => '<',
27
+ }
28
+ html.gsub(/&(?:amp|quot|[gl]t);/) { |entity| replacements[entity] }
29
+ end
30
+ undef_method :code, :bc_open, :bc_close, :escape_pre
31
+ def code(opts) # :nodoc:
32
+ opts[:block] = true
33
+ if opts[:lang] && !filter_coderay
34
+ require 'coderay'
35
+ @in_bc ||= nil
36
+ format = @in_bc ? :div : :span
37
+ highlighted_code = CodeRay.encode opts[:text], opts[:lang], format, :stream => true
38
+ highlighted_code.sub!(/\A<(span|div)/) { |m| m + pba(@in_bc || opts) }
39
+ highlighted_code = unescape(highlighted_code) unless @in_bc
40
+ highlighted_code
41
+ else
42
+ "<code#{pba(opts)}>#{opts[:text]}</code>"
43
+ end
44
+ end
45
+ def bc_open(opts) # :nodoc:
46
+ opts[:block] = true
47
+ @in_bc = opts
48
+ opts[:lang] ? '' : "<pre#{pba(opts)}>"
49
+ end
50
+ def bc_close(opts) # :nodoc:
51
+ opts = @in_bc
52
+ @in_bc = nil
53
+ opts[:lang] ? '' : "</pre>\n"
54
+ end
55
+ def escape_pre(text)
56
+ if @in_bc ||= nil
57
+ text
58
+ else
59
+ html_esc(text, :html_escape_preformatted)
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ module TextileDoc # :nodoc:
66
+ attr_accessor :filter_coderay
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ CodeRay::ForRedCloth.install