Title / Description
Code Index: lib/coderay/encoders/html.rb =================================================================== --- lib/coderay/encoders/html.rb (revision 200) +++ lib/coderay/encoders/html.rb (revision 250) @@ -1,3 +1,5 @@ +require "set" + module CodeRay module Encoders @@ -10,7 +12,8 @@ # # require 'coderay' # puts CodeRay.scan('Some /code/', :ruby).html #-> a HTML page - # puts CodeRay.scan('Some /code/', :ruby).html(:wrap => :span) #-> <span class="CodeRay"><span class="co">Some</span> /code/</span> + # puts CodeRay.scan('Some /code/', :ruby).html(:wrap => :span) + # #-> <span class="CodeRay"><span class="co">Some</span> /code/</span> # puts CodeRay.scan('Some /code/', :ruby).span #-> the same # # puts CodeRay.scan('Some code', :ruby).html( @@ -55,7 +58,8 @@ # # === :hint # Include some information into the output using the title attribute. - # Can be :info (show token type on mouse-over), :info_long (with full path) or :debug (via inspect). + # Can be :info (show token type on mouse-over), :info_long (with full path) + # or :debug (via inspect). # # Default: false class HTML < Encoder @@ -82,7 +86,7 @@ :hint => false, } - helper :classes, :output, :css + helper :output, :css attr_reader :css @@ -115,11 +119,14 @@ end } + 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. def self.token_path_to_hint hint, classes - return '' unless hint title = case hint when :info @@ -129,7 +136,7 @@ when :debug classes.inspect end - " title=\"#{title}\"" + title ? " title=\"#{title}\"" : '' end def setup options @@ -143,42 +150,45 @@ hint = options[:hint] if hint and not [:debug, :info, :info_long].include? hint - raise ArgumentError, "Unknown value %p for :hint; expected :info, :debug, false or nil." % hint + raise ArgumentError, "Unknown value %p for :hint; \ + expected :info, :debug, false, or nil." % hint end case options[:css] when :class @css_style = Hash.new do |h, k| - if k.is_a? Array - type = k.first - else - type = k - end - c = ClassOfKind[type] + c = CodeRay::Tokens::ClassOfKind[k.first] if c == :NO_HIGHLIGHT and not hint - h[k] = false + h[k.dup] = false else - title = HTML.token_path_to_hint hint, (k[1..-1] << k.first) - h[k] = '<span%s class="%s">' % [title, c] + title = if hint + HTML.token_path_to_hint(hint, k[1..-1] << k.first) + else + '' + end + if c == :NO_HIGHLIGHT + h[k.dup] = '<span%s>' % [title] + else + h[k.dup] = '<span%s class="%s">' % [title, c] + end end end when :style @css_style = Hash.new do |h, k| - if k.is_a? Array + if k.is_a? ::Array styles = k.dup else styles = [k] end type = styles.first - classes = styles.map { |c| ClassOfKind[c] } + classes = styles.map { |c| Tokens::ClassOfKind[c] } if classes.first == :NO_HIGHLIGHT and not hint h[k] = false else - styles.shift if [:delimiter, :modifier, :content, :escape].include? styles.first + styles.shift if TRANSPARENT_TOKEN_KINDS.include? styles.first title = HTML.token_path_to_hint hint, styles - classes.delete 'il' style = @css[*classes] h[k] = if style @@ -198,7 +208,9 @@ def finish options not_needed = @opened.shift @out << '</span>' * @opened.size - warn '%d tokens still open: %p' % [@opened.size, @opened] unless @opened.empty? + unless @opened.empty? + warn '%d tokens still open: %p' % [@opened.size, @opened] + end @out.extend Output @out.css = @css @@ -229,8 +241,9 @@ if @opened.empty? # nothing to close else - if @opened.size == 1 or @opened.last != type - raise 'Malformed token stream: Trying to close a token (%p) that is not open. Open are: %p.' % [type, @opened[1..-1]] if $DEBUG + if $DEBUG and (@opened.size == 1 or @opened.last != type) + raise 'Malformed token stream: Trying to close a token (%p) \ + that is not open. Open are: %p.' % [type, @opened[1..-1]] end @out << '</span>' @opened.pop
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code