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 | = | [ :delimiter, :modifier, :content, :escape, :inline_delimiter, ].to_set |
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 223 223: def finish options 224: @opened.shift 225: @out << '</span>' * @opened.size 226: unless @opened.empty? 227: warn '%d tokens still open: %p' % [@opened.size, @opened] 228: end 229: 230: @out.extend Output 231: @out.css = @css 232: @out.numerize! options[:line_numbers], options 233: @out.wrap! options[:wrap] 234: @out.apply_title! options[:title] 235: 236: super 237: 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: classes = styles.map { |c| Tokens::ClassOfKind[c] } 202: if classes.first == :NO_HIGHLIGHT and not hint 203: h[k] = false 204: else 205: styles.shift if TRANSPARENT_TOKEN_KINDS.include? styles.first 206: title = HTML.token_path_to_hint hint, styles 207: style = @css[*classes] 208: h[k] = 209: if style 210: '<span%s style="%s">' % [title, style] 211: else 212: false 213: end 214: end 215: end 216: 217: else 218: raise ArgumentError, "Unknown value %p for :css." % options[:css] 219: 220: end 221: end
# File lib/coderay/encoders/html.rb, line 239 239: def token text, type = :plain 240: case text 241: 242: when nil 243: # raise 'Token with nil as text was given: %p' % [[text, type]] 244: 245: when String 246: if text =~ /#{HTML_ESCAPE_PATTERN}/o 247: text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } 248: end 249: @opened[0] = type 250: if text != "\n" && style = @css_style[@opened] 251: @out << style << text << '</span>' 252: else 253: @out << text 254: end 255: 256: 257: # token groups, eg. strings 258: when :open 259: @opened[0] = type 260: @out << (@css_style[@opened] || '<span>') 261: @opened << type 262: when :close 263: if @opened.empty? 264: # nothing to close 265: else 266: if $CODERAY_DEBUG and (@opened.size == 1 or @opened.last != type) 267: raise 'Malformed token stream: Trying to close a token (%p) \ 268: 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: if style['class="'] 279: @out << style.sub('class="', 'class="line ') 280: else 281: @out << style.sub('>', ' class="line">') 282: end 283: else 284: @out << '<span class="line">' 285: end 286: @opened << type 287: when :end_line 288: if @opened.empty? 289: # nothing to close 290: else 291: if $CODERAY_DEBUG and (@opened.size == 1 or @opened.last != type) 292: raise 'Malformed token stream: Trying to close a line (%p) \ 293: that is not open. Open are: %p.' % [type, @opened[1..-1]] 294: end 295: @out << '</span>' 296: @opened.pop 297: end 298: 299: else 300: raise 'unknown token kind: %p' % [text] 301: 302: end 303: end