diff options
Diffstat (limited to 'lib/coderay')
-rw-r--r-- | lib/coderay/encoders/html/numbering.rb | 39 | ||||
-rw-r--r-- | lib/coderay/encoders/html/output.rb | 2 | ||||
-rw-r--r-- | lib/coderay/scanners/diff.rb | 80 | ||||
-rw-r--r-- | lib/coderay/scanners/html.rb | 2 | ||||
-rw-r--r-- | lib/coderay/scanners/php.rb | 5 | ||||
-rw-r--r-- | lib/coderay/scanners/ruby.rb | 33 | ||||
-rw-r--r-- | lib/coderay/scanners/ruby/patterns.rb | 36 | ||||
-rw-r--r-- | lib/coderay/version.rb | 2 |
8 files changed, 117 insertions, 82 deletions
diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb index e717429..332145b 100644 --- a/lib/coderay/encoders/html/numbering.rb +++ b/lib/coderay/encoders/html/numbering.rb @@ -1,15 +1,15 @@ module CodeRay module Encoders - + class HTML - + module Numbering # :nodoc: - + def self.number! output, 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 @@ -56,12 +56,17 @@ module Encoders 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 + if position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n) after_last_newline = output[position_of_last_newline + 1 .. -1] ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/] - line_count += 1 if not ends_with_newline + + if ends_with_newline + line_count = output.count("\n") + else + line_count = output.count("\n") + 1 + end + else + line_count = 1 end case mode @@ -74,30 +79,30 @@ module Encoders line_number += 1 "<span class=\"line-numbers\">#{indent}#{line_number_text}</span>#{line}" end - + when :table line_numbers = (start ... start + line_count).map(&bolding).join("\n") line_numbers << "\n" line_numbers_table_template = Output::TABLE.apply('LINE_NUMBERS', line_numbers) - + output.gsub!(/<\/div>\n/, '</div>') output.wrap_in! line_numbers_table_template output.wrapped_in = :div - + when :list raise NotImplementedError, 'The :list option is no longer available. Use :table.' - + else raise ArgumentError, 'Unknown value %p for mode: expected one of %p' % [mode, [:table, :inline]] end - + output end - + end - + end - + end end diff --git a/lib/coderay/encoders/html/output.rb b/lib/coderay/encoders/html/output.rb index 9132d94..de6f6ea 100644 --- a/lib/coderay/encoders/html/output.rb +++ b/lib/coderay/encoders/html/output.rb @@ -124,7 +124,7 @@ module Encoders TABLE = Template.new <<-TABLE <table class="CodeRay"><tr> - <td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><%LINE_NUMBERS%></pre></td> + <td class="line-numbers"><pre><%LINE_NUMBERS%></pre></td> <td class="code"><pre><%CONTENT%></pre></td> </tr></table> TABLE diff --git a/lib/coderay/scanners/diff.rb b/lib/coderay/scanners/diff.rb index 9e899c3..38efaf4 100644 --- a/lib/coderay/scanners/diff.rb +++ b/lib/coderay/scanners/diff.rb @@ -20,7 +20,7 @@ module Scanners line_kind = nil state = :initial - deleted_lines = 0 + deleted_lines_count = 0 scanners = Hash.new do |h, lang| h[lang] = Scanners[lang].new '', :keep_tokens => true, :keep_state => true end @@ -30,7 +30,7 @@ module Scanners until eos? if match = scan(/\n/) - deleted_lines = 0 unless line_kind == :delete + deleted_lines_count = 0 unless line_kind == :delete if line_kind encoder.end_line line_kind line_kind = nil @@ -99,37 +99,59 @@ module Scanners end next elsif match = scan(/-/) - deleted_lines += 1 - encoder.begin_line line_kind = :delete - encoder.text_token match, :delete - if options[:inline_diff] && deleted_lines == 1 && check(/(?>.*)\n\+(?>.*)$(?!\n\+)/) - content_scanner_entry_state = content_scanner.state - skip(/(.*)\n\+(.*)$/) - head, deletion, insertion, tail = diff self[1], self[2] - pre, deleted, post = content_scanner.tokenize [head, deletion, tail], :tokens => Tokens.new - encoder.tokens pre - unless deleted.empty? - encoder.begin_group :eyecatcher - encoder.tokens deleted - encoder.end_group :eyecatcher + deleted_lines_count += 1 + if options[:inline_diff] && deleted_lines_count == 1 && (changed_lines_count = 1 + check(/.*(?:\n\-.*)*/).count("\n")) && match?(/(?>.*(?:\n\-.*){#{changed_lines_count - 1}}(?:\n\+.*){#{changed_lines_count}})$(?!\n\+)/) + deleted_lines = Array.new(changed_lines_count) { |i| skip(/\n\-/) if i > 0; scan(/.*/) } + inserted_lines = Array.new(changed_lines_count) { |i| skip(/\n\+/) ; scan(/.*/) } + + deleted_lines_tokenized = [] + inserted_lines_tokenized = [] + for deleted_line, inserted_line in deleted_lines.zip(inserted_lines) + pre, deleted_part, inserted_part, post = diff deleted_line, inserted_line + content_scanner_entry_state = content_scanner.state + deleted_lines_tokenized << content_scanner.tokenize([pre, deleted_part, post], :tokens => Tokens.new) + content_scanner.state = content_scanner_entry_state || :initial + inserted_lines_tokenized << content_scanner.tokenize([pre, inserted_part, post], :tokens => Tokens.new) end - encoder.tokens post - encoder.end_line line_kind - encoder.text_token "\n", :space - encoder.begin_line line_kind = :insert - encoder.text_token '+', :insert - content_scanner.state = content_scanner_entry_state || :initial - pre, inserted, post = content_scanner.tokenize [head, insertion, tail], :tokens => Tokens.new - encoder.tokens pre - unless inserted.empty? - encoder.begin_group :eyecatcher - encoder.tokens inserted - encoder.end_group :eyecatcher + + for pre, deleted_part, post in deleted_lines_tokenized + encoder.begin_line :delete + encoder.text_token '-', :delete + encoder.tokens pre + unless deleted_part.empty? + encoder.begin_group :eyecatcher + encoder.tokens deleted_part + encoder.end_group :eyecatcher + end + encoder.tokens post + encoder.end_line :delete + encoder.text_token "\n", :space + end + + for pre, inserted_part, post in inserted_lines_tokenized + encoder.begin_line :insert + encoder.text_token '+', :insert + encoder.tokens pre + unless inserted_part.empty? + encoder.begin_group :eyecatcher + encoder.tokens inserted_part + encoder.end_group :eyecatcher + end + encoder.tokens post + changed_lines_count -= 1 + if changed_lines_count > 0 + encoder.end_line :insert + encoder.text_token "\n", :space + end end - encoder.tokens post + + line_kind = :insert + elsif match = scan(/.*/) + encoder.begin_line line_kind = :delete + encoder.text_token '-', :delete if options[:highlight_code] - if deleted_lines == 1 + if deleted_lines_count == 1 content_scanner_entry_state = content_scanner.state end content_scanner.tokenize match, :tokens => encoder unless match.empty? diff --git a/lib/coderay/scanners/html.rb b/lib/coderay/scanners/html.rb index 49c346d..3ba3b79 100644 --- a/lib/coderay/scanners/html.rb +++ b/lib/coderay/scanners/html.rb @@ -101,7 +101,7 @@ module Scanners when :initial if match = scan(/<!--(?:.*?-->|.*)/m) encoder.text_token match, :comment - elsif match = scan(/<!DOCTYPE(?:.*?>|.*)/m) + elsif match = scan(/<!(\w+)(?:.*?>|.*)|\]>/m) encoder.text_token match, :doctype elsif match = scan(/<\?xml(?:.*?\?>|.*)/m) encoder.text_token match, :preprocessor diff --git a/lib/coderay/scanners/php.rb b/lib/coderay/scanners/php.rb index 8acfff5..6c68834 100644 --- a/lib/coderay/scanners/php.rb +++ b/lib/coderay/scanners/php.rb @@ -1,4 +1,4 @@ -# encoding: ASCII-8BIT +# encoding: utf-8 module CodeRay module Scanners @@ -11,7 +11,6 @@ module Scanners register_for :php file_extension 'php' - encoding 'BINARY' KINDS_NOT_LOC = HTML::KINDS_NOT_LOC @@ -211,7 +210,7 @@ module Scanners HTML_INDICATOR = /<!DOCTYPE html|<(?:html|body|div|p)[> ]/i - IDENTIFIER = /[a-z_\x7f-\xFF][a-z0-9_\x7f-\xFF]*/i + IDENTIFIER = 'ä'[/[[:alpha:]]/] == 'ä' ? Regexp.new('[[:alpha:]_[^\0-\177]][[:alnum:]_[^\0-\177]]*') : Regexp.new('[a-z_\x7f-\xFF][a-z0-9_\x7f-\xFF]*', true) VARIABLE = /\$#{IDENTIFIER}/ OPERATOR = / diff --git a/lib/coderay/scanners/ruby.rb b/lib/coderay/scanners/ruby.rb index 2be98a6..c5cf1e2 100644 --- a/lib/coderay/scanners/ruby.rb +++ b/lib/coderay/scanners/ruby.rb @@ -94,18 +94,27 @@ module Scanners if !method_call_expected && match = scan(unicode ? /#{patterns::METHOD_NAME}/uo : /#{patterns::METHOD_NAME}/o) - value_expected = false + kind = patterns::IDENT_KIND[match] - if kind == :ident - if match[/\A[A-Z]/] && !(match[/[!?]$/] || match?(/\(/)) - kind = :constant + if kind == :ident && value_expected != :colon_expected && scan(/:(?!:)/) + value_expected = true + encoder.text_token match, :key + encoder.text_token ':', :operator + else + value_expected = false + if kind == :ident + if match[/\A[A-Z]/] && !(match[/[!?]$/] || match?(/\(/)) + kind = :constant + end + elsif kind == :keyword + state = patterns::KEYWORD_NEW_STATE[match] + if patterns::KEYWORDS_EXPECTING_VALUE[match] + value_expected = match == 'when' ? :colon_expected : true + end end - elsif kind == :keyword - state = patterns::KEYWORD_NEW_STATE[match] - value_expected = true if patterns::KEYWORDS_EXPECTING_VALUE[match] + value_expected = true if !value_expected && check(/#{patterns::VALUE_FOLLOWS}/o) + encoder.text_token match, kind end - value_expected = true if !value_expected && check(/#{patterns::VALUE_FOLLOWS}/o) - encoder.text_token match, kind elsif method_call_expected && match = scan(unicode ? /#{patterns::METHOD_AFTER_DOT}/uo : @@ -119,9 +128,9 @@ module Scanners value_expected = check(/#{patterns::VALUE_FOLLOWS}/o) # OPERATORS # - elsif !method_call_expected && match = scan(/ (\.(?!\.)|::) | (?: \.\.\.? | ==?=? | [,\(\[\{] )() | [\)\]\}] /x) + elsif !method_call_expected && match = scan(/ (\.(?!\.)|::) | ( \.\.\.? | ==?=? | [,\(\[\{] ) | [\)\]\}] /x) method_call_expected = self[1] - value_expected = !method_call_expected && self[2] + value_expected = !method_call_expected && !!self[2] if inline_block_stack case match when '{' @@ -213,7 +222,7 @@ module Scanners encoder.text_token match, :integer elsif match = scan(/ %=? | <(?:<|=>?)? | \? /x) - value_expected = true + value_expected = match == '?' ? :colon_expected : true encoder.text_token match, :operator elsif match = scan(/`/) diff --git a/lib/coderay/scanners/ruby/patterns.rb b/lib/coderay/scanners/ruby/patterns.rb index a52198e..ed071d2 100644 --- a/lib/coderay/scanners/ruby/patterns.rb +++ b/lib/coderay/scanners/ruby/patterns.rb @@ -1,9 +1,9 @@ # encoding: utf-8 module CodeRay module Scanners - + module Ruby::Patterns # :nodoc: all - + KEYWORDS = %w[ and def end in or unless begin defined? ensure module redo super until @@ -12,7 +12,7 @@ module Scanners while alias class elsif if not return undef yield ] - + # See http://murfy.de/ruby-constants. PREDEFINED_CONSTANTS = %w[ nil true false self @@ -24,19 +24,19 @@ module Scanners RUBY_PLATFORM RUBY_RELEASE_DATE RUBY_REVISION RUBY_VERSION __FILE__ __LINE__ __ENCODING__ ] - + IDENT_KIND = WordList.new(:ident). add(KEYWORDS, :keyword). add(PREDEFINED_CONSTANTS, :predefined_constant) - + KEYWORD_NEW_STATE = WordList.new(:initial). add(%w[ def ], :def_expected). add(%w[ undef ], :undef_expected). add(%w[ alias ], :alias_expected). add(%w[ class module ], :module_expected) - - IDENT = 'ä'[/[[:alpha:]]/] == 'ä' ? /[[:alpha:]_][[:alnum:]_]*/ : /[^\W\d]\w*/ - + + IDENT = 'ä'[/[[:alpha:]]/] == 'ä' ? Regexp.new('[[:alpha:]_[^\0-\177]][[:alnum:]_[^\0-\177]]*') : /[^\W\d]\w*/ + METHOD_NAME = / #{IDENT} [?!]? /ox METHOD_NAME_OPERATOR = / \*\*? # multiplication and power @@ -57,25 +57,25 @@ module Scanners GLOBAL_VARIABLE = / \$ (?: #{IDENT} | [1-9]\d* | 0\w* | [~&+`'=\/,;_.<>!@$?*":\\] | -[a-zA-Z_0-9] ) /ox PREFIX_VARIABLE = / #{GLOBAL_VARIABLE} | #{OBJECT_VARIABLE} /ox VARIABLE = / @?@? #{IDENT} | #{GLOBAL_VARIABLE} /ox - + QUOTE_TO_TYPE = { '`' => :shell, '/'=> :regexp, } QUOTE_TO_TYPE.default = :string - + REGEXP_MODIFIERS = /[mousenix]*/ - + DECIMAL = /\d+(?:_\d+)*/ OCTAL = /0_?[0-7]+(?:_[0-7]+)*/ HEXADECIMAL = /0x[0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*/ BINARY = /0b[01]+(?:_[01]+)*/ - + EXPONENT = / [eE] [+-]? #{DECIMAL} /ox FLOAT_SUFFIX = / #{EXPONENT} | \. #{DECIMAL} #{EXPONENT}? /ox FLOAT_OR_INT = / #{DECIMAL} (?: #{FLOAT_SUFFIX} () )? /ox NUMERIC = / (?: (?=0) (?: #{OCTAL} | #{HEXADECIMAL} | #{BINARY} ) | #{FLOAT_OR_INT} ) /ox - + SYMBOL = / : (?: @@ -85,7 +85,7 @@ module Scanners ) /ox METHOD_NAME_OR_SYMBOL = / #{METHOD_NAME_EX} | #{SYMBOL} /ox - + SIMPLE_ESCAPE = / [abefnrstv] | [0-7]{1,3} @@ -110,7 +110,7 @@ module Scanners | \\ #{ESCAPE} ) /mox - + # NOTE: This is not completely correct, but # nobody needs heredoc delimiters ending with \n. HEREDOC_OPEN = / @@ -122,13 +122,13 @@ module Scanners ( [^\n]*? ) \3 # $4 = delim ) /mx - + RUBYDOC = / =begin (?!\S) .*? (?: \Z | ^=end (?!\S) [^\n]* ) /mx - + DATA = / __END__$ .*? @@ -136,7 +136,7 @@ module Scanners /mx RUBYDOC_OR_DATA = / #{RUBYDOC} | #{DATA} /xo - + # Checks for a valid value to follow. This enables # value_expected in method calls without parentheses. VALUE_FOLLOWS = / diff --git a/lib/coderay/version.rb b/lib/coderay/version.rb index 87d1cff..bfb5f24 100644 --- a/lib/coderay/version.rb +++ b/lib/coderay/version.rb @@ -1,3 +1,3 @@ module CodeRay - VERSION = '1.0.8' + VERSION = '1.0.9' end |