summaryrefslogtreecommitdiff
path: root/lib/coderay
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2005-10-29 04:55:15 +0000
committermurphy <murphy@rubychan.de>2005-10-29 04:55:15 +0000
commitfb64737038ca13b4047219c429560b97b98fe22b (patch)
treee1b180bc151643d1a782623fbe94c0c38814b1d1 /lib/coderay
parent840e80d149a2c5651b71f87c6627e475d5401e04 (diff)
downloadcoderay-fb64737038ca13b4047219c429560b97b98fe22b.tar.gz
encoder.rb: Added Encoder#file_extension
plugin.rb: #load_plugin --> #load; no more debug messages encoders/html.rb: Documentation; hint system; moved NUMERIZABLE_WRAPPINGS to html_output.rb encoders/helpers/html_output.rb: made stylesheet a method; disabled code cell hint; fixed inline numerizing encoders/debug.rb: changed extension to 'raydebug' etc folder added included etc/raydebug.vim for cool vim highlighting
Diffstat (limited to 'lib/coderay')
-rw-r--r--lib/coderay/encoder.rb5
-rw-r--r--lib/coderay/encoders/debug.rb2
-rw-r--r--lib/coderay/encoders/helpers/html_output.rb20
-rw-r--r--lib/coderay/encoders/html.rb75
-rw-r--r--lib/coderay/helpers/plugin.rb6
5 files changed, 86 insertions, 22 deletions
diff --git a/lib/coderay/encoder.rb b/lib/coderay/encoder.rb
index 448a45b..362cfb4 100644
--- a/lib/coderay/encoder.rb
+++ b/lib/coderay/encoder.rb
@@ -109,6 +109,11 @@ module CodeRay
method(:token).to_proc
end
+ # Return the default file extension for outputs of this encoder.
+ def file_extension
+ self.class::FILE_EXTENSION
+ end
+
protected
# Called with merged options before encoding starts.
diff --git a/lib/coderay/encoders/debug.rb b/lib/coderay/encoders/debug.rb
index b084733..7358e8b 100644
--- a/lib/coderay/encoders/debug.rb
+++ b/lib/coderay/encoders/debug.rb
@@ -7,7 +7,7 @@ module CodeRay
include Streamable
register_for :debug
- FILE_EXTENSION = 'debug'
+ FILE_EXTENSION = 'raydebug'
protected
def text_token text, kind
diff --git a/lib/coderay/encoders/helpers/html_output.rb b/lib/coderay/encoders/helpers/html_output.rb
index 849fe1a..2ff5c19 100644
--- a/lib/coderay/encoders/helpers/html_output.rb
+++ b/lib/coderay/encoders/helpers/html_output.rb
@@ -35,8 +35,12 @@ module CodeRay
warn "The Output module is intended to extend instances of String, not #{o.class}." unless o.respond_to? :to_str
end
+ def stylesheet
+ CSS::DEFAULT_STYLESHEET
+ end
+
def page_template_for_css css = :default
- css = CSS::DEFAULT_STYLESHEET if css == :default
+ css = stylesheet if css == :default
PAGE.apply 'CSS', css
end
@@ -94,6 +98,12 @@ module CodeRay
clone.wrap!(*args)
end
+ NUMERIZABLE_WRAPPINGS = {
+ :table => [:div, :page],
+ :inline => :all,
+ nil => :all
+ }
+
def numerize! mode = :table, options = {}
return self unless mode
@@ -104,7 +114,8 @@ module CodeRay
raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
end
- unless NUMERIZABLE_WRAPPINGS.include? options[:wrap]
+ allowed_wrappings = NUMERIZABLE_WRAPPINGS[mode]
+ unless allowed_wrappings == :all or allowed_wrappings.include? options[:wrap]
raise ArgumentError, "Can't numerize, :wrap must be in %p, but is %p" % [NUMERIZABLE_WRAPPINGS, options[:wrap]]
end
@@ -136,7 +147,7 @@ module CodeRay
line += 1
"<span class=\"no\">#{ line_number.rjust(max_width) }</span> "
end
- wrap! :div
+ #wrap! :div
when :table
# This is really ugly.
@@ -206,9 +217,10 @@ module CodeRay
DIV_TABLE = <<-`DIV_TABLE`
<table class="CodeRay"> <tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><%LINE_NUMBERS%></pre></td>
- <td class="code"><pre title="double click to expand" ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><%CONTENT%></pre></td>
+ <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><%CONTENT%></pre></td>
</tr> </table>
DIV_TABLE
+ # title="double click to expand"
PAGE = <<-`PAGE`
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
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
diff --git a/lib/coderay/helpers/plugin.rb b/lib/coderay/helpers/plugin.rb
index 2518e5a..5c90901 100644
--- a/lib/coderay/helpers/plugin.rb
+++ b/lib/coderay/helpers/plugin.rb
@@ -90,7 +90,7 @@ module PluginHost
id = validate_id(plugin_id)
path = path_to id
begin
- $stderr.puts 'Loading plugin: ' + path if $DEBUG
+ #$stderr.puts 'Loading plugin: ' + path if $DEBUG
require path
rescue LoadError => boom
raise PluginNotFound, 'Could not load plugin %p: %s' % [id, boom]
@@ -151,7 +151,7 @@ module PluginHost
end
# Alias for +[]+.
- alias load_plugin []
+ alias load []
# Returns the Plugin for +id+.
# Use it like Hash#fetch.
@@ -239,7 +239,7 @@ def require_plugin path
host = PluginHost.host_by_id(host_id)
raise PluginHost::HostNotFound,
"No host for #{host_id.inspect} found." unless host
- host.load_plugin plugin_id
+ host.load plugin_id
end