From 3d8868dd0b9898d589ecdff5151ed5d47956f937 Mon Sep 17 00:00:00 2001 From: murphy Date: Sat, 1 Oct 2005 06:04:52 +0000 Subject: Demos updated, rewritten, enhanced, tested. Some code cleanups. Bugs fixed, scanner and encoder improved: count.rb: marked Streamable html_css.rb: style for inline numbers html.rb: changed options; :line_numbers_offset is now :line_number_start html_output.rb: offset for inline numbers fixed html.rb: token text no longer changed by gsub! while highlighting (this is even faster!) text.rb, plugin.rb: reindented ruby.rb: eleminated multiple assignments for speed tokens.rb: reindented, Tokens#to_s added, #<< returns self Plugin system: bugs fixed, error messages improved. --- lib/coderay/encoders/count.rb | 1 + lib/coderay/encoders/helpers/html_css.rb | 6 ++++- lib/coderay/encoders/helpers/html_output.rb | 23 +++++++++------- lib/coderay/encoders/html.rb | 7 ++--- lib/coderay/encoders/text.rb | 42 ++++++++++++++--------------- 5 files changed, 44 insertions(+), 35 deletions(-) (limited to 'lib/coderay/encoders') diff --git a/lib/coderay/encoders/count.rb b/lib/coderay/encoders/count.rb index 80aec57..d49e0bc 100644 --- a/lib/coderay/encoders/count.rb +++ b/lib/coderay/encoders/count.rb @@ -3,6 +3,7 @@ module Encoders class Count < Encoder + include Streamable register_for :count protected diff --git a/lib/coderay/encoders/helpers/html_css.rb b/lib/coderay/encoders/helpers/html_css.rb index 5c39527..a3c50eb 100644 --- a/lib/coderay/encoders/helpers/html_css.rb +++ b/lib/coderay/encoders/helpers/html_css.rb @@ -46,6 +46,8 @@ module CodeRay module Encoders border: 1px solid silver; font-family: 'Courier New', 'Terminal', monospace; color: black; + width: 100%; + padding: 2px; } .CodeRay pre { margin: 0px; } @@ -56,12 +58,14 @@ span.CodeRay { white-space: pre; border: 0; } table.CodeRay { border-collapse: collapse; } table.CodeRay td { padding: 2px 4px; vertical-align: top; } -.CodeRay .line_numbers { +.CodeRay .line_numbers, .CodeRay .no { background-color: #def; color: gray; text-align: right; } .CodeRay .line_numbers tt { font-weight: bold; } +.CodeRay .no { padding: 0px 4px; } +.CodeRay .code { width: 100%; } .CodeRay .code { } diff --git a/lib/coderay/encoders/helpers/html_output.rb b/lib/coderay/encoders/helpers/html_output.rb index c873896..f6a2b11 100644 --- a/lib/coderay/encoders/helpers/html_output.rb +++ b/lib/coderay/encoders/helpers/html_output.rb @@ -100,9 +100,9 @@ module CodeRay def numerize! mode = :table, options = {} return self unless mode - offset = options.fetch :line_numbers_offset, DEFAULT_OPTIONS[:line_numbers_offset] - unless offset.is_a? Integer - raise ArgumentError, "Invalid value %p for :offset; Integer expected." % offset + start = options.fetch :line_number_start, DEFAULT_OPTIONS[:line_number_start] + unless start.is_a? Integer + raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start end unless NUMERIZABLE_WRAPPINGS.include? options[:wrap] @@ -130,11 +130,11 @@ module CodeRay case mode when :inline - max_width = line_count.to_s.size - line = offset - 1 + max_width = (start + line_count).to_s.size + line = start gsub!(/^/) do - line += 1 line_number = bolding.call line + line += 1 "#{ line_number.rjust(max_width) } " end wrap! :div @@ -144,7 +144,7 @@ module CodeRay # Because even monospace fonts seem to have different heights when bold, # I make the newline bold, both in the code and the line numbers. # FIXME Still not working perfect for Mr. Internet Exploder - line_numbers = (offset ... offset + line_count).to_a.map(&bolding).join("\n") + line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n") line_numbers << "\n" # also for Mr. MS Internet Exploder :-/ line_numbers.gsub!(/\n/) { "\n" } @@ -196,19 +196,22 @@ module CodeRay #-- don't include the templates in docu - SPAN = `<%CONTENT%>` + SPAN = `<%CONTENT%>` - DIV, DIV_TABLE, PAGE = - <<-`DIV`, <<-`DIV_TABLE`, <<-`PAGE` + DIV = <<-`DIV`
<%CONTENT%>
DIV + + DIV_TABLE = <<-`DIV_TABLE`
<%LINE_NUMBERS%>
<%CONTENT%>
DIV_TABLE + + PAGE = <<-`PAGE` diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 58fdd86..15120a2 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -17,7 +17,7 @@ module Encoders :wrap => :page, :line_numbers => nil, - :line_numbers_offset => 1, + :line_number_start => 1, :bold_every => 10, } NUMERIZABLE_WRAPPINGS = [:div, :page] @@ -133,8 +133,9 @@ module Encoders def token text, type if text.is_a? String - # be careful when streaming: text is changed! - text.gsub!(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } + if text =~ /#{HTML_ESCAPE_PATTERN}/o + text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } + end @opened[0] = type style = @css_style[@opened] if style diff --git a/lib/coderay/encoders/text.rb b/lib/coderay/encoders/text.rb index 4f0a754..02f76cb 100644 --- a/lib/coderay/encoders/text.rb +++ b/lib/coderay/encoders/text.rb @@ -1,33 +1,33 @@ module CodeRay - module Encoders +module Encoders - class Text < Encoder + class Text < Encoder - include Streamable - register_for :text + include Streamable + register_for :text - FILE_EXTENSION = 'txt' + FILE_EXTENSION = 'txt' - DEFAULT_OPTIONS = { - :separator => '' - } + DEFAULT_OPTIONS = { + :separator => '' + } - protected - def setup options - super - @sep = options[:separator] - end - - def token text, kind - return unless text.respond_to :to_str - @out << text + @sep - end + protected + def setup options + super + @sep = options[:separator] + end - def finish options - @out.chomp @sep - end + def token text, kind + return unless text.respond_to? :to_str + @out << text + @sep + end + def finish options + @out.chomp @sep end end + +end end -- cgit v1.2.1