You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
884 B

  1. # frozen_string_literal: true
  2. module Extractor
  3. extend Twitter::Extractor
  4. module_function
  5. def extract_mentions_or_lists_with_indices(text) # :yields: username, list_slug, start, end
  6. return [] unless text =~ Twitter::Regex[:at_signs]
  7. possible_entries = []
  8. text.to_s.scan(Account::MENTION_RE) do |screen_name, _|
  9. match_data = $LAST_MATCH_INFO
  10. after = $'
  11. unless after =~ Twitter::Regex[:end_mention_match]
  12. start_position = match_data.char_begin(1) - 1
  13. end_position = match_data.char_end(1)
  14. possible_entries << {
  15. screen_name: screen_name,
  16. indices: [start_position, end_position],
  17. }
  18. end
  19. end
  20. if block_given?
  21. possible_entries.each do |mention|
  22. yield mention[:screen_name], mention[:indices].first, mention[:indices].last
  23. end
  24. end
  25. possible_entries
  26. end
  27. end