From 7371093d78689728c1bf8326987c24f73864edb2 Mon Sep 17 00:00:00 2001 From: murphy Date: Fri, 4 Nov 2005 00:16:58 +0000 Subject: HTML highlighting: html_css.rb: Bugfixes CSS style scanning went for wrong range moved some styles into the right place html_output.rb: numerization excluded templates renamed new LIST template html_numerization.rb (new): new :line_numbers style :list (beta) Benchmarking: Split Options into lines :list style for testing --- lib/coderay/encoders/helpers/html_css.rb | 13 ++- lib/coderay/encoders/helpers/html_numerization.rb | 112 ++++++++++++++++++++++ lib/coderay/encoders/helpers/html_output.rb | 86 ++--------------- 3 files changed, 126 insertions(+), 85 deletions(-) create mode 100644 lib/coderay/encoders/helpers/html_numerization.rb (limited to 'lib/coderay/encoders/helpers') diff --git a/lib/coderay/encoders/helpers/html_css.rb b/lib/coderay/encoders/helpers/html_css.rb index 6629bbf..80ccab4 100644 --- a/lib/coderay/encoders/helpers/html_css.rb +++ b/lib/coderay/encoders/helpers/html_css.rb @@ -12,7 +12,7 @@ module CodeRay module Encoders cl = @classes[styles.first] return '' unless cl style = false - 1.upto(cl.size + 1) do |offset| + 1.upto(styles.size) do |offset| break if style = cl[styles[offset .. -1]] end return style @@ -46,16 +46,14 @@ module CodeRay module Encoders border: 1px solid silver; font-family: 'Courier New', 'Terminal', monospace; color: black; - width: 100%; - padding: 2px; } .CodeRay pre { margin: 0px; } div.CodeRay { } -span.CodeRay { white-space: pre; border: 0; } +span.CodeRay { white-space: pre; border: 0px; padding: 2px; } -table.CodeRay { border-collapse: collapse; } +table.CodeRay { border-collapse: collapse; width: 100%; padding: 2px; } table.CodeRay td { padding: 2px 4px; vertical-align: top; } .CodeRay .line_numbers, .CodeRay .no { @@ -67,8 +65,9 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; } .CodeRay .no { padding: 0px 4px; } .CodeRay .code { width: 100%; } -.CodeRay .code { -} +ol.CodeRay { font-size: 10pt; } +ol.CodeRay li { white-space: pre; } + .CodeRay .code pre { overflow: auto; } MAIN diff --git a/lib/coderay/encoders/helpers/html_numerization.rb b/lib/coderay/encoders/helpers/html_numerization.rb new file mode 100644 index 0000000..f51b5d3 --- /dev/null +++ b/lib/coderay/encoders/helpers/html_numerization.rb @@ -0,0 +1,112 @@ +module CodeRay + module Encoders + + class HTML + + module Output + + def numerize *args + clone.numerize!(*args) + end + + NUMERIZABLE_WRAPPINGS = { + :table => [:div, :page], + :inline => :all, + :list => [:div, :page], + nil => :all + } + + def numerize! mode = :table, options = {} + return self unless mode + + options = DEFAULT_OPTIONS.merge options + + start = options[:line_number_start] + unless start.is_a? Integer + raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start + end + + 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 + + bold_every = options[:bold_every] + bolding = + if bold_every == :no_bolding or bold_every == 0 + proc { |line| line.to_s } + elsif bold_every.is_a? Integer + proc do |line| + if line % bold_every == 0 + "#{line}" # every bold_every-th number in bold + else + line.to_s + end + end + else + raise ArgumentError, "Invalid value %p for :bolding; :no_bolding or Integer expected." % bolding + end + + line_count = count("\n") + line_count += 1 if self[-1] != ?\n + + case mode + when :inline + max_width = (start + line_count).to_s.size + line = start + gsub!(/^/) do + line_number = bolding.call line + line += 1 + "#{ line_number.rjust(max_width) } " + end + #wrap! :div + + when :table + # This is really ugly. + # 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 + # FIXME Firefox struggles with very long codes (> 200 lines) + 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" } + + line_numbers_table_tpl = TABLE.apply('LINE_NUMBERS', line_numbers) + gsub!(/\n/) { "\n" } + wrap_in! line_numbers_table_tpl + @wrapped_in = :div + + when :list + opened_tags = [] + gsub!(/^.*$\n?/) do |line| + line.chomp! + + open = opened_tags.join + line.scan(%r!<(/)?span[^>]*>?!) do |close,| + if close + opened_tags.pop + else + opened_tags << $& + end + end + close = '' * opened_tags.size + + "
  • #{open}#{line}#{close}
  • " + end + wrap_in! LIST + @wrapped_in = :div + + else + raise ArgumentError, "Unknown value %p for mode: expected one of %p" % + [mode, NUMERIZABLE_WRAPPINGS.keys - [:all]] + end + + self + end + + end + + end + +end +end diff --git a/lib/coderay/encoders/helpers/html_output.rb b/lib/coderay/encoders/helpers/html_output.rb index c6012f7..f17965d 100644 --- a/lib/coderay/encoders/helpers/html_output.rb +++ b/lib/coderay/encoders/helpers/html_output.rb @@ -13,6 +13,8 @@ module CodeRay # TODO: more doc. module Output + require 'coderay/encoders/helpers/html_numerization.rb' + attr_accessor :wrapped_in class << self @@ -104,82 +106,6 @@ module CodeRay clone.wrap!(*args) end - NUMERIZABLE_WRAPPINGS = { - :table => [:div, :page], - :inline => :all, - nil => :all - } - - def numerize! mode = :table, options = {} - return self unless mode - - options = DEFAULT_OPTIONS.merge options - - start = options[:line_number_start] - unless start.is_a? Integer - raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start - end - - 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 - - bold_every = options[:bold_every] - bolding = - if bold_every == :no_bolding or bold_every == 0 - proc { |line| line.to_s } - elsif bold_every.is_a? Integer - proc do |line| - if line % bold_every == 0 - "#{line}" # every bold_every-th number in bold - else - line.to_s - end - end - else - raise ArgumentError, "Invalid value %p for :bolding; :no_bolding or Integer expected." % bolding - end - - line_count = count("\n") - line_count += 1 if self[-1] != ?\n - - case mode - when :inline - max_width = (start + line_count).to_s.size - line = start - gsub!(/^/) do - line_number = bolding.call line - line += 1 - "#{ line_number.rjust(max_width) } " - end - #wrap! :div - - when :table - # This is really ugly. - # 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 = (start ... start + line_count).to_a.map(&bolding).join("\n") - line_numbers << "\n" # also for Mr. MS Internet Exploder :-/ - line_numbers.gsub!(/\n/) { "\n" } - - line_numbers_tpl = DIV_TABLE.apply('LINE_NUMBERS', line_numbers) - gsub!(/\n/) { "\n" } - wrap_in! line_numbers_tpl - @wrapped_in = :div - - else - raise ArgumentError, "Unknown value %p for mode: :inline or :table expected" % mode - end - - self - end - - def numerize *args - clone.numerize!(*args) - end - def stylesheet in_tag = false Output.stylesheet in_tag end @@ -224,14 +150,18 @@ module CodeRay DIV - DIV_TABLE = <<-`DIV_TABLE` + TABLE = <<-`TABLE`
    <%LINE_NUMBERS%>
    <%CONTENT%>
    - DIV_TABLE + TABLE # title="double click to expand" + LIST = <<-`LIST` +
      <%CONTENT%>
    + LIST + PAGE = <<-`PAGE` -- cgit v1.2.1