]> cat aescling's git repositories - mastodon.git/blob - app/lib/extractor.rb
Upgrade ESLint to version 4.x (#6276)
[mastodon.git] / app / lib / extractor.rb
1 # frozen_string_literal: true
2
3 module Extractor
4 extend Twitter::Extractor
5
6 module_function
7
8 # :yields: username, list_slug, start, end
9 def extract_mentions_or_lists_with_indices(text)
10 return [] unless text =~ Twitter::Regex[:at_signs]
11
12 possible_entries = []
13
14 text.to_s.scan(Account::MENTION_RE) do |screen_name, _|
15 match_data = $LAST_MATCH_INFO
16 after = $'
17 unless after =~ Twitter::Regex[:end_mention_match]
18 start_position = match_data.char_begin(1) - 1
19 end_position = match_data.char_end(1)
20 possible_entries << {
21 screen_name: screen_name,
22 indices: [start_position, end_position],
23 }
24 end
25 end
26
27 if block_given?
28 possible_entries.each do |mention|
29 yield mention[:screen_name], mention[:indices].first, mention[:indices].last
30 end
31 end
32 possible_entries
33 end
34
35 def extract_hashtags_with_indices(text, **)
36 return [] unless text =~ /#/
37
38 tags = []
39 text.scan(Tag::HASHTAG_RE) do |hash_text, _|
40 match_data = $LAST_MATCH_INFO
41 start_position = match_data.char_begin(1) - 1
42 end_position = match_data.char_end(1)
43 after = $'
44 if after =~ %r{\A://}
45 hash_text.match(/(.+)(https?\Z)/) do |matched|
46 hash_text = matched[1]
47 end_position -= matched[2].char_length
48 end
49 end
50
51 tags << {
52 hashtag: hash_text,
53 indices: [start_position, end_position],
54 }
55 end
56
57 tags.each { |tag| yield tag[:hashtag], tag[:indices].first, tag[:indices].last } if block_given?
58 tags
59 end
60
61 def extract_cashtags_with_indices(_text)
62 [] # always returns empty array
63 end
64 end
This page took 0.079582 seconds and 4 git commands to generate.