Skip to content

Commit 720aa80

Browse files
committed
quick hack to force some html output that faji wants
1 parent a4b8b09 commit 720aa80

File tree

1 file changed

+52
-46
lines changed

1 file changed

+52
-46
lines changed

lib/coderay/encoders/html.rb

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module CodeRay
44
module Encoders
5-
5+
66
# = HTML Encoder
77
#
88
# This is CodeRay's most important highlighter:
@@ -15,7 +15,7 @@ module Encoders
1515
# puts CodeRay.scan('Some /code/', :ruby).html(:wrap => :span)
1616
# #-> <span class="CodeRay"><span class="co">Some</span> /code/</span>
1717
# puts CodeRay.scan('Some /code/', :ruby).span #-> the same
18-
#
18+
#
1919
# puts CodeRay.scan('Some code', :ruby).html(
2020
# :wrap => nil,
2121
# :line_numbers => :inline,
@@ -26,7 +26,7 @@ module Encoders
2626
#
2727
# === :tab_width
2828
# Convert \t characters to +n+ spaces (a number.)
29-
#
29+
#
3030
# Default: 8
3131
#
3232
# === :css
@@ -42,13 +42,13 @@ module Encoders
4242
# Default: nil
4343
#
4444
# === :title
45-
#
45+
#
4646
# The title of the HTML page (works only when :wrap is set to :page.)
4747
#
4848
# Default: 'CodeRay output'
4949
#
5050
# === :break_lines
51-
#
51+
#
5252
# Split multiline blocks at line breaks.
5353
# Forced to true if :line_numbers option is set to :inline.
5454
#
@@ -78,10 +78,10 @@ module Encoders
7878
# Default: 10
7979
#
8080
# === :highlight_lines
81-
#
81+
#
8282
# Highlights certain line numbers.
8383
# Can be any Enumerable, typically just an Array or Range, of numbers.
84-
#
84+
#
8585
# Bolding is deactivated when :highlight_lines is set. It only makes sense
8686
# in combination with :line_numbers.
8787
#
@@ -94,45 +94,45 @@ module Encoders
9494
#
9595
# Default: false
9696
class HTML < Encoder
97-
97+
9898
register_for :html
99-
99+
100100
FILE_EXTENSION = 'snippet.html'
101-
101+
102102
DEFAULT_OPTIONS = {
103103
:tab_width => 8,
104-
104+
105105
:css => :class,
106106
:style => :alpha,
107107
:wrap => nil,
108108
:title => 'CodeRay output',
109-
109+
110110
:break_lines => false,
111-
111+
112112
:line_numbers => nil,
113113
:line_number_anchors => 'n',
114114
:line_number_start => 1,
115115
:bold_every => 10,
116116
:highlight_lines => nil,
117-
117+
118118
:hint => false,
119119
}
120-
120+
121121
autoload :Output, CodeRay.coderay_path('encoders', 'html', 'output')
122122
autoload :CSS, CodeRay.coderay_path('encoders', 'html', 'css')
123123
autoload :Numbering, CodeRay.coderay_path('encoders', 'html', 'numbering')
124-
124+
125125
attr_reader :css
126-
126+
127127
protected
128-
128+
129129
HTML_ESCAPE = { #:nodoc:
130130
'&' => '&amp;',
131131
'"' => '&quot;',
132132
'>' => '&gt;',
133133
'<' => '&lt;',
134134
}
135-
135+
136136
# This was to prevent illegal HTML.
137137
# Strange chars should still be avoided in codes.
138138
evil_chars = Array(0x00...0x20) - [?\n, ?\t, ?\s]
@@ -142,15 +142,15 @@ class HTML < Encoder
142142
# \x9 (\t) and \xA (\n) not included
143143
#HTML_ESCAPE_PATTERN = /[\t&"><\0-\x8\xB-\x1f\x7f-\xff]/
144144
HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1f]/
145-
145+
146146
TOKEN_KIND_TO_INFO = Hash.new do |h, kind|
147147
h[kind] = kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
148148
end
149-
149+
150150
TRANSPARENT_TOKEN_KINDS = Set[
151151
:delimiter, :modifier, :content, :escape, :inline_delimiter,
152152
]
153-
153+
154154
# Generate a hint about the given +kinds+ in a +hint+ style.
155155
#
156156
# +hint+ may be :info, :info_long or :debug.
@@ -168,32 +168,33 @@ def self.token_path_to_hint hint, kinds
168168
end
169169
title ? " title=\"#{title}\"" : ''
170170
end
171-
171+
172172
def setup options
173173
super
174-
174+
175+
@line_number = 1
175176
if options[:wrap] || options[:line_numbers]
176177
@real_out = @out
177178
@out = ''
178179
end
179-
180+
180181
options[:break_lines] = true if options[:line_numbers] == :inline
181-
182+
182183
@break_lines = (options[:break_lines] == true)
183-
184+
184185
@HTML_ESCAPE = HTML_ESCAPE.dup
185186
@HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
186-
187+
187188
@opened = []
188189
@last_opened = nil
189190
@css = CSS.new options[:style]
190-
191+
191192
hint = options[:hint]
192193
if hint && ![:debug, :info, :info_long].include?(hint)
193194
raise ArgumentError, "Unknown value %p for :hint; \
194195
expected :info, :info_long, :debug, false, or nil." % hint
195196
end
196-
197+
197198
css_classes = TokenKinds
198199
case options[:css]
199200
when :class
@@ -225,42 +226,42 @@ def setup options
225226
else
226227
raise ArgumentError, "Unknown value %p for :css." % options[:css]
227228
end
228-
229+
229230
@set_last_opened = options[:hint] || options[:css] == :style
230231
end
231-
232+
232233
def finish options
233234
unless @opened.empty?
234235
warn '%d tokens still open: %p' % [@opened.size, @opened] if $CODERAY_DEBUG
235236
@out << '</span>' while @opened.pop
236237
@last_opened = nil
237238
end
238-
239+
239240
@out.extend Output
240241
@out.css = @css
241242
if options[:line_numbers]
242243
Numbering.number! @out, options[:line_numbers], options
243244
end
244245
@out.wrap! options[:wrap]
245246
@out.apply_title! options[:title]
246-
247+
247248
if defined?(@real_out) && @real_out
248249
@real_out << @out
249250
@out = @real_out
250251
end
251-
252+
252253
super
253254
end
254-
255+
255256
public
256-
257+
257258
def text_token text, kind
258259
if text =~ /#{HTML_ESCAPE_PATTERN}/o
259260
text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
260261
end
261-
262+
262263
style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
263-
264+
264265
if @break_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0
265266
close = '</span>' * c
266267
reopen = ''
@@ -269,21 +270,21 @@ def text_token text, kind
269270
end
270271
text[i .. -1] = text[i .. -1].gsub("\n", "#{close}\n#{reopen}#{style}")
271272
end
272-
273+
273274
if style
274275
@out << style << text << '</span>'
275276
else
276277
@out << text
277278
end
278279
end
279-
280+
280281
# token groups, eg. strings
281282
def begin_group kind
282283
@out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '<span>')
283284
@opened << kind
284285
@last_opened = kind if @set_last_opened
285286
end
286-
287+
287288
def end_group kind
288289
if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind)
289290
warn 'Malformed token stream: Trying to close a token (%p) ' \
@@ -294,9 +295,12 @@ def end_group kind
294295
@last_opened = @opened.last if @last_opened
295296
end
296297
end
297-
298+
298299
# whole lines to be highlighted, eg. a deleted line in a diff
299300
def begin_line kind
301+
@out << '<div class="line'
302+
@out << ' odd' if @line_number.odd?
303+
@out << '">'
300304
if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
301305
if style['class="']
302306
@out << style.sub('class="', 'class="line ')
@@ -309,7 +313,7 @@ def begin_line kind
309313
@opened << kind
310314
@last_opened = kind if @options[:css] == :style
311315
end
312-
316+
313317
def end_line kind
314318
if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind)
315319
warn 'Malformed token stream: Trying to close a line (%p) ' \
@@ -319,9 +323,11 @@ def end_line kind
319323
@out << '</span>'
320324
@last_opened = @opened.last if @last_opened
321325
end
326+
@out << '</div>'
327+
@line_number += 1
322328
end
323-
329+
324330
end
325-
331+
326332
end
327333
end

0 commit comments

Comments
 (0)