This module holds the Encoder class and its subclasses. For example, the HTML encoder is named CodeRay::Encoders::HTML can be found in coderay/encoders/html.
Encoders also provides methods and constants for the register mechanism and the [] method that returns the Encoder class belonging to the given format.
Generate a hint about the given classes in a hint style.
hint may be :info, :info_long or :debug.
# File lib/coderay/encoders/html.rb, line 145
145: def self.token_path_to_hint hint, classes
146: title =
147: case hint
148: when :info
149: TOKEN_KIND_TO_INFO[classes.first]
150: when :info_long
151: classes.reverse.map { |kind| TOKEN_KIND_TO_INFO[kind] }.join('/')
152: when :debug
153: classes.inspect
154: end
155: title ? " title=\"#{title}\"" : ''
156: end
# File lib/coderay/encoders/html.rb, line 224
224: def finish options
225: not_needed = @opened.shift
226: @out << '</span>' * @opened.size
227: unless @opened.empty?
228: warn '%d tokens still open: %p' % [@opened.size, @opened]
229: end
230:
231: @out.extend Output
232: @out.css = @css
233: @out.numerize! options[:line_numbers], options
234: @out.wrap! options[:wrap]
235: @out.apply_title! options[:title]
236:
237: super
238: end
# File lib/coderay/encoders/html.rb, line 158
158: def setup options
159: super
160:
161: @HTML_ESCAPE = HTML_ESCAPE.dup
162: @HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
163:
164: @opened = [nil]
165: @css = CSS.new options[:style]
166:
167: hint = options[:hint]
168: if hint and not [:debug, :info, :info_long].include? hint
169: raise ArgumentError, "Unknown value %p for :hint; \
170: expected :info, :debug, false, or nil." % hint
171: end
172:
173: case options[:css]
174:
175: when :class
176: @css_style = Hash.new do |h, k|
177: c = CodeRay::Tokens::ClassOfKind[k.first]
178: if c == :NO_HIGHLIGHT and not hint
179: h[k.dup] = false
180: else
181: title = if hint
182: HTML.token_path_to_hint(hint, k[1..1] << k.first)
183: else
184: ''
185: end
186: if c == :NO_HIGHLIGHT
187: h[k.dup] = '<span%s>' % [title]
188: else
189: h[k.dup] = '<span%s class="%s">' % [title, c]
190: end
191: end
192: end
193:
194: when :style
195: @css_style = Hash.new do |h, k|
196: if k.is_a? ::Array
197: styles = k.dup
198: else
199: styles = [k]
200: end
201: type = styles.first
202: classes = styles.map { |c| Tokens::ClassOfKind[c] }
203: if classes.first == :NO_HIGHLIGHT and not hint
204: h[k] = false
205: else
206: styles.shift if TRANSPARENT_TOKEN_KINDS.include? styles.first
207: title = HTML.token_path_to_hint hint, styles
208: style = @css[*classes]
209: h[k] =
210: if style
211: '<span%s style="%s">' % [title, style]
212: else
213: false
214: end
215: end
216: end
217:
218: else
219: raise ArgumentError, "Unknown value %p for :css." % options[:css]
220:
221: end
222: end
# File lib/coderay/encoders/html.rb, line 240
240: def token text, type = :plain
241: case text
242:
243: when nil
244: # raise 'Token with nil as text was given: %p' % [[text, type]]
245:
246: when String
247: if text =~ /#{HTML_ESCAPE_PATTERN}/
248: text = text.gsub(/#{HTML_ESCAPE_PATTERN}/) { |m| @HTML_ESCAPE[m] }
249: end
250: @opened[0] = type
251: if text != "\n" && style = @css_style[@opened]
252: @out << style << text << '</span>'
253: else
254: @out << text
255: end
256:
257:
258: # token groups, eg. strings
259: when :open
260: @opened[0] = type
261: @out << (@css_style[@opened] || '<span>')
262: @opened << type
263: when :close
264: if @opened.empty?
265: # nothing to close
266: else
267: if $CODERAY_DEBUG and (@opened.size == 1 or @opened.last != type)
268: raise 'Malformed token stream: Trying to close a token (%p) \ that is not open. Open are: %p.' % [type, @opened[1..1]]
269: end
270: @out << '</span>'
271: @opened.pop
272: end
273:
274: # whole lines to be highlighted, eg. a deleted line in a diff
275: when :begin_line
276: @opened[0] = type
277: if style = @css_style[@opened]
278: @out << style.sub('<span', '<div')
279: else
280: @out << '<div>'
281: end
282: @opened << type
283: when :end_line
284: if @opened.empty?
285: # nothing to close
286: else
287: if $CODERAY_DEBUG and (@opened.size == 1 or @opened.last != type)
288: raise 'Malformed token stream: Trying to close a line (%p) \ that is not open. Open are: %p.' % [type, @opened[1..1]]
289: end
290: @out << '</div>'
291: @opened.pop
292: end
293:
294: else
295: raise 'unknown token kind: %p' % [text]
296:
297: end
298: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.