coderay 0.7.1.147 → 0.7.2.165

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/bin/coderay +54 -56
  2. data/demo/suite.rb +54 -54
  3. data/lib/coderay.rb +187 -187
  4. data/lib/coderay/duo.rb +29 -29
  5. data/lib/coderay/encoder.rb +173 -173
  6. data/lib/coderay/encoders/_map.rb +8 -8
  7. data/lib/coderay/encoders/count.rb +21 -21
  8. data/lib/coderay/encoders/debug.rb +46 -46
  9. data/lib/coderay/encoders/div.rb +20 -20
  10. data/lib/coderay/encoders/html.rb +249 -245
  11. data/lib/coderay/encoders/html/classes.rb +73 -73
  12. data/lib/coderay/encoders/html/css.rb +65 -65
  13. data/lib/coderay/encoders/html/numerization.rb +122 -122
  14. data/lib/coderay/encoders/html/output.rb +195 -195
  15. data/lib/coderay/encoders/null.rb +26 -26
  16. data/lib/coderay/encoders/page.rb +21 -21
  17. data/lib/coderay/encoders/span.rb +20 -20
  18. data/lib/coderay/encoders/statistic.rb +81 -81
  19. data/lib/coderay/encoders/text.rb +33 -33
  20. data/lib/coderay/encoders/tokens.rb +44 -44
  21. data/lib/coderay/encoders/xml.rb +71 -71
  22. data/lib/coderay/encoders/yaml.rb +22 -22
  23. data/lib/coderay/helpers/filetype.rb +152 -153
  24. data/lib/coderay/helpers/gzip_simple.rb +67 -68
  25. data/lib/coderay/helpers/plugin.rb +297 -297
  26. data/lib/coderay/helpers/word_list.rb +46 -47
  27. data/lib/coderay/scanner.rb +238 -238
  28. data/lib/coderay/scanners/_map.rb +15 -14
  29. data/lib/coderay/scanners/c.rb +163 -155
  30. data/lib/coderay/scanners/delphi.rb +131 -129
  31. data/lib/coderay/scanners/html.rb +174 -167
  32. data/lib/coderay/scanners/nitro_xhtml.rb +130 -0
  33. data/lib/coderay/scanners/plaintext.rb +15 -15
  34. data/lib/coderay/scanners/rhtml.rb +73 -65
  35. data/lib/coderay/scanners/ruby.rb +404 -397
  36. data/lib/coderay/scanners/ruby/patterns.rb +216 -216
  37. data/lib/coderay/scanners/xml.rb +18 -18
  38. data/lib/coderay/style.rb +20 -20
  39. data/lib/coderay/styles/_map.rb +3 -3
  40. data/lib/coderay/styles/cycnus.rb +18 -18
  41. data/lib/coderay/styles/murphy.rb +18 -18
  42. data/lib/coderay/tokens.rb +322 -322
  43. metadata +86 -86
  44. data/lib/coderay/scanners/nitro_html.rb +0 -125
  45. data/lib/coderay/scanners/yaml.rb +0 -85
@@ -1,73 +1,73 @@
1
- module CodeRay
2
- module Encoders
3
-
4
- class HTML
5
-
6
- ClassOfKind = {
7
- :attribute_name => 'an',
8
- :attribute_name_fat => 'af',
9
- :attribute_value => 'av',
10
- :attribute_value_fat => 'aw',
11
- :bin => 'bi',
12
- :char => 'ch',
13
- :class => 'cl',
14
- :class_variable => 'cv',
15
- :color => 'cr',
16
- :comment => 'c',
17
- :constant => 'co',
18
- :content => 'k',
19
- :definition => 'df',
20
- :delimiter => 'dl',
21
- :directive => 'di',
22
- :doc => 'do',
23
- :doc_string => 'ds',
24
- :entity => 'en',
25
- :error => 'er',
26
- :escape => 'e',
27
- :exception => 'ex',
28
- :float => 'fl',
29
- :function => 'fu',
30
- :global_variable => 'gv',
31
- :hex => 'hx',
32
- :include => 'ic',
33
- :inline => 'il',
34
- :instance_variable => 'iv',
35
- :integer => 'i',
36
- :interpreted => 'in',
37
- :label => 'la',
38
- :local_variable => 'lv',
39
- :modifier => 'mod',
40
- :oct => 'oc',
41
- :operator_name => 'on',
42
- :pre_constant => 'pc',
43
- :pre_type => 'pt',
44
- :predefined => 'pd',
45
- :preprocessor => 'pp',
46
- :regexp => 'rx',
47
- :reserved => 'r',
48
- :shell => 'sh',
49
- :string => 's',
50
- :symbol => 'sy',
51
- :tag => 'ta',
52
- :tag_fat => 'tf',
53
- :tag_special => 'ts',
54
- :type => 'ty',
55
- :variable => 'v',
56
- :xml_text => 'xt',
57
-
58
- :ident => :NO_HIGHLIGHT, # 'id'
59
- #:operator => 'op',
60
- :operator => :NO_HIGHLIGHT, # 'op'
61
- :space => :NO_HIGHLIGHT, # 'sp'
62
- :plain => :NO_HIGHLIGHT,
63
- }
64
- ClassOfKind[:procedure] = ClassOfKind[:method] = ClassOfKind[:function]
65
- ClassOfKind[:open] = ClassOfKind[:close] = ClassOfKind[:delimiter]
66
- ClassOfKind[:nesting_delimiter] = ClassOfKind[:delimiter]
67
- ClassOfKind[:escape] = ClassOfKind[:delimiter]
68
- ClassOfKind.default = ClassOfKind[:error] or raise 'no class found for :error!'
69
-
70
- end
71
-
72
- end
73
- end
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ class HTML
5
+
6
+ ClassOfKind = {
7
+ :attribute_name => 'an',
8
+ :attribute_name_fat => 'af',
9
+ :attribute_value => 'av',
10
+ :attribute_value_fat => 'aw',
11
+ :bin => 'bi',
12
+ :char => 'ch',
13
+ :class => 'cl',
14
+ :class_variable => 'cv',
15
+ :color => 'cr',
16
+ :comment => 'c',
17
+ :constant => 'co',
18
+ :content => 'k',
19
+ :definition => 'df',
20
+ :delimiter => 'dl',
21
+ :directive => 'di',
22
+ :doc => 'do',
23
+ :doc_string => 'ds',
24
+ :entity => 'en',
25
+ :error => 'er',
26
+ :escape => 'e',
27
+ :exception => 'ex',
28
+ :float => 'fl',
29
+ :function => 'fu',
30
+ :global_variable => 'gv',
31
+ :hex => 'hx',
32
+ :include => 'ic',
33
+ :inline => 'il',
34
+ :instance_variable => 'iv',
35
+ :integer => 'i',
36
+ :interpreted => 'in',
37
+ :label => 'la',
38
+ :local_variable => 'lv',
39
+ :modifier => 'mod',
40
+ :oct => 'oc',
41
+ :operator_name => 'on',
42
+ :pre_constant => 'pc',
43
+ :pre_type => 'pt',
44
+ :predefined => 'pd',
45
+ :preprocessor => 'pp',
46
+ :regexp => 'rx',
47
+ :reserved => 'r',
48
+ :shell => 'sh',
49
+ :string => 's',
50
+ :symbol => 'sy',
51
+ :tag => 'ta',
52
+ :tag_fat => 'tf',
53
+ :tag_special => 'ts',
54
+ :type => 'ty',
55
+ :variable => 'v',
56
+ :xml_text => 'xt',
57
+
58
+ :ident => :NO_HIGHLIGHT, # 'id'
59
+ #:operator => 'op',
60
+ :operator => :NO_HIGHLIGHT, # 'op'
61
+ :space => :NO_HIGHLIGHT, # 'sp'
62
+ :plain => :NO_HIGHLIGHT,
63
+ }
64
+ ClassOfKind[:procedure] = ClassOfKind[:method] = ClassOfKind[:function]
65
+ ClassOfKind[:open] = ClassOfKind[:close] = ClassOfKind[:delimiter]
66
+ ClassOfKind[:nesting_delimiter] = ClassOfKind[:delimiter]
67
+ ClassOfKind[:escape] = ClassOfKind[:delimiter]
68
+ ClassOfKind.default = ClassOfKind[:error] or raise 'no class found for :error!'
69
+
70
+ end
71
+
72
+ end
73
+ end
@@ -1,65 +1,65 @@
1
- module CodeRay
2
- module Encoders
3
-
4
- class HTML
5
- class CSS
6
-
7
- attr :stylesheet
8
-
9
- def CSS.load_stylesheet style = nil
10
- CodeRay::Styles[style]
11
- end
12
-
13
- def initialize style = :default
14
- @classes = Hash.new
15
- style = CSS.load_stylesheet style
16
- @stylesheet = [
17
- style::CSS_MAIN_STYLES,
18
- style::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ')
19
- ].join("\n")
20
- parse style::TOKEN_COLORS
21
- end
22
-
23
- def [] *styles
24
- cl = @classes[styles.first]
25
- return '' unless cl
26
- style = ''
27
- 1.upto(styles.size) do |offset|
28
- break if style = cl[styles[offset .. -1]]
29
- end
30
- raise 'Style not found: %p' % [styles] if $DEBUG and style.empty?
31
- return style
32
- end
33
-
34
- private
35
-
36
- CSS_CLASS_PATTERN = /
37
- ( (?: # $1 = classes
38
- \s* \. [-\w]+
39
- )+ )
40
- \s* \{ \s*
41
- ( [^\}]+ )? # $2 = style
42
- \s* \} \s*
43
- |
44
- ( . ) # $3 = error
45
- /mx
46
- def parse stylesheet
47
- stylesheet.scan CSS_CLASS_PATTERN do |classes, style, error|
48
- raise "CSS parse error: '#{error.inspect}' not recognized" if error
49
- styles = classes.scan(/[-\w]+/)
50
- cl = styles.pop
51
- @classes[cl] ||= Hash.new
52
- @classes[cl][styles] = style.to_s.strip
53
- end
54
- end
55
-
56
- end
57
- end
58
-
59
- end
60
- end
61
-
62
- if $0 == __FILE__
63
- require 'pp'
64
- pp CodeRay::Encoders::HTML::CSS.new
65
- end
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ class HTML
5
+ class CSS
6
+
7
+ attr :stylesheet
8
+
9
+ def CSS.load_stylesheet style = nil
10
+ CodeRay::Styles[style]
11
+ end
12
+
13
+ def initialize style = :default
14
+ @classes = Hash.new
15
+ style = CSS.load_stylesheet style
16
+ @stylesheet = [
17
+ style::CSS_MAIN_STYLES,
18
+ style::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ')
19
+ ].join("\n")
20
+ parse style::TOKEN_COLORS
21
+ end
22
+
23
+ def [] *styles
24
+ cl = @classes[styles.first]
25
+ return '' unless cl
26
+ style = ''
27
+ 1.upto(styles.size) do |offset|
28
+ break if style = cl[styles[offset .. -1]]
29
+ end
30
+ raise 'Style not found: %p' % [styles] if $DEBUG and style.empty?
31
+ return style
32
+ end
33
+
34
+ private
35
+
36
+ CSS_CLASS_PATTERN = /
37
+ ( (?: # $1 = classes
38
+ \s* \. [-\w]+
39
+ )+ )
40
+ \s* \{ \s*
41
+ ( [^\}]+ )? # $2 = style
42
+ \s* \} \s*
43
+ |
44
+ ( . ) # $3 = error
45
+ /mx
46
+ def parse stylesheet
47
+ stylesheet.scan CSS_CLASS_PATTERN do |classes, style, error|
48
+ raise "CSS parse error: '#{error.inspect}' not recognized" if error
49
+ styles = classes.scan(/[-\w]+/)
50
+ cl = styles.pop
51
+ @classes[cl] ||= Hash.new
52
+ @classes[cl][styles] = style.to_s.strip
53
+ end
54
+ end
55
+
56
+ end
57
+ end
58
+
59
+ end
60
+ end
61
+
62
+ if $0 == __FILE__
63
+ require 'pp'
64
+ pp CodeRay::Encoders::HTML::CSS.new
65
+ end
@@ -1,122 +1,122 @@
1
- module CodeRay
2
- module Encoders
3
-
4
- class HTML
5
-
6
- module Output
7
-
8
- def numerize *args
9
- clone.numerize!(*args)
10
- end
11
-
12
- =begin NUMERIZABLE_WRAPPINGS = {
13
- :table => [:div, :page, nil],
14
- :inline => :all,
15
- :list => [:div, :page, nil]
16
- }
17
- NUMERIZABLE_WRAPPINGS.default = :all
18
- =end
19
- def numerize! mode = :table, options = {}
20
- return self unless mode
21
-
22
- options = DEFAULT_OPTIONS.merge options
23
-
24
- start = options[:line_number_start]
25
- unless start.is_a? Integer
26
- raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
27
- end
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
33
-
34
- bold_every = options[:bold_every]
35
- bolding =
36
- if bold_every == false
37
- proc { |line| line.to_s }
38
- elsif bold_every.is_a? Integer
39
- raise ArgumentError, ":bolding can't be 0." if bold_every == 0
40
- proc do |line|
41
- if line % bold_every == 0
42
- "<strong>#{line}</strong>" # every bold_every-th number in bold
43
- else
44
- line.to_s
45
- end
46
- end
47
- else
48
- raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
49
- end
50
-
51
- case mode
52
- when :inline
53
- max_width = (start + line_count).to_s.size
54
- line = start
55
- gsub!(/^/) do
56
- line_number = bolding.call line
57
- indent = ' ' * (max_width - line.to_s.size)
58
- res = "<span class=\"no\">#{indent}#{line_number}</span> "
59
- line += 1
60
- res
61
- end
62
-
63
- when :table
64
- # This is really ugly.
65
- # Because even monospace fonts seem to have different heights when bold,
66
- # I make the newline bold, both in the code and the line numbers.
67
- # FIXME Still not working perfect for Mr. Internet Exploder
68
- # FIXME Firefox struggles with very long codes (> 200 lines)
69
- line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n")
70
- line_numbers << "\n" # also for Mr. MS Internet Exploder :-/
71
- line_numbers.gsub!(/\n/) { "<tt>\n</tt>" }
72
-
73
- line_numbers_table_tpl = TABLE.apply('LINE_NUMBERS', line_numbers)
74
- gsub!(/\n/) { "<tt>\n</tt>" }
75
- wrap_in! line_numbers_table_tpl
76
- @wrapped_in = :div
77
-
78
- when :list
79
- opened_tags = []
80
- gsub!(/^.*$\n?/) do |line|
81
- line.chomp!
82
-
83
- open = opened_tags.join
84
- line.scan(%r!<(/)?span[^>]*>?!) do |close,|
85
- if close
86
- opened_tags.pop
87
- else
88
- opened_tags << $&
89
- end
90
- end
91
- close = '</span>' * opened_tags.size
92
-
93
- "<li>#{open}#{line}#{close}</li>"
94
- end
95
- wrap_in! LIST
96
- @wrapped_in = :div
97
-
98
- else
99
- raise ArgumentError, 'Unknown value %p for mode: expected one of %p' %
100
- [mode, [:table, :list, :inline]]
101
- end
102
-
103
- self
104
- end
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
-
117
- end
118
-
119
- end
120
-
121
- end
122
- end
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ class HTML
5
+
6
+ module Output
7
+
8
+ def numerize *args
9
+ clone.numerize!(*args)
10
+ end
11
+
12
+ =begin NUMERIZABLE_WRAPPINGS = {
13
+ :table => [:div, :page, nil],
14
+ :inline => :all,
15
+ :list => [:div, :page, nil]
16
+ }
17
+ NUMERIZABLE_WRAPPINGS.default = :all
18
+ =end
19
+ def numerize! mode = :table, options = {}
20
+ return self unless mode
21
+
22
+ options = DEFAULT_OPTIONS.merge options
23
+
24
+ start = options[:line_number_start]
25
+ unless start.is_a? Integer
26
+ raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
27
+ end
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
33
+
34
+ bold_every = options[:bold_every]
35
+ bolding =
36
+ if bold_every == false
37
+ proc { |line| line.to_s }
38
+ elsif bold_every.is_a? Integer
39
+ raise ArgumentError, ":bolding can't be 0." if bold_every == 0
40
+ proc do |line|
41
+ if line % bold_every == 0
42
+ "<strong>#{line}</strong>" # every bold_every-th number in bold
43
+ else
44
+ line.to_s
45
+ end
46
+ end
47
+ else
48
+ raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
49
+ end
50
+
51
+ case mode
52
+ when :inline
53
+ max_width = (start + line_count).to_s.size
54
+ line = start
55
+ gsub!(/^/) do
56
+ line_number = bolding.call line
57
+ indent = ' ' * (max_width - line.to_s.size)
58
+ res = "<span class=\"no\">#{indent}#{line_number}</span> "
59
+ line += 1
60
+ res
61
+ end
62
+
63
+ when :table
64
+ # This is really ugly.
65
+ # Because even monospace fonts seem to have different heights when bold,
66
+ # I make the newline bold, both in the code and the line numbers.
67
+ # FIXME Still not working perfect for Mr. Internet Exploder
68
+ # FIXME Firefox struggles with very long codes (> 200 lines)
69
+ line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n")
70
+ line_numbers << "\n" # also for Mr. MS Internet Exploder :-/
71
+ line_numbers.gsub!(/\n/) { "<tt>\n</tt>" }
72
+
73
+ line_numbers_table_tpl = TABLE.apply('LINE_NUMBERS', line_numbers)
74
+ gsub!(/\n/) { "<tt>\n</tt>" }
75
+ wrap_in! line_numbers_table_tpl
76
+ @wrapped_in = :div
77
+
78
+ when :list
79
+ opened_tags = []
80
+ gsub!(/^.*$\n?/) do |line|
81
+ line.chomp!
82
+
83
+ open = opened_tags.join
84
+ line.scan(%r!<(/)?span[^>]*>?!) do |close,|
85
+ if close
86
+ opened_tags.pop
87
+ else
88
+ opened_tags << $&
89
+ end
90
+ end
91
+ close = '</span>' * opened_tags.size
92
+
93
+ "<li>#{open}#{line}#{close}</li>"
94
+ end
95
+ wrap_in! LIST
96
+ @wrapped_in = :div
97
+
98
+ else
99
+ raise ArgumentError, 'Unknown value %p for mode: expected one of %p' %
100
+ [mode, [:table, :list, :inline]]
101
+ end
102
+
103
+ self
104
+ end
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
+
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+ end