Module CodeRay::Encoders
In: lib/coderay/encoder.rb
lib/coderay/encoders/_map.rb
lib/coderay/encoders/token_class_filter.rb
lib/coderay/encoders/xml.rb
lib/coderay/encoders/null.rb
lib/coderay/encoders/page.rb
lib/coderay/encoders/term.rb
lib/coderay/encoders/filter.rb
lib/coderay/encoders/yaml.rb
lib/coderay/encoders/span.rb
lib/coderay/encoders/text.rb
lib/coderay/encoders/statistic.rb
lib/coderay/encoders/comment_filter.rb
lib/coderay/encoders/count.rb
lib/coderay/encoders/html/output.rb
lib/coderay/encoders/html/numerization.rb
lib/coderay/encoders/html/css.rb
lib/coderay/encoders/html.rb
lib/coderay/encoders/lines_of_code.rb
lib/coderay/encoders/div.rb
lib/coderay/encoders/json.rb
lib/coderay/encoders/debug.rb

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.

Methods

Classes and Modules

Class CodeRay::Encoders::CommentFilter
Class CodeRay::Encoders::Count
Class CodeRay::Encoders::Debug
Class CodeRay::Encoders::Div
Class CodeRay::Encoders::Encoder
Class CodeRay::Encoders::Filter
Class CodeRay::Encoders::HTML
Class CodeRay::Encoders::JSON
Class CodeRay::Encoders::LinesOfCode
Class CodeRay::Encoders::Null
Class CodeRay::Encoders::Page
Class CodeRay::Encoders::Span
Class CodeRay::Encoders::Statistic
Class CodeRay::Encoders::Term
Class CodeRay::Encoders::Text
Class CodeRay::Encoders::TokenClassFilter
Class CodeRay::Encoders::XML
Class CodeRay::Encoders::YAML

Constants

TRANSPARENT_TOKEN_KINDS = [ :delimiter, :modifier, :content, :escape, :inline_delimiter, ].to_set

Public Class methods

Generate a hint about the given classes in a hint style.

hint may be :info, :info_long or :debug.

[Source]

     # 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

Public Instance methods

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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}/o
248:           text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |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) \
269:               that is not open. Open are: %p.' % [type, @opened[1..-1]]
270:           end
271:           @out << '</span>'
272:           @opened.pop
273:         end
274:       
275:       # whole lines to be highlighted, eg. a deleted line in a diff
276:       when :begin_line
277:         @opened[0] = type
278:         if style = @css_style[@opened]
279:           if style['class="']
280:             @out << style.sub('class="', 'class="line ')
281:           else
282:             @out << style.sub('>', ' class="line">')
283:           end
284:         else
285:           @out << '<span class="line">'
286:         end
287:         @opened << type
288:       when :end_line
289:         if @opened.empty?
290:           # nothing to close
291:         else
292:           if $CODERAY_DEBUG and (@opened.size == 1 or @opened.last != type)
293:             raise 'Malformed token stream: Trying to close a line (%p) \
294:               that is not open. Open are: %p.' % [type, @opened[1..-1]]
295:           end
296:           @out << '</span>'
297:           @opened.pop
298:         end
299:       
300:       else
301:         raise 'unknown token kind: %p' % [text]
302:         
303:       end
304:     end

[Validate]