From 720aa80f827583e60628bcdea7b3e53d529686b6 Mon Sep 17 00:00:00 2001 From: TJ Koblentz Date: Sun, 8 Jul 2012 18:03:30 -0700 Subject: [PATCH 1/4] quick hack to force some html output that faji wants --- lib/coderay/encoders/html.rb | 98 +++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 635a4d89..8ba01eea 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,33 @@ def self.token_path_to_hint hint, kinds end title ? " title=\"#{title}\"" : '' end - + def setup options super - + + @line_number = 1 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 +226,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 +244,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 +270,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,9 +295,12 @@ 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 + @out << '
' if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] if style['class="'] @out << style.sub('class="', 'class="line ') @@ -309,7 +313,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) ' \ @@ -319,9 +323,11 @@ def end_line kind @out << '' @last_opened = @opened.last if @last_opened end + @out << '
' + @line_number += 1 end - + end - + end end From a53f94dcba47446be8a16d659bf742d510621770 Mon Sep 17 00:00:00 2001 From: TJ Koblentz Date: Sun, 8 Jul 2012 18:21:30 -0700 Subject: [PATCH 2/4] patched elsewhere - ugly hack, but it should do --- lib/coderay/encoders/html.rb | 6 ------ lib/coderay/encoders/html/numbering.rb | 10 +++++----- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 8ba01eea..d2b73393 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -172,7 +172,6 @@ def self.token_path_to_hint hint, kinds def setup options super - @line_number = 1 if options[:wrap] || options[:line_numbers] @real_out = @out @out = '' @@ -298,9 +297,6 @@ def end_group kind # whole lines to be highlighted, eg. a deleted line in a diff def begin_line kind - @out << '
' if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] if style['class="'] @out << style.sub('class="', 'class="line ') @@ -323,8 +319,6 @@ def end_line kind @out << '' @last_opened = @opened.last if @last_opened end - @out << '
' - @line_number += 1 end end diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb index 8bc6259f..8400e2ed 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 From 87930b1e1b27d7048ee92074cbb0f39614d66495 Mon Sep 17 00:00:00 2001 From: TJ Koblentz Date: Sun, 8 Jul 2012 18:32:45 -0700 Subject: [PATCH 3/4] odd if even - lol --- lib/coderay/encoders/html/numbering.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb index 8400e2ed..8e987e49 100644 --- a/lib/coderay/encoders/html/numbering.rb +++ b/lib/coderay/encoders/html/numbering.rb @@ -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 From d1d78a98fa8056c1d482b83c03ac55b447bbef1d Mon Sep 17 00:00:00 2001 From: TJ Koblentz Date: Sun, 8 Jul 2012 20:34:38 -0700 Subject: [PATCH 4/4] hacky fix to avoid the middleman html tidying up, adding newlines after the divs and wrecking shit up --- lib/coderay/encoders/html/numbering.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb index 8e987e49..858c5599 100644 --- a/lib/coderay/encoders/html/numbering.rb +++ b/lib/coderay/encoders/html/numbering.rb @@ -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