coderay 0.4.5.73 → 0.5.0.100

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,108 @@
1
+ module CodeRay
2
+ module Scanners
3
+
4
+ class Scanner
5
+
6
+ # A WordList is a Hash with some additional features.
7
+ # It is intended to be used for keyword recognition.
8
+ #
9
+ # WordList is highly optimized to be used in Scanners,
10
+ # typically to decide whether a given ident is a keyword.
11
+ #
12
+ # For case insensitive words use CaseIgnoringWordList.
13
+ #
14
+ # Example:
15
+ #
16
+ # # define word arrays
17
+ # RESERVED_WORDS = %w[
18
+ # asm break case continue default do else
19
+ # ...
20
+ # ]
21
+ #
22
+ # PREDEFINED_TYPES = %w[
23
+ # int long short char void
24
+ # ...
25
+ # ]
26
+ #
27
+ # PREDEFINED_CONSTANTS = %w[
28
+ # EOF NULL ...
29
+ # ]
30
+ #
31
+ # # make a WordList
32
+ # IDENT_KIND = WordList.new(:ident).
33
+ # add(RESERVED_WORDS, :reserved).
34
+ # add(PREDEFINED_TYPES, :pre_type).
35
+ # add(PREDEFINED_CONSTANTS, :pre_constant)
36
+ #
37
+ # ...
38
+ #
39
+ # def scan_tokens tokens, options
40
+ # ...
41
+ #
42
+ # elsif scan(/[A-Za-z_][A-Za-z_0-9]*/)
43
+ # # use it
44
+ # kind = IDENT_KIND[match]
45
+ # ...
46
+ #
47
+ class WordList < Hash
48
+
49
+ # Creates a new WordList with +default+ as default value.
50
+ def initialize default = false, &block
51
+ super default, &block
52
+ end
53
+
54
+ # Checks if a word is included.
55
+ def include? word
56
+ has_key? word
57
+ end
58
+
59
+ # Add words to the list and associate them with +kind+.
60
+ def add words, kind = true
61
+ words.each do |word|
62
+ self[word] = kind
63
+ end
64
+ self
65
+ end
66
+
67
+ end
68
+
69
+
70
+ # A WordList is a Hash with some additional features.
71
+ # It is intended to be used for keyword recognition.
72
+ #
73
+ # Keys are compared case-insensitively.
74
+ #
75
+ # See WordList.
76
+ class CaseIgnoringWordList < WordList
77
+
78
+ # Creates a new WordList with +default+ as default value.
79
+ #
80
+ # Text case is ignored.
81
+ def initialize default = false, &block
82
+ block ||= proc do |h, k|
83
+ h[k] = h.fetch k.downcase, default
84
+ end
85
+ super default
86
+ end
87
+
88
+ # Checks if a word is included.
89
+ def include? word
90
+ has_key? word.downcase
91
+ end
92
+
93
+ # Add words to the list and associate them with +kind+.
94
+ def add words, kind = true
95
+ words.each do |word|
96
+ self[word.downcase] = kind
97
+ end
98
+ self
99
+ end
100
+
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+ end
107
+
108
+ # vim:sw=2:ts=2:noet:tw=78
@@ -4,7 +4,7 @@ module CodeRay
4
4
 
5
5
  # = Scanners
6
6
  #
7
- # $Id: scanner.rb 39 2005-09-29 04:35:37Z murphy $
7
+ # $Id: scanner.rb 90 2005-11-05 14:37:40Z murphy $
8
8
  #
9
9
  # This module holds the Scanner class and its subclasses.
10
10
  # For example, the Ruby scanner is named CodeRay::Scanners::Ruby
@@ -17,7 +17,7 @@ module CodeRay
17
17
  # See PluginHost.
18
18
  module Scanners
19
19
  extend PluginHost
20
- plugin_path 'coderay/scanners'
20
+ plugin_path File.dirname(__FILE__), 'scanners'
21
21
 
22
22
  require 'strscan'
23
23
 
@@ -51,7 +51,7 @@ module CodeRay
51
51
  # Raised if a Scanner fails while scanning
52
52
  ScanError = Class.new(Exception)
53
53
 
54
- require 'coderay/helpers/scanner_helper'
54
+ require 'coderay/helpers/word_list'
55
55
 
56
56
  # The default options for all scanner classes.
57
57
  #
@@ -0,0 +1,10 @@
1
+ module CodeRay
2
+ module Scanners
3
+
4
+ map :cpp => :c,
5
+ :plain => :plaintext,
6
+ :pascal => :delphi,
7
+ :irb => :ruby
8
+
9
+ end
10
+ end
@@ -25,7 +25,7 @@ module CodeRay module Scanners
25
25
  'true', 'false', # C99
26
26
  ]
27
27
 
28
- IDENT_KIND = Scanner::WordList.new(:ident).
28
+ IDENT_KIND = WordList.new(:ident).
29
29
  add(RESERVED_WORDS, :reserved).
30
30
  add(PREDEFINED_TYPES, :pre_type).
31
31
  add(PREDEFINED_CONSTANTS, :pre_constant)
@@ -28,7 +28,7 @@ module CodeRay module Scanners
28
28
  'virtual', 'write', 'writeonly'
29
29
  ]
30
30
 
31
- IDENT_KIND = Scanner::WordList.new(:ident, :case_ignore).
31
+ IDENT_KIND = CaseIgnoringWordList.new(:ident).
32
32
  add(RESERVED_WORDS, :reserved).
33
33
  add(DIRECTIVES, :directive)
34
34
 
@@ -18,7 +18,7 @@ module CodeRay module Scanners
18
18
 
19
19
  register_for :ruby
20
20
 
21
- require 'coderay/scanners/helpers/ruby_helper'
21
+ helper :patterns
22
22
 
23
23
  DEFAULT_OPTIONS = {
24
24
  :parse_regexps => true,
@@ -0,0 +1,20 @@
1
+ module CodeRay
2
+
3
+ # This module holds the Style class and its subclasses.
4
+ #
5
+ # See Plugin.
6
+ module Styles
7
+ extend PluginHost
8
+ plugin_path File.dirname(__FILE__), 'styles'
9
+
10
+ class Style
11
+ extend Plugin
12
+ plugin_host Styles
13
+
14
+ DEFAULT_OPTIONS = { }
15
+
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -1,65 +1,33 @@
1
- module CodeRay module Encoders
2
-
3
- class HTML
4
- class CSS
5
-
6
- def initialize stylesheet = TOKENS
7
- @classes = Hash.new
8
- parse stylesheet
9
- end
10
-
11
- def [] *styles
12
- cl = @classes[styles.first]
13
- return '' unless cl
14
- style = false
15
- 1.upto(cl.size + 1) do |offset|
16
- break if style = cl[styles[offset .. -1]]
17
- end
18
- return style
19
- end
1
+ module CodeRay
2
+ module Styles
3
+
4
+ class CYcnus < Style
20
5
 
21
- private
6
+ register_for :cycnus
22
7
 
23
- CSS_CLASS_PATTERN = /
24
- ( (?: # $1 = classes
25
- \s* \. [-\w]+
26
- )+ )
27
- \s* \{
28
- ( [^\}]* ) # $2 = style
29
- \} \s*
30
- |
31
- ( . ) # $3 = error
32
- /mx
33
- def parse stylesheet
34
- stylesheet.scan CSS_CLASS_PATTERN do |classes, style, error|
35
- raise "CSS parse error: '#{error.inspect}' not recognized" if error
36
- styles = classes.scan(/[-\w]+/)
37
- cl = styles.pop
38
- @classes[cl] ||= Hash.new
39
- @classes[cl][styles] = style.strip
40
- end
41
- end
8
+ code_background = '#f8f8f8'
9
+ numbers_background = '#def'
10
+ border_color = 'silver'
11
+ normal_color = '#100'
42
12
 
43
- MAIN = <<-'MAIN'
13
+ CSS_MAIN_STYLES = <<-MAIN
44
14
  .CodeRay {
45
- background-color: #f8f8f8;
46
- border: 1px solid silver;
15
+ background-color: #{code_background};
16
+ border: 1px solid #{border_color};
47
17
  font-family: 'Courier New', 'Terminal', monospace;
48
- color: black;
49
- width: 100%;
50
- padding: 2px;
18
+ color: #{normal_color};
51
19
  }
52
20
  .CodeRay pre { margin: 0px; }
53
21
 
54
22
  div.CodeRay { }
55
23
 
56
- span.CodeRay { white-space: pre; border: 0; }
24
+ span.CodeRay { white-space: pre; border: 0px; padding: 2px; }
57
25
 
58
- table.CodeRay { border-collapse: collapse; }
26
+ table.CodeRay { border-collapse: collapse; width: 100%; padding: 2px; }
59
27
  table.CodeRay td { padding: 2px 4px; vertical-align: top; }
60
28
 
61
29
  .CodeRay .line_numbers, .CodeRay .no {
62
- background-color: #def;
30
+ background-color: #{numbers_background};
63
31
  color: gray;
64
32
  text-align: right;
65
33
  }
@@ -67,12 +35,13 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
67
35
  .CodeRay .no { padding: 0px 4px; }
68
36
  .CodeRay .code { width: 100%; }
69
37
 
70
- .CodeRay .code {
71
- }
38
+ ol.CodeRay { font-size: 10pt; }
39
+ ol.CodeRay li { white-space: pre; }
40
+
72
41
  .CodeRay .code pre { overflow: auto; }
73
42
  MAIN
74
43
 
75
- TOKENS = <<-'TOKENS'
44
+ TOKEN_COLORS = <<-'TOKENS'
76
45
  .af { color:#00C; }
77
46
  .an { color:#007; }
78
47
  .av { color:#700; }
@@ -109,6 +78,7 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
109
78
  .lv { color:#963; }
110
79
  .oc { color:#40E; font-weight:bold; }
111
80
  .on { color:#000; font-weight:bold; }
81
+ .op { }
112
82
  .pc { color:#038; font-weight:bold; }
113
83
  .pd { color:#369; font-weight:bold; }
114
84
  .pp { color:#579; }
@@ -143,14 +113,7 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
143
113
  .xt { color:#444; }
144
114
  TOKENS
145
115
 
146
- DEFAULT_STYLESHEET = MAIN + TOKENS.gsub(/^(?!$)/, '.CodeRay ')
147
-
148
116
  end
117
+
149
118
  end
150
-
151
- end end
152
-
153
- if $0 == __FILE__
154
- require 'pp'
155
- pp CodeRay::Encoders::HTML::CSS.new
156
119
  end
@@ -0,0 +1,119 @@
1
+ module CodeRay
2
+ module Styles
3
+
4
+ class Murphy < Style
5
+
6
+ register_for :murphy
7
+
8
+ code_background = '#001129'
9
+ numbers_background = code_background
10
+ border_color = 'silver'
11
+ normal_color = '#C0C0C0'
12
+
13
+ CSS_MAIN_STYLES = <<-MAIN
14
+ .CodeRay {
15
+ background-color: #{code_background};
16
+ border: 1px solid #{border_color};
17
+ font-family: 'Courier New', 'Terminal', monospace;
18
+ color: #{normal_color};
19
+ }
20
+ .CodeRay pre { margin: 0px; }
21
+
22
+ div.CodeRay { }
23
+
24
+ span.CodeRay { white-space: pre; border: 0px; padding: 2px; }
25
+
26
+ table.CodeRay { border-collapse: collapse; width: 100%; padding: 2px; }
27
+ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
28
+
29
+ .CodeRay .line_numbers, .CodeRay .no {
30
+ background-color: #{numbers_background};
31
+ color: gray;
32
+ text-align: right;
33
+ }
34
+ .CodeRay .line_numbers tt { font-weight: bold; }
35
+ .CodeRay .no { padding: 0px 4px; }
36
+ .CodeRay .code { width: 100%; }
37
+
38
+ ol.CodeRay { font-size: 10pt; }
39
+ ol.CodeRay li { white-space: pre; }
40
+
41
+ .CodeRay .code pre { overflow: auto; }
42
+ MAIN
43
+
44
+ TOKEN_COLORS = <<-'TOKENS'
45
+ .af { color:#00C; }
46
+ .an { color:#007; }
47
+ .av { color:#700; }
48
+ .aw { color:#C00; }
49
+ .bi { color:#509; font-weight:bold; }
50
+ .c { color:#666; }
51
+
52
+ .ch { color:#88F; }
53
+ .ch .k { color:#04D; }
54
+ .ch .dl { color:#039; }
55
+
56
+ .cl { color:#e9e; font-weight:bold; }
57
+ .co { color:#5ED; font-weight:bold; }
58
+ .cr { color:#0A0; }
59
+ .cv { color:#ccf; }
60
+ .df { color:#099; font-weight:bold; }
61
+ .di { color:#088; font-weight:bold; }
62
+ .dl { color:black; }
63
+ .do { color:#970; }
64
+ .ds { color:#D42; font-weight:bold; }
65
+ .e { color:#666; font-weight:bold; }
66
+ .er { color:#F00; background-color:#FAA; }
67
+ .ex { color:#F00; font-weight:bold; }
68
+ .fl { color:#60E; font-weight:bold; }
69
+ .fu { color:#5ed; font-weight:bold; }
70
+ .gv { color:#f84; }
71
+ .hx { color:#058; font-weight:bold; }
72
+ .i { color:#66f; font-weight:bold; }
73
+ .ic { color:#B44; font-weight:bold; }
74
+ .il { }
75
+ .in { color:#B2B; font-weight:bold; }
76
+ .iv { color:#aaf; }
77
+ .la { color:#970; font-weight:bold; }
78
+ .lv { color:#963; }
79
+ .oc { color:#40E; font-weight:bold; }
80
+ .on { color:#000; font-weight:bold; }
81
+ .op { }
82
+ .pc { color:#08f; font-weight:bold; }
83
+ .pd { color:#369; font-weight:bold; }
84
+ .pp { color:#579; }
85
+ .pt { color:#66f; font-weight:bold; }
86
+ .r { color:#5de; font-weight:bold; }
87
+
88
+ .rx { background-color:#221133; }
89
+ .rx .k { color:#f8f; }
90
+ .rx .dl { color:#f0f; }
91
+ .rx .mod { color:#f0b; }
92
+ .rx .fu { color:#404; font-weight: bold; }
93
+
94
+ .s { background-color:#331122; }
95
+ .s .s { background-color:#ffe0e0; }
96
+ .s .s .s { background-color:#ffd0d0; }
97
+ .s .k { color:#F88; }
98
+ .s .dl { color:#f55; }
99
+
100
+ .sh { background-color:#f0fff0; }
101
+ .sh .k { color:#2B2; }
102
+ .sh .dl { color:#161; }
103
+
104
+ .sy { color:#Fc8; }
105
+ .sy .k { color:#Fc8; }
106
+ .sy .dl { color:#F84; }
107
+
108
+ .ta { color:#070; }
109
+ .tf { color:#070; font-weight:bold; }
110
+ .ts { color:#D70; font-weight:bold; }
111
+ .ty { color:#339; font-weight:bold; }
112
+ .v { color:#036; }
113
+ .xt { color:#444; }
114
+ TOKENS
115
+
116
+ end
117
+
118
+ end
119
+ end
@@ -101,7 +101,7 @@ module CodeRay
101
101
  unless kind_filter
102
102
  super(&block)
103
103
  else
104
- super do |text, kind|
104
+ super() do |text, kind|
105
105
  next unless kind == kind_filter
106
106
  yield text, kind
107
107
  end