Module CodeRay::Encoders
In: lib/coderay/encoder.rb
lib/coderay/encoders/_map.rb
lib/coderay/encoders/count.rb
lib/coderay/encoders/debug.rb
lib/coderay/encoders/div.rb
lib/coderay/encoders/html/css.rb
lib/coderay/encoders/html/numerization.rb
lib/coderay/encoders/html/output.rb
lib/coderay/encoders/html.rb
lib/coderay/encoders/null.rb
lib/coderay/encoders/page.rb
lib/coderay/encoders/span.rb
lib/coderay/encoders/statistic.rb
lib/coderay/encoders/text.rb
lib/coderay/encoders/tokens.rb
lib/coderay/encoders/xml.rb
lib/coderay/encoders/yaml.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::Count
Class CodeRay::Encoders::Debug
Class CodeRay::Encoders::Div
Class CodeRay::Encoders::Encoder
Class CodeRay::Encoders::HTML
Class CodeRay::Encoders::Null
Class CodeRay::Encoders::Page
Class CodeRay::Encoders::Span
Class CodeRay::Encoders::Statistic
Class CodeRay::Encoders::Text
Class CodeRay::Encoders::Tokens
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 129
129:     def self.token_path_to_hint hint, classes
130:       title =
131:         case hint
132:         when :info
133:           TOKEN_KIND_TO_INFO[classes.first]
134:         when :info_long
135:           classes.reverse.map { |kind| TOKEN_KIND_TO_INFO[kind] }.join('/')
136:         when :debug
137:           classes.inspect
138:         end
139:       title ? " title=\"#{title}\"" : ''
140:     end

Public Instance methods

[Source]

     # File lib/coderay/encoders/html.rb, line 208
208:     def finish options
209:       not_needed = @opened.shift
210:       @out << '</span>' * @opened.size
211:       unless @opened.empty?
212:         warn '%d tokens still open: %p' % [@opened.size, @opened]
213:       end
214: 
215:       @out.extend Output
216:       @out.css = @css
217:       @out.numerize! options[:line_numbers], options
218:       @out.wrap! options[:wrap]
219: 
220:       super
221:     end

[Source]

     # File lib/coderay/encoders/html.rb, line 142
142:     def setup options
143:       super
144: 
145:       @HTML_ESCAPE = HTML_ESCAPE.dup
146:       @HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
147: 
148:       @opened = [nil]
149:       @css = CSS.new options[:style]
150: 
151:       hint = options[:hint]
152:       if hint and not [:debug, :info, :info_long].include? hint
153:         raise ArgumentError, "Unknown value %p for :hint; \
154:           expected :info, :debug, false, or nil." % hint
155:       end
156: 
157:       case options[:css]
158: 
159:       when :class
160:         @css_style = Hash.new do |h, k|
161:           c = CodeRay::Tokens::ClassOfKind[k.first]
162:           if c == :NO_HIGHLIGHT and not hint
163:             h[k.dup] = false
164:           else
165:             title = if hint
166:               HTML.token_path_to_hint(hint, k[1..-1] << k.first)
167:             else
168:               ''
169:             end
170:             if c == :NO_HIGHLIGHT
171:               h[k.dup] = '<span%s>' % [title]
172:             else
173:               h[k.dup] = '<span%s class="%s">' % [title, c]
174:             end
175:           end
176:         end
177: 
178:       when :style
179:         @css_style = Hash.new do |h, k|
180:           if k.is_a? ::Array
181:             styles = k.dup
182:           else
183:             styles = [k]
184:           end
185:           type = styles.first
186:           classes = styles.map { |c| Tokens::ClassOfKind[c] }
187:           if classes.first == :NO_HIGHLIGHT and not hint
188:             h[k] = false
189:           else
190:             styles.shift if TRANSPARENT_TOKEN_KINDS.include? styles.first
191:             title = HTML.token_path_to_hint hint, styles
192:             style = @css[*classes]
193:             h[k] =
194:               if style
195:                 '<span%s style="%s">' % [title, style]
196:               else
197:                 false
198:               end
199:           end
200:         end
201: 
202:       else
203:         raise ArgumentError, "Unknown value %p for :css." % options[:css]
204: 
205:       end
206:     end

[Source]

     # File lib/coderay/encoders/html.rb, line 223
223:     def token text, type = :plain
224:       case text
225:       
226:       when nil
227:         # raise 'Token with nil as text was given: %p' % [[text, type]] 
228:       
229:       when String
230:         if text =~ /#{HTML_ESCAPE_PATTERN}/o
231:           text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
232:         end
233:         @opened[0] = type
234:         if text != "\n" && style = @css_style[@opened]
235:           @out << style << text << '</span>'
236:         else
237:           @out << text
238:         end
239:         
240:       
241:       # token groups, eg. strings
242:       when :open
243:         @opened[0] = type
244:         @out << (@css_style[@opened] || '<span>')
245:         @opened << type
246:       when :close
247:         if @opened.empty?
248:           # nothing to close
249:         else
250:           if $DEBUG and (@opened.size == 1 or @opened.last != type)
251:             raise 'Malformed token stream: Trying to close a token (%p) \
252:               that is not open. Open are: %p.' % [type, @opened[1..-1]]
253:           end
254:           @out << '</span>'
255:           @opened.pop
256:         end
257:       
258:       # whole lines to be highlighted, eg. a deleted line in a diff
259:       when :begin_line
260:         @opened[0] = type
261:         if style = @css_style[@opened]
262:           @out << style.sub('<span', '<div')
263:         else
264:           @out << '<div>'
265:         end
266:         @opened << type
267:       when :end_line
268:         if @opened.empty?
269:           # nothing to close
270:         else
271:           if $DEBUG and (@opened.size == 1 or @opened.last != type)
272:             raise 'Malformed token stream: Trying to close a line (%p) \
273:               that is not open. Open are: %p.' % [type, @opened[1..-1]]
274:           end
275:           @out << '</div>'
276:           @opened.pop
277:         end
278:       
279:       else
280:         raise 'unknown token kind: %p' % [text]
281:         
282:       end
283:     end

[Validate]