lib/linkify-it-rb/index.rb
def compile
    @re = build_re(@__opts__)

    # Define dynamic patterns
    tlds = @__tlds__.dup

    onCompile

    tlds.push(TLDS_2CH_SRC_RE) if (!@__tlds_replaced__)
    tlds.push(@re[:src_xn])

    @re[:src_tlds]         = tlds.join('|')
    @re[:email_fuzzy]      = Regexp.new(@re[:tpl_email_fuzzy].gsub('%TLDS%', @re[:src_tlds]), true)
    @re[:link_fuzzy]       = Regexp.new(@re[:tpl_link_fuzzy].gsub('%TLDS%', @re[:src_tlds]), true)
    @re[:link_no_ip_fuzzy] = Regexp.new(@re[:tpl_link_no_ip_fuzzy].gsub('%TLDS%', @re[:src_tlds]), true)
    @re[:host_fuzzy_test]  = Regexp.new(@re[:tpl_host_fuzzy_test].gsub('%TLDS%', @re[:src_tlds]), true)

    #
    # Compile each schema
    #

    aliases = []

    @__compiled__ = {} # Reset compiled data

    schemaError = lambda do |name, val|
      raise Error, ('(LinkifyIt) Invalid schema "' + name + '": ' + val)
    end

    @__schemas__.each do |name, val|

      # skip disabled methods
      next if (val == nil)

      compiled = { validate: nil, link: nil }

      @__compiled__[name] = compiled

      if (val.is_a? Hash)
        if (val[:validate].is_a? Regexp)
          compiled[:validate] = createValidator(val[:validate])
        elsif (val[:validate].is_a? Proc)
          compiled[:validate] = val[:validate]
        else
          schemaError(name, val)
        end

        if (val[:normalize].is_a? Proc)
          compiled[:normalize] = val[:normalize]
        elsif (!val[:normalize])
          compiled[:normalize] = createNormalizer()
        else
          schemaError(name, val)
        end
        next
      end

      if (val.is_a? String)
        aliases.push(name)
        next
      end

      schemaError(name, val)
    end

    #
    # Compile postponed aliases
    #

    aliases.each do |an_alias|
      if (!@__compiled__[@__schemas__[an_alias]])
        # Silently fail on missed schemas to avoid errons on disable.
        # schemaError(an_alias, self.__schemas__[an_alias]);
      else
        @__compiled__[an_alias][:validate]  = @__compiled__[@__schemas__[an_alias]][:validate]
        @__compiled__[an_alias][:normalize] = @__compiled__[@__schemas__[an_alias]][:normalize]
      end
    end

    #
    # Fake record for guessed links
    #
    @__compiled__[''] = { validate: nil, normalize: createNormalizer }

    #
    # Build schema condition, and filter disabled & fake schemas
    #
    slist = @__compiled__.select {|name, val| name.length > 0 && !val.nil? }.keys.map {|str| escapeRE(str)}.join('|')

    # (?!_) cause 1.5x slowdown
    @re[:schema_test]   = Regexp.new('(^|(?!_)(?:[><\uff5c]|' + @re[:src_XPCc] + '))(' + slist + ')', 'i')
    @re[:schema_search] = Regexp.new('(^|(?!_)(?:[><\uff5c]|' + @re[:src_XPCc] + '))(' + slist + ')', 'ig')

    @re[:pretest]       = Regexp.new(
                              '(' + @re[:schema_test].source + ')|' +
                              '(' + @re[:host_fuzzy_test].source + ')|' + '@', 'i')

    #
    # Cleanup
    #

    resetScanCache
  end
# Schemas compiler. Build regexps.
#
#------------------------------------------------------------------------------
Schemas compiler. Build regexps.
Schemas compiler . Build regexps .
def compile
    @re = build_re(@__opts__)
    tlds = @__tlds__.dup
    onCompile
    tlds.push(TLDS_2CH_SRC_RE) if (!@__tlds_replaced__)
    tlds.push(@re[:src_xn])
    @re[:src_tlds]         = tlds.join('|')
    @re[:email_fuzzy]      = Regexp.new(@re[:tpl_email_fuzzy].gsub('%TLDS%', @re[:src_tlds]), true)
    @re[:link_fuzzy]       = Regexp.new(@re[:tpl_link_fuzzy].gsub('%TLDS%', @re[:src_tlds]), true)
    @re[:link_no_ip_fuzzy] = Regexp.new(@re[:tpl_link_no_ip_fuzzy].gsub('%TLDS%', @re[:src_tlds]), true)
    @re[:host_fuzzy_test]  = Regexp.new(@re[:tpl_host_fuzzy_test].gsub('%TLDS%', @re[:src_tlds]), true)
     Compile each schema
    aliases = []
    @__compiled__ = {}  Reset compiled data
    schemaError = lambda do |name, val|
      raise Error, ('(LinkifyIt) Invalid schema "' + name + '": ' + val)
    end
    @__schemas__.each do |name, val|
       skip disabled methods
      next if (val == nil)
      compiled = { validate: nil, link: nil }
      @__compiled__[name] = compiled
      if (val.is_a? Hash)
        if (val[:validate].is_a? Regexp)
          compiled[:validate] = createValidator(val[:validate])
        elsif (val[:validate].is_a? Proc)
          compiled[:validate] = val[:validate]
        else
          schemaError(name, val)
        end
        if (val[:normalize].is_a? Proc)
          compiled[:normalize] = val[:normalize]
        elsif (!val[:normalize])
          compiled[:normalize] = createNormalizer()
        else
          schemaError(name, val)
        end
        next
      end
      if (val.is_a? String)
        aliases.push(name)
        next
      end
      schemaError(name, val)
    end
     Compile postponed aliases
    aliases.each do |an_alias|
      if (!@__compiled__[@__schemas__[an_alias]])
         Silently fail on missed schemas to avoid errons on disable.
         schemaError(an_alias, self.__schemas__[an_alias]);
      else
        @__compiled__[an_alias][:validate]  = @__compiled__[@__schemas__[an_alias]][:validate]
        @__compiled__[an_alias][:normalize] = @__compiled__[@__schemas__[an_alias]][:normalize]
      end
    end
     Fake record for guessed links
    @__compiled__[''] = { validate: nil, normalize: createNormalizer }
     Build schema condition, and filter disabled & fake schemas
    slist = @__compiled__.select {|name, val| name.length > 0 && !val.nil? }.keys.map {|str| escapeRE(str)}.join('|')
     (?!_) cause 1.5x slowdown
    @re[:schema_test]   = Regexp.new('(^|(?!_)(?:[><\uff5c]|' + @re[:src_XPCc] + '))(' + slist + ')', 'i')
    @re[:schema_search] = Regexp.new('(^|(?!_)(?:[><\uff5c]|' + @re[:src_XPCc] + '))(' + slist + ')', 'ig')
    @re[:pretest]       = Regexp.new(
                              '(' + @re[:schema_test].source + ')|' +
                              '(' + @re[:host_fuzzy_test].source + ')|' + '@', 'i')
     Cleanup
    resetScanCache
  end
def compile @re = build_re ( @__opts__ ) tlds = @__tlds__ . dup onCompile tlds . push ( TLDS_2CH_SRC_RE ) if ( ! @__tlds_replaced__ ) tlds . push ( @re [ :src_xn ] ) @re [ :src_tlds ] = tlds . join ( '|' ) @re [ :email_fuzzy ] = Regexp . new ( @re [ :tpl_email_fuzzy ] . gsub ( '%TLDS%' , @re [ :src_tlds ] ) , true ) @re [ :link_fuzzy ] = Regexp . new ( @re [ :tpl_link_fuzzy ] . gsub ( '%TLDS%' , @re [ :src_tlds ] ) , true ) @re [ :link_no_ip_fuzzy ] = Regexp . new ( @re [ :tpl_link_no_ip_fuzzy ] . gsub ( '%TLDS%' , @re [ :src_tlds ] ) , true ) @re [ :host_fuzzy_test ] = Regexp . new ( @re [ :tpl_host_fuzzy_test ] . gsub ( '%TLDS%' , @re [ :src_tlds ] ) , true ) aliases = [ ] @__compiled__ = { } schemaError = lambda do | name , val | raise Error , ( '(LinkifyIt) Invalid schema "' + name + '": ' + val ) end @__schemas__ . each do | name , val | next if ( val == nil ) compiled = { validate : nil , link : nil } @__compiled__ [ name ] = compiled if ( val . is_a? Hash ) if ( val [ :validate ] . is_a? Regexp ) compiled [ :validate ] = createValidator ( val [ :validate ] ) elsif ( val [ :validate ] . is_a? Proc ) compiled [ :validate ] = val [ :validate ] else schemaError ( name , val ) end if ( val [ :normalize ] . is_a? Proc ) compiled [ :normalize ] = val [ :normalize ] elsif ( ! val [ :normalize ] ) compiled [ :normalize ] = createNormalizer ( ) else schemaError ( name , val ) end next end if ( val . is_a? String ) aliases . push ( name ) next end schemaError ( name , val ) end aliases . each do | an_alias | if ( ! @__compiled__ [ @__schemas__ [ an_alias ] ] ) else @__compiled__ [ an_alias ] [ :validate ] = @__compiled__ [ @__schemas__ [ an_alias ] ] [ :validate ] @__compiled__ [ an_alias ] [ :normalize ] = @__compiled__ [ @__schemas__ [ an_alias ] ] [ :normalize ] end end @__compiled__ [ '' ] = { validate : nil , normalize : createNormalizer } slist = @__compiled__ . select { | name , val | name . length > 0 && ! val . nil? } . keys . map { | str | escapeRE ( str ) } . join ( '|' ) @re [ :schema_test ] = Regexp . new ( '(^|(?!_)(?:[><\uff5c]|' + @re [ :src_XPCc ] + '))(' + slist + ')' , 'i' ) @re [ :schema_search ] = Regexp . new ( '(^|(?!_)(?:[><\uff5c]|' + @re [ :src_XPCc ] + '))(' + slist + ')' , 'ig' ) @re [ :pretest ] = Regexp . new ( '(' + @re [ :schema_test ] . source + ')|' + '(' + @re [ :host_fuzzy_test ] . source + ')|' + '@' , 'i' ) resetScanCache end
Schemas compiler.
Schemas compiler .
# Define dynamic patterns # # Compile each schema # # Reset compiled data # skip disabled methods # # Compile postponed aliases # # Silently fail on missed schemas to avoid errons on disable. # schemaError(an_alias, self.__schemas__[an_alias]); # # Fake record for guessed links # # # Build schema condition, and filter disabled & fake schemas # # (?!_) cause 1.5x slowdown # # Cleanup #