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.
| TRANSPARENT_TOKEN_KINDS | = | Set[ :delimiter, :modifier, :content, :escape, :inline_delimiter, ] |
Generate a hint about the given kinds in a hint style.
hint may be :info, :info_long or :debug.
# File lib/coderay/encoders/html.rb, line 148
148: def self.token_path_to_hint hint, kinds
149: kinds = Array kinds
150: title =
151: case hint
152: when :info
153: kinds = kinds[1..-1] if TRANSPARENT_TOKEN_KINDS.include? kinds.first
154: TOKEN_KIND_TO_INFO[kinds.first]
155: when :info_long
156: kinds.reverse.map { |kind| TOKEN_KIND_TO_INFO[kind] }.join('/')
157: when :debug
158: kinds.inspect
159: end
160: title ? " title=\"#{title}\"" : ''
161: end
token groups, eg. strings
# File lib/coderay/encoders/html.rb, line 256
256: def begin_group kind
257: @out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '<span>')
258: @opened << kind
259: @last_opened = kind if @set_last_opened
260: end
whole lines to be highlighted, eg. a deleted line in a diff
# File lib/coderay/encoders/html.rb, line 274
274: def begin_line kind
275: if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
276: if style['class="']
277: @out << style.sub('class="', 'class="line ')
278: else
279: @out << style.sub('>', ' class="line">')
280: end
281: else
282: @out << '<span class="line">'
283: end
284: @opened << kind
285: @last_opened = kind if @options[:css] == :style
286: end
# File lib/coderay/encoders/html.rb, line 262
262: def end_group kind
263: if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind)
264: warn 'Malformed token stream: Trying to close a token (%p) ' \
265: 'that is not open. Open are: %p.' % [kind, @opened[1..-1]]
266: end
267: if @opened.pop
268: @out << '</span>'
269: @last_opened = @opened.last if @last_opened
270: end
271: end
# File lib/coderay/encoders/html.rb, line 288
288: def end_line kind
289: if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind)
290: warn 'Malformed token stream: Trying to close a line (%p) ' \
291: 'that is not open. Open are: %p.' % [kind, @opened[1..-1]]
292: end
293: if @opened.pop
294: @out << '</span>'
295: @last_opened = @opened.last if @last_opened
296: end
297: end
# File lib/coderay/encoders/html.rb, line 219
219: def finish options
220: unless @opened.empty?
221: warn '%d tokens still open: %p' % [@opened.size, @opened] if $CODERAY_DEBUG
222: @out << '</span>' while @opened.pop
223: @last_opened = nil
224: end
225:
226: @out.extend Output
227: @out.css = @css
228: if options[:line_numbers]
229: Numbering.number! @out, options[:line_numbers], options
230: end
231: @out.wrap! options[:wrap]
232: @out.apply_title! options[:title]
233:
234: if defined?(@real_out) && @real_out
235: @real_out << @out
236: @out = @real_out
237: end
238:
239: super
240: end
# File lib/coderay/encoders/html.rb, line 163
163: def setup options
164: super
165:
166: if options[:wrap] || options[:line_numbers]
167: @real_out = @out
168: @out = ''
169: end
170:
171: @HTML_ESCAPE = HTML_ESCAPE.dup
172: @HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
173:
174: @opened = []
175: @last_opened = nil
176: @css = CSS.new options[:style]
177:
178: hint = options[:hint]
179: if hint && ![:debug, :info, :info_long].include?(hint)
180: raise ArgumentError, "Unknown value %p for :hint; \
181: expected :info, :info_long, :debug, false, or nil." % hint
182: end
183:
184: css_classes = TokenKinds
185: case options[:css]
186: when :class
187: @span_for_kind = Hash.new do |h, k|
188: if k.is_a? ::Symbol
189: kind = k_dup = k
190: else
191: kind = k.first
192: k_dup = k.dup
193: end
194: if kind != :space && (hint || css_class = css_classes[kind])
195: title = HTML.token_path_to_hint hint, k if hint
196: css_class ||= css_classes[kind]
197: h[k_dup] = "<span#{title}#{" class=\"#{css_class}\"" if css_class}>"
198: else
199: h[k_dup] = nil
200: end
201: end
202: when :style
203: @span_for_kind = Hash.new do |h, k|
204: kind = k.is_a?(Symbol) ? k : k.first
205: h[k.is_a?(Symbol) ? k : k.dup] =
206: if kind != :space && (hint || css_classes[kind])
207: title = HTML.token_path_to_hint hint, k if hint
208: style = @css.get_style Array(k).map { |c| css_classes[c] }
209: "<span#{title}#{" style=\"#{style}\"" if style}>"
210: end
211: end
212: else
213: raise ArgumentError, "Unknown value %p for :css." % options[:css]
214: end
215:
216: @set_last_opened = options[:hint] || options[:css] == :style
217: end
# File lib/coderay/encoders/html.rb, line 244
244: def text_token text, kind
245: if text =~ /#{HTML_ESCAPE_PATTERN}/o
246: text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
247: end
248: if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
249: @out << style << text << '</span>'
250: else
251: @out << text
252: end
253: end