view vendor/gems/coderay-1.0.0/lib/coderay/scanners/ruby/string_state.rb @ 1082:997f6d7738f7 bug_531

In repo controller entry action, show the page for the file even if it's binary (so user still has access to history etc links). This makes it possible to use the entry action as the default when a file is clicked on
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 22 Nov 2012 18:04:17 +0000
parents cbb26bc654de
children
line wrap: on
line source
# encoding: utf-8
module CodeRay
module Scanners
  
  class Ruby
    
    class StringState < Struct.new :type, :interpreted, :delim, :heredoc,
      :opening_paren, :paren_depth, :pattern, :next_state  # :nodoc: all
      
      CLOSING_PAREN = Hash[ *%w[
        ( )
        [ ]
        < >
        { }
      ] ].each { |k,v| k.freeze; v.freeze }  # debug, if I try to change it with <<
      
      STRING_PATTERN = Hash.new do |h, k|
        delim, interpreted = *k
        # delim = delim.dup  # workaround for old Ruby
        delim_pattern = Regexp.escape(delim)
        if closing_paren = CLOSING_PAREN[delim]
          delim_pattern << Regexp.escape(closing_paren)
        end
        delim_pattern << '\\\\' unless delim == '\\'
        
        # special_escapes =
        #   case interpreted
        #   when :regexp_symbols
        #     '| [|?*+(){}\[\].^$]'
        #   end
        
        h[k] =
          if interpreted && delim != '#'
            / (?= [#{delim_pattern}] | \# [{$@] ) /mx
          else
            / (?= [#{delim_pattern}] ) /mx
          end
      end
      
      def initialize kind, interpreted, delim, heredoc = false
        if heredoc
          pattern = heredoc_pattern delim, interpreted, heredoc == :indented
          delim = nil
        else
          pattern = STRING_PATTERN[ [delim, interpreted] ]
          if closing_paren = CLOSING_PAREN[delim]
            opening_paren = delim
            delim = closing_paren
            paren_depth = 1
          end
        end
        super kind, interpreted, delim, heredoc, opening_paren, paren_depth, pattern, :initial
      end
      
      def heredoc_pattern delim, interpreted, indented
        # delim = delim.dup  # workaround for old Ruby
        delim_pattern = Regexp.escape(delim)
        delim_pattern = / (?:\A|\n) #{ '(?>[ \t]*)' if indented } #{ Regexp.new delim_pattern } $ /x
        if interpreted
          / (?= #{delim_pattern}() | \\ | \# [{$@] ) /mx  # $1 set == end of heredoc
        else
          / (?= #{delim_pattern}() | \\ ) /mx
        end
      end
      
    end
    
  end
  
end
end