1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
Index: lib/coderay/encoders/html.rb ===================================================================--- lib/coderay/encoders/html.rb (revision 200)+++ lib/coderay/encoders/html.rb (revision 250)@@ -1,3 +1,5 @@+require "set"+module CodeRay
module Encoders
@@ -10,7 +12,8 @@ #
# require 'coderay'
# puts CodeRay.scan('Some /code/', :ruby).html #-> a HTML page
- # puts CodeRay.scan('Some /code/', :ruby).html(:wrap => :span) #-> <span class="CodeRay"><span class="co">Some</span> /code/</span> + # puts CodeRay.scan('Some /code/', :ruby).html(:wrap => :span)+ # #-> <span class="CodeRay"><span class="co">Some</span> /code/</span># puts CodeRay.scan('Some /code/', :ruby).span #-> the same
#
# puts CodeRay.scan('Some code', :ruby).html(
@@ -55,7 +58,8 @@ #
# === :hint
# Include some information into the output using the title attribute.
- # Can be :info (show token type on mouse-over), :info_long (with full path) or :debug (via inspect). + # Can be :info (show token type on mouse-over), :info_long (with full path)+ # or :debug (via inspect).#
# Default: false
class HTML < Encoder
@@ -82,7 +86,7 @@ :hint => false,
}
- helper :classes, :output, :css + helper :output, :cssattr_reader :css
@@ -115,11 +119,14 @@ end
}
+ TRANSPARENT_TOKEN_KINDS = [ + :delimiter, :modifier, :content, :escape, :inline_delimiter,+ ].to_set+# Generate a hint about the given +classes+ in a +hint+ style.
#
# +hint+ may be :info, :info_long or :debug.
def self.token_path_to_hint hint, classes
- return '' unless hint title =
case hint
when :info
@@ -129,7 +136,7 @@ when :debug
classes.inspect
end
- " title=\"#{title}\"" + title ? " title=\"#{title}\"" : ''end
def setup options
@@ -143,42 +150,45 @@ hint = options[:hint]
if hint and not [:debug, :info, :info_long].include? hint
- raise ArgumentError, "Unknown value %p for :hint; expected :info, :debug, false or nil." % hint + raise ArgumentError, "Unknown value %p for :hint; \+ expected :info, :debug, false, or nil." % hintend
case options[:css]
when :class
@css_style = Hash.new do |h, k|
- if k.is_a? Array - type = k.first- else- type = k- end- c = ClassOfKind[type]+ c = CodeRay::Tokens::ClassOfKind[k.first]if c == :NO_HIGHLIGHT and not hint
- h[k] = false + h[k.dup] = falseelse
- title = HTML.token_path_to_hint hint, (k[1..-1] << k.first) - h[k] = '<span%s class="%s">' % [title, c]+ title = if hint+ HTML.token_path_to_hint(hint, k[1..-1] << k.first)+ else+ ''+ end+ if c == :NO_HIGHLIGHT+ h[k.dup] = '<span%s>' % [title]+ else+ h[k.dup] = '<span%s class="%s">' % [title, c]+ endend
end
when :style
@css_style = Hash.new do |h, k|
- if k.is_a? Array + if k.is_a? ::Arraystyles = k.dup
else
styles = [k]
end
type = styles.first
- classes = styles.map { |c| ClassOfKind[c] } + classes = styles.map { |c| Tokens::ClassOfKind[c] }if classes.first == :NO_HIGHLIGHT and not hint
h[k] = false
else
- styles.shift if [:delimiter, :modifier, :content, :escape].include? styles.first + styles.shift if TRANSPARENT_TOKEN_KINDS.include? styles.firsttitle = HTML.token_path_to_hint hint, styles
- classes.delete 'il' style = @css[*classes]
h[k] =
if style
@@ -198,7 +208,9 @@ def finish options
not_needed = @opened.shift
@out << '</span>' * @opened.size
- warn '%d tokens still open: %p' % [@opened.size, @opened] unless @opened.empty? + unless @opened.empty?+ warn '%d tokens still open: %p' % [@opened.size, @opened]+ end@out.extend Output
@out.css = @css
@@ -229,8 +241,9 @@ if @opened.empty?
# nothing to close
else
- if @opened.size == 1 or @opened.last != type - raise 'Malformed token stream: Trying to close a token (%p) that is not open. Open are: %p.' % [type, @opened[1..-1]] if $DEBUG+ if $DEBUG and (@opened.size == 1 or @opened.last != type)+ raise 'Malformed token stream: Trying to close a token (%p) \+ that is not open. Open are: %p.' % [type, @opened[1..-1]]end
@out << '</span>'
@opened.pop
|