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.
- data/README +3 -2
- data/demo/demo_css.rb +1 -1
- data/demo/demo_load_encoder.rb +8 -0
- data/demo/demo_load_scanner.rb +25 -0
- data/demo/demo_more.rb +3 -2
- data/demo/suite.rb +82 -0
- data/lib/coderay.rb +8 -2
- data/lib/coderay/encoder.rb +1 -1
- data/lib/coderay/encoders/_map.rb +8 -0
- data/lib/coderay/encoders/div.rb +3 -1
- data/lib/coderay/encoders/html.rb +8 -5
- data/lib/coderay/encoders/{helpers/html_helper.rb → html/classes.rb} +1 -0
- data/lib/coderay/encoders/html/css.rb +66 -0
- data/lib/coderay/encoders/html/numerization.rb +112 -0
- data/lib/coderay/encoders/{helpers/html_output.rb → html/output.rb} +27 -90
- data/lib/coderay/encoders/span.rb +2 -1
- data/lib/coderay/encoders/statistic.rb +1 -1
- data/lib/coderay/helpers/plugin.rb +100 -88
- data/lib/coderay/helpers/word_list.rb +108 -0
- data/lib/coderay/scanner.rb +3 -3
- data/lib/coderay/scanners/_map.rb +10 -0
- data/lib/coderay/scanners/c.rb +1 -1
- data/lib/coderay/scanners/delphi.rb +1 -1
- data/lib/coderay/scanners/ruby.rb +1 -1
- data/lib/coderay/scanners/{helpers/ruby_helper.rb → ruby/patterns.rb} +0 -0
- data/lib/coderay/style.rb +20 -0
- data/lib/coderay/{encoders/helpers/html_css.rb → styles/cycnus.rb} +22 -59
- data/lib/coderay/styles/murphy.rb +119 -0
- data/lib/coderay/tokens.rb +1 -1
- metadata +23 -15
- data/lib/coderay/helpers/scanner_helper.rb +0 -63
@@ -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
|
data/lib/coderay/scanner.rb
CHANGED
@@ -4,7 +4,7 @@ module CodeRay
|
|
4
4
|
|
5
5
|
# = Scanners
|
6
6
|
#
|
7
|
-
# $Id: scanner.rb
|
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 '
|
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/
|
54
|
+
require 'coderay/helpers/word_list'
|
55
55
|
|
56
56
|
# The default options for all scanner classes.
|
57
57
|
#
|
data/lib/coderay/scanners/c.rb
CHANGED
@@ -25,7 +25,7 @@ module CodeRay module Scanners
|
|
25
25
|
'true', 'false', # C99
|
26
26
|
]
|
27
27
|
|
28
|
-
IDENT_KIND =
|
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)
|
File without changes
|
@@ -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
|
2
|
-
|
3
|
-
|
4
|
-
class
|
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
|
-
|
6
|
+
register_for :cycnus
|
22
7
|
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
13
|
+
CSS_MAIN_STYLES = <<-MAIN
|
44
14
|
.CodeRay {
|
45
|
-
background-color: #
|
46
|
-
border: 1px solid
|
15
|
+
background-color: #{code_background};
|
16
|
+
border: 1px solid #{border_color};
|
47
17
|
font-family: 'Courier New', 'Terminal', monospace;
|
48
|
-
color:
|
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:
|
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: #
|
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
|
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
|
-
|
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
|