diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb
index 635a4d89..d2b73393 100644
--- a/lib/coderay/encoders/html.rb
+++ b/lib/coderay/encoders/html.rb
@@ -2,7 +2,7 @@
module CodeRay
module Encoders
-
+
# = HTML Encoder
#
# This is CodeRay's most important highlighter:
@@ -15,7 +15,7 @@ module Encoders
# puts CodeRay.scan('Some /code/', :ruby).html(:wrap => :span)
# #-> Some /code/
# puts CodeRay.scan('Some /code/', :ruby).span #-> the same
- #
+ #
# puts CodeRay.scan('Some code', :ruby).html(
# :wrap => nil,
# :line_numbers => :inline,
@@ -26,7 +26,7 @@ module Encoders
#
# === :tab_width
# Convert \t characters to +n+ spaces (a number.)
- #
+ #
# Default: 8
#
# === :css
@@ -42,13 +42,13 @@ module Encoders
# Default: nil
#
# === :title
- #
+ #
# The title of the HTML page (works only when :wrap is set to :page.)
#
# Default: 'CodeRay output'
#
# === :break_lines
- #
+ #
# Split multiline blocks at line breaks.
# Forced to true if :line_numbers option is set to :inline.
#
@@ -78,10 +78,10 @@ module Encoders
# Default: 10
#
# === :highlight_lines
- #
+ #
# Highlights certain line numbers.
# Can be any Enumerable, typically just an Array or Range, of numbers.
- #
+ #
# Bolding is deactivated when :highlight_lines is set. It only makes sense
# in combination with :line_numbers.
#
@@ -94,45 +94,45 @@ module Encoders
#
# Default: false
class HTML < Encoder
-
+
register_for :html
-
+
FILE_EXTENSION = 'snippet.html'
-
+
DEFAULT_OPTIONS = {
:tab_width => 8,
-
+
:css => :class,
:style => :alpha,
:wrap => nil,
:title => 'CodeRay output',
-
+
:break_lines => false,
-
+
:line_numbers => nil,
:line_number_anchors => 'n',
:line_number_start => 1,
:bold_every => 10,
:highlight_lines => nil,
-
+
:hint => false,
}
-
+
autoload :Output, CodeRay.coderay_path('encoders', 'html', 'output')
autoload :CSS, CodeRay.coderay_path('encoders', 'html', 'css')
autoload :Numbering, CodeRay.coderay_path('encoders', 'html', 'numbering')
-
+
attr_reader :css
-
+
protected
-
+
HTML_ESCAPE = { #:nodoc:
'&' => '&',
'"' => '"',
'>' => '>',
'<' => '<',
}
-
+
# This was to prevent illegal HTML.
# Strange chars should still be avoided in codes.
evil_chars = Array(0x00...0x20) - [?\n, ?\t, ?\s]
@@ -142,15 +142,15 @@ class HTML < Encoder
# \x9 (\t) and \xA (\n) not included
#HTML_ESCAPE_PATTERN = /[\t&"><\0-\x8\xB-\x1f\x7f-\xff]/
HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1f]/
-
+
TOKEN_KIND_TO_INFO = Hash.new do |h, kind|
h[kind] = kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
end
-
+
TRANSPARENT_TOKEN_KINDS = Set[
:delimiter, :modifier, :content, :escape, :inline_delimiter,
]
-
+
# Generate a hint about the given +kinds+ in a +hint+ style.
#
# +hint+ may be :info, :info_long or :debug.
@@ -168,32 +168,32 @@ def self.token_path_to_hint hint, kinds
end
title ? " title=\"#{title}\"" : ''
end
-
+
def setup options
super
-
+
if options[:wrap] || options[:line_numbers]
@real_out = @out
@out = ''
end
-
+
options[:break_lines] = true if options[:line_numbers] == :inline
-
+
@break_lines = (options[:break_lines] == true)
-
+
@HTML_ESCAPE = HTML_ESCAPE.dup
@HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
-
+
@opened = []
@last_opened = nil
@css = CSS.new options[:style]
-
+
hint = options[:hint]
if hint && ![:debug, :info, :info_long].include?(hint)
raise ArgumentError, "Unknown value %p for :hint; \
expected :info, :info_long, :debug, false, or nil." % hint
end
-
+
css_classes = TokenKinds
case options[:css]
when :class
@@ -225,17 +225,17 @@ def setup options
else
raise ArgumentError, "Unknown value %p for :css." % options[:css]
end
-
+
@set_last_opened = options[:hint] || options[:css] == :style
end
-
+
def finish options
unless @opened.empty?
warn '%d tokens still open: %p' % [@opened.size, @opened] if $CODERAY_DEBUG
@out << '' while @opened.pop
@last_opened = nil
end
-
+
@out.extend Output
@out.css = @css
if options[:line_numbers]
@@ -243,24 +243,24 @@ def finish options
end
@out.wrap! options[:wrap]
@out.apply_title! options[:title]
-
+
if defined?(@real_out) && @real_out
@real_out << @out
@out = @real_out
end
-
+
super
end
-
+
public
-
+
def text_token text, kind
if text =~ /#{HTML_ESCAPE_PATTERN}/o
text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
end
-
+
style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
-
+
if @break_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0
close = '' * c
reopen = ''
@@ -269,21 +269,21 @@ def text_token text, kind
end
text[i .. -1] = text[i .. -1].gsub("\n", "#{close}\n#{reopen}#{style}")
end
-
+
if style
@out << style << text << ''
else
@out << text
end
end
-
+
# token groups, eg. strings
def begin_group kind
@out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '')
@opened << kind
@last_opened = kind if @set_last_opened
end
-
+
def end_group kind
if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind)
warn 'Malformed token stream: Trying to close a token (%p) ' \
@@ -294,7 +294,7 @@ def end_group kind
@last_opened = @opened.last if @last_opened
end
end
-
+
# whole lines to be highlighted, eg. a deleted line in a diff
def begin_line kind
if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
@@ -309,7 +309,7 @@ def begin_line kind
@opened << kind
@last_opened = kind if @options[:css] == :style
end
-
+
def end_line kind
if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind)
warn 'Malformed token stream: Trying to close a line (%p) ' \
@@ -320,8 +320,8 @@ def end_line kind
@last_opened = @opened.last if @last_opened
end
end
-
+
end
-
+
end
end
diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb
index 8bc6259f..858c5599 100644
--- a/lib/coderay/encoders/html/numbering.rb
+++ b/lib/coderay/encoders/html/numbering.rb
@@ -14,7 +14,7 @@ def self.number! output, mode = :table, options = {}
unless start.is_a? Integer
raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
end
-
+
anchor_prefix = options[:line_number_anchors]
anchor_prefix = 'line' if anchor_prefix == true
anchor_prefix = anchor_prefix.to_s[/\w+/] if anchor_prefix
@@ -28,7 +28,7 @@ def self.number! output, mode = :table, options = {}
else
proc { |line| line.to_s } # :to_s.to_proc in Ruby 1.8.7+
end
-
+
bold_every = options[:bold_every]
highlight_lines = options[:highlight_lines]
bolding =
@@ -55,7 +55,7 @@ def self.number! output, mode = :table, options = {}
else
raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
end
-
+
line_count = output.count("\n")
position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n)
if position_of_last_newline
@@ -63,7 +63,7 @@ def self.number! output, mode = :table, options = {}
ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/]
line_count += 1 if not ends_with_newline
end
-
+
case mode
when :inline
max_width = (start + line_count).to_s.size
@@ -72,7 +72,7 @@ def self.number! output, mode = :table, options = {}
line_number_text = bolding.call line_number
indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x)
line_number += 1
- "#{indent}#{line_number_text}#{line}"
+ "#{indent}#{line_number_text}#{line}"
end
when :table