about summary refs log tree commit diff
path: root/app/lib/extractor.rb
blob: ead4cddddec25286d110c38d0e09024562a22e68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

module Extractor
  MAX_DOMAIN_LENGTH = 253

  extend Twitter::TwitterText::Extractor

  module_function

  def extract_entities_with_indices(text, options = {}, &block)
    entities = begin
      extract_urls_with_indices(text, options) +
        extract_hashtags_with_indices(text, check_url_overlap: false) +
        extract_mentions_or_lists_with_indices(text) +
        extract_extra_uris_with_indices(text)
    end

    return [] if entities.empty?

    entities = remove_overlapping_entities(entities)
    entities.each(&block) if block
    entities
  end

  def extract_mentions_or_lists_with_indices(text)
    return [] unless text && Twitter::TwitterText::Regex[:at_signs].match?(text)

    possible_entries = []

    text.scan(Account::MENTION_RE) do |screen_name, _|
      match_data = $LAST_MATCH_INFO
      after      = $'

      unless Twitter::TwitterText::Regex[:end_mention_match].match?(after)
        _, domain = screen_name.split('@')

        next if domain.present? && domain.length > MAX_DOMAIN_LENGTH

        start_position = match_data.char_begin(1) - 1
        end_position   = match_data.char_end(1)

        possible_entries << {
          screen_name: screen_name,
          indices: [start_position, end_position],
        }
      end
    end

    if block_given?
      possible_entries.each do |mention|
        yield mention[:screen_name], mention[:indices].first, mention[:indices].last
      end
    end

    possible_entries
  end

  def extract_hashtags_with_indices(text, _options = {})
    return [] unless text&.index('#')

    possible_entries = []

    text.scan(Tag::HASHTAG_RE) do |hash_text, _|
      match_data     = $LAST_MATCH_INFO
      start_position = match_data.char_begin(1) - 1
      end_position   = match_data.char_end(1)
      after          = $'

      if %r{\A://}.match?(after)
        hash_text.match(/(.+)(https?\Z)/) do |matched|
          hash_text     = matched[1]
          end_position -= matched[2].codepoint_length
        end
      end

      possible_entries << {
        hashtag: hash_text,
        indices: [start_position, end_position],
      }
    end

    if block_given?
      possible_entries.each do |tag|
        yield tag[:hashtag], tag[:indices].first, tag[:indices].last
      end
    end

    possible_entries
  end

  def extract_cashtags_with_indices(_text)
    []
  end

  def extract_extra_uris_with_indices(text)
    return [] unless text&.index(':')

    possible_entries = []

    text.scan(Twitter::TwitterText::Regex[:valid_extended_uri]) do
      valid_uri_match_data = $LAST_MATCH_INFO

      start_position = valid_uri_match_data.char_begin(3)
      end_position   = valid_uri_match_data.char_end(3)

      possible_entries << {
        url: valid_uri_match_data[3],
        indices: [start_position, end_position],
      }
    end

    if block_given?
      possible_entries.each do |url|
        yield url[:url], url[:indices].first, url[:indices].last
      end
    end

    possible_entries
  end
end