Sha256: 90b05ecbfec34577d8b2013f3516872f930e0c614bb83363e79c6c3318a405c9

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'uri'

module WordCountAnalyzer
  class Hyperlink
    NON_HYPERLINK_REGEX = /\A\w+:$/

    # Rubular: http://rubular.com/r/fXa4lp0gfS
    HYPERLINK_REGEX = /(http|https|www)(\.|:)/

    def hyperlink?(text)
      !(text !~ URI.regexp) && text !~ NON_HYPERLINK_REGEX && !(text !~ HYPERLINK_REGEX)
    end

    def occurences(text)
      text.scan(URI.regexp).map { |link| link.compact.size > 1 ? 1 : 0 }.inject(0) { |sum, x| sum + x }
    end

    def replace(text)
      text.split(/\s+/).each do |token|
        if !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX) && token.include?('">')
          text = text.gsub(/#{Regexp.escape(token.split('">')[0])}/, ' wslinkword ')
        elsif !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX)
          text = text.gsub(/#{Regexp.escape(token)}/, ' wslinkword ')
        end
      end
      text
    end

    def replace_split_at_period(text)
      text.split(/\s+/).each do |token|
        if !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX) && token.include?('">')
          text.gsub!(/#{Regexp.escape(token.split('">')[0])}/) do |match|
            match.split('.').join(' ')
          end
        elsif !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX)
          text.gsub!(/#{Regexp.escape(token)}/) do |match|
            match.split('.').join(' ')
          end
        end
      end
      text
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
word_count_analyzer-1.0.0 lib/word_count_analyzer/hyperlink.rb