summaryrefslogtreecommitdiff
path: root/lib/coderay/encoders/html.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/coderay/encoders/html.rb')
-rw-r--r--lib/coderay/encoders/html.rb75
1 files changed, 61 insertions, 14 deletions
diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb
index 15120a2..5da4e59 100644
--- a/lib/coderay/encoders/html.rb
+++ b/lib/coderay/encoders/html.rb
@@ -1,6 +1,43 @@
module CodeRay
module Encoders
+ # = HTML Encoder
+ #
+ # This is CodeRay's most important highlighter:
+ # It provides save, fast XHTML generation and CSS support.
+ #
+ # == Usage
+ #
+ # 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).span #-> the same
+ #
+ # puts CodeRay.scan('Some code', :ruby).html(
+ # :wrap => nil,
+ # :line_numbers => :inline,
+ # :css => :style
+ # )
+ # #-> <span class="no">1</span> <span style="color:#036; font-weight:bold;">Some</span> code
+ #
+ # == Options
+ #
+ # :tab_width:: Convert \t characters to +n+ spaces (a number.)
+ # Default: 8
+ # :css:: How to include the styles; can be :class or :style.
+ # Default: :class
+ # :wrap:: Wrap in :page, :div, :span or nil.
+ # Default: :page
+ # You can also use Encoders::Div and Encoders::Span.
+ # :line_numbers:: Include line numbers in :table, :inline or nil (no line numbers)
+ # Default: nil
+ # :line_number_start: Where to start with line number counting.
+ # Default: 1
+ # :bold_every:: Make every +n+-th number appear bold.
+ # Default: 10
+ # :hint:: Include some information into the output using the title attribute.
+ # Can be :info (show token type on mouse-over) or :debug.
+ # Default: false
class HTML < Encoder
include Streamable
@@ -19,8 +56,9 @@ module Encoders
:line_numbers => nil,
:line_number_start => 1,
:bold_every => 10,
+
+ :hint => false,
}
- NUMERIZABLE_WRAPPINGS = [:div, :page]
require 'coderay/encoders/helpers/html_helper'
require 'coderay/encoders/helpers/html_output'
@@ -50,9 +88,6 @@ module Encoders
HTML_ESCAPE_PATTERN = /[&"><\0-\x8\xB-\x1f\x7f-\xff]/
def setup options
- if options[:line_numbers] and not NUMERIZABLE_WRAPPINGS.include? options[:wrap]
- warn ':line_numbers wanted, but :wrap is %p' % options[:wrap]
- end
super
return if options == @last_options
@last_options = options
@@ -63,6 +98,9 @@ module Encoders
@opened = [nil]
@css = CSS.new
+ hint = options[:hint]
+ raise ArgumentError if hint and not [:debug, :info].include? hint
+
case options[:css]
when :class
@@ -73,15 +111,19 @@ module Encoders
type = k
end
c = ClassOfKind[type]
- if c == :NO_HIGHLIGHT
+ if c == :NO_HIGHLIGHT and not hint
h[k] = false
else
- if options[:debug]
- debug_info = ' title="%p"' % [ k ]
+ title = if hint
+ if hint == :debug
+ ' title="%p"' % [ k ]
+ elsif hint == :info and k.size == 1
+ " title=\"#{type.to_s.gsub(/_/, " ").capitalize}\""
+ end
else
- debug_info = ''
+ ''
end
- h[k] = '<span%s class="%s">' % [debug_info, c]
+ h[k] = '<span%s class="%s">' % [title, c]
end
end
@@ -92,19 +134,24 @@ module Encoders
else
styles = [k]
end
+ type = styles.first
styles.map! { |c| ClassOfKind[c] }
- if styles.first == :NO_HIGHLIGHT
+ if styles.first == :NO_HIGHLIGHT and not hint
h[k] = false
else
- if options[:debug]
- debug_info = ' title="%s"' % [ styles.inspect.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } ]
+ title = if hint
+ if hint == :debug
+ ' title="%p"' % [ styles ]
+ elsif hint == :info and styles.size == 1
+ " title=\"#{type.to_s.gsub(/_/, " ").capitalize}\""
+ end
else
- debug_info = ''
+ ''
end
style = @css[*styles]
h[k] =
if style
- '<span%s style="%s">' % [debug_info, style]
+ '<span%s style="%s">' % [title, style]
else
false
end